1 19 20 package org.netbeans.modules.web.wizards; 21 22 import java.awt.Dialog ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.Collection ; 28 import java.util.Collections ; 29 import javax.swing.JButton ; 30 import javax.swing.JScrollPane ; 31 import org.netbeans.api.project.SourceGroup; 32 import org.openide.util.NbBundle; 33 import org.openide.DialogDescriptor; 34 import org.openide.DialogDisplayer; 35 import org.openide.explorer.ExplorerManager; 36 import org.openide.explorer.view.BeanTreeView; 37 import org.openide.filesystems.FileObject; 38 import org.openide.loaders.DataObject; 39 import org.openide.loaders.DataObjectNotFoundException; 40 import org.openide.loaders.DataFolder; 41 import org.openide.nodes.Children; 42 import org.openide.nodes.Node; 43 import org.openide.nodes.AbstractNode; 44 import org.openide.nodes.FilterNode; 45 import org.openide.nodes.NodeNotFoundException; 46 import org.openide.nodes.NodeOp; 47 import org.openide.util.NbCollections; 48 49 51 55 public class BrowseFolders extends javax.swing.JPanel implements ExplorerManager.Provider { 56 57 private ExplorerManager manager; 58 private SourceGroup[] folders; 59 private Class target; 60 BeanTreeView btv; 61 62 private static JScrollPane SAMPLE_SCROLL_PANE = new JScrollPane (); 63 64 65 public BrowseFolders( SourceGroup[] folders, Class target, String preselectedFileName ) { 66 initComponents(); 67 getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(BrowseFolders.class, (target == DataFolder.class?"ACSD_BrowseFolders":"ACSD_BrowseFiles"))); 68 69 this.folders = folders; 70 this.target = target; 71 manager = new ExplorerManager(); 72 AbstractNode rootNode = new AbstractNode( new SourceGroupsChildren( folders ) ); 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 expandSelection( preselectedFileName ); 81 folderPanel.add( btv, java.awt.BorderLayout.CENTER ); 82 } 83 84 86 public ExplorerManager getExplorerManager() { 87 return manager; 88 } 89 90 private void expandSelection( String preselectedFileName ) { 91 92 Node root = manager.getRootContext(); 93 Children ch = root.getChildren(); 94 if ( ch == Children.LEAF ) { 95 return; 96 } 97 Node nodes[] = ch.getNodes( true ); 98 99 Node sel = null; 100 101 if ( preselectedFileName != null && preselectedFileName.length() > 0 ) { 102 for ( int i = 0; i < nodes.length; i++ ) { 104 try { 105 sel = NodeOp.findPath( nodes[i], NbCollections.checkedEnumerationByFilter( new java.util.StringTokenizer ( preselectedFileName, "/" ), String .class, false ) ); 106 break; 107 } 108 catch ( NodeNotFoundException e ) { 109 } 111 } 112 } 113 114 if ( sel == null ) { 115 btv.expandNode( root ); 117 for ( int i = 0; i < nodes.length; i++ ) { 118 btv.expandNode( nodes[i] ); 119 if ( i == 0 ) { 120 sel = nodes[i]; 121 } 122 } 123 } 124 125 126 if ( sel != null ) { 127 try { 129 manager.setSelectedNodes( new Node[] { sel } ); 130 } 131 catch ( java.beans.PropertyVetoException e ) { 132 } 134 } 135 136 } 137 138 143 private void initComponents() { 145 java.awt.GridBagConstraints gridBagConstraints; 146 147 jLabel1 = new javax.swing.JLabel (); 148 folderPanel = new javax.swing.JPanel (); 149 150 setLayout(new java.awt.GridBagLayout ()); 151 152 setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (12, 12, 12, 12))); 153 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(BrowseFolders.class, "LBL_Folders")); 154 gridBagConstraints = new java.awt.GridBagConstraints (); 155 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 156 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 157 gridBagConstraints.insets = new java.awt.Insets (0, 0, 2, 0); 158 add(jLabel1, gridBagConstraints); 159 160 folderPanel.setLayout(new java.awt.BorderLayout ()); 161 162 gridBagConstraints = new java.awt.GridBagConstraints (); 163 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 164 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 165 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 166 gridBagConstraints.weightx = 1.0; 167 gridBagConstraints.weighty = 1.0; 168 add(folderPanel, gridBagConstraints); 169 170 } 171 173 174 private javax.swing.JPanel folderPanel; 176 private javax.swing.JLabel jLabel1; 177 179 public static FileObject showDialog( SourceGroup[] folders, Class target, String preselectedFileName) { 180 181 BrowseFolders bf = new BrowseFolders( folders, target, preselectedFileName); 182 183 JButton selectButton = new JButton ( NbBundle.getMessage(BrowseFolders.class,(target == DataFolder.class?"LBL_SelectFolder":"LBL_SelectFile"))); 184 selectButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(BrowseFolders.class, (target == DataFolder.class?"ACSD_SelectFolder":"ACSD_SelectFile"))); 185 JButton cancelButton = new JButton ( NbBundle.getMessage(BrowseFolders.class,"LBL_Cancel") ); 186 cancelButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(BrowseFolders.class, "ACSD_Cancel")); 187 JButton options[] = new JButton [] { 188 selectButton, 191 cancelButton, 192 }; 193 194 OptionsListener optionsListener = new OptionsListener( bf, target ); 195 196 options[ 0 ].setActionCommand( OptionsListener.COMMAND_SELECT ); 197 options[ 0 ].addActionListener( optionsListener ); 198 options[ 1 ].setActionCommand( OptionsListener.COMMAND_CANCEL ); 199 options[ 1 ].addActionListener( optionsListener ); 200 201 DialogDescriptor dialogDescriptor = new DialogDescriptor( 202 bf, NbBundle.getMessage(BrowseFolders.class, (target == DataFolder.class? "LBL_BrowseFolders":"LBL_BrowseFiles")), true, options, options[ 0 ], DialogDescriptor.BOTTOM_ALIGN, null, null ); 211 dialogDescriptor.setClosingOptions( new Object [] { options[ 0 ], options[ 1 ] } ); 212 213 Dialog dialog = DialogDisplayer.getDefault().createDialog( dialogDescriptor ); 214 dialog.show(); 215 216 return optionsListener.getResult(); 217 218 } 219 220 221 223 225 226 private final class SourceGroupsChildren extends Children.Keys { 227 228 private SourceGroup[] groups; 229 private SourceGroup group; 230 private FileObject fo; 231 232 public SourceGroupsChildren( SourceGroup[] groups ) { 233 this.groups = groups; 234 } 235 236 public SourceGroupsChildren( FileObject fo, SourceGroup group ) { 237 this.fo = fo; 238 this.group = group; 239 } 240 241 protected void addNotify() { 242 super.addNotify(); 243 setKeys( getKeys() ); 244 } 245 246 protected void removeNotify() { 247 setKeys( Collections.EMPTY_SET ); 248 super.removeNotify(); 249 } 250 251 protected Node[] createNodes(Object key) { 252 253 FileObject fObj = null; 254 SourceGroup group = null; 255 boolean isFile=false; 256 257 if ( key instanceof SourceGroup ) { 258 fObj = ((SourceGroup)key).getRootFolder(); 259 group = (SourceGroup)key; 260 } 261 else if ( key instanceof Key ) { 262 fObj = ((Key)key).folder; 263 group = ((Key)key).group; 264 if (!fObj.isFolder()) isFile=true; 265 } 266 267 try { 268 DataObject dobj = DataObject.find( fObj ); 269 FilterNode fn = (isFile?new FilterNode(dobj.getNodeDelegate(),Children.LEAF): 270 new FilterNode(dobj.getNodeDelegate(), new SourceGroupsChildren( fObj, group ))); 271 272 if ( key instanceof SourceGroup ) { 273 fn.setDisplayName( group.getDisplayName() ); 274 } 275 276 return new Node[] { fn }; 277 } 278 catch ( DataObjectNotFoundException e ) { 279 return null; 280 } 281 } 282 283 private Collection getKeys() { 284 285 if ( groups != null ) { 286 return Arrays.asList( groups ); 287 } 288 else { 289 FileObject files[] = fo.getChildren(); 290 Arrays.sort(files,new BrowseFolders.FileObjectComparator()); 291 ArrayList children = new ArrayList ( files.length ); 292 293 if (BrowseFolders.this.target==org.openide.loaders.DataFolder.class) 294 for( int i = 0; i < files.length; i++ ) { 295 if ( files[i].isFolder() && group.contains( files[i] ) ) { 296 children.add( new Key( files[i], group ) ); 297 } 298 } 299 else { 300 for( int i = 0; i < files.length; i++ ) { 302 if ( group.contains( files[i]) && files[i].isFolder() ) children.add( new Key( files[i], group ) ); 303 } 304 for( int i = 0; i < files.length; i++ ) { 306 if ( group.contains( files[i]) && !files[i].isFolder() ) children.add( new Key( files[i], group ) ); 307 } 308 } 309 310 return children; 311 } 312 313 } 314 315 private class Key { 316 317 private FileObject folder; 318 private SourceGroup group; 319 320 private Key ( FileObject folder, SourceGroup group ) { 321 this.folder = folder; 322 this.group = group; 323 } 324 325 326 } 327 328 } 329 330 private class FileObjectComparator implements java.util.Comparator { 331 public int compare(Object o1, Object o2) { 332 FileObject fo1 = (FileObject)o1; 333 FileObject fo2 = (FileObject)o2; 334 return fo1.getName().compareTo(fo2.getName()); 335 } 336 } 337 338 private static final class OptionsListener implements ActionListener { 339 340 public static final String COMMAND_SELECT = "SELECT"; public static final String COMMAND_CANCEL = "CANCEL"; 343 private BrowseFolders browsePanel; 344 345 private FileObject result; 346 private Class target; 347 348 public OptionsListener( BrowseFolders browsePanel, Class target ) { 349 this.browsePanel = browsePanel; 350 this.target=target; 351 } 352 353 public void actionPerformed( ActionEvent e ) { 354 String command = e.getActionCommand(); 355 356 if ( COMMAND_SELECT.equals( command ) ) { 357 Node selection[] = browsePanel.getExplorerManager().getSelectedNodes(); 358 359 if ( selection != null && selection.length > 0 ) { 360 DataObject dobj = (DataObject)selection[0].getLookup().lookup( DataObject.class ); 361 if (dobj!=null && dobj.getClass().isAssignableFrom(target)) { 362 result = dobj.getPrimaryFile(); 363 } 364 372 } 373 374 375 } 376 } 377 378 public FileObject getResult() { 379 return result; 380 } 381 } 382 383 384 } 385 | Popular Tags |