📄 elementscreen.java.svn-base
字号:
package org.celllife.clforms;
import java.util.Date;
import java.util.Vector;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Ticker;
import org.celllife.clforms.api.XFElement;
import de.enough.polish.util.Iterator;
import de.enough.polish.util.Locale;
public class ElementScreen extends MVCComponent {
private XFElement e;
private static Displayable screen = null;
private static Command backCommand;
private static Command saveCommand;
private static Command nextCommand;
private static TextField commentField;
private static DateField datePicker;
private static ChoiceGroup selectChoice;
private int screenIndex;
private int totalScreens;
private String trueString = Locale.get("text.true"); //$NON-NLS-1$
private String falseString = Locale.get("text.false"); //$NON-NLS-1$
public ElementScreen(XFElement e) {
this.e = e;
}
public ElementScreen(XFElement e, int screenIndex, int totalScreens) {
this.e = e;
this.screenIndex = screenIndex;
this.totalScreens = totalScreens;
}
public Displayable getScreen() {
return screen;
}
public void save() {
switch (e.getDataType()) {
case Constants.INT:
if (commentField.getString().length() != 0){
e.setValue(new Integer(Integer.parseInt(commentField.getString())));
}
else {
e.setValue(null);
}
break;
case Constants.STRING:
e.setValue(commentField.getString());
break;
case Constants.DATE:
e.setValue(datePicker.getDate());
break;
case Constants.SELECT1:
String choice = selectChoice.getString(selectChoice
.getSelectedIndex());
e.setSelectedIndex(selectChoice.getSelectedIndex());
e.setValue(e.getSelect().get(choice));
break;
case Constants.SELECT:
// Find the selected values and add them to a vector
boolean selected[] = new boolean[selectChoice.size()];
// Fill array indicating whether each element is checked
selectChoice.getSelectedFlags(selected);
Vector chosenValues = new Vector();
for (int i = 0; i < selectChoice.size(); i++){
if (selected[i])
chosenValues.addElement(e.getSelect().get(selectChoice.getString(i)));
}
e.setValue(chosenValues);
break;
case Constants.BOOLEAN:
String result = selectChoice.getString(selectChoice
.getSelectedIndex());
e.setSelectedIndex(selectChoice.getSelectedIndex());
if (result.equals(this.trueString)){
e.setValue(new Boolean(true));
}else
e.setValue(new Boolean(false));
break;
}
}
/*
* Method checks if element is required, i.e. cannot be empty
* returns false if element is not required or is not empty
* returns true if element is required AND is empty
*/
public boolean checkRequired() {
boolean result = false;
//only if the XFElement is required we perform the check
if (e.isRequired()){
switch (e.getDataType()) {
case Constants.INT:
if (commentField.getString().length() == 0){
result = true;
}
else {
result = false;
}
break;
case Constants.STRING:
if (commentField.getString().length() == 0){
result = true;
}
else {
result = false;
}
break;
case Constants.DATE:
if (datePicker.getDate() == null){
result = true;
}
else {
result = false;
}
break;
case Constants.SELECT1:
// TODO - this is always has a default selected -how do we handle this?
result = false;
break;
case Constants.SELECT:
// Find the selected values and add them to a vector
boolean selected[] = new boolean[selectChoice.size()];
// Fill array indicating whether each element is checked
selectChoice.getSelectedFlags(selected);
result = true;
for (int i = 0; i < selectChoice.size(); i++){
if (selected[i])
result = false;
}
break;
case Constants.BOOLEAN:
// TODO - this is always has a default selected -how do we handle this?
result = false;
break;
}
}
return result;
}
public void requiredAlert(){
Alert alert = new Alert(Locale.get("text.alert")); //$NON-NLS-1$
alert.setType(AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
alert.setString(Locale.get("error.required")); //$NON-NLS-1$
display.setCurrent(alert);
}
public void commandAction(Command c, Displayable s) {
try {
if (c == backCommand) {
save();
(new XFormScreen()).showScreen();
} else if (c == saveCommand) {
save();
(new XFormScreen()).showScreen();
} else if (c == nextCommand) {
if (!checkRequired()){
save();
(new XFormScreen()).getNextElementScreen();
}
else
requiredAlert();
}
} catch (Exception e) {
Alert a = new Alert(Locale.get("error.screen") + " 2"); //$NON-NLS-1$
a.setString(e.getMessage());
a.setTimeout(Alert.FOREVER);
display.setCurrent(a);
}
}
// Initialize. If a data member is not backed by RMS, make sure
// it is uninitilzed (null) before you put in values.
protected void initModel() throws Exception {
}
protected void createView() throws Exception {
backCommand = new Command(Locale.get("menu.main"), Command.BACK, 2); //$NON-NLS-1$
// saveCommand = new Command("SAVE", Command.OK, 1);
nextCommand = new Command(Locale.get("menu.next"), Command.OK, 1); //$NON-NLS-1$
// screen by type
switch (e.getDataType()) {
case Constants.INT:
intForm();
break;
case Constants.STRING:
stringForm();
break;
case Constants.DATE:
dateForm();
break;
case Constants.SELECT1:
select1Form();
break;
case Constants.SELECT:
selectForm();
break;
case Constants.BOOLEAN:
booleanForm();
break;
}
setHint();
screen.addCommand(backCommand);
screen.addCommand(nextCommand);
}
public void setHint(){
Ticker t = new Ticker(Locale.get("text.hint")+e.getHintText());
((Form) screen).setTicker(t); //$NON-NLS-1$
}
private void intForm() {
commentField = new TextField(e.getLabel(), "", 40, TextField.NUMERIC); //$NON-NLS-1$
String[] parameters = new String[2];
parameters[0] = new Integer(screenIndex+1).toString();
parameters[1] = new Integer(this.totalScreens).toString();
screen = new Form(Locale.get("text.progress", parameters));
//check if the field has already been filled in - if so display value
if (e.getValue() != null){
commentField.setString(((Integer)e.getValue()).toString());
}
((Form) screen).append(commentField);
}
private void stringForm() {
commentField = new TextField(e.getLabel(), "", 40, TextField.ANY); //$NON-NLS-1$
String[] parameters = new String[2];
parameters[0] = new Integer(screenIndex+1).toString();
parameters[1] = new Integer(this.totalScreens).toString();
screen = new Form(Locale.get("text.progress", parameters));
//check if the field has already been filled in - if so display value
if (e.getValue() != null){
commentField.setString((String)e.getValue());
}
((Form) screen).append(commentField);
}
private void dateForm() {
datePicker = new DateField(e.getLabel(), DateField.DATE);
String[] parameters = new String[2];
parameters[0] = new Integer(screenIndex+1).toString();
parameters[1] = new Integer(this.totalScreens).toString();
screen = new Form(Locale.get("text.progress", parameters));
//check if the field has already been filled in - if so display value
if (e.getValue() != null){
datePicker.setDate((Date)e.getValue());
}
((Form) screen).append(datePicker);
}
private void select1Form() {
selectChoice = new ChoiceGroup(e.getLabel(), ChoiceGroup.EXCLUSIVE);
Iterator itr = e.getSelect().keysIterator();
int i =0;
while (itr.hasNext()) {
String label = (String) itr.next();
selectChoice.append(label, null);
i++;
}
String[] parameters = new String[2];
parameters[0] = new Integer(screenIndex+1).toString();
parameters[1] = new Integer(this.totalScreens).toString();
screen = new Form(Locale.get("text.progress", parameters));
//check if the field has already been filled in - if so display value
if (e.getValue() != null){
selectChoice.setSelectedIndex(e.getSelectedIndex(),true);
}
// ((Form) screen).append(e.getHintText());
((Form) screen).append(selectChoice);
}
private void selectForm() {
selectChoice = new ChoiceGroup(e.getLabel(), ChoiceGroup.MULTIPLE);
Iterator itr = e.getSelect().keysIterator();
int i = 0;
Vector v = null;
if(e.getValue() != null){
v = (Vector) e.getValue();
}
while (itr.hasNext()) {
String label = (String) itr.next();
selectChoice.append(label, null);
if(e.getValue() != null){
try{
if(v.contains(e.getSelect().get(label)))
selectChoice.setSelectedIndex(i,true);
}
catch(ClassCastException e){
System.out.println(Locale.get("error.type")); //$NON-NLS-1$
e.printStackTrace();
}
}
i++;
}
String[] parameters = new String[2];
parameters[0] = new Integer(screenIndex+1).toString();
parameters[1] = new Integer(this.totalScreens).toString();
screen = new Form(Locale.get("text.progress", parameters));
// ((Form) screen).append(e.getHintText());
((Form) screen).append(selectChoice);
}
private void booleanForm() {
selectChoice = new ChoiceGroup(e.getLabel(), ChoiceGroup.EXCLUSIVE);
selectChoice.append(this.trueString, null);
selectChoice.append(this.falseString, null);
String[] parameters = new String[2];
parameters[0] = new Integer(screenIndex+1).toString();
parameters[1] = new Integer(this.totalScreens).toString();
screen = new Form(Locale.get("text.progress", parameters));
//check if the field has already been filled in - if so display value
if (e.getValue() != null){
selectChoice.setSelectedIndex(e.getSelectedIndex(),true);
}
((Form) screen).append(selectChoice);
}
protected void updateView() throws Exception {
createView();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -