📄 syncmidlet.java
字号:
/**
* @(#)$RCSfile: SyncMidlet.java,v $ $Revision: 1.2 $
*
* ====================================================================
* Copyright 2001, Reaxion Corp.,
* 11418 105th PL NE, Kirkland, WA, 98033, USA
* All rights reserved.
* ====================================================================
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is the Tequila SyncML.
*
* The Initial Developer of the Original Code is Reaxion Corp.
* All Rights Reserved.
*/
package com.reaxion.tequila.client.syncml.ui;
import java.util.Enumeration;
import java.util.Vector;
import java.io.PrintStream;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import com.reaxion.tequila.syncml.*;
import com.reaxion.tequila.syncml.sync.*;
import com.reaxion.tequila.syncml.xml.node.*;
import com.reaxion.tequila.syncml.util.*;
import com.reaxion.tequila.client.syncml.*;
import com.reaxion.tequila.client.syncml.comm.*;
/**
* @version $1.0$
* @author Oleg A. Oksyuk
*/
public class SyncMidlet extends MIDlet
implements CommandListener
{
private final static String PAR_SERVER_URL = "Server";
private final static String PAR_CLIENT_URL = "Client";
private final static String PAR_DOC = "Doc";
/**
* This is an instance of the UI demo to which we return when the
* back button is pressed.
*/
private static SyncMidlet instance = null;
private IClient client = null;
/**
* This is the demo we are currently displaying.
*/
private BaseDialog currentDialog = null;
private SelectDialog selectDialog;
/**
* This is the array of demo classes available for the user to select.
* When the user selects a specific demo it is instanciated and
* displayed for him.
*/
private Class[] dialogs =
{
getClassObject("com.reaxion.tequila.client.syncml.ui.SyncDialog"),
getClassObject("com.reaxion.tequila.client.syncml.ui.AddAttrDialog"),
getClassObject("com.reaxion.tequila.client.syncml.ui.AddDataDialog"),
getClassObject("com.reaxion.tequila.client.syncml.ui.DeleteAttrDialog"),
getClassObject("com.reaxion.tequila.client.syncml.ui.DeleteDataDialog"),
getClassObject("com.reaxion.tequila.client.syncml.ui.ReplaceAttrDialog"),
getClassObject("com.reaxion.tequila.client.syncml.ui.ReplaceDataDialog"),
};
/**
* This is the array of names of the demos that contains the names for
* all the demos for the user to select.
*/
private static final String[] dialogNames =
{
"Sync",
"Add Attr",
"Add Data",
"Delete Attr",
"Delete Data",
"Replace Attr",
"Replace Data"
};
/**
* This is the list of demos used to allow the user to select a demo.
*/
private List selectionList = new List("Sync Client",
List.IMPLICIT,
dialogNames,
null);
/**
* The default constructor initializes instance.
*/
public SyncMidlet()
{
Log.println("SyncMidlet");
instance = this;
String serverUrl = getAppProperty(PAR_SERVER_URL);
String clientUrl = getAppProperty(PAR_CLIENT_URL);
String doc = getAppProperty(PAR_DOC);
Enumeration docs = parseParam(doc);
Log.println("serverUrl="+serverUrl);
Log.println("clientUrl="+clientUrl);
Log.println("doc="+doc);
if (null == serverUrl)
{
createAlert("Server URL is not specified");
}
else
if (null == clientUrl)
{
createAlert("Client URL is not specified");
}
else
if (null == docs)
{
createAlert("Documents to synchronize are not specified");
}
else
{
ISyncServer syncServer = new CommSyncmlClient(
new CommClient(serverUrl));
client = new Client(parseParam(doc), syncServer,
serverUrl, clientUrl);
selectDialog = new SelectDialog();
}
}
public IClient getClient()
{
return client;
}
/**
* This method dynamically loads a class to work around a limitation
* in the Forte compiler that prevents us from compiling ClassName.class
* syntax.
*/
private static Class getClassObject(String className)
{
try
{
return(Class.forName(className));
}
catch(Throwable err)
{
System.out.println(err.getClass().getName() + ": " +
err.getMessage());
err.printStackTrace();
}
return(null);
}
/**
* This method is used since the MIDP API requires that a MIDlet
* would set the screen.
*/
static SyncMidlet getInstance()
{
return instance;
}
/**
* Signals the MIDlet to terminate and enter the Destroyed state.
*/
protected void destroyApp(boolean unconditional)
{
dialogs = null;
selectionList = null;
currentDialog = null;
}
/**
* Signals the MIDlet to stop and enter the Paused state.
*/
protected void pauseApp()
{
}
/**
* Signals the MIDlet to start and enter the Active state.
*/
protected void startApp()
{
if (null == alert)
{
Display.getDisplay(this).setCurrent(selectionList);
selectionList.setCommandListener(this) ;
}
else
{
Display.getDisplay(this).setCurrent(alert, new Form(""));
}
}
public static void okPressed()
{
Log.println("OK pressed");
instance.currentDialog.ok();
instance.currentDialog = null;
instance.startApp();
}
public static void cancelPressed()
{
instance.currentDialog = null;
instance.startApp();
}
public void selectDoc(String dbName)
{
Log.println("selectDoc("+dbName+")"+currentDialog.getClass().getName());
CommandDialog commandDialog = (CommandDialog) currentDialog;
commandDialog.setDoc(client.getDocument(dbName));
Display.getDisplay(this).setCurrent(commandDialog);
}
public void cancelSelectDoc()
{
Log.println("cancelSelectDoc");
instance.currentDialog = null;
instance.startApp();
}
/**
* Indicates that a command event has occurred.
*/
public void commandAction(Command c, Displayable d)
{
int pos = selectionList.getSelectedIndex();
try
{
currentDialog = (BaseDialog)dialogs[pos].newInstance();
if (currentDialog instanceof SyncDialog)
{
Display.getDisplay(this).setCurrent(currentDialog);
}
else if (currentDialog instanceof CommandDialog)
{
Display.getDisplay(this).setCurrent(selectDialog);
}
}
catch(Exception err)
{
Alert a = new Alert("Error");
a.setString("Failed in handling the command at position: " + pos);
a.setTimeout(Alert.FOREVER);
Display.getDisplay(this).setCurrent(a);
}
}
/**
* This method returns the set of commands available within the individual
* demos such as back, exit and possibly menu.
*/
static Command[] getDemoCommands()
{
Command[] commands =
{
new Command("OK", Command.OK, 2),
new Command("Cancel", Command.CANCEL, 2)
};
return(commands);
}
/**
* This method returns the set of callbacks to match the commands
* returned in getCommandDemo(). The callbacks are Runnables whose
* run method should be invoked upon an event.
*/
static Runnable[] getDemoActions()
{
class OKCallback implements Runnable
{
public void run()
{
Log.println("OKCallback");
SyncMidlet.okPressed();
}
}
class CancelCallback implements Runnable
{
public void run()
{
Log.println("CancelCallback");
SyncMidlet.cancelPressed();
}
}
Runnable[] callbacks =
{
new OKCallback(),
new CancelCallback()
};
return(callbacks);
}
public static void main(String [] s)
{
new SyncMidlet().startApp();
}
private Alert alert;
private void createAlert(String msg)
{
alert = new Alert(msg);
alert.setType(AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
}
private Enumeration parseParam(String param)
{
if ((null == param) || (0 == param.trim().length()))
{
return null;
}
Vector res = new Vector();
int ind0 = 0, ind1;
for (;;)
{
ind1 = param.indexOf(";", ind0);
Log.println("ind1="+ind1);
if (ind1 == -1)
{
res.addElement(param.substring(ind0).trim());
break;
}
else
{
res.addElement(param.substring(ind0, ind1).trim());
ind0 = ind1+1;
Log.println("ind0="+ind0);
}
}
return res.elements();
}
}
/* -----------------------------------------------------------------------------
* Change log:
* -----------------------------------------------------------------------------
* $Log: SyncMidlet.java,v $
* Revision 1.2 2001/10/17 15:27:41 OlegO
* changed comments for better javadoc
*
* Revision 1.1.1.1 2001/10/11 13:13:32 OlegO
* no message
*
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -