1 19 20 package org.netbeans.modules.project.ui; 21 22 import java.awt.Dialog ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.beans.PropertyVetoException ; 26 import java.util.ArrayList ; 27 import java.util.Collections ; 28 import java.util.List ; 29 import java.util.StringTokenizer ; 30 import javax.swing.JButton ; 31 import javax.swing.JScrollPane ; 32 import org.netbeans.api.project.Project; 33 import org.netbeans.api.project.SourceGroup; 34 import org.netbeans.api.queries.VisibilityQuery; 35 import org.openide.DialogDescriptor; 36 import org.openide.DialogDisplayer; 37 import org.openide.awt.Mnemonics; 38 import org.openide.explorer.ExplorerManager; 39 import org.openide.explorer.view.BeanTreeView; 40 import org.openide.filesystems.FileObject; 41 import org.openide.loaders.DataFolder; 42 import org.openide.loaders.DataObject; 43 import org.openide.nodes.Children; 44 import org.openide.nodes.Node; 45 import org.openide.nodes.AbstractNode; 46 import org.openide.nodes.FilterNode; 47 import org.openide.nodes.NodeNotFoundException; 48 import org.openide.nodes.NodeOp; 49 import org.openide.util.NbBundle; 50 import org.openide.util.NbCollections; 51 52 56 public class BrowseFolders extends javax.swing.JPanel implements ExplorerManager.Provider { 57 58 private ExplorerManager manager; 59 private SourceGroup[] folders; 60 private BeanTreeView btv; 61 private Project project; 62 63 private static JScrollPane SAMPLE_SCROLL_PANE = new JScrollPane (); 64 65 66 public BrowseFolders( SourceGroup[] folders, Project project, String preselectedFileName ) { 67 initComponents(); 68 this.folders = folders; 69 this.project = project; 70 71 manager = new ExplorerManager(); 72 AbstractNode rootNode = new AbstractNode( new SourceGroupsChildren( folders, project ) ); 73 manager.setRootContext( rootNode ); 74 75 btv = new BeanTreeView(); 77 btv.setRootVisible( false ); 78 btv.setSelectionMode( javax.swing.tree.TreeSelectionModel.SINGLE_TREE_SELECTION ); 79 btv.setBorder( SAMPLE_SCROLL_PANE.getBorder() ); 80 btv.setPopupAllowed( false ); 81 btv.getAccessibleContext ().setAccessibleName (NbBundle.getMessage(BrowseFolders.class, "ACSN_BrowseFolders_folderPanel")); 82 btv.getAccessibleContext ().setAccessibleDescription (NbBundle.getMessage(BrowseFolders.class, "ACSD_BrowseFolders_folderPanel")); 83 expandSelection( preselectedFileName ); 84 folderPanel.add( btv, java.awt.BorderLayout.CENTER ); 86 } 87 88 90 public ExplorerManager getExplorerManager() { 91 return manager; 92 } 93 94 95 100 private void initComponents() { 102 java.awt.GridBagConstraints gridBagConstraints; 103 104 jLabel1 = new javax.swing.JLabel (); 105 folderPanel = new javax.swing.JPanel (); 106 107 setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 12, 12)); 108 setLayout(new java.awt.GridBagLayout ()); 109 110 jLabel1.setLabelFor(folderPanel); 111 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(BrowseFolders.class, "LBL_BrowseFolders_jLabel1")); gridBagConstraints = new java.awt.GridBagConstraints (); 113 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 114 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 115 gridBagConstraints.insets = new java.awt.Insets (0, 0, 2, 0); 116 add(jLabel1, gridBagConstraints); 117 jLabel1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(BrowseFolders.class, "ACSN_BrowseFolders_jLabel1")); 119 folderPanel.setLayout(new java.awt.BorderLayout ()); 120 gridBagConstraints = new java.awt.GridBagConstraints (); 121 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 122 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 123 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 124 gridBagConstraints.weightx = 1.0; 125 gridBagConstraints.weighty = 1.0; 126 add(folderPanel, gridBagConstraints); 127 128 getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(BrowseFolders.class, "ACSN_BrowseFolders")); getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(BrowseFolders.class, "ACSN_BrowseFolders")); } 132 133 private javax.swing.JPanel folderPanel; 135 private javax.swing.JLabel jLabel1; 136 138 public static FileObject showDialog( SourceGroup[] folders, Project project, String preselectedFileName ) { 139 140 BrowseFolders bf = new BrowseFolders( folders, project, preselectedFileName ); 141 142 JButton options[] = new JButton [] { 143 new JButton (), 144 new JButton ( NbBundle.getMessage( BrowseFolders.class, "BTN_BrowseFolders_Cancel_Option") ), }; 146 147 OptionsListener optionsListener = new OptionsListener( bf ); 148 149 options[ 0 ].setActionCommand( OptionsListener.COMMAND_SELECT ); 150 options[ 0 ].addActionListener( optionsListener ); 151 Mnemonics.setLocalizedText(options[0], NbBundle.getMessage( BrowseFolders.class, "BTN_BrowseFolders_Select_Option") ); 152 options[ 0 ].getAccessibleContext ().setAccessibleName (NbBundle.getMessage( BrowseFolders.class, "ACSN_BrowseFolders_Select_Option")); 153 options[ 0 ].getAccessibleContext ().setAccessibleDescription (NbBundle.getMessage( BrowseFolders.class, "ACSD_BrowseFolders_Select_Option")); 154 options[ 1 ].setActionCommand( OptionsListener.COMMAND_CANCEL ); 155 options[ 1 ].addActionListener( optionsListener ); 156 options[ 1 ].getAccessibleContext ().setAccessibleName (NbBundle.getMessage( BrowseFolders.class, "ACSN_BrowseFolders_Cancel_Option")); 157 options[ 1 ].getAccessibleContext ().setAccessibleDescription (NbBundle.getMessage( BrowseFolders.class, "ACSD_BrowseFolders_Cancel_Option")); 158 159 DialogDescriptor dialogDescriptor = new DialogDescriptor( 160 bf, NbBundle.getMessage( BrowseFolders.class, "LBL_BrowseFolders_Dialog"), true, options, options[ 0 ], DialogDescriptor.BOTTOM_ALIGN, null, null ); 169 dialogDescriptor.setClosingOptions( new Object [] { options[ 0 ], options[ 1 ] } ); 170 171 Dialog dialog = DialogDisplayer.getDefault().createDialog( dialogDescriptor ); 172 dialog.setVisible(true); 173 174 return optionsListener.getResult(); 175 176 } 177 178 private void expandSelection( String preselectedFileName ) { 179 180 Node root = manager.getRootContext(); 181 Children ch = root.getChildren(); 182 if ( ch == Children.LEAF ) { 183 return; 184 } 185 Node nodes[] = ch.getNodes( true ); 186 187 Node sel = null; 188 189 if ( preselectedFileName != null && preselectedFileName.length() > 0 ) { 190 for ( int i = 0; i < nodes.length; i++ ) { 192 try { 193 sel = NodeOp.findPath(nodes[i], NbCollections.checkedEnumerationByFilter(new StringTokenizer (preselectedFileName, "/"), String .class, true)); 194 break; 195 } 196 catch ( NodeNotFoundException e ) { 197 System.out.println( e.getMissingChildName() ); 198 } 200 } 201 } 202 203 if ( sel == null ) { 204 btv.expandNode( root ); 206 for ( int i = 0; i < nodes.length; i++ ) { 207 btv.expandNode( nodes[i] ); 208 if ( i == 0 ) { 209 sel = nodes[i]; 210 } 211 } 212 } 213 214 215 if ( sel != null ) { 216 try { 218 manager.setSelectedNodes( new Node[] { sel } ); 219 } 220 catch ( PropertyVetoException e ) { 221 } 223 } 224 225 } 226 227 228 245 246 248 private static final class SourceGroupsChildren extends Children.Keys<SourceGroup> { 249 250 private final SourceGroup[] groups; 251 private final Project project; 252 253 public SourceGroupsChildren(SourceGroup[] groups, Project project) { 254 assert groups != null; 255 assert project != null; 256 this.groups = groups; 257 this.project = project; 258 } 259 260 @Override 261 protected void addNotify() { 262 super.addNotify(); 263 setKeys(groups); 264 } 265 266 @Override 267 protected void removeNotify() { 268 setKeys(Collections.<SourceGroup>emptySet()); 269 super.removeNotify(); 270 } 271 272 @Override 273 protected Node[] createNodes(SourceGroup g) { 274 FileObject folder = g.getRootFolder(); 275 FilterNode fn = new FilterNode( 276 new PhysicalView.GroupNode(project, g, folder.equals(project.getProjectDirectory()), DataFolder.findFolder(folder)), 277 new SourceGroupChildren(folder, g)); 278 return new Node[] { fn }; 279 } 280 281 } 282 283 private static final class SourceGroupChildren extends Children.Keys<FileObject> { 284 285 private final SourceGroup group; 286 private final FileObject fo; 287 288 public SourceGroupChildren(FileObject fo, SourceGroup group) { 289 assert fo != null; 290 assert group != null; 291 this.fo = fo; 292 this.group = group; 293 } 294 295 @Override 296 protected void addNotify() { 297 super.addNotify(); 298 List <FileObject> l = new ArrayList <FileObject>(); 299 for (FileObject f : fo.getChildren()) { 300 if (f.isFolder() && group.contains(f) && VisibilityQuery.getDefault().isVisible(f)) { 301 l.add(f); 302 } 303 } 304 setKeys(l); 305 } 306 307 @Override 308 protected void removeNotify() { 309 setKeys(Collections.<FileObject>emptySet()); 310 super.removeNotify(); 311 } 312 313 @Override 314 protected Node[] createNodes(FileObject folder) { 315 FilterNode fn = new FilterNode( 316 DataFolder.findFolder(folder).getNodeDelegate(), 317 new SourceGroupChildren(folder, group)); 318 return new Node[] { fn }; 319 } 320 321 } 322 323 324 private static final class OptionsListener implements ActionListener { 325 326 public static final String COMMAND_SELECT = "SELECT"; 327 public static final String COMMAND_CANCEL = "CANCEL"; 328 329 private BrowseFolders browsePanel; 330 331 private FileObject result; 332 333 public OptionsListener( BrowseFolders browsePanel ) { 334 this.browsePanel = browsePanel; 335 } 336 337 public void actionPerformed( ActionEvent e ) { 338 String command = e.getActionCommand(); 339 340 if ( COMMAND_SELECT.equals( command ) ) { 341 Node selection[] = browsePanel.getExplorerManager().getSelectedNodes(); 342 343 if ( selection != null && selection.length > 0 ) { 344 DataObject dobj = (DataObject)selection[0].getLookup().lookup( DataObject.class ); 345 if ( dobj != null ) { 346 FileObject fo = dobj.getPrimaryFile(); 347 if ( fo.isFolder() ) { 348 result = fo; 349 } 350 } 351 } 352 353 354 } 355 } 356 357 public FileObject getResult() { 358 return result; 359 } 360 } 361 362 363 } 364 | Popular Tags |