Ignore:
Timestamp:
2003-05-27T15:40:47+12:00 (21 years ago)
Author:
mdewsnip
Message:

Fixed tabbing.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/util/SynchronizedTreeModelTools.java

    r4293 r4364  
    3535 */
    3636public class SynchronizedTreeModelTools {
    37     /** Adds an insertNodeInto model update onto the AWT Event queue. This gets around the lack of synchronization illustrated above. */
    38     static final public Runnable insertNodeInto(final DefaultTreeModel model, final MutableTreeNode parent, final MutableTreeNode target_node) {
    39           final Runnable doInsertNodeInto = new Runnable() {
    40                      public void run() {
    41                           int index = -1;
    42                           int pos = 0;
    43                           while(index == -1 && pos < parent.getChildCount()) {
    44                                 TreeNode node = parent.getChildAt(pos);
    45                                 int result = 0;
    46                                 ///ystem.err.println("Compare " + target_node + " to " + node);
    47                                 if((target_node.isLeaf() && node.isLeaf()) || (!target_node.isLeaf() && !node.isLeaf())) {
    48                                     result = target_node.toString().toLowerCase().compareTo(node.toString().toLowerCase());
    49                                 }
    50                                 else if(target_node.isLeaf()) {
    51                                     result = -1;
    52                                 }
    53                                 else {
    54                                     result = 1;
    55                                 }
    56                                 if(result > 0) {
    57                                     ///ystem.err.println("Keep searching...");
    58                                     pos++;
    59                                 }
    60                                 else {
    61                                     ///ystem.err.println("Found!");
    62                                     index = pos;
    63                                 }
    64                           }
    65                           if(index == -1) {
    66                                 index = parent.getChildCount();
    67                           }
    68                           model.insertNodeInto(target_node, parent, index);
    69                      }
    70                 };
    71           try {
     37    /** Adds an insertNodeInto model update onto the AWT Event queue. This gets around the lack of synchronization illustrated above. */
     38    static final public Runnable insertNodeInto(final DefaultTreeModel model, final MutableTreeNode parent, final MutableTreeNode target_node) {
     39    final Runnable doInsertNodeInto = new Runnable() {
     40        public void run() {
     41            int index = -1;
     42            int pos = 0;
     43            while(index == -1 && pos < parent.getChildCount()) {
     44            TreeNode node = parent.getChildAt(pos);
     45            int result = 0;
     46            ///ystem.err.println("Compare " + target_node + " to " + node);
     47            if((target_node.isLeaf() && node.isLeaf()) || (!target_node.isLeaf() && !node.isLeaf())) {
     48                result = target_node.toString().toLowerCase().compareTo(node.toString().toLowerCase());
     49            }
     50            else if(target_node.isLeaf()) {
     51                result = -1;
     52            }
     53            else {
     54                result = 1;
     55            }
     56            if(result > 0) {
     57                ///ystem.err.println("Keep searching...");
     58                pos++;
     59            }
     60            else {
     61                ///ystem.err.println("Found!");
     62                index = pos;
     63            }
     64            }
     65            if(index == -1) {
     66            index = parent.getChildCount();
     67            }
     68            model.insertNodeInto(target_node, parent, index);
     69        }
     70        };
     71    try {
    7272                //SwingUtilities.invokeLater(doInsertNodeInto);
    73                 SwingUtilities.invokeAndWait(doInsertNodeInto);
    74           }
    75           catch (Exception e) {
    76                 e.printStackTrace();
    77           }
    78           // If (and only if) we've thrown an error because we tried to invoke the runnable task and wait, when we are in the AWTEvent thread already, then try agin but with an invoke later.
    79           catch (java.lang.Error error) {
    80                 if(error.toString().equals("java.lang.Error: Cannot call invokeAndWait from the event dispatcher thread")) {
    81                      SwingUtilities.invokeLater(doInsertNodeInto);
    82                 }
    83           }
    84           return doInsertNodeInto;
    85     }
    86     /** Adds a removeNodeFromParent model update onto the AWT Event queue. This gets around the lack of synchronization illustrated above.
    87       * @param model The <strong>GTreeModel</strong> we want to remove the node from.
    88       * @param target_node The <strong>GTreeNode</strong> to remove.
    89       */
    90     static final public void removeNodeFromParent(final DefaultTreeModel model, final MutableTreeNode target_node) {
    91           ///ystem.err.println("Remove " + target_node + " from parent in model " + model);
    92           final Runnable doRemoveNodeFromParent = new Runnable() {
    93                      public void run() {
    94                           model.removeNodeFromParent(target_node);
    95                      }
    96                 };
    97           try {
     73        SwingUtilities.invokeAndWait(doInsertNodeInto);
     74    }
     75    catch (Exception e) {
     76        e.printStackTrace();
     77    }
     78    // If (and only if) we've thrown an error because we tried to invoke the runnable task and wait, when we are in the AWTEvent thread already, then try agin but with an invoke later.
     79    catch (java.lang.Error error) {
     80        if(error.toString().equals("java.lang.Error: Cannot call invokeAndWait from the event dispatcher thread")) {
     81        SwingUtilities.invokeLater(doInsertNodeInto);
     82        }
     83    }
     84    return doInsertNodeInto;
     85    }
     86    /** Adds a removeNodeFromParent model update onto the AWT Event queue. This gets around the lack of synchronization illustrated above.
     87     * @param model The <strong>GTreeModel</strong> we want to remove the node from.
     88     * @param target_node The <strong>GTreeNode</strong> to remove.
     89     */
     90    static final public void removeNodeFromParent(final DefaultTreeModel model, final MutableTreeNode target_node) {
     91    ///ystem.err.println("Remove " + target_node + " from parent in model " + model);
     92    final Runnable doRemoveNodeFromParent = new Runnable() {
     93        public void run() {
     94            model.removeNodeFromParent(target_node);
     95        }
     96        };
     97    try {
    9898                //SwingUtilities.invokeLater(doRemoveNodeFromParent);
    99                 SwingUtilities.invokeAndWait(doRemoveNodeFromParent);
    100           }
    101           catch (Exception e) {
    102                 e.printStackTrace();
     99        SwingUtilities.invokeAndWait(doRemoveNodeFromParent);
     100    }
     101    catch (Exception e) {
     102        e.printStackTrace();
    103103                ///ystem.err.println(e);
    104           }
    105           // If we've thrown an error because we tried to invoke the runnable task and wait, when we are in the AWTEvent thread already, then try agin but with an invoke later.
    106           catch (java.lang.Error error) {
    107                 if(error.toString().equals("java.lang.Error: Cannot call invokeAndWait from the event dispatcher thread")) {
    108                      SwingUtilities.invokeLater(doRemoveNodeFromParent);
    109                 }
    110           }
    111     }
    112     /** Adds a replaceNode model update onto the AWT Event queue. This gets around the lack of synchronization illustrated above.
    113       * @param model The <strong>GTreeModel</strong> we want to change.
    114       * @param old_node The <strong>GTreeNode</strong> to replace.
    115       * @param new_node The <strong>GTreeNode</strong> to replace it with.
    116       */
    117     static final public void replaceNode(final DefaultTreeModel model, final MutableTreeNode old_node, final MutableTreeNode new_node) {
    118           final Runnable doReplaceNode = new Runnable() {
    119                      public void run() {
    120                           MutableTreeNode parent = (MutableTreeNode) old_node.getParent();
    121                           if(parent != null) {
    122                                 int index = parent.getIndex(old_node);
    123                                 model.removeNodeFromParent(old_node);
    124                                 model.insertNodeInto(new_node, parent, index);
    125                           }
    126                           parent = null;
    127                      }
    128                 };
    129           try {
     104    }
     105    // If we've thrown an error because we tried to invoke the runnable task and wait, when we are in the AWTEvent thread already, then try agin but with an invoke later.
     106    catch (java.lang.Error error) {
     107        if(error.toString().equals("java.lang.Error: Cannot call invokeAndWait from the event dispatcher thread")) {
     108        SwingUtilities.invokeLater(doRemoveNodeFromParent);
     109        }
     110    }
     111    }
     112    /** Adds a replaceNode model update onto the AWT Event queue. This gets around the lack of synchronization illustrated above.
     113     * @param model The <strong>GTreeModel</strong> we want to change.
     114     * @param old_node The <strong>GTreeNode</strong> to replace.
     115     * @param new_node The <strong>GTreeNode</strong> to replace it with.
     116     */
     117    static final public void replaceNode(final DefaultTreeModel model, final MutableTreeNode old_node, final MutableTreeNode new_node) {
     118    final Runnable doReplaceNode = new Runnable() {
     119        public void run() {
     120            MutableTreeNode parent = (MutableTreeNode) old_node.getParent();
     121            if(parent != null) {
     122            int index = parent.getIndex(old_node);
     123            model.removeNodeFromParent(old_node);
     124            model.insertNodeInto(new_node, parent, index);
     125            }
     126            parent = null;
     127        }
     128        };
     129    try {
    130130                //SwingUtilities.invokeLater(doReplaceNode);
    131                 SwingUtilities.invokeAndWait(doReplaceNode);
    132           }
    133           catch (Exception e) {
    134                 e.printStackTrace();
    135           }
    136     }
     131        SwingUtilities.invokeAndWait(doReplaceNode);
     132    }
     133    catch (Exception e) {
     134        e.printStackTrace();
     135    }
     136    }
    137137}
Note: See TracChangeset for help on using the changeset viewer.