1 19 20 package org.openide.actions; 21 22 import java.beans.PropertyChangeListener ; 23 import javax.swing.event.ChangeEvent ; 24 import javax.swing.event.ChangeListener ; 25 import javax.swing.event.EventListenerList ; 26 import org.openide.awt.Actions; 27 import org.openide.explorer.ExplorerManager; 28 import org.openide.nodes.Node; 29 import org.openide.util.Exceptions; 30 import org.openide.util.HelpCtx; 31 import org.openide.util.Lookup; 32 import org.openide.util.NbBundle; 33 import org.openide.util.actions.NodeAction; 34 import org.openide.util.actions.Presenter; 35 import org.openide.util.datatransfer.NewType; 36 import org.openide.windows.WindowManager; 37 38 43 public final class NewAction extends NodeAction { 44 45 private static ActSubMenuModel model = new ActSubMenuModel(null); 46 47 protected void performAction(Node[] activatedNodes) { 48 performAction(activatedNodes, 0); 49 } 50 51 protected boolean asynchronous() { 52 return false; 53 } 54 55 57 private static void performAction(Node[] activatedNodes, int indx) { 58 NewType[] types = getNewTypes(activatedNodes); 59 60 if (types.length <= indx) { 61 return; 62 } 63 64 performAction(activatedNodes, types[indx]); 65 } 66 67 69 private static void performAction(Node[] activatedNodes, NewType type) { 70 PasteAction.NodeSelector sel = null; 71 72 try { 73 ExplorerManager em = PasteAction.findExplorerManager(); 74 75 if (em != null) { 76 sel = new PasteAction.NodeSelector(em, activatedNodes); 77 } 78 79 type.create(); 80 } catch (java.io.IOException e) { 81 Exceptions.printStackTrace(e); 82 } finally { 83 if (sel != null) { 84 sel.select(); 85 } 86 } 87 } 88 89 91 private static NewType[] getNewTypes() { 92 return getNewTypes(WindowManager.getDefault().getRegistry().getCurrentNodes()); 93 } 94 95 97 private static NewType[] getNewTypes(Node[] activatedNodes) { 98 if ((activatedNodes == null) || (activatedNodes.length != 1)) { 99 return new NewType[0]; 100 } else { 101 return activatedNodes[0].getNewTypes(); 102 } 103 } 104 105 protected boolean enable(Node[] activatedNodes) { 106 NewType[] types = getNewTypes(); 107 108 Object [] listeners = model.getListenerList(); 110 111 if (listeners.length > 0) { 112 ChangeEvent ev = new ChangeEvent (model); 113 114 for (int i = listeners.length - 1; i >= 0; i -= 2) { 115 ((ChangeListener ) listeners[i]).stateChanged(ev); 116 } 117 } 118 119 return (types.length > 0); 120 } 121 122 public String getName() { 123 return createName(getNewTypes()); 124 } 125 126 public HelpCtx getHelpCtx() { 127 return new HelpCtx(NewAction.class); 128 } 129 130 public javax.swing.JMenuItem getMenuPresenter() { 131 return new Actions.SubMenu(this, model, false); 132 } 133 134 public javax.swing.JMenuItem getPopupPresenter() { 135 return new Actions.SubMenu(this, model, true); 136 } 137 138 public javax.swing.Action createContextAwareInstance(Lookup actionContext) { 139 return new DelegateAction(this, actionContext); 140 } 141 142 143 private static String createName(NewType[] newTypes) { 144 if ((newTypes != null) && (newTypes.length == 1)) { 145 return NbBundle.getMessage(NewAction.class, "NewArg", newTypes[0].getName()); 146 } else { 147 return NbBundle.getMessage(NewAction.class, "New"); 148 } 149 } 150 151 152 private static class ActSubMenuModel extends EventListenerList implements Actions.SubMenuModel { 153 static final long serialVersionUID = -4273674308662494596L; 154 155 158 private Lookup lookup; 159 160 private Node prevNode; 161 private NewType[] prevTypes; 162 163 ActSubMenuModel(Lookup lookup) { 164 this.lookup = lookup; 165 } 166 167 private NewType[] newTypes() { 168 if (lookup != null) { 169 java.util.Collection c = lookup.lookupResult(Node.class).allItems(); 170 171 if (c.size() == 1) { 172 java.util.Iterator it = c.iterator(); 173 174 while (it.hasNext()) { 175 Lookup.Item item = (Lookup.Item) it.next(); 176 Node n = (Node) item.getInstance(); 177 178 if (n != null) { 179 if (n == prevNode && prevTypes != null) { 180 return prevTypes; 181 } 182 prevNode = n; 183 prevTypes = n.getNewTypes(); 184 return prevTypes; 185 } 186 } 187 } 188 } 189 190 return getNewTypes(); 191 } 192 193 public int getCount() { 194 return newTypes().length; 195 } 196 197 public String getLabel(int index) { 198 NewType[] newTypes = newTypes(); 199 200 if (newTypes.length <= index) { 201 return null; 202 } else { 203 return newTypes[index].getName(); 204 } 205 } 206 207 public HelpCtx getHelpCtx(int index) { 208 NewType[] newTypes = newTypes(); 209 210 if (newTypes.length <= index) { 211 return null; 212 } else { 213 return newTypes[index].getHelpCtx(); 214 } 215 } 216 217 public void performActionAt(int index) { 218 NewType[] nt = newTypes(); 219 220 if (nt.length <= index) { 222 return; 223 } 224 225 Node[] arr; 226 227 if (lookup != null) { 228 arr = (Node[]) lookup.lookupAll(Node.class).toArray(new Node[0]); 229 } else { 230 arr = WindowManager.getDefault().getRegistry().getCurrentNodes(); 231 } 232 233 performAction(arr, nt[index]); 234 } 235 236 238 public void addChangeListener(ChangeListener l) { 239 add(ChangeListener .class, l); 240 } 241 242 244 public void removeChangeListener(ChangeListener l) { 245 remove(ChangeListener .class, l); 246 } 247 } 248 250 254 private static final class DelegateAction extends Object implements javax.swing.Action , Presenter.Menu, 255 Presenter.Popup { 256 257 private final NodeAction delegate; 258 259 260 private final ActSubMenuModel model; 261 262 public DelegateAction(NodeAction a, Lookup actionContext) { 263 this.delegate = a; 264 this.model = new ActSubMenuModel(actionContext); 265 } 266 267 268 public String toString() { 269 return super.toString() + "[delegate=" + delegate + "]"; } 271 272 274 public void actionPerformed(java.awt.event.ActionEvent e) { 275 model.performActionAt(0); 276 } 277 278 public void addPropertyChangeListener(PropertyChangeListener listener) { 279 } 280 281 public void removePropertyChangeListener(PropertyChangeListener listener) { 282 } 283 284 public void putValue(String key, Object o) { 285 } 286 287 public Object getValue(String key) { 288 if (javax.swing.Action.NAME.equals(key)) { 289 return createName(model.newTypes()); 291 } else { 292 return delegate.getValue(key); 293 } 294 } 295 296 public boolean isEnabled() { 297 return model.getCount() > 0; 298 } 299 300 public void setEnabled(boolean b) { 301 } 302 303 public javax.swing.JMenuItem getMenuPresenter() { 304 return new Actions.SubMenu(this, model, false); 305 } 306 307 public javax.swing.JMenuItem getPopupPresenter() { 308 return new Actions.SubMenu(this, model, true); 309 } 310 } 311 } 313 | Popular Tags |