1 19 20 package org.netbeans.modules.web.struts.dialogs; 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 btv.getAccessibleContext().setAccessibleDescription( 73 NbBundle.getMessage(BrowseFolders.class, "ACSD_SelectFile")); 74 folderPanel.add( btv, java.awt.BorderLayout.CENTER ); 75 } 76 77 79 public ExplorerManager getExplorerManager() { 80 return manager; 81 } 82 83 84 89 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 91 92 jLabel1 = new javax.swing.JLabel (); 93 folderPanel = new javax.swing.JPanel (); 94 95 setLayout(new java.awt.GridBagLayout ()); 96 97 setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (12, 12, 12, 12))); 98 jLabel1.setText(org.openide.util.NbBundle.getMessage(BrowseFolders.class, "LBL_Folders")); 99 gridBagConstraints = new java.awt.GridBagConstraints (); 100 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 101 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 102 gridBagConstraints.insets = new java.awt.Insets (0, 0, 2, 0); 103 add(jLabel1, gridBagConstraints); 104 105 folderPanel.setLayout(new java.awt.BorderLayout ()); 106 107 gridBagConstraints = new java.awt.GridBagConstraints (); 108 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 109 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 110 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 111 gridBagConstraints.weightx = 1.0; 112 gridBagConstraints.weighty = 1.0; 113 add(folderPanel, gridBagConstraints); 114 115 } 117 118 private javax.swing.JPanel folderPanel; 120 private javax.swing.JLabel jLabel1; 121 123 public static FileObject showDialog( SourceGroup[] folders ) { 124 125 BrowseFolders bf = new BrowseFolders( folders ); 126 127 JButton options[] = new JButton [] { 128 new JButton ( NbBundle.getMessage(BrowseFolders.class,"LBL_SelectFile")), 131 new JButton ( NbBundle.getMessage(BrowseFolders.class,"LBL_Cancel") ), 132 }; 133 options[0].getAccessibleContext().setAccessibleDescription( 134 NbBundle.getMessage(BrowseFolders.class, "ACSD_SelectFile")); 135 options[1].getAccessibleContext().setAccessibleDescription( 136 NbBundle.getMessage(BrowseFolders.class, "ACSD_Cancel")); 137 138 OptionsListener optionsListener = new OptionsListener( bf ); 139 140 options[ 0 ].setActionCommand( OptionsListener.COMMAND_SELECT ); 141 options[ 0 ].addActionListener( optionsListener ); 142 options[ 1 ].setActionCommand( OptionsListener.COMMAND_CANCEL ); 143 options[ 1 ].addActionListener( optionsListener ); 144 145 DialogDescriptor dialogDescriptor = new DialogDescriptor( 146 bf, NbBundle.getMessage(BrowseFolders.class, "LBL_BrowseFiles"), true, options, options[ 0 ], DialogDescriptor.BOTTOM_ALIGN, null, null ); 155 dialogDescriptor.setClosingOptions( new Object [] { options[ 0 ], options[ 1 ] } ); 156 157 Dialog dialog = DialogDisplayer.getDefault().createDialog( dialogDescriptor ); 158 dialog.getAccessibleContext().setAccessibleDescription( 159 NbBundle.getMessage(BrowseFolders.class, "ACSD_BrowseFoldersDialog")); 160 dialog.setVisible(true); 161 162 return optionsListener.getResult(); 163 164 } 165 166 167 169 171 172 private final class SourceGroupsChildren extends Children.Keys { 173 174 private SourceGroup[] groups; 175 private SourceGroup group; 176 private FileObject fo; 177 178 public SourceGroupsChildren( SourceGroup[] groups ) { 179 this.groups = groups; 180 } 181 182 public SourceGroupsChildren( FileObject fo, SourceGroup group ) { 183 this.fo = fo; 184 this.group = group; 185 } 186 187 protected void addNotify() { 188 super.addNotify(); 189 setKeys( getKeys() ); 190 } 191 192 protected void removeNotify() { 193 setKeys( Collections.EMPTY_SET ); 194 super.removeNotify(); 195 } 196 197 protected Node[] createNodes(Object key) { 198 199 FileObject fObj = null; 200 SourceGroup group = null; 201 boolean isFile=false; 202 203 if ( key instanceof SourceGroup ) { 204 fObj = ((SourceGroup)key).getRootFolder(); 205 group = (SourceGroup)key; 206 } 207 else if ( key instanceof Key ) { 208 fObj = ((Key)key).folder; 209 group = ((Key)key).group; 210 if (!fObj.isFolder()) isFile=true; 211 } 212 213 try { 214 DataObject dobj = DataObject.find( fObj ); 215 FilterNode fn = (isFile?new SimpleFilterNode(dobj.getNodeDelegate(),Children.LEAF): 216 new SimpleFilterNode(dobj.getNodeDelegate(), new SourceGroupsChildren( fObj, group ))); 217 if ( key instanceof SourceGroup ) { 218 fn.setDisplayName( group.getDisplayName() ); 219 } 220 221 return new Node[] { fn }; 222 } 223 catch ( DataObjectNotFoundException e ) { 224 return null; 225 } 226 } 227 228 private Collection getKeys() { 229 230 if ( groups != null ) { 231 return Arrays.asList( groups ); 232 } 233 else { 234 FileObject files[] = fo.getChildren(); 235 Arrays.sort(files,new BrowseFolders.FileObjectComparator()); 236 ArrayList children = new ArrayList ( files.length ); 237 244 for( int i = 0; i < files.length; i++ ) { 247 if ( group.contains( files[i]) && files[i].isFolder() ) children.add( new Key( files[i], group ) ); 248 } 249 for( int i = 0; i < files.length; i++ ) { 251 if ( group.contains( files[i]) && !files[i].isFolder() ) children.add( new Key( files[i], group ) ); 252 } 253 255 return children; 256 } 257 258 } 259 260 private class Key { 261 262 private FileObject folder; 263 private SourceGroup group; 264 265 private Key ( FileObject folder, SourceGroup group ) { 266 this.folder = folder; 267 this.group = group; 268 } 269 270 271 } 272 273 } 274 275 private class FileObjectComparator implements java.util.Comparator { 276 public int compare(Object o1, Object o2) { 277 FileObject fo1 = (FileObject)o1; 278 FileObject fo2 = (FileObject)o2; 279 return fo1.getName().compareTo(fo2.getName()); 280 } 281 } 282 283 private static final class OptionsListener implements ActionListener { 284 285 public static final String COMMAND_SELECT = "SELECT"; public static final String COMMAND_CANCEL = "CANCEL"; 288 private BrowseFolders browsePanel; 289 290 private FileObject result; 291 293 public OptionsListener( BrowseFolders browsePanel ) { 294 this.browsePanel = browsePanel; 295 } 296 297 public void actionPerformed( ActionEvent e ) { 298 String command = e.getActionCommand(); 299 300 if ( COMMAND_SELECT.equals( command ) ) { 301 Node selection[] = browsePanel.getExplorerManager().getSelectedNodes(); 302 303 if ( selection != null && selection.length > 0 ) { 304 DataObject dobj = (DataObject)selection[0].getLookup().lookup( DataObject.class ); 305 result = dobj.getPrimaryFile(); 307 316 } 317 318 319 } 320 } 321 322 public FileObject getResult() { 323 return result; 324 } 325 } 326 327 class SimpleFilterNode extends FilterNode { 328 329 public SimpleFilterNode(org.openide.nodes.Node node, org.openide.nodes.Children children) { 330 super(node, children); 331 332 } 333 334 public org.openide.util.actions.SystemAction[] getActions() { 335 return new org.openide.util.actions.SystemAction[]{}; 336 } 337 public org.openide.util.actions.SystemAction getDefaultAction() { 338 return null; 339 } 340 } 341 342 } 343 | Popular Tags |