1 19 20 21 package org.netbeans.modules.i18n; 22 23 import java.awt.*; 24 import java.io.IOException ; 25 import java.util.*; 26 import javax.swing.*; 27 import org.netbeans.api.java.classpath.ClassPath; 28 import org.netbeans.api.java.project.JavaProjectConstants; 29 import org.netbeans.api.project.FileOwnerQuery; 30 import org.netbeans.api.project.Project; 31 import org.netbeans.api.project.ProjectUtils; 32 import org.netbeans.api.project.SourceGroup; 33 import org.netbeans.api.project.Sources; 34 import org.netbeans.modules.properties.PropertiesDataObject; import org.openide.filesystems.FileObject; 36 import org.openide.loaders.DataFolder; 37 import org.openide.loaders.DataObject; 38 import org.openide.nodes.AbstractNode; 39 import org.openide.nodes.Children; 40 import org.openide.nodes.Node; 41 import org.openide.nodes.NodeAcceptor; 42 import org.openide.nodes.NodeOperation; 43 import org.openide.util.UserCancelException; 44 import org.openide.filesystems.FileUtil; 45 import org.netbeans.api.java.queries.SourceForBinaryQuery; 46 import java.net.URL ; 47 import org.netbeans.api.project.ProjectInformation; 48 import org.netbeans.api.queries.VisibilityQuery; 49 import org.openide.awt.Mnemonics; 50 51 52 53 54 55 59 public class SelectorUtils { 60 61 64 public static final FilteredNode.NodeFilter BUNDLES_FILTER = 65 new FilteredNode.NodeFilter() { 66 public boolean acceptNode(Node n) { 67 DataObject dataObject = (DataObject)n.getCookie(DataObject.class); 69 if(dataObject == null) 70 return false; 71 72 return 74 ((dataObject instanceof DataFolder) && (isVisible(dataObject))) || 75 (dataObject instanceof PropertiesDataObject); } 77 }; 78 79 private static boolean isVisible(DataObject dobj) { 80 return (dobj.getPrimaryFile()==null) || 81 (VisibilityQuery.getDefault().isVisible(dobj.getPrimaryFile())); 82 83 } 84 85 86 public static final FilteredNode.NodeFilter ALL_FILTER = 87 new FilteredNode.NodeFilter() { 88 public boolean acceptNode(Node n) { 89 return true; 90 } 91 }; 92 93 94 95 101 static public DataObject selectBundle(Project prj, FileObject file) { 102 try { 103 Node root = bundlesNode(prj, file, true); 104 105 Node[] selectedNodes= 106 NodeOperation.getDefault(). 107 select( 108 Util.getString("CTL_SelectPropDO_Dialog_Title"), 109 Util.getString("CTL_SelectPropDO_Dialog_RootTitle"), 110 root, 111 new NodeAcceptor() { 112 public boolean acceptNodes(Node[] nodes) { 113 if(nodes == null || nodes.length != 1) { 114 return false; 115 } 116 117 DataObject dataObject = (DataObject)nodes[0].getCookie(DataObject.class); 119 if(dataObject == null) 120 return false; 121 122 return dataObject.getClass().equals(PropertiesDataObject.class); } 125 } 126 ); 127 return (DataObject)selectedNodes[0].getCookie(DataObject.class); 128 } catch (UserCancelException uce) { 129 return null; 132 } 133 134 } 135 136 137 146 static public Node bundlesNode(Project prj, FileObject file, boolean includeFiles) { 147 java.util.List nodes = new LinkedList(); 148 if (prj == null) 149 prj = FileOwnerQuery.getOwner(file); 150 151 ClassPath cp = ClassPath.getClassPath(file, ClassPath.EXECUTE); 152 if (cp != null) nodes.addAll(getRootNodes(prj, getRoots(cp), BUNDLES_FILTER, includeFiles)); 153 154 return createRootFor(nodes, prj); 155 } 156 157 private static java.util.List getRoots(ClassPath cp) { 158 ArrayList l = new ArrayList(cp.entries().size()); 159 Iterator eit = cp.entries().iterator(); 160 while(eit.hasNext()) { 161 ClassPath.Entry e = (ClassPath.Entry)eit.next(); 162 163 URL url = e.getURL(); 165 SourceForBinaryQuery.Result r= SourceForBinaryQuery.findSourceRoots(url); 166 FileObject [] fos = r.getRoots(); 167 if (fos.length > 0) { 168 for (int i = 0 ; i < fos.length; i++) l.add(fos[i]); 169 } else { 170 if (e.getRoot()!=null) 171 l.add(e.getRoot()); } 174 } 175 176 return l; 177 } 178 179 180 private static java.util.List getRootNodes(Project prj, 181 java.util.List roots, 182 FilteredNode.NodeFilter filter, 183 boolean includeFiles) { 184 java.util.List nodes = new ArrayList(roots.size()); 185 Iterator it = roots.iterator(); 186 while (it.hasNext()) { 187 try { 188 FileObject rfo = (FileObject)it.next(); 189 if (includeFiles || (FileUtil.toFile(rfo)!=null)) { 190 Project owner = org.netbeans.api.project.FileOwnerQuery.getOwner(rfo); 191 Node origNode = DataObject.find(rfo).getNodeDelegate(); 192 FilteredNode node = new FilteredNode(origNode,filter, getDisplayName(rfo, owner, prj!=owner)); 193 nodes.add(node); 194 } 195 } catch (org.openide.loaders.DataObjectNotFoundException ex) {} 196 } 197 return nodes; 198 } 199 200 201 private static String getDisplayName(FileObject fo, Project owner, boolean incPrjName) { 202 if (owner != null) { 203 SourceGroup grp = getSourceGroup(fo, owner); 204 String n = (grp!=null)?grp.getDisplayName():FileUtil.getFileDisplayName(fo); 205 if (incPrjName) { 206 ProjectInformation pi = ProjectUtils.getInformation(owner); 207 n += " [" + pi.getDisplayName() + "]"; 208 } 209 return n; 210 } else 211 return FileUtil.getFileDisplayName(fo); 212 } 213 214 private static SourceGroup getSourceGroup(FileObject file, Project prj) { 215 Sources src = ProjectUtils.getSources(prj); 216 SourceGroup[] srcgrps = src.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 217 for (int i = 0 ; i < srcgrps.length; i++) { 218 if (file == srcgrps[i].getRootFolder()) 219 return srcgrps[i]; 220 } 221 return null; 222 } 223 224 225 232 static public Node sourcesNode(Project prj, FilteredNode.NodeFilter filter) { 233 Sources src = ProjectUtils.getSources(prj); 234 SourceGroup[] srcgrps = src.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 235 java.util.List nodes = new ArrayList(); 236 for (int i = 0 ; i < srcgrps.length; i++) { 237 try { 238 FileObject rfo = srcgrps[i].getRootFolder(); 239 FilteredNode node = new FilteredNode(DataObject.find(rfo).getNodeDelegate(), 240 filter); 241 node.setDisplayName(srcgrps[i].getDisplayName()); 243 245 nodes.add(node); 246 } catch (org.openide.loaders.DataObjectNotFoundException ex) {} 247 } 248 249 return createRootFor(nodes, prj); 250 } 251 252 private static Node createRootFor(java.util.List nodes, Project prj) { 253 Children ch = new Children.Array(); 255 Node[] nodesArray = new Node[ nodes.size() ]; 256 nodes.toArray( nodesArray ); 257 ch.add( nodesArray ); 258 259 AbstractNode ret = new AbstractNode(ch); 260 ret.setDisplayName(ProjectUtils.getInformation(prj).getDisplayName()); 261 return ret; 262 } 263 264 265 266 267 277 public static DataObject instantiateTemplate(Project project, FileObject refFile, DataObject template) throws IOException { 278 ObjectNameInputPanel panel = new ObjectNameInputPanel(); 280 281 Node repositoryNode = SelectorUtils.bundlesNode(project, refFile, false); 282 283 DataFolder dataFolder = 285 (DataFolder)NodeOperation.getDefault().select 286 ( 287 I18nUtil.getBundle().getString ("CTL_Template_Dialog_Title"), 288 I18nUtil.getBundle().getString ("CTL_Template_Dialog_RootTitle"), 289 repositoryNode, 290 new NodeAcceptor() { 291 public boolean acceptNodes(Node[] nodes) { 292 if(nodes == null || nodes.length != 1) { 293 return false; 294 } 295 296 DataFolder cookie = (DataFolder)nodes[0].getCookie(DataFolder.class); 297 return (cookie != null && cookie.getPrimaryFile().canWrite()); 298 } 299 }, 300 panel 301 )[0].getCookie(DataFolder.class); 302 303 String name = panel.getText(); 304 305 DataObject newObject; 306 307 if(name.equals ("")) { newObject = template.createFromTemplate(dataFolder); 309 } else { 310 newObject = template.createFromTemplate(dataFolder, name); 311 } 312 313 try { 314 return newObject; 315 } catch(ClassCastException cce) { 316 throw new UserCancelException(); 317 } 318 } 319 320 public static DataObject selectOrCreateBundle(FileObject refFile, DataObject template) { 321 Node rootNode = bundlesNode(null, refFile, true); 322 FileSelector fs = new FileSelector(refFile, template); 323 fs.getDialog(I18nUtil.getBundle().getString ("CTL_SelectPropDO_Dialog_Title"), null) .setVisible(true); 325 return fs.isConfirmed() ? fs.getSelectedDataObject() : null; 326 } 327 328 329 private static class ObjectNameInputPanel extends JPanel { 330 331 332 static final long serialVersionUID = 1980214734060402958L; 333 334 335 JTextField text; 336 337 338 339 public ObjectNameInputPanel () { 340 BorderLayout layout = new BorderLayout(); 341 layout.setVgap(5); 342 layout.setHgap(5); 343 setLayout(layout); 344 345 String labelText = I18nUtil.getBundle().getString ("LBL_TemplateName"); 347 JLabel label = new JLabel(); 348 Mnemonics.setLocalizedText(label, labelText); 349 text = new JTextField(); 350 text.getAccessibleContext().setAccessibleDescription(I18nUtil.getBundle().getString ("ACS_TEXT_ObjectNameInputPanel")); 351 352 label.setLabelFor(text); 353 354 add(BorderLayout.WEST, label); 355 add(BorderLayout.CENTER, text); 356 } 357 358 359 360 public void requestFocus () { 361 text.requestFocus (); 362 } 363 364 365 public String getText () { 366 return text.getText (); 367 } 368 369 370 public void setText (String s) { 371 setText(s); 372 } 373 } 375 376 377 378 } 379 | Popular Tags |