diff --git a/i18n/src/org/netbeans/modules/i18n/FileSelector.java b/i18n/src/org/netbeans/modules/i18n/FileSelector.java --- a/i18n/src/org/netbeans/modules/i18n/FileSelector.java +++ b/i18n/src/org/netbeans/modules/i18n/FileSelector.java @@ -47,6 +47,9 @@ import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.beans.PropertyVetoException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; @@ -56,6 +59,7 @@ import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import org.jdesktop.layout.GroupLayout; +import org.netbeans.api.java.classpath.ClassPath; import org.openide.ErrorManager; import org.openide.awt.Mnemonics; import org.openide.explorer.ExplorerManager; @@ -63,13 +67,17 @@ import org.openide.filesystems.FileObject; import org.openide.loaders.DataObject; import org.openide.nodes.Node; +import org.openide.util.Exceptions; import org.openide.util.HelpCtx; import org.openide.util.NbBundle; import org.netbeans.modules.properties.PropertiesDataObject; import org.openide.DialogDescriptor; import org.openide.DialogDisplayer; +import org.openide.filesystems.FileUtil; import org.openide.loaders.DataFolder; import org.openide.loaders.DataObjectNotFoundException; +import org.openide.nodes.NodeNotFoundException; +import org.openide.nodes.NodeOp; /** * Panel for selecting a properties file (by browsing a project tree). Also @@ -98,18 +106,56 @@ public FileSelector(FileObject fileInProject, DataObject template) { this(SelectorUtils.bundlesNode(null, fileInProject, template == null), template); + preselectDefaultBundle(fileInProject); } + public void preselectDefaultBundle(FileObject fo) { + ClassPath cp = ClassPath.getClassPath(fo, ClassPath.SOURCE); + if (cp == null) { + return; + } + String packageName = cp.getResourceName(fo.getParent()); + Node root = manager.getRootContext(); + List path = new ArrayList(); + for (FileObject fo2 : cp.getRoots()) { + if (FileUtil.isParentOf(fo2, fo)) { + path.add(fo2.getName()); + break; + } + } + assert path.size() == 1; + path.addAll(Arrays.asList(packageName.split("/"))); //NOI18N + path.add("Bundle"); // NOI18N + try { + manager.setSelectedNodes(new Node[] {NodeOp.findPath(root, path.toArray(new String[path.size()]))}); + return; + } catch (PropertyVetoException ex) { + Exceptions.printStackTrace(ex); + } catch (NodeNotFoundException e) { + //Ignore it + } + // removes Bundle and selects package: + path = path.subList(0, path.size()-1); + try { + manager.setSelectedNodes(new Node[] {NodeOp.findPath(root, path.toArray(new String[path.size()]))}); + return; + } catch (PropertyVetoException ex) { + Exceptions.printStackTrace(ex); + } catch (NodeNotFoundException ex) { + // ignore it + } + try { + manager.setSelectedNodes(new Node[] {root}); + } catch (PropertyVetoException ex) { + Exceptions.printStackTrace(ex); + } + } + private FileSelector(Node root, DataObject template) { this.template = template; manager = new ExplorerManager(); manager.setRootContext(root); - try { - manager.setSelectedNodes (new Node[] { root }); - } catch(PropertyVetoException ex) { - ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); - } manager.addPropertyChangeListener(this); if (template != null) { diff --git a/i18n/src/org/netbeans/modules/i18n/SelectorUtils.java b/i18n/src/org/netbeans/modules/i18n/SelectorUtils.java --- a/i18n/src/org/netbeans/modules/i18n/SelectorUtils.java +++ b/i18n/src/org/netbeans/modules/i18n/SelectorUtils.java @@ -175,13 +175,18 @@ prj = FileOwnerQuery.getOwner(file); ClassPath cp = ClassPath.getClassPath(file, ClassPath.EXECUTE); - if (cp != null) nodes.addAll(getRootNodes(prj, getRoots(cp), BUNDLES_FILTER, includeFiles)); + if (cp != null) nodes.addAll(getRootNodes(prj, getRoots(ClassPath.getClassPath(file, ClassPath.SOURCE), cp), BUNDLES_FILTER, includeFiles)); return createRootFor(nodes, prj); } - private static List getRoots(ClassPath cp) { + private static List getRoots(ClassPath sources, ClassPath cp) { ArrayList l = new ArrayList(cp.entries().size()); + for (ClassPath.Entry e : sources.entries()) { + if (e.getRoot() != null) { + l.add(e.getRoot()); + } + } for (ClassPath.Entry e : cp.entries()) { // try to map it to sources @@ -190,12 +195,16 @@ FileObject [] fos = r.getRoots(); if (fos.length > 0) { for (FileObject fo : fos) { - l.add(fo); + if (!l.contains(fo)) { + l.add(fo); + } } } else { if (e.getRoot()!=null) - l.add(e.getRoot()); // add the class-path location + if (!l.contains(e.getRoot())) { + l.add(e.getRoot()); // add the class-path location // directly + } } }