1 19 20 21 package org.netbeans.modules.group; 22 23 24 import java.awt.BorderLayout ; 25 import java.io.IOException ; 26 import java.util.ArrayList ; 27 import javax.swing.JLabel ; 28 import javax.swing.JPanel ; 29 import javax.swing.JTextField ; 30 import org.openide.ErrorManager; 31 import org.openide.awt.Mnemonics; 32 import org.openide.filesystems.FileObject; 33 import org.openide.filesystems.FileStateInvalidException; 34 import org.openide.loaders.DataFolder; 35 import org.openide.loaders.DataObject; 36 import org.openide.nodes.Node; 37 import org.openide.nodes.NodeAcceptor; 38 import org.openide.loaders.DataFilter; 39 import org.openide.loaders.RepositoryNodeFactory; 40 import org.openide.nodes.NodeOperation; 41 import org.openide.util.HelpCtx; 42 import org.openide.util.NbBundle; 43 import org.openide.util.UserCancelException; 44 import org.openide.util.actions.CookieAction; 45 46 47 54 public class CreateGroupAction extends CookieAction { 55 56 57 static final long serialVersionUID =-280394671195477993L; 58 59 60 61 protected void performAction(final Node[] nodes) { 62 try { 63 FileObject fo = selectFile(); 64 ArrayList list; 65 66 if (fo != null) { 67 list = new ArrayList (); 68 for(int i = 0; i < nodes.length; i++) { 69 Object obj = nodes[i].getCookie(DataObject.class); 70 if (obj != null) { 71 list.add(GroupShadow.getLinkName(((DataObject)obj).getPrimaryFile())); 72 } 73 } 74 GroupShadow.writeLinks(list, fo); 75 } 76 } catch(IOException ex) { 77 ErrorManager.getDefault().notify(ex); 78 } 79 } 80 81 82 protected String iconResource () { 83 return "org/netbeans/modules/group/resources/groupShadow.gif"; } 85 86 88 protected Class [] cookieClasses() { 89 return new Class [] { DataObject.class }; 90 } 91 92 96 protected boolean enable (Node[] activatedNodes) { 97 boolean enable = super.enable (activatedNodes); 98 99 if ( enable ) { for (int i = 0; i < activatedNodes.length; i++) { 102 DataObject dataObject = (DataObject)activatedNodes[i].getCookie (DataObject.class); 103 FileObject fileObject = dataObject.getPrimaryFile(); 104 try { 105 if ( fileObject.isRoot() || fileObject.getFileSystem().isDefault() ) { 106 enable = false; 107 break; 108 } 109 } catch (FileStateInvalidException fsie) { 110 } 111 } } 113 114 return enable; 115 } 116 117 119 protected int mode() { 120 return MODE_ALL; 121 } 122 123 124 public String getName() { 125 return NbBundle.getBundle(CreateGroupAction.class).getString("CreateGroup"); 126 } 127 128 129 public HelpCtx getHelpCtx() { 130 return new HelpCtx(CreateGroupAction.class); 131 } 132 133 136 protected static FileObject selectFile() throws IOException { 137 138 InputPanel jp = new InputPanel(); 139 140 try { 141 Node an = RepositoryNodeFactory.getDefault() 143 .repository(DataFilter.ALL); 144 145 NodeAcceptor na = new NodeAcceptor() { 146 public boolean acceptNodes(Node[] nodes) { 147 if (nodes == null || nodes.length != 1) { 148 return false; 149 } 150 DataFolder cookie = (DataFolder)nodes[0].getCookie (DataFolder.class); 151 return cookie != null && !cookie.getPrimaryFile ().isReadOnly (); 152 } 153 }; 154 155 Node[] nodes = NodeOperation.getDefault().select( 157 NbBundle.getBundle (CreateGroupAction.class).getString ("PROP_Select_File"), 158 NbBundle.getBundle (CreateGroupAction.class).getString ("PROP_Look_In"), 159 an, na, jp 160 ); 161 162 FileObject folder = ((DataFolder)nodes[0].getCookie(DataFolder.class)).getPrimaryFile(); 163 return folder.createData(jp.getText(), GroupShadow.GS_EXTENSION ); 164 } catch (UserCancelException ex) { 165 return null; 166 } catch (NullPointerException ex) { 167 return null; 170 } 171 } 173 174 175 private static class InputPanel extends JPanel { 176 177 178 JTextField text; 179 180 181 static final long serialVersionUID =2856913107896554654L; 182 183 184 185 public InputPanel () { 186 BorderLayout lay = new BorderLayout (); 187 lay.setVgap(5); 188 lay.setHgap(5); 189 setLayout (lay); 190 191 JLabel label = new JLabel (); 193 Mnemonics.setLocalizedText(label, NbBundle.getBundle (CreateGroupAction.class).getString ("CTL_Group_Name")); 194 195 text = new JTextField (); 196 text.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle (CreateGroupAction.class).getString ("ACS_TextField")); 197 198 label.setLabelFor(text); 199 add (BorderLayout.WEST, label); 200 add (BorderLayout.CENTER, text); 201 } 202 203 204 205 public void requestFocus () { 206 text.requestFocus (); 207 } 208 209 210 public boolean requestFocusInWindow () { 211 return text.requestFocusInWindow (); 212 } 213 214 215 public String getText () { 216 return text.getText (); 217 } 218 219 220 public void setText (String s) { 221 setText (s); 222 } 223 } } 225 | Popular Tags |