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