1 19 package org.openide.actions; 20 21 import java.util.logging.Logger ; 22 import org.openide.nodes.Index; 23 import org.openide.nodes.Node; 24 import org.openide.util.HelpCtx; 25 import org.openide.util.NbBundle; 26 import org.openide.util.actions.*; 27 28 import java.lang.ref.Reference ; 29 import java.lang.ref.WeakReference ; 30 31 import java.util.Arrays ; 32 33 import javax.swing.event.ChangeEvent ; 34 import javax.swing.event.ChangeListener ; 35 36 37 42 public final class MoveUpAction extends NodeAction { 43 44 private static final String PROP_ORDER_LISTENER = "sellistener"; private static Logger err = Logger.getLogger("org.openide.actions.MoveUpAction"); 47 48 private Reference curIndexCookie; 49 50 52 protected void initialize() { 53 err.fine("initialize"); 54 55 super.initialize(); 56 57 OrderingListener sl = new OrderingListener(); 59 putProperty(PROP_ORDER_LISTENER, sl); 60 } 61 62 63 private Index getCurIndexCookie() { 64 return ((curIndexCookie == null) ? null : (Index) curIndexCookie.get()); 65 } 66 67 protected void performAction(Node[] activatedNodes) { 68 Index cookie = getIndexCookie(activatedNodes); 72 73 if (cookie == null) { 74 return; 75 } 76 77 int nodeIndex = cookie.indexOf(activatedNodes[0]); 78 79 if (nodeIndex > 0) { 80 cookie.moveUp(nodeIndex); 81 } 82 } 83 84 protected boolean asynchronous() { 85 return false; 86 } 87 88 protected boolean enable(Node[] activatedNodes) { 89 err.fine( 90 "enable; activatedNodes=" + ((activatedNodes == null) ? null : Arrays.asList(activatedNodes)) 91 ); 92 93 Index idx = getCurIndexCookie(); 95 96 if (idx != null) { 97 idx.removeChangeListener((ChangeListener ) getProperty(PROP_ORDER_LISTENER)); 98 } 99 100 Index cookie = getIndexCookie(activatedNodes); 101 102 if (err != null) { 103 err.fine("enable; cookie=" + cookie); 104 } 105 106 if (cookie == null) { 107 return false; 108 } 109 110 cookie.addChangeListener((OrderingListener) getProperty(PROP_ORDER_LISTENER)); 112 curIndexCookie = new WeakReference (cookie); 113 114 int index = cookie.indexOf(activatedNodes[0]); 115 116 if (err != null) { 117 err.fine("enable; index=" + index); 118 119 if (index == -1) { 120 Node parent = activatedNodes[0].getParentNode(); 121 err.fine( 122 "enable; parent=" + parent + "; parent.children=" + Arrays.asList(parent.getChildren().getNodes()) 123 ); 124 } 125 } 126 127 return index > 0; 128 } 129 130 public String getName() { 131 return NbBundle.getMessage(MoveUpAction.class, "MoveUp"); 132 } 133 134 public HelpCtx getHelpCtx() { 135 return new HelpCtx(MoveUpAction.class); 136 } 137 138 140 private Index getIndexCookie(Node[] activatedNodes) { 141 if ((activatedNodes == null) || (activatedNodes.length != 1)) { 142 return null; 143 } 144 145 Node parent = activatedNodes[0].getParentNode(); 146 147 if (parent == null) { 148 return null; 149 } 150 151 return (Index) parent.getCookie(Index.class); 152 } 153 154 156 private final class OrderingListener implements ChangeListener { 157 OrderingListener() { 158 } 159 160 public void stateChanged(ChangeEvent e) { 161 Node[] activatedNodes = getActivatedNodes(); 162 163 err.fine( 164 "stateChanged; activatedNodes=" + ((activatedNodes == null) ? null : Arrays.asList(activatedNodes)) 165 ); 166 167 Index cookie = getIndexCookie(activatedNodes); 168 169 if (err != null) { 170 err.fine("stateChanged; cookie=" + cookie); 171 } 172 173 if (cookie == null) { 174 setEnabled(false); 175 } else { 176 int index = cookie.indexOf(activatedNodes[0]); 177 178 if (err != null) { 179 err.fine("stateChanged; index=" + index); 180 } 181 182 setEnabled(index > 0); 183 } 184 } 185 } 186 } 187 | Popular Tags |