1 19 20 package org.netbeans.modules.html.palette; 21 import java.io.File ; 22 import java.io.FileFilter ; 23 import org.netbeans.api.project.SourceGroup; 24 import org.openide.DialogDescriptor; 25 import org.openide.DialogDisplayer; 26 import org.openide.explorer.ExplorerManager; 27 import org.openide.explorer.view.BeanTreeView; 28 import org.openide.filesystems.FileObject; 29 import org.openide.filesystems.FileUtil; 30 import org.openide.loaders.DataObject; 31 import org.openide.loaders.DataObjectNotFoundException; 32 import org.openide.nodes.AbstractNode; 33 import org.openide.nodes.Children; 34 import org.openide.nodes.FilterNode; 35 import org.openide.nodes.Node; 36 37 import javax.swing.*; 38 import javax.swing.event.TreeSelectionEvent ; 39 import javax.swing.event.TreeSelectionListener ; 40 import java.awt.*; 41 import java.awt.event.ActionEvent ; 42 import java.awt.event.ActionListener ; 43 import java.awt.event.MouseAdapter ; 44 import java.awt.event.MouseEvent ; 45 import java.awt.event.MouseListener ; 46 import java.util.ArrayList ; 47 import java.util.Arrays ; 48 import java.util.Collection ; 49 import java.util.Collections ; 50 import org.openide.util.NbBundle; 51 52 55 public class BrowseFolders extends JPanel implements ExplorerManager.Provider { 56 57 private ExplorerManager manager; 58 59 private static JScrollPane SAMPLE_SCROLL_PANE = new JScrollPane(); 60 private FileFilter filter; 61 62 public static final FileFilter imageFileFilter = new FileFilter () { 63 public boolean accept(File pathname) { 64 return FileUtil.toFileObject(pathname).getMIMEType().startsWith("image/"); } 66 }; 67 68 69 72 public BrowseFolders(SourceGroup[] folders, FileFilter filter) { 73 initComponents(); 74 75 this.filter = filter; 76 if (this.filter == null) { 77 this.filter = new FileFilter () { 78 public boolean accept(File pathname) { 79 return true; 80 } 81 }; 82 } 83 84 manager = new ExplorerManager(); 85 AbstractNode rootNode = new AbstractNode(new SourceGroupsChildren(folders)); 86 manager.setRootContext(rootNode); 87 88 BeanTreeView btv = new BeanTreeView(); 90 btv.setRootVisible(false); 91 btv.setSelectionMode(javax.swing.tree.TreeSelectionModel.SINGLE_TREE_SELECTION); 92 btv.setBorder(SAMPLE_SCROLL_PANE.getBorder()); 93 btv.setDefaultActionAllowed(false); 94 expandFirstLevel(btv); 95 folderPanel.add(btv, java.awt.BorderLayout.CENTER); 96 } 97 98 100 public ExplorerManager getExplorerManager() { 101 return manager; 102 } 103 104 private void expandFirstLevel(BeanTreeView btv) { 105 106 Node root = manager.getRootContext(); 107 Children ch = root.getChildren(); 108 if ( ch == Children.LEAF ) { 109 return; 110 } 111 Node nodes[] = ch.getNodes(true); 112 113 btv.expandNode(root); 114 for ( int i = 0; i < nodes.length; i++ ) { 115 btv.expandNode( nodes[i] ); 116 if ( i == 0 ) { 117 try { 118 manager.setSelectedNodes( new Node[] { nodes[i] } ); 119 } 120 catch ( java.beans.PropertyVetoException e ) { 121 } 123 } 124 } 125 } 126 127 133 private void initComponents() { 135 java.awt.GridBagConstraints gridBagConstraints; 136 137 jLabel1 = new javax.swing.JLabel (); 138 folderPanel = new javax.swing.JPanel (); 139 140 setLayout(new java.awt.GridBagLayout ()); 141 142 setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (12, 12, 12, 12))); 143 jLabel1.setText(org.openide.util.NbBundle.getMessage(BrowseFolders.class, "LBL_Folders")); 144 gridBagConstraints = new java.awt.GridBagConstraints (); 145 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 146 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 147 gridBagConstraints.insets = new java.awt.Insets (0, 0, 2, 0); 148 add(jLabel1, gridBagConstraints); 149 150 folderPanel.setLayout(new java.awt.BorderLayout ()); 151 152 gridBagConstraints = new java.awt.GridBagConstraints (); 153 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 154 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 155 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 156 gridBagConstraints.weightx = 1.0; 157 gridBagConstraints.weighty = 1.0; 158 add(folderPanel, gridBagConstraints); 159 160 } 161 163 private javax.swing.JPanel folderPanel; 165 private javax.swing.JLabel jLabel1; 166 168 public static FileObject showDialog(SourceGroup[] folders) { 169 return showDialog(folders, null); 170 } 171 172 static class TreeMouseAdapter extends MouseAdapter { 173 174 JTree tree; 175 BrowseFolders bf; 176 JButton[] options; 177 178 TreeMouseAdapter(JTree tree, BrowseFolders bf, JButton[] options) { 179 this.tree = tree; 180 this.bf = bf; 181 this.options = options; 182 } 183 184 public void mouseClicked(MouseEvent e) { 185 int selRow = tree.getRowForLocation(e.getX(), e.getY()); 186 187 if ((selRow != -1) && SwingUtilities.isLeftMouseButton(e) && (e.getClickCount() % 2) == 0) { 188 FileObject fileObject = bf.getSelectedFileObject(); 189 if (fileObject != null && !fileObject.isFolder()) 190 options[0].doClick(); 191 } 192 } 193 } 194 195 public static FileObject showDialog(SourceGroup[] folders, FileFilter filter) { 196 197 final BrowseFolders bf = new BrowseFolders(folders, filter); 198 199 final JButton options[] = new JButton[]{ 200 new JButton(NbBundle.getMessage(BrowseFolders.class, "LBL_SelectFile")), 201 new JButton(NbBundle.getMessage(BrowseFolders.class, "LBL_Cancel")), 202 }; 203 204 options[0].setEnabled(false); 205 206 JTree tree = HTMLPaletteUtilities.findTreeComponent(bf); 207 tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener () { 208 public void valueChanged(TreeSelectionEvent e) { 209 FileObject fileObject = bf.getSelectedFileObject(); 210 options[0].setEnabled(fileObject != null && !fileObject.isFolder()); 211 } 212 }); 213 214 OptionsListener optionsListener = new OptionsListener(bf); 215 216 options[0].setActionCommand(OptionsListener.COMMAND_SELECT); 217 options[0].addActionListener(optionsListener); 218 options[1].setActionCommand(OptionsListener.COMMAND_CANCEL); 219 options[1].addActionListener(optionsListener); 220 221 MouseListener ml = new TreeMouseAdapter(tree, bf, options); 222 tree.addMouseListener(ml); 223 224 225 DialogDescriptor dialogDescriptor = new DialogDescriptor(bf, NbBundle.getMessage(BrowseFolders.class, "LBL_BrowseFiles"), true, options, options[0], DialogDescriptor.BOTTOM_ALIGN, null, null); 234 dialogDescriptor.setClosingOptions(new Object []{options[0], options[1]}); 235 236 Dialog dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor); 237 dialog.setVisible(true); 238 239 return optionsListener.getResult(); 240 241 } 242 243 244 private FileObject getSelectedFileObject() { 245 Node selection[] = getExplorerManager().getSelectedNodes(); 246 if (selection != null && selection.length > 0) { 247 DataObject dobj = (DataObject) selection[0].getLookup().lookup(DataObject.class); 248 return dobj.getPrimaryFile(); 249 } 250 return null; 251 } 252 253 255 258 259 private final class SourceGroupsChildren extends Children.Keys { 260 261 private SourceGroup[] groups; 262 private SourceGroup group; 263 private FileObject fo; 264 265 public SourceGroupsChildren(SourceGroup[] groups) { 266 this.groups = groups; 267 } 268 269 public SourceGroupsChildren(FileObject fo, SourceGroup group) { 270 this.fo = fo; 271 this.group = group; 272 } 273 274 protected void addNotify() { 275 super.addNotify(); 276 setKeys(getKeys()); 277 } 278 279 protected void removeNotify() { 280 setKeys(Collections.EMPTY_SET); 281 super.removeNotify(); 282 } 283 284 protected Node[] createNodes(Object key) { 285 286 FileObject fObj = null; 287 SourceGroup group = null; 288 boolean isFile = false; 289 290 if (key instanceof SourceGroup) { 291 fObj = ((SourceGroup) key).getRootFolder(); 292 group = (SourceGroup) key; 293 } else if (key instanceof Key) { 294 fObj = ((Key) key).folder; 295 group = ((Key) key).group; 296 if (!fObj.isFolder()) { 297 isFile = true; 298 } 299 } 300 301 try { 302 DataObject dobj = DataObject.find(fObj); 303 FilterNode fn = (isFile ? 304 new FilterNode(dobj.getNodeDelegate(), Children.LEAF) : 305 new FilterNode(dobj.getNodeDelegate(), new SourceGroupsChildren(fObj, group))); 306 307 if (key instanceof SourceGroup) { 308 fn.setDisplayName(group.getDisplayName()); 309 } 310 311 return new Node[]{fn}; 312 } catch (DataObjectNotFoundException e) { 313 return null; 314 } 315 } 316 317 private Collection getKeys() { 318 319 if (groups != null) { 320 return Arrays.asList(groups); 321 } else { 322 FileObject files[] = fo.getChildren(); 323 Arrays.sort(files, new FileObjectComparator()); 324 ArrayList children = new ArrayList (files.length); 325 for (int i = 0; i < files.length; i++) { 326 FileObject file = files[i]; 327 if (group.contains(files[i]) && file.isFolder()) { 328 children.add(new Key(files[i], group)); 329 } 330 } 331 for (int i = 0; i < files.length; i++) { 333 FileObject file = files[i]; 334 if (group.contains(file) && !files[i].isFolder()) { 335 if (filter.accept(FileUtil.toFile(file))) { 336 children.add(new Key(files[i], group)); 337 } 338 } 339 } 340 342 return children; 343 } 344 345 } 346 347 private class Key { 348 349 private FileObject folder; 350 private SourceGroup group; 351 352 private Key(FileObject folder, SourceGroup group) { 353 this.folder = folder; 354 this.group = group; 355 } 356 357 } 358 359 } 360 361 private class FileObjectComparator implements java.util.Comparator { 362 public int compare(Object o1, Object o2) { 363 FileObject fo1 = (FileObject) o1; 364 FileObject fo2 = (FileObject) o2; 365 return fo1.getName().compareTo(fo2.getName()); 366 } 367 } 368 369 private static final class OptionsListener implements ActionListener { 370 371 public static final String COMMAND_SELECT = "SELECT"; public static final String COMMAND_CANCEL = "CANCEL"; 374 private BrowseFolders browsePanel; 375 376 private FileObject result; 377 379 public OptionsListener(BrowseFolders browsePanel) { 380 this.browsePanel = browsePanel; 381 } 382 383 public void actionPerformed(ActionEvent e) { 384 String command = e.getActionCommand(); 385 386 if (COMMAND_SELECT.equals(command)) { 387 this.result = browsePanel.getSelectedFileObject(); 388 } 389 } 390 391 public FileObject getResult() { 392 return result; 393 } 394 } 395 396 } 397 | Popular Tags |