1 19 20 package org.netbeans.modules.j2ee.ddloaders.web.multiview; 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 46 48 52 public class BrowseFolders extends javax.swing.JPanel implements ExplorerManager.Provider { 53 54 private ExplorerManager manager; 55 private SourceGroup[] folders; 56 57 private static JScrollPane SAMPLE_SCROLL_PANE = new JScrollPane (); 58 59 60 public BrowseFolders( SourceGroup[] folders) { 61 initComponents(); 62 this.folders = folders; 63 manager = new ExplorerManager(); 64 AbstractNode rootNode = new AbstractNode( new SourceGroupsChildren( folders ) ); 65 manager.setRootContext( rootNode ); 66 67 BeanTreeView btv = new BeanTreeView(); 69 btv.setRootVisible( false ); 70 btv.setSelectionMode( javax.swing.tree.TreeSelectionModel.SINGLE_TREE_SELECTION ); 71 btv.setBorder( SAMPLE_SCROLL_PANE.getBorder() ); 72 folderPanel.add( btv, java.awt.BorderLayout.CENTER ); 73 } 74 75 77 public ExplorerManager getExplorerManager() { 78 return manager; 79 } 80 81 82 87 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 89 90 jLabel1 = new javax.swing.JLabel (); 91 folderPanel = new javax.swing.JPanel (); 92 93 setLayout(new java.awt.GridBagLayout ()); 94 95 setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (12, 12, 12, 12))); 96 jLabel1.setText(org.openide.util.NbBundle.getMessage(BrowseFolders.class, "LBL_Folders")); 97 gridBagConstraints = new java.awt.GridBagConstraints (); 98 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 99 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 100 gridBagConstraints.insets = new java.awt.Insets (0, 0, 2, 0); 101 add(jLabel1, gridBagConstraints); 102 103 folderPanel.setLayout(new java.awt.BorderLayout ()); 104 105 gridBagConstraints = new java.awt.GridBagConstraints (); 106 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 107 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 108 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 109 gridBagConstraints.weightx = 1.0; 110 gridBagConstraints.weighty = 1.0; 111 add(folderPanel, gridBagConstraints); 112 113 } 115 116 private javax.swing.JPanel folderPanel; 118 private javax.swing.JLabel jLabel1; 119 121 public static FileObject showDialog( SourceGroup[] folders ) { 122 123 BrowseFolders bf = new BrowseFolders( folders ); 124 125 JButton options[] = new JButton [] { 126 new JButton ( NbBundle.getMessage(BrowseFolders.class,"LBL_SelectFile")), 129 new JButton ( NbBundle.getMessage(BrowseFolders.class,"LBL_Cancel") ), 130 }; 131 132 OptionsListener optionsListener = new OptionsListener( bf ); 133 134 options[ 0 ].setActionCommand( OptionsListener.COMMAND_SELECT ); 135 options[ 0 ].addActionListener( optionsListener ); 136 options[ 1 ].setActionCommand( OptionsListener.COMMAND_CANCEL ); 137 options[ 1 ].addActionListener( optionsListener ); 138 139 DialogDescriptor dialogDescriptor = new DialogDescriptor( 140 bf, NbBundle.getMessage(BrowseFolders.class, "LBL_BrowseFiles"), true, options, options[ 0 ], DialogDescriptor.BOTTOM_ALIGN, null, null ); 149 dialogDescriptor.setClosingOptions( new Object [] { options[ 0 ], options[ 1 ] } ); 150 151 Dialog dialog = DialogDisplayer.getDefault().createDialog( dialogDescriptor ); 152 dialog.setVisible(true); 153 154 return optionsListener.getResult(); 155 156 } 157 158 159 161 163 164 private final class SourceGroupsChildren extends Children.Keys { 165 166 private SourceGroup[] groups; 167 private SourceGroup group; 168 private FileObject fo; 169 170 public SourceGroupsChildren( SourceGroup[] groups ) { 171 this.groups = groups; 172 } 173 174 public SourceGroupsChildren( FileObject fo, SourceGroup group ) { 175 this.fo = fo; 176 this.group = group; 177 } 178 179 protected void addNotify() { 180 super.addNotify(); 181 setKeys( getKeys() ); 182 } 183 184 protected void removeNotify() { 185 setKeys( Collections.EMPTY_SET ); 186 super.removeNotify(); 187 } 188 189 protected Node[] createNodes(Object key) { 190 191 FileObject fObj = null; 192 SourceGroup group = null; 193 boolean isFile=false; 194 195 if ( key instanceof SourceGroup ) { 196 fObj = ((SourceGroup)key).getRootFolder(); 197 group = (SourceGroup)key; 198 } 199 else if ( key instanceof Key ) { 200 fObj = ((Key)key).folder; 201 group = ((Key)key).group; 202 if (!fObj.isFolder()) isFile=true; 203 } 204 205 try { 206 DataObject dobj = DataObject.find( fObj ); 207 FilterNode fn = (isFile?new SimpleFilterNode(dobj.getNodeDelegate(),Children.LEAF): 208 new SimpleFilterNode(dobj.getNodeDelegate(), new SourceGroupsChildren( fObj, group ))); 209 if ( key instanceof SourceGroup ) { 210 fn.setDisplayName( group.getDisplayName() ); 211 } 212 213 return new Node[] { fn }; 214 } 215 catch ( DataObjectNotFoundException e ) { 216 return null; 217 } 218 } 219 220 private Collection getKeys() { 221 222 if ( groups != null ) { 223 return Arrays.asList( groups ); 224 } 225 else { 226 FileObject files[] = fo.getChildren(); 227 Arrays.sort(files,new BrowseFolders.FileObjectComparator()); 228 ArrayList children = new ArrayList ( files.length ); 229 236 for( int i = 0; i < files.length; i++ ) { 239 if ( group.contains( files[i]) && files[i].isFolder() ) children.add( new Key( files[i], group ) ); 240 } 241 for( int i = 0; i < files.length; i++ ) { 243 if ( group.contains( files[i]) && !files[i].isFolder() ) children.add( new Key( files[i], group ) ); 244 } 245 247 return children; 248 } 249 250 } 251 252 private class Key { 253 254 private FileObject folder; 255 private SourceGroup group; 256 257 private Key ( FileObject folder, SourceGroup group ) { 258 this.folder = folder; 259 this.group = group; 260 } 261 262 263 } 264 265 } 266 267 private class FileObjectComparator implements java.util.Comparator { 268 public int compare(Object o1, Object o2) { 269 FileObject fo1 = (FileObject)o1; 270 FileObject fo2 = (FileObject)o2; 271 return fo1.getName().compareTo(fo2.getName()); 272 } 273 } 274 275 private static final class OptionsListener implements ActionListener { 276 277 public static final String COMMAND_SELECT = "SELECT"; public static final String COMMAND_CANCEL = "CANCEL"; 280 private BrowseFolders browsePanel; 281 282 private FileObject result; 283 285 public OptionsListener( BrowseFolders browsePanel ) { 286 this.browsePanel = browsePanel; 287 } 288 289 public void actionPerformed( ActionEvent e ) { 290 String command = e.getActionCommand(); 291 292 if ( COMMAND_SELECT.equals( command ) ) { 293 Node selection[] = browsePanel.getExplorerManager().getSelectedNodes(); 294 295 if ( selection != null && selection.length > 0 ) { 296 DataObject dobj = (DataObject)selection[0].getLookup().lookup( DataObject.class ); 297 result = dobj.getPrimaryFile(); 299 308 } 309 310 311 } 312 } 313 314 public FileObject getResult() { 315 return result; 316 } 317 } 318 319 class SimpleFilterNode extends FilterNode { 320 321 public SimpleFilterNode(org.openide.nodes.Node node, org.openide.nodes.Children children) { 322 super(node, children); 323 324 } 325 326 public org.openide.util.actions.SystemAction[] getActions() { 327 return new org.openide.util.actions.SystemAction[]{}; 328 } 329 public org.openide.util.actions.SystemAction getDefaultAction() { 330 return null; 331 } 332 } 333 334 } 335 | Popular Tags |