📄 servermidlet.java
字号:
package example.bluetooth.demo;
/*
* DemoMIDlet.java
*
* Created on March 15, 2005, 1:07 PM
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Vector;
/**
*
* @author bohemius
*/
public class ServerMIDlet extends MIDlet implements javax.microedition.lcdui.CommandListener {
private boolean[] published;
private Vector imageNames;
private Image onImage, offImage;
private BTImageServer bt_server;
/** Creates a new instance of DemoMIDlet */
public ServerMIDlet() {
}
private void publishImageAction() {
/*
* Changing the state of base of published images
*/
int index = get_ImageServerList().getSelectedIndex();
if (published[index]==false) {
published[index]=true;
Image stateImg = true ? onImage : offImage;
get_ImageServerList().set(index, get_ImageServerList().getString(index), stateImg);
// update bluetooth service information
if (!bt_server.changeImageInfo(get_ImageServerList().getString(index),
published[index])) {
// either a bad record or SDDB is buzy
get_BlueToothAlert().setString("Can't update base");
Display.getDisplay(this).setCurrent(get_BlueToothAlert(), this.get_ImageServerList());
// restore internal information
published[index] = !published[index];
stateImg = published[index] ? onImage : offImage;
get_ImageServerList().set(index, get_ImageServerList().getString(index), stateImg);
}
}
}
private void unpublishImageAction() {
/*
* Changing the state of base of published images
*/
int index = get_ImageServerList().getSelectedIndex();
if (published[index]==true) {
published[index]=false;
Image stateImg = false ? onImage : offImage;
get_ImageServerList().set(index, get_ImageServerList().getString(index), stateImg);
// update bluetooth service information
if (!bt_server.changeImageInfo(get_ImageServerList().getString(index),
published[index])) {
// either a bad record or SDDB is buzy
get_BlueToothAlert().setString("Can't update base");
Display.getDisplay(this).setCurrent(get_BlueToothAlert(), this.get_ImageServerList());
// restore internal information
published[index] = !published[index];
stateImg = published[index] ? onImage : offImage;
get_ImageServerList().set(index, get_ImageServerList().getString(index), stateImg);
}
}
}
// --- This section is auto-generated by NetBeans IDE. Do not modify or you may lose your changes.//<editor-fold defaultstate="collapsed" desc="This section is auto-generated by NetBeans IDE.">//GEN-BEGIN:MVDMethods
/**
* This method initializes UI of the application.
*/
private void initialize() {
// For adding user code into this block, select "Design" item in the inspector and invoke property editor on Action property in Properties window.
setupIndicatorImage();
setupImageList();
published=new boolean[get_ImageServerList().size()];
javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_ImageServerList());
bt_server = new BTImageServer(this);
}
/**
* Called by the system to indicate that a command has been invoked on a particular displayable.
* @param command the Command that ws invoked
* @param displayable the Displayable on which the command was invoked
**/
public void commandAction(javax.microedition.lcdui.Command command, javax.microedition.lcdui.Displayable displayable) {
if (displayable == ImageServerList) {
if (command == addCommand) {
// For adding user code into this block, select "Design | Screens | ImageServerList [List] | Assigned Commands | addCommand" item in the inspector and invoke property editor on Action property in Properties window.
publishImageAction();
} else if (command == exitCommand) {
// For adding user code into this block, select "Design | Screens | ImageServerList [List] | Assigned Commands | exitCommand" item in the inspector and invoke property editor on Action property in Properties window.
javax.microedition.lcdui.Display.getDisplay(this).setCurrent(null);
destroyApp(true);
notifyDestroyed();
} else if (command == helpCommand) {
// For adding user code into this block, select "Design | Screens | ImageServerList [List] | Assigned Commands | helpCommand" item in the inspector and invoke property editor on Action property in Properties window.
javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_helpScreen(), get_ImageServerList());
} else if (command == removeCommand) {
// For adding user code into this block, select "Design | Screens | ImageServerList [List] | Assigned Commands | removeCommand" item in the inspector and invoke property editor on Action property in Properties window.
unpublishImageAction();
}
}
}
/**
* This method returns instance for ImageServerList component and should be called instead of accessing ImageServerList field directly.
* @return Instance for ImageServerList component
**/
private javax.microedition.lcdui.List get_ImageServerList() {
if (ImageServerList == null) {
ImageServerList = new javax.microedition.lcdui.List("Configure Server", javax.microedition.lcdui.Choice.IMPLICIT, new java.lang.String[0], new javax.microedition.lcdui.Image[0]);
ImageServerList.addCommand(get_addCommand());
ImageServerList.addCommand(get_removeCommand());
ImageServerList.addCommand(get_exitCommand());
ImageServerList.addCommand(get_helpCommand());
ImageServerList.setCommandListener(this);
ImageServerList.setSelectedFlags(new boolean[0]);
}
return ImageServerList;
}
/**
* This method returns instance for helpScreen component and should be called instead of accessing helpScreen field directly.
* @return Instance for helpScreen component
**/
private javax.microedition.lcdui.Alert get_helpScreen() {
if (helpScreen == null) {
helpScreen = new javax.microedition.lcdui.Alert("Help", "The server is started by default. No images are published initially. Change this by corresponding commands - the changes take effect immediately. If image is removed from the published list, it can\'t be downloaded.", null, javax.microedition.lcdui.AlertType.INFO);
helpScreen.setTimeout(-2);
}
return helpScreen;
}
/**
* This method returns instance for addCommand component and should be called instead of accessing addCommand field directly.
* @return Instance for addCommand component
**/
private javax.microedition.lcdui.Command get_addCommand() {
if (addCommand == null) {
addCommand = new javax.microedition.lcdui.Command("Publish Image", javax.microedition.lcdui.Command.SCREEN, 1);
}
return addCommand;
}
/**
* This method returns instance for removeCommand component and should be called instead of accessing removeCommand field directly.
* @return Instance for removeCommand component
**/
private javax.microedition.lcdui.Command get_removeCommand() {
if (removeCommand == null) {
removeCommand = new javax.microedition.lcdui.Command("Remove Image", javax.microedition.lcdui.Command.SCREEN, 1);
}
return removeCommand;
}
/**
* This method returns instance for exitCommand component and should be called instead of accessing exitCommand field directly.
* @return Instance for exitCommand component
**/
private javax.microedition.lcdui.Command get_exitCommand() {
if (exitCommand == null) {
exitCommand = new javax.microedition.lcdui.Command("Exit", javax.microedition.lcdui.Command.EXIT, 1);
}
return exitCommand;
}
/**
* This method returns instance for helpCommand component and should be called instead of accessing helpCommand field directly.
* @return Instance for helpCommand component
**/
private javax.microedition.lcdui.Command get_helpCommand() {
if (helpCommand == null) {
helpCommand = new javax.microedition.lcdui.Command("Help", javax.microedition.lcdui.Command.HELP, 1);
}
return helpCommand;
}
/**
* This method returns instance for BlueToothAlert component and should be called instead of accessing BlueToothAlert field directly.
* @return Instance for BlueToothAlert component
**/
private javax.microedition.lcdui.Alert get_BlueToothAlert() {
if (BlueToothAlert == null) {
BlueToothAlert = new javax.microedition.lcdui.Alert("Error", "", null, javax.microedition.lcdui.AlertType.ERROR);
BlueToothAlert.setTimeout(3000);
destroyApp(true);
notifyDestroyed();
}
return BlueToothAlert;
}
javax.microedition.lcdui.List ImageServerList;
javax.microedition.lcdui.Alert helpScreen;
javax.microedition.lcdui.Command addCommand;
javax.microedition.lcdui.Command removeCommand;
javax.microedition.lcdui.Command exitCommand;
javax.microedition.lcdui.Command helpCommand;
javax.microedition.lcdui.Alert BlueToothAlert;
// --- This is the end of auto-generated section.//</editor-fold>//GEN-END:MVDMethods
/** Gets the image file name from its title (label). */
public String getImageFileName(String fname) {
if (fname == null) {
return null;
}
// no interface in List to get the index - should find
int index = -1;
for (int i = 0; i < get_ImageServerList().size(); i++) {
if (get_ImageServerList().getString(i).equals(fname)) {
index = i;
break;
}
}
// not found or not published
if (index == -1 || !published[index]) {
return null;
}
return (String) imageNames.elementAt(index);
}
/**
* Gets the description of images from manifest and
* prepares the list to contol the configuration.
* <p>
* The attributes are named "ImageTitle-n" and "ImageImage-n".
* The value "n" must start at "1" and be incremented by 1.
*/
private void setupImageList() {
imageNames = new Vector();
get_ImageServerList().deleteAll();
for (int n = 1; n < 100; n++) {
String name = this.getAppProperty("ImageName-" + n);
// no more images available
if (name == null || name.length() == 0) {
break;
}
String label = this.getAppProperty("ImageTitle-" + n);
// no lable available - use picture name instead
if (label == null || label.length() == 0) {
label = name;
}
imageNames.addElement(name);
get_ImageServerList().append(label, offImage);
}
}
/**
* Creates the image to idicate the base state.
*/
private void setupIndicatorImage() {
// create "on" image
try {
onImage = Image.createImage("/images/st-on.png");
} catch (java.io.IOException e) {
// provide off-screen image then
onImage = createIndicatorImage(12, 12, 0, 255, 0);
}
// create "off" image
try {
offImage = Image.createImage("/images/st-off.png");
} catch (java.io.IOException e) {
// provide off-screen image then
offImage = createIndicatorImage(12, 12, 255, 0, 0);
}
}
/**
* Creates the off-screen image with specified size an color.
*/
private Image createIndicatorImage(int w, int h, int r, int g, int b) {
Image res = Image.createImage(w, h);
Graphics gc = res.getGraphics();
gc.setColor(r, g, b);
gc.fillRect(0, 0, w, h);
return res;
}
/**
* We have to provide this method due to "do not do network
* operation in command listener method" restriction, which
* is caused by crooked midp design.
*
* This method is called by BTImageServer after it is done
* with bluetooth initialization and next screen is ready
* to appear.
*/
public void completeInitialization(boolean isBTReady) {
// bluetooth was initialized successfully.
if (isBTReady) {
Ticker t = new Ticker("Choose images you want to publish...");
get_ImageServerList().setTicker(t);
return;
}
// something wrong
get_BlueToothAlert().setString("Can't inititialize bluetooth");
Display.getDisplay(this).setCurrent(get_BlueToothAlert());
}
public void startApp() {
initialize();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -