1 19 20 package org.netbeans.modules.favorites.templates; 21 22 import java.awt.Dialog ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.beans.PropertyChangeListener ; 26 import java.lang.ref.Reference ; 27 import java.lang.ref.WeakReference ; 28 import java.util.Arrays ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.Set ; 32 import javax.swing.JButton ; 33 import org.openide.awt.Mnemonics; 34 import org.openide.cookies.EditCookie; 35 import org.openide.cookies.OpenCookie; 36 import org.openide.util.HelpCtx; 37 import org.openide.util.NbBundle; 38 import org.openide.util.actions.CallableSystemAction; 39 40 import org.openide.DialogDescriptor; 41 import org.openide.DialogDisplayer; 42 import org.openide.explorer.ExplorerManager; 43 import org.openide.loaders.DataObject; 44 import org.openide.nodes.Node; 45 46 50 public class TemplatesAction extends CallableSystemAction { 51 52 53 private Reference <Dialog > dialogWRef = new WeakReference <Dialog > (null); 54 55 public TemplatesAction() { 56 putValue("noIconInMenu", Boolean.TRUE); } 58 59 public void performAction () { 60 61 Dialog dialog = dialogWRef.get (); 62 63 if (dialog == null || ! dialog.isShowing ()) { 64 65 final TemplatesPanel tp = new TemplatesPanel (); 66 JButton closeButton = new JButton (); 67 Mnemonics.setLocalizedText (closeButton, NbBundle.getMessage (TemplatesAction.class, "BTN_TemplatesPanel_CloseButton")); JButton openInEditor = new JButton (); 69 openInEditor.setEnabled (false); 70 OpenInEditorListener l = new OpenInEditorListener (tp, openInEditor); 71 openInEditor.addActionListener (l); 72 tp.getExplorerManager ().addPropertyChangeListener (l); 73 Mnemonics.setLocalizedText (openInEditor, NbBundle.getMessage (TemplatesAction.class, "BTN_TemplatesPanel_OpenInEditorButton")); DialogDescriptor dd = new DialogDescriptor (tp, 75 NbBundle.getMessage (TemplatesAction.class, "LBL_TemplatesPanel_Title"), false, new Object [] { openInEditor, closeButton }, 78 closeButton, 79 DialogDescriptor.DEFAULT_ALIGN, 80 null, 81 null); 82 dd.setClosingOptions (null); 83 dd.setHelpCtx (null); 85 86 dialog = DialogDisplayer.getDefault ().createDialog (dd); 87 dialog.setVisible (true); 88 dialogWRef = new WeakReference <Dialog > (dialog); 89 90 } else { 91 dialog.toFront (); 92 } 93 94 } 95 96 protected boolean asynchronous() { 97 return true; 98 } 99 100 public String getName () { 101 return NbBundle.getMessage (TemplatesAction.class, "LBL_TemplatesAction_Name"); } 103 104 protected String iconResource () { 105 return null; 106 } 107 108 public HelpCtx getHelpCtx () { 109 return HelpCtx.DEFAULT_HELP; 110 } 111 112 115 protected void initialize () { 116 super.initialize (); 117 putProperty (TemplatesAction.SHORT_DESCRIPTION, NbBundle.getMessage (TemplatesAction.class, "HINT_TemplatesAction")); } 119 120 private static class OpenInEditorListener implements ActionListener , PropertyChangeListener { 122 TemplatesPanel tp; 123 JButton b; 124 public OpenInEditorListener (TemplatesPanel panel, JButton button) { 125 tp = panel; 126 b = button; 127 } 128 129 public void actionPerformed (ActionEvent ev) { 131 Node [] nodes = (Node []) tp.getExplorerManager ().getSelectedNodes (); 132 assert nodes != null && nodes.length > 0 : "Selected templates cannot be null or empty."; 133 Set nodes2open = getNodes2Open (nodes); 134 assert ! nodes2open.isEmpty () : "Selected templates to open cannot by empty for nodes " + Arrays.asList (nodes); 135 Iterator it = nodes2open.iterator (); 136 while (it.hasNext ()) { 137 Node n = (Node) it.next (); 138 EditCookie ec = (EditCookie) n.getLookup ().lookup (EditCookie.class); 139 if (ec != null) { 140 ec.edit (); 141 } else { 142 OpenCookie oc = (OpenCookie) n.getLookup ().lookup (OpenCookie.class); 143 if (oc != null) { 144 oc.open (); 145 } else { 146 assert false : "Node " + n + " has to have a EditCookie or OpenCookie."; 147 } 148 } 149 } 150 } 151 152 public void propertyChange (java.beans.PropertyChangeEvent evt) { 154 if (ExplorerManager.PROP_SELECTED_NODES.equals (evt.getPropertyName ())) { 155 Node [] nodes = (Node []) evt.getNewValue (); 156 boolean res = nodes != null; 157 int i = 0; 158 while (res && i < nodes.length) { 159 Node n = nodes [i]; 160 EditCookie ec = (EditCookie) n.getLookup ().lookup (EditCookie.class); 161 OpenCookie oc = (OpenCookie) n.getLookup ().lookup (OpenCookie.class); 162 res = ec != null || oc != null; 163 164 if (res) { 166 DataObject dobj = (DataObject) n.getLookup ().lookup (DataObject.class); 167 assert dobj != null : "DataObject for node " + n; 168 res = dobj.getPrimaryFile ().getSize () > 0; 169 } 170 171 i++; 172 } 173 b.setEnabled (res); 174 } 175 } 176 } 177 178 static private Set <Node> getNodes2Open (Node [] nodes) { 179 Set <Node> nodes2open = new HashSet <Node> (nodes.length); 180 for (int i = 0; i < nodes.length; i++) { 181 if (nodes [i].isLeaf ()) { 182 nodes2open.add (nodes [i]); 183 } else { 184 nodes2open.addAll (getNodes2Open (nodes [i].getChildren ().getNodes (true))); 185 } 186 } 187 return nodes2open; 188 } 189 190 } 191 | Popular Tags |