📄 outliner.java
字号:
} /** * Returns whether this item can be outdented further. */ public boolean isOutdentable() { return ( indent > 0 ); } /** * Outdent the specified node by one unit. */ public void outdent() { outdentChildren(); setIndent( indent-1 ); } private void outdentChildren() { 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; } } } } /** * Returns whether this item can be moved up. */ public boolean canMoveUp() { if ( parentForm == null ) return false; OutlineItem item; int index = getIndex(); for ( int i = getIndex(); i > 0; i = i-1 ) { item = (OutlineItem) parentForm.get( i ); if ( item.indent == this.indent ) { return true; } if ( item.indent < this.indent ) { return false; } } return false; } /** * Outdent the specified node by one unit. */ public void moveUp() { OutlineItem item; for ( int i = getIndex()-1; i > 0; i = i-1 ) { item = (OutlineItem) parentForm.get( i ); if ( item.indent == this.indent ) { // collapse children before moving node Form f = this.parentForm; boolean collapsed = this.isCollapsed(); if ( !collapsed ) this.collapse(); this.removeFromForm(); this.insertBeforeItem( f, item ); if ( !collapsed ) this.expand(); break; } if ( item.indent < this.indent ) { break; } } updateCommands(); } /** * Returns whether this item can be moved down. */ public boolean canMoveDown() { if ( parentForm == null ) return false; OutlineItem item; int size = parentForm.size(); for ( int i = getIndex(); i < size-1; i = i+1 ) { item = (OutlineItem) parentForm.get( i ); if ( item.indent == this.indent ) { return true; } if ( item.indent < this.indent ) { return false; } } return false; } /** * Outdent the specified node by one unit. */ public void moveDown() { OutlineItem item; int size = parentForm.size(); for ( int i = getIndex(); i < size; i = i+1 ) { item = (OutlineItem) parentForm.get( i ); if ( item.indent == this.indent ) { i = i+1; Form f = this.parentForm; boolean collapsed = this.isCollapsed(); if ( !collapsed ) this.collapse(); if ( i > size ) { this.removeFromForm(); this.appendToForm( f ); } else { // collapse children before moving node item = (OutlineItem) parentForm.get( i ); this.removeFromForm(); boolean targetCollapsed = item.isCollapsed(); if ( !targetCollapsed ) item.collapse(); this.insertAfterItem( f, item ); if ( !targetCollapsed ) item.expand(); } if ( !collapsed ) this.expand(); break; } } updateCommands(); } /** * Returns whether this item has hidden children. */ public boolean isCollapsed() { return ( children != null ); } /** * Hide all subsequent items with greater indent * and add them to the hidden children list. */ public void collapse() { if ( children != null ) return; Vector hidden = new Vector(); for ( int i = 0; i < parentForm.size(); i++ ) { if ( parentForm.get( i ) == this ) { OutlineItem item; i = i + 1; while ( i < parentForm.size() ) { item = (OutlineItem)parentForm.get( i ); if ( item.indent > this.indent ) { parentForm.delete( i ); // delete item hidden.addElement( item ); } else { break; } } break; } } if ( hidden.size() > 0 ) { setHiddenChildren( hidden ); updateCommands(); } } /** * Show this nodes hidden children, if any. */ public void expand() { if ( children == null ) return; int i; int size = parentForm.size(); for ( i = 0; i < size; i++ ) { if ( parentForm.get( i ) == this ) { i = i + 1; break; } } OutlineItem item; Enumeration e = children.elements(); while ( e.hasMoreElements() ) { item = (OutlineItem) e.nextElement(); parentForm.insert( i, item ); // insert item i = i + 1; } setHiddenChildren( null ); updateCommands(); } /** * Returns the children that are currently collapsed * and are therefore not referenced by the parent form. */ protected Vector getHiddenChildren() { return children; } /** * Set the children that are currently collapsed * so that we retain a reference to them when they * are removed from the form. */ protected void setHiddenChildren( Vector inChildren ) { children = inChildren; } private boolean hasPointerPress() { return ( getInteractionModes() & POINTER_PRESS ) != 0; } private boolean hasHorizontalTraversal() { return ( getInteractionModes() & TRAVERSE_HORIZONTAL ) != 0; } private void updateCommands() { removeCommand( expandCommand ); removeCommand( collapseCommand ); if ( !hasPointerPress() ) { // fall back on commands to expand/collapse if ( isCollapsed() ) addCommand( expandCommand ); else addCommand( collapseCommand ); } removeCommand( indentCommand ); removeCommand( outdentCommand ); if ( !hasHorizontalTraversal() ) { // fall back on commands to indent/outdent if ( isIndentable() ) addCommand( indentCommand ); if ( isOutdentable() ) addCommand( outdentCommand ); } removeCommand( upCommand ); removeCommand( downCommand ); if ( canMoveUp() ) addCommand( upCommand ); if ( canMoveDown() ) addCommand( downCommand ); removeCommand( deleteCommand ); if ( getIndex() > 0 ) { // if not root addCommand( deleteCommand ); } // force a repaint if ( parentForm != null ) { invalidate(); //repaint(); } } // event handler overrides /** * Overridden to grab or drop the current item * when the FIRE key is pressed. */ protected void keyPressed( int keyCode ) { if ( getGameAction( keyCode ) == Canvas.FIRE ) { if ( isCollapsed() ) expand(); else collapse(); } } /** * Overridden to expand or collapse an item * if the 'expansion widget' is clicked. * Note that the reference implementation only * delivers this event if the item is "focused". */ protected void pointerPressed( int x, int y ) { // if in widget area if ( x < FONT_HEIGHT ) { if ( isCollapsed() ) expand(); else collapse(); } } /** * Used to indent and outdent the item when possible. */ protected boolean traverse( int dir, int viewportWidth, int viewportHeight, int[] visRect_inout ) { // this flag distinguishes between // traversing INTO this item and // traversing WITHIN this item: if ( traversingItem != this ) { // traversing INTO: mark self and return true traversingItem = this; return true; } // handle traversing WITHIN this item switch ( dir ) { case Canvas.RIGHT: if ( isIndentable() ) { indent(); } return true; case Canvas.LEFT: if ( isOutdentable() ) { outdent(); } return true; case NONE: // do nothing: just a form layout reflow return true; default: // break out } return false; } // implementation of abstract methods public int getMinContentHeight() { return FONT_HEIGHT; } public int getMinContentWidth() { return indent * INDENT_MARGIN + FONT_HEIGHT; // we might not have text to calculate font width } public int getPrefContentWidth( int height ) { return indent * INDENT_MARGIN + FONT.stringWidth( text ) + FONT_HEIGHT; } public int getPrefContentHeight( int width ) { return FONT_HEIGHT; } public void paint( Graphics g, int w, int h ) { // clear all with background color g.setColor( DISPLAY.getColor( DISPLAY.COLOR_BACKGROUND ) ); g.fillRect( 0, 0, w, h ); // now use foreground color for drawing g.setColor( DISPLAY.getColor( DISPLAY.COLOR_FOREGROUND ) ); if ( isCollapsed() ) { // draw a filled circle to represent hidden items g.fillArc( indent * INDENT_MARGIN + 2, 2, FONT_HEIGHT-7, FONT_HEIGHT-7, 0, 360 ); } else { // no hidden items so draw an empty circle g.drawArc( indent * INDENT_MARGIN + 2, 2, FONT_HEIGHT-7, FONT_HEIGHT-7, 0, 360 ); } // draw the text: word-wrap is an exercise for the reader g.drawString( text, indent * INDENT_MARGIN + FONT_HEIGHT, 0, g.TOP | g.LEFT ); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -