1 19 package org.openide.actions; 20 21 import org.openide.nodes.Index; 22 import org.openide.nodes.Node; 23 import org.openide.util.HelpCtx; 24 import org.openide.util.NbBundle; 25 import org.openide.util.actions.*; 26 27 import java.lang.ref.Reference ; 28 import java.lang.ref.WeakReference ; 29 30 import javax.swing.event.ChangeEvent ; 31 import javax.swing.event.ChangeListener ; 32 33 34 40 public final class MoveDownAction extends NodeAction { 41 42 private static final String PROP_ORDER_LISTENER = "sellistener"; 44 45 private Reference curIndexCookie; 46 47 49 protected void initialize() { 50 super.initialize(); 51 52 OrderingListener sl = new OrderingListener(); 54 putProperty(PROP_ORDER_LISTENER, sl); 55 } 56 57 58 private Index getCurIndexCookie() { 59 return ((curIndexCookie == null) ? null : (Index) curIndexCookie.get()); 60 } 61 62 protected void performAction(Node[] activatedNodes) { 63 Index cookie = getIndexCookie(activatedNodes); 67 68 if (cookie == null) { 69 return; 70 } 71 72 int nodeIndex = cookie.indexOf(activatedNodes[0]); 73 74 if ((nodeIndex >= 0) && (nodeIndex < (cookie.getNodesCount() - 1))) { 75 cookie.moveDown(nodeIndex); 76 } 77 } 78 79 protected boolean asynchronous() { 80 return false; 81 } 82 83 protected boolean enable(Node[] activatedNodes) { 84 Index idx = getCurIndexCookie(); 86 87 if (idx != null) { 88 idx.removeChangeListener((ChangeListener ) getProperty(PROP_ORDER_LISTENER)); 89 idx = null; 90 } 91 92 Index cookie = getIndexCookie(activatedNodes); 93 94 if (cookie == null) { 95 return false; 96 } 97 98 int nodeIndex = cookie.indexOf(activatedNodes[0]); 99 100 cookie.addChangeListener((OrderingListener) getProperty(PROP_ORDER_LISTENER)); 102 curIndexCookie = new WeakReference (cookie); 103 104 return (nodeIndex >= 0) && (nodeIndex < (cookie.getNodesCount() - 1)); 105 } 106 107 public String getName() { 108 return NbBundle.getMessage(MoveDownAction.class, "MoveDown"); 109 } 110 111 public HelpCtx getHelpCtx() { 112 return new HelpCtx(MoveDownAction.class); 113 } 114 115 117 private Index getIndexCookie(Node[] activatedNodes) { 118 if ((activatedNodes == null) || (activatedNodes.length != 1)) { 119 return null; 120 } 121 122 Node parent = activatedNodes[0].getParentNode(); 123 124 if (parent == null) { 125 return null; 126 } 127 128 return (Index) parent.getCookie(Index.class); 129 } 130 131 133 private final class OrderingListener implements ChangeListener { 134 OrderingListener() { 135 } 136 137 public void stateChanged(ChangeEvent e) { 138 Node[] activatedNodes = getActivatedNodes(); 139 Index cookie = getIndexCookie(activatedNodes); 140 141 if (cookie == null) { 142 setEnabled(false); 143 } else { 144 int nodeIndex = cookie.indexOf(activatedNodes[0]); 145 setEnabled((nodeIndex >= 0) && (nodeIndex < (cookie.getNodesCount() - 1))); 146 } 147 } 148 } 149 } 150 | Popular Tags |