📄 outliner.java
字号:
import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import java.util.Vector;import java.util.Enumeration;/*** A simple outlining midlet.*/ public class Outliner extends MIDlet implements CommandListener, ItemCommandListener{ private static Display DISPLAY; private static Command exitCommand, editCommand, insertCommand, indentCommand, outdentCommand, expandCommand, collapseCommand, deleteCommand, okCommand, cancelCommand, upCommand, downCommand; private Form form; private TextBox textBox; private OutlineItem editedItem; // user-visible strings private final static String EXIT = "Exit"; private final static String EDIT = "Edit"; private final static String UP = "Move Up"; private final static String DOWN = "Move Down"; private final static String INDENT = "Indent"; private final static String OUTDENT = "Outdent"; private final static String EXPAND = "Expand"; private final static String COLLAPSE = "Collapse"; private final static String INSERT = "Insert"; private final static String DELETE = "Delete"; private final static String OK = "OK"; private final static String CANCEL = "Cancel"; // font for rendering items public static Font FONT = Font.getFont( Font.FONT_STATIC_TEXT ); public static int FONT_HEIGHT = Math.max( FONT.getHeight(), 9 ); public Outliner() { DISPLAY = null; editedItem = null; exitCommand = new Command( EXIT, Command.EXIT, 0 ); okCommand = new Command( OK, Command.OK, 1 ); cancelCommand = new Command( CANCEL, Command.CANCEL, 2 ); editCommand = new Command( EDIT, Command.ITEM, 1 ); insertCommand = new Command( INSERT, Command.ITEM, 2 ); indentCommand = new Command( INDENT, Command.ITEM, 3 ); outdentCommand = new Command( OUTDENT, Command.ITEM, 4 ); upCommand = new Command( UP, Command.ITEM, 5 ); downCommand = new Command( DOWN, Command.ITEM, 6 ); expandCommand = new Command( EXPAND, Command.ITEM, 7 ); collapseCommand = new Command( COLLAPSE, Command.ITEM, 8 ); deleteCommand = new Command( DELETE, Command.ITEM, 9 ); // set up outline form form = new Form( "CustomItem 演示" ); form.addCommand( exitCommand ); form.setCommandListener( this ); // setup input form textBox = new TextBox( null, "", 255, TextField.ANY ); textBox.addCommand( okCommand ); textBox.addCommand( cancelCommand ); textBox.setCommandListener( this ); OutlineItem item; item = new OutlineItem( 0, "This is an outline" ); item.setItemCommandListener( this ); item.appendToForm( form ); item = new OutlineItem( 1, "This is a nested item" ); item.setItemCommandListener( this ); item.appendToForm( form ); item = new OutlineItem( 2, "This is a double-nested item" ); item.setItemCommandListener( this ); item.appendToForm( form ); item = new OutlineItem( 1, "This is another nested item" ); item.setItemCommandListener( this ); item.appendToForm( form ); } public void startApp() { if ( DISPLAY == null ) { DISPLAY = Display.getDisplay( this ); DISPLAY.setCurrent( form ); } } public void pauseApp() { } public void destroyApp( boolean unconditional ) { } public void onShowInput() { if ( editedItem != null ) { textBox.setString( editedItem.getString() ); } DISPLAY.setCurrent( textBox ); } public void onDoInput() { if ( editedItem != null ) { editedItem.setString( textBox.getString() ); editedItem = null; } } public void onHideInput() { DISPLAY.setCurrent( form ); textBox.setString( "" ); // clear last command if any editedItem = null; } // interface CommandListener public void commandAction( Command aCommand, Displayable aDisplayable ) { if ( aCommand == exitCommand ) { destroyApp( true ); notifyDestroyed(); } else if ( aCommand == okCommand ) { onDoInput(); onHideInput(); } else if ( aCommand == cancelCommand ) { onHideInput(); } } // interface ItemCommandListener public void commandAction( Command aCommand, Item anItem ) { OutlineItem item = (OutlineItem) anItem; if ( aCommand == expandCommand ) { item.expand(); } else if ( aCommand == collapseCommand ) { item.collapse(); } else if ( aCommand == indentCommand ) { item.indent(); } else if ( aCommand == outdentCommand ) { item.outdent(); } else if ( aCommand == upCommand ) { item.moveUp(); } else if ( aCommand == downCommand ) { item.moveDown(); } else if ( aCommand == editCommand ) { editedItem = item; onShowInput(); } else if ( aCommand == insertCommand ) { OutlineItem newItem = new OutlineItem( item.indent, "" ); newItem.setItemCommandListener( this ); newItem.insertAfterItem( form, item ); } else if ( aCommand == deleteCommand ) { item.removeFromForm(); } } public static class OutlineItem extends CustomItem { /** * The number of pixels to shift for each level of indent. */ private static final int INDENT_MARGIN = 8; /** * The number of levels to indent. */ private int indent; /** * The text to display when painted. */ private String text; /** * The parent form of this item. Needed to * perform expand and collapse operations. */ private Form parentForm; /** * A list of OutlineItems that are collapsed under this one. * If this item is expanded, this vector is null. */ private Vector children; /** * The traversing item. */ private static OutlineItem traversingItem; /** * Creates an OutlineItem with the specified initial indent and text. */ public OutlineItem( int inIndent, String inText ) { // we don't want a system-supplied label super( null ); indent = inIndent; text = inText; children = null; // define layout constraitns setLayout( LAYOUT_2 | LAYOUT_LEFT | LAYOUT_TOP | LAYOUT_EXPAND | LAYOUT_NEWLINE_AFTER ); // add the commands that always apply addCommand( editCommand ); addCommand( insertCommand ); } /** * Returns the indent for this item. */ public int getIndent() { return indent; } /** * Sets the indent for this item. */ public void setIndent( int inIndent ) { indent = inIndent; updateCommands(); } /** * Returns the text for this item. */ public String getString() { return text; } /** * Sets the text for this item. */ public void setString( String inText ) { text = inText; invalidate(); } /** * Add this item to the end of the form. */ public void appendToForm( Form inForm ) { insertBeforeItem( inForm, null ); } /** * Add this item to the form before the specified item. */ public void insertBeforeItem( Form inForm, OutlineItem inItem ) { if ( parentForm != null ) { removeFromForm(); } parentForm = inForm; int i; int size = parentForm.size(); for ( i = 0; i < size; i++ ) { if ( parentForm.get( i ) == inItem ) { break; } } parentForm.insert( i, this ); if ( inItem != null ) inItem.updateCommands(); updateCommands(); } /** * Add this item to the form before the specified item. */ public void insertAfterItem( Form inForm, OutlineItem inItem ) { if ( parentForm != null ) { removeFromForm(); } parentForm = inForm; int i; int size = parentForm.size(); for ( i = 0; i < size; i++ ) { if ( parentForm.get( i ) == inItem ) { break; } } parentForm.insert( i+1, this ); if ( inItem != null ) inItem.updateCommands(); updateCommands(); } /** * Remove this item from the form. */ public void removeFromForm() { int size = parentForm.size(); for ( int i = 0; i < size; i++ ) { if ( parentForm.get( i ) == this ) { parentForm.delete( i ); // delete this break; } } } /** * Returns the index of this item on the form. * Returns -1 if the item is not on the form. */ public int getIndex() { if ( parentForm != null ) { int size = parentForm.size(); for ( int i = 0; i < size; i++ ) { if ( parentForm.get( i ) == this ) { return i; } } } return -1; } /** * Returns whether this item can be indented further. */ public boolean isIndentable() { int index = getIndex(); // root cannot be indented if ( index == 0 ) return false; OutlineItem parent = (OutlineItem)parentForm.get( index - 1 ); if ( this.indent < parent.indent ) return true; return ( !parent.isCollapsed() && this.indent <= parent.indent); } /** * Indent the specified node by one unit. */ public void indent() { indentChildren(); setIndent( indent+1 ); } private void indentChildren() { if ( isCollapsed() ) { Enumeration e = getHiddenChildren().elements(); while ( e.hasMoreElements() ) { ((OutlineItem)e.nextElement()).indent++; } } else { OutlineItem item; for ( int index = getIndex() + 1; index < parentForm.size(); index++ ) { item = (OutlineItem) parentForm.get( index ); if ( item.getIndent() > getIndent() ) // our indent hasn't changed yet { item.indent++; } else // end of children { break; } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -