PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/browser/VFSBrowser.java

#
Java | 1699 lines | 1233 code | 212 blank | 254 comment | 242 complexity | 86850756872f828476079623b650ecc5 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * VFSBrowser.java - VFS browser
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2000, 2003 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.browser;
  23. //{{{ Imports
  24. import bsh.*;
  25. import javax.swing.border.EmptyBorder;
  26. import javax.swing.event.*;
  27. import javax.swing.*;
  28. import java.awt.event.*;
  29. import java.awt.*;
  30. import java.io.File;
  31. import java.util.*;
  32. import java.util.regex.Pattern;
  33. import org.gjt.sp.jedit.io.*;
  34. import org.gjt.sp.jedit.gui.*;
  35. import org.gjt.sp.jedit.msg.*;
  36. import org.gjt.sp.jedit.search.*;
  37. import org.gjt.sp.jedit.*;
  38. import org.gjt.sp.util.Log;
  39. //}}}
  40. /**
  41. * The main class of the VFS browser.
  42. * @author Slava Pestov
  43. * @version $Id: VFSBrowser.java 5487 2006-06-23 22:58:12Z kpouer $
  44. */
  45. public class VFSBrowser extends JPanel implements EBComponent, DefaultFocusComponent
  46. {
  47. public static final String NAME = "vfs.browser";
  48. //{{{ Browser types
  49. /**
  50. * Open file dialog mode. Equals JFileChooser.OPEN_DIALOG for
  51. * backwards compatibility.
  52. */
  53. public static final int OPEN_DIALOG = 0;
  54. /**
  55. * Save file dialog mode. Equals JFileChooser.SAVE_DIALOG for
  56. * backwards compatibility.
  57. */
  58. public static final int SAVE_DIALOG = 1;
  59. /**
  60. * Choose directory dialog mode.
  61. */
  62. public static final int BROWSER_DIALOG = 4;
  63. /**
  64. * Choose directory dialog mode.
  65. */
  66. public static final int CHOOSE_DIRECTORY_DIALOG = 3;
  67. /**
  68. * Stand-alone browser mode.
  69. */
  70. public static final int BROWSER = 2;
  71. //}}}
  72. //{{{ browseDirectoryInNewWindow() method
  73. /**
  74. * Opens the specified directory in a new, floating, file system browser.
  75. * @param view The view
  76. * @param path The directory's path
  77. * @since jEdit 4.1pre2
  78. */
  79. public static void browseDirectoryInNewWindow(View view, String path)
  80. {
  81. DockableWindowManager wm = view.getDockableWindowManager();
  82. if(path != null)
  83. {
  84. // this is such a bad way of doing it, but oh well...
  85. jEdit.setTemporaryProperty("vfs.browser.path.tmp",path);
  86. }
  87. wm.floatDockableWindow("vfs.browser");
  88. jEdit.unsetProperty("vfs.browser.path.tmp");
  89. } //}}}
  90. //{{{ browseDirectory() method
  91. /**
  92. * Opens the specified directory in a file system browser.
  93. * @param view The view
  94. * @param path The directory's path
  95. * @since jEdit 4.0pre3
  96. */
  97. public static void browseDirectory(View view, String path)
  98. {
  99. DockableWindowManager wm = view.getDockableWindowManager();
  100. VFSBrowser browser = (VFSBrowser)wm.getDockable(NAME);
  101. if(browser != null)
  102. {
  103. wm.showDockableWindow(NAME);
  104. browser.setDirectory(path);
  105. }
  106. else
  107. {
  108. if(path != null)
  109. {
  110. // this is such a bad way of doing it, but oh well...
  111. jEdit.setTemporaryProperty("vfs.browser.path.tmp",path);
  112. }
  113. wm.addDockableWindow("vfs.browser");
  114. jEdit.unsetProperty("vfs.browser.path.tmp");
  115. }
  116. } //}}}
  117. //{{{ getActionContext() method
  118. /**
  119. * Returns the browser action context.
  120. * @since jEdit 4.2pre1
  121. */
  122. public static ActionContext getActionContext()
  123. {
  124. return actionContext;
  125. } //}}}
  126. //{{{ VFSBrowser constructor
  127. /**
  128. * Creates a new VFS browser.
  129. * @param view The view to open buffers in by default
  130. */
  131. public VFSBrowser(View view, String position)
  132. {
  133. this(view,null,BROWSER,true,position);
  134. } //}}}
  135. //{{{ VFSBrowser constructor
  136. /**
  137. * Creates a new VFS browser.
  138. * @param view The view to open buffers in by default
  139. * @param path The path to display
  140. * @param mode The browser mode
  141. * @param multipleSelection True if multiple selection should be allowed
  142. * @param position Where the browser is located
  143. * @since jEdit 4.2pre1
  144. */
  145. public VFSBrowser(View view, String path, int mode,
  146. boolean multipleSelection, String position)
  147. {
  148. super(new BorderLayout());
  149. listenerList = new EventListenerList();
  150. this.mode = mode;
  151. this.multipleSelection = multipleSelection;
  152. this.view = view;
  153. currentEncoding = jEdit.getProperty("buffer.encoding",
  154. System.getProperty("file.encoding"));
  155. autoDetectEncoding = jEdit.getBooleanProperty(
  156. "buffer.encodingAutodetect");
  157. ActionHandler actionHandler = new ActionHandler();
  158. Box topBox = new Box(BoxLayout.Y_AXIS);
  159. horizontalLayout = (mode != BROWSER
  160. || DockableWindowManager.TOP.equals(position)
  161. || DockableWindowManager.BOTTOM.equals(position));
  162. toolbarBox = new Box(horizontalLayout
  163. ? BoxLayout.X_AXIS
  164. : BoxLayout.Y_AXIS);
  165. topBox.add(toolbarBox);
  166. GridBagLayout layout = new GridBagLayout();
  167. JPanel pathAndFilterPanel = new JPanel(layout);
  168. GridBagConstraints cons = new GridBagConstraints();
  169. cons.gridwidth = cons.gridheight = 1;
  170. cons.gridx = cons.gridy = 0;
  171. cons.fill = GridBagConstraints.BOTH;
  172. cons.anchor = GridBagConstraints.EAST;
  173. JLabel label = new JLabel(jEdit.getProperty("vfs.browser.path"),
  174. SwingConstants.RIGHT);
  175. label.setBorder(new EmptyBorder(0,0,0,12));
  176. layout.setConstraints(label,cons);
  177. pathAndFilterPanel.add(label);
  178. pathField = new HistoryTextField("vfs.browser.path");
  179. pathField.setInstantPopups(true);
  180. pathField.setEnterAddsToHistory(false);
  181. pathField.setSelectAllOnFocus(true);
  182. // because its preferred size can be quite wide, we
  183. // don't want it to make the browser way too big,
  184. // so set the preferred width to 0.
  185. Dimension prefSize = pathField.getPreferredSize();
  186. prefSize.width = 0;
  187. pathField.setPreferredSize(prefSize);
  188. pathField.addActionListener(actionHandler);
  189. cons.gridx = 1;
  190. cons.weightx = 1.0f;
  191. layout.setConstraints(pathField,cons);
  192. pathAndFilterPanel.add(pathField);
  193. filterCheckbox = new JCheckBox(jEdit.getProperty("vfs.browser.filter"));
  194. filterCheckbox.setMargin(new Insets(0,0,0,0));
  195. filterCheckbox.setRequestFocusEnabled(false);
  196. filterCheckbox.setBorder(new EmptyBorder(0,0,0,12));
  197. filterCheckbox.setSelected(jEdit.getBooleanProperty(
  198. "vfs.browser.filter-enabled"));
  199. filterCheckbox.addActionListener(actionHandler);
  200. if(mode != CHOOSE_DIRECTORY_DIALOG)
  201. {
  202. cons.gridx = 0;
  203. cons.weightx = 0.0f;
  204. cons.gridy = 1;
  205. layout.setConstraints(filterCheckbox,cons);
  206. pathAndFilterPanel.add(filterCheckbox);
  207. }
  208. filterField = new HistoryTextField("vfs.browser.filter");
  209. filterField.setInstantPopups(true);
  210. filterField.setSelectAllOnFocus(true);
  211. filterField.addActionListener(actionHandler);
  212. if(mode != CHOOSE_DIRECTORY_DIALOG)
  213. {
  214. cons.gridx = 1;
  215. cons.weightx = 1.0f;
  216. layout.setConstraints(filterField,cons);
  217. pathAndFilterPanel.add(filterField);
  218. }
  219. topBox.add(pathAndFilterPanel);
  220. add(BorderLayout.NORTH,topBox);
  221. add(BorderLayout.CENTER,browserView = new BrowserView(this));
  222. propertiesChanged();
  223. String filter;
  224. if(mode == BROWSER || !jEdit.getBooleanProperty(
  225. "vfs.browser.currentBufferFilter"))
  226. {
  227. filter = jEdit.getProperty("vfs.browser.last-filter");
  228. if(filter == null)
  229. filter = jEdit.getProperty("vfs.browser.default-filter");
  230. }
  231. else
  232. {
  233. String ext = MiscUtilities.getFileExtension(
  234. view.getBuffer().getName());
  235. if(ext.length() == 0)
  236. filter = jEdit.getProperty("vfs.browser.default-filter");
  237. else
  238. filter = "*" + ext;
  239. }
  240. filterField.setText(filter);
  241. filterField.addCurrentToHistory();
  242. updateFilterEnabled();
  243. // see VFSBrowser.browseDirectory()
  244. if(path == null)
  245. path = jEdit.getProperty("vfs.browser.path.tmp");
  246. if(path == null || path.length() == 0)
  247. {
  248. String userHome = System.getProperty("user.home");
  249. String defaultPath = jEdit.getProperty("vfs.browser.defaultPath");
  250. if(defaultPath.equals("home"))
  251. path = userHome;
  252. else if(defaultPath.equals("working"))
  253. path = System.getProperty("user.dir");
  254. else if(defaultPath.equals("buffer"))
  255. {
  256. if(view != null)
  257. {
  258. Buffer buffer = view.getBuffer();
  259. path = buffer.getDirectory();
  260. }
  261. else
  262. path = userHome;
  263. }
  264. else if(defaultPath.equals("last"))
  265. {
  266. HistoryModel pathModel = HistoryModel.getModel("vfs.browser.path");
  267. if(pathModel.getSize() == 0)
  268. path = "~";
  269. else
  270. path = pathModel.getItem(0);
  271. }
  272. else if(defaultPath.equals("favorites"))
  273. path = "favorites:";
  274. else
  275. {
  276. // unknown value??!!!
  277. path = userHome;
  278. }
  279. }
  280. final String _path = path;
  281. SwingUtilities.invokeLater(new Runnable()
  282. {
  283. public void run()
  284. {
  285. setDirectory(_path);
  286. }
  287. });
  288. } //}}}
  289. //{{{ focusOnDefaultComponent() method
  290. public void focusOnDefaultComponent()
  291. {
  292. browserView.focusOnFileView();
  293. } //}}}
  294. //{{{ addNotify() method
  295. public void addNotify()
  296. {
  297. super.addNotify();
  298. EditBus.addToBus(this);
  299. } //}}}
  300. //{{{ removeNotify() method
  301. public void removeNotify()
  302. {
  303. super.removeNotify();
  304. jEdit.setBooleanProperty("vfs.browser.filter-enabled",
  305. filterCheckbox.isSelected());
  306. if(mode == BROWSER || !jEdit.getBooleanProperty(
  307. "vfs.browser.currentBufferFilter"))
  308. {
  309. jEdit.setProperty("vfs.browser.last-filter",
  310. filterField.getText());
  311. }
  312. EditBus.removeFromBus(this);
  313. } //}}}
  314. //{{{ handleMessage() method
  315. public void handleMessage(EBMessage msg)
  316. {
  317. if(msg instanceof PropertiesChanged)
  318. propertiesChanged();
  319. else if(msg instanceof BufferUpdate)
  320. {
  321. BufferUpdate bmsg = (BufferUpdate)msg;
  322. if(bmsg.getWhat() == BufferUpdate.CREATED
  323. || bmsg.getWhat() == BufferUpdate.CLOSED)
  324. browserView.updateFileView();
  325. }
  326. else if(msg instanceof PluginUpdate)
  327. {
  328. PluginUpdate pmsg = (PluginUpdate)msg;
  329. if(pmsg.getWhat() == PluginUpdate.LOADED
  330. || pmsg.getWhat() == PluginUpdate.UNLOADED)
  331. {
  332. plugins.updatePopupMenu();
  333. }
  334. }
  335. else if(msg instanceof VFSUpdate)
  336. {
  337. maybeReloadDirectory(((VFSUpdate)msg).getPath());
  338. }
  339. } //}}}
  340. //{{{ getView() method
  341. public View getView()
  342. {
  343. return view;
  344. } //}}}
  345. //{{{ getMode() method
  346. public int getMode()
  347. {
  348. return mode;
  349. } //}}}
  350. //{{{ isMultipleSelectionEnabled() method
  351. public boolean isMultipleSelectionEnabled()
  352. {
  353. return multipleSelection;
  354. } //}}}
  355. //{{{ isHorizontalLayout() method
  356. public boolean isHorizontalLayout()
  357. {
  358. return horizontalLayout;
  359. } //}}}
  360. //{{{ getShowHiddenFiles() method
  361. public boolean getShowHiddenFiles()
  362. {
  363. return showHiddenFiles;
  364. } //}}}
  365. //{{{ setShowHiddenFiles() method
  366. public void setShowHiddenFiles(boolean showHiddenFiles)
  367. {
  368. this.showHiddenFiles = showHiddenFiles;
  369. } //}}}
  370. //{{{ getFilenameFilter() method
  371. /**
  372. * Returns the file name filter glob.
  373. * @since jEdit 3.2pre2
  374. */
  375. public String getFilenameFilter()
  376. {
  377. if(filterCheckbox.isSelected())
  378. {
  379. String filter = filterField.getText();
  380. if(filter.length() == 0)
  381. return "*";
  382. else
  383. return filter;
  384. }
  385. else
  386. return "*";
  387. } //}}}
  388. //{{{ setFilenameFilter() method
  389. public void setFilenameFilter(String filter)
  390. {
  391. if(filter == null || filter.length() == 0 || filter.equals("*"))
  392. filterCheckbox.setSelected(false);
  393. else
  394. {
  395. filterCheckbox.setSelected(true);
  396. filterField.setText(filter);
  397. }
  398. } //}}}
  399. //{{{ getDirectoryField() method
  400. public HistoryTextField getDirectoryField()
  401. {
  402. return pathField;
  403. } //}}}
  404. //{{{ getDirectory() method
  405. public String getDirectory()
  406. {
  407. return path;
  408. } //}}}
  409. //{{{ setDirectory() method
  410. public void setDirectory(String path)
  411. {
  412. if(path.startsWith("file:"))
  413. path = path.substring(5);
  414. pathField.setText(path);
  415. if(!startRequest())
  416. return;
  417. updateFilenameFilter();
  418. browserView.saveExpansionState();
  419. browserView.loadDirectory(null,path,true);
  420. this.path = path;
  421. VFSManager.runInAWTThread(new Runnable()
  422. {
  423. public void run()
  424. {
  425. endRequest();
  426. }
  427. });
  428. } //}}}
  429. //{{{ getRootDirectory() method
  430. public static String getRootDirectory()
  431. {
  432. if(OperatingSystem.isMacOS() || OperatingSystem.isDOSDerived())
  433. return FileRootsVFS.PROTOCOL + ":";
  434. else
  435. return "/";
  436. } //}}}
  437. //{{{ rootDirectory() method
  438. /**
  439. * Goes to the local drives directory.
  440. * @since jEdit 4.0pre4
  441. */
  442. public void rootDirectory()
  443. {
  444. setDirectory(getRootDirectory());
  445. } //}}}
  446. //{{{ reloadDirectory() method
  447. public void reloadDirectory()
  448. {
  449. // used by FTP plugin to clear directory cache
  450. VFSManager.getVFSForPath(path).reloadDirectory(path);
  451. updateFilenameFilter();
  452. browserView.saveExpansionState();
  453. browserView.loadDirectory(null,path,false);
  454. } //}}}
  455. //{{{ delete() method
  456. /**
  457. * Note that all files must be on the same VFS.
  458. * @since jEdit 4.3pre2
  459. */
  460. public void delete(VFSFile[] files)
  461. {
  462. String dialogType;
  463. if(MiscUtilities.isURL(files[0].getDeletePath())
  464. && FavoritesVFS.PROTOCOL.equals(
  465. MiscUtilities.getProtocolOfURL(files[0].getDeletePath())))
  466. {
  467. dialogType = "vfs.browser.delete-favorites";
  468. }
  469. else
  470. {
  471. dialogType = "vfs.browser.delete-confirm";
  472. }
  473. StringBuffer buf = new StringBuffer();
  474. for(int i = 0; i < files.length; i++)
  475. {
  476. buf.append(files[i].getPath());
  477. buf.append('\n');
  478. }
  479. Object[] args = { buf.toString() };
  480. int result = GUIUtilities.confirm(this,dialogType,args,
  481. JOptionPane.YES_NO_OPTION,
  482. JOptionPane.WARNING_MESSAGE);
  483. if(result != JOptionPane.YES_OPTION)
  484. return;
  485. VFS vfs = VFSManager.getVFSForPath(files[0].getDeletePath());
  486. if(!startRequest())
  487. return;
  488. for(int i = 0; i < files.length; i++)
  489. {
  490. Object session = vfs.createVFSSession(files[i].getDeletePath(),this);
  491. if(session == null)
  492. continue;
  493. VFSManager.runInWorkThread(new BrowserIORequest(
  494. BrowserIORequest.DELETE,this,
  495. session,vfs,files[i].getDeletePath(),
  496. null,null));
  497. }
  498. VFSManager.runInAWTThread(new Runnable()
  499. {
  500. public void run()
  501. {
  502. endRequest();
  503. }
  504. });
  505. } //}}}
  506. //{{{ rename() method
  507. public void rename(String from)
  508. {
  509. VFS vfs = VFSManager.getVFSForPath(from);
  510. String filename = vfs.getFileName(from);
  511. String[] args = { filename };
  512. String to = GUIUtilities.input(this,"vfs.browser.rename",
  513. args,filename);
  514. if(to == null)
  515. return;
  516. to = MiscUtilities.constructPath(vfs.getParentOfPath(from),to);
  517. Object session = vfs.createVFSSession(from,this);
  518. if(session == null)
  519. return;
  520. if(!startRequest())
  521. return;
  522. VFSManager.runInWorkThread(new BrowserIORequest(
  523. BrowserIORequest.RENAME,this,
  524. session,vfs,from,to,null));
  525. VFSManager.runInAWTThread(new Runnable()
  526. {
  527. public void run()
  528. {
  529. endRequest();
  530. }
  531. });
  532. } //}}}
  533. //{{{ mkdir() method
  534. public void mkdir()
  535. {
  536. String newDirectory = GUIUtilities.input(this,"vfs.browser.mkdir",null);
  537. if(newDirectory == null)
  538. return;
  539. // if a directory is selected, create new dir in there.
  540. // if a file is selected, create new dir inside its parent.
  541. VFSFile[] selected = getSelectedFiles();
  542. String parent;
  543. if(selected.length == 0)
  544. parent = path;
  545. else if(selected[0].getType() == VFSFile.FILE)
  546. {
  547. parent = selected[0].getPath();
  548. parent = VFSManager.getVFSForPath(parent)
  549. .getParentOfPath(parent);
  550. }
  551. else
  552. parent = selected[0].getPath();
  553. VFS vfs = VFSManager.getVFSForPath(parent);
  554. // path is the currently viewed directory in the browser
  555. newDirectory = MiscUtilities.constructPath(parent,newDirectory);
  556. Object session = vfs.createVFSSession(newDirectory,this);
  557. if(session == null)
  558. return;
  559. if(!startRequest())
  560. return;
  561. VFSManager.runInWorkThread(new BrowserIORequest(
  562. BrowserIORequest.MKDIR,this,
  563. session,vfs,newDirectory,null,null));
  564. VFSManager.runInAWTThread(new Runnable()
  565. {
  566. public void run()
  567. {
  568. endRequest();
  569. }
  570. });
  571. } //}}}
  572. //{{{ newFile() method
  573. /**
  574. * Creates a new file in the current directory.
  575. * @since jEdit 4.0pre2
  576. */
  577. public void newFile()
  578. {
  579. VFSFile[] selected = getSelectedFiles();
  580. if(selected.length >= 1)
  581. {
  582. VFSFile file = selected[0];
  583. if(file.getType() == VFSFile.DIRECTORY)
  584. jEdit.newFile(view,file.getPath());
  585. else
  586. {
  587. VFS vfs = VFSManager.getVFSForPath(file.getPath());
  588. jEdit.newFile(view,vfs.getParentOfPath(file.getPath()));
  589. }
  590. }
  591. else
  592. jEdit.newFile(view,path);
  593. } //}}}
  594. //{{{ searchInDirectory() method
  595. /**
  596. * Opens a directory search in the current directory.
  597. * @since jEdit 4.0pre2
  598. */
  599. public void searchInDirectory()
  600. {
  601. VFSFile[] selected = getSelectedFiles();
  602. if(selected.length >= 1)
  603. {
  604. VFSFile file = selected[0];
  605. searchInDirectory(file.getPath(),file.getType() != VFSFile.FILE);
  606. }
  607. else
  608. {
  609. searchInDirectory(this.path,true);
  610. }
  611. } //}}}
  612. //{{{ searchInDirectory() method
  613. /**
  614. * Opens a directory search in the specified directory.
  615. * @param path The path name
  616. * @param directory True if the path is a directory, false if it is a file
  617. * @since jEdit 4.2pre1
  618. */
  619. public void searchInDirectory(String path, boolean directory)
  620. {
  621. String filter;
  622. if(directory)
  623. {
  624. filter = getFilenameFilter();
  625. }
  626. else
  627. {
  628. String name = MiscUtilities.getFileName(path);
  629. String ext = MiscUtilities.getFileExtension(name);
  630. filter = (ext == null || ext.length() == 0
  631. ? getFilenameFilter()
  632. : "*" + ext);
  633. path = MiscUtilities.getParentOfPath(path);
  634. }
  635. SearchAndReplace.setSearchFileSet(new DirectoryListSet(
  636. path,filter,true));
  637. SearchDialog.showSearchDialog(view,null,SearchDialog.DIRECTORY);
  638. } //}}}
  639. //{{{ getBrowserView() method
  640. public BrowserView getBrowserView()
  641. {
  642. return browserView;
  643. } //}}}
  644. //{{{ getSelectedFiles() method
  645. /**
  646. * @since jEdit 4.3pre2
  647. */
  648. public VFSFile[] getSelectedFiles()
  649. {
  650. return browserView.getSelectedFiles();
  651. } //}}}
  652. //{{{ locateFile() method
  653. /**
  654. * Goes to the given file's directory and selects the file in the list.
  655. * @param path The file
  656. * @since jEdit 4.2pre2
  657. */
  658. public void locateFile(final String path)
  659. {
  660. if(!filenameFilter.matcher(MiscUtilities.getFileName(path)).matches())
  661. setFilenameFilter(null);
  662. setDirectory(MiscUtilities.getParentOfPath(path));
  663. VFSManager.runInAWTThread(new Runnable()
  664. {
  665. public void run()
  666. {
  667. browserView.getTable().selectFile(path);
  668. }
  669. });
  670. } //}}}
  671. //{{{ createPluginsMenu() method
  672. public JComponent createPluginsMenu(JComponent pluginMenu, boolean showManagerOptions)
  673. {
  674. ActionHandler actionHandler = new ActionHandler();
  675. if(showManagerOptions && getMode() == VFSBrowser.BROWSER)
  676. {
  677. pluginMenu.add(GUIUtilities.loadMenuItem("plugin-manager",false));
  678. pluginMenu.add(GUIUtilities.loadMenuItem("plugin-options",false));
  679. if (pluginMenu instanceof JMenu)
  680. ((JMenu)pluginMenu).addSeparator();
  681. else if (pluginMenu instanceof JPopupMenu)
  682. ((JPopupMenu)pluginMenu).addSeparator();
  683. }
  684. else
  685. /* we're in a modal dialog */;
  686. ArrayList vec = new ArrayList();
  687. //{{{ old API
  688. Enumeration e = VFSManager.getFilesystems();
  689. while(e.hasMoreElements())
  690. {
  691. VFS vfs = (VFS)e.nextElement();
  692. if((vfs.getCapabilities() & VFS.BROWSE_CAP) == 0)
  693. continue;
  694. JMenuItem menuItem = new JMenuItem(jEdit.getProperty(
  695. "vfs." + vfs.getName() + ".label"));
  696. menuItem.setActionCommand(vfs.getName());
  697. menuItem.addActionListener(actionHandler);
  698. vec.add(menuItem);
  699. } //}}}
  700. //{{{ new API
  701. EditPlugin[] plugins = jEdit.getPlugins();
  702. for(int i = 0; i < plugins.length; i++)
  703. {
  704. JMenuItem menuItem = plugins[i].createBrowserMenuItems();
  705. if(menuItem != null)
  706. vec.add(menuItem);
  707. } //}}}
  708. if(vec.size() != 0)
  709. {
  710. Collections.sort(vec,new MiscUtilities.MenuItemCompare());
  711. for(int i = 0; i < vec.size(); i++)
  712. pluginMenu.add((JMenuItem)vec.get(i));
  713. }
  714. else
  715. {
  716. JMenuItem mi = new JMenuItem(jEdit.getProperty(
  717. "vfs.browser.plugins.no-plugins.label"));
  718. mi.setEnabled(false);
  719. pluginMenu.add(mi);
  720. }
  721. return pluginMenu;
  722. } //}}}
  723. //{{{ addBrowserListener() method
  724. public void addBrowserListener(BrowserListener l)
  725. {
  726. listenerList.add(BrowserListener.class,l);
  727. } //}}}
  728. //{{{ removeBrowserListener() method
  729. public void removeBrowserListener(BrowserListener l)
  730. {
  731. listenerList.remove(BrowserListener.class,l);
  732. } //}}}
  733. //{{{ filesActivated() method
  734. // canDoubleClickClose set to false when ENTER pressed
  735. public static final int M_OPEN = 0;
  736. public static final int M_OPEN_NEW_VIEW = 1;
  737. public static final int M_OPEN_NEW_PLAIN_VIEW = 2;
  738. public static final int M_OPEN_NEW_SPLIT = 3;
  739. public static final int M_INSERT = 4;
  740. /**
  741. * This method does the "double-click" handling. It is public so that
  742. * <code>browser.actions.xml</code> can bind to it.
  743. * @since jEdit 4.2pre2
  744. */
  745. public void filesActivated(int mode, boolean canDoubleClickClose)
  746. {
  747. VFSFile[] selectedFiles = browserView.getSelectedFiles();
  748. Buffer buffer = null;
  749. check_selected: for(int i = 0; i < selectedFiles.length; i++)
  750. {
  751. VFSFile file = selectedFiles[i];
  752. if(file.getType() == VFSFile.DIRECTORY
  753. || file.getType() == VFSFile.FILESYSTEM)
  754. {
  755. if(mode == M_OPEN_NEW_VIEW && this.mode == BROWSER)
  756. browseDirectoryInNewWindow(view,file.getPath());
  757. else
  758. setDirectory(file.getPath());
  759. }
  760. else if(this.mode == BROWSER || this.mode == BROWSER_DIALOG)
  761. {
  762. if(mode == M_INSERT)
  763. {
  764. view.getBuffer().insertFile(view,
  765. file.getPath());
  766. continue check_selected;
  767. }
  768. Buffer _buffer = jEdit.getBuffer(file.getPath());
  769. if(_buffer == null)
  770. {
  771. Hashtable props = new Hashtable();
  772. props.put(Buffer.ENCODING,currentEncoding);
  773. props.put(Buffer.ENCODING_AUTODETECT,
  774. Boolean.valueOf(autoDetectEncoding));
  775. _buffer = jEdit.openFile(null,null,
  776. file.getPath(),false,props);
  777. }
  778. else if(doubleClickClose && canDoubleClickClose
  779. && this.mode != BROWSER_DIALOG
  780. && selectedFiles.length == 1)
  781. {
  782. // close if this buffer is currently
  783. // visible in the view.
  784. EditPane[] editPanes = view.getEditPanes();
  785. for(int j = 0; j < editPanes.length; j++)
  786. {
  787. if(editPanes[j].getBuffer() == _buffer)
  788. {
  789. jEdit.closeBuffer(view,_buffer);
  790. return;
  791. }
  792. }
  793. }
  794. if(_buffer != null)
  795. buffer = _buffer;
  796. }
  797. else
  798. {
  799. // if a file is selected in OPEN_DIALOG or
  800. // SAVE_DIALOG mode, just let the listener(s)
  801. // handle it
  802. }
  803. }
  804. if(buffer != null)
  805. {
  806. switch(mode)
  807. {
  808. case M_OPEN:
  809. view.setBuffer(buffer);
  810. break;
  811. case M_OPEN_NEW_VIEW:
  812. jEdit.newView(view,buffer,false);
  813. break;
  814. case M_OPEN_NEW_PLAIN_VIEW:
  815. jEdit.newView(view,buffer,true);
  816. break;
  817. case M_OPEN_NEW_SPLIT:
  818. view.splitHorizontally().setBuffer(buffer);
  819. break;
  820. }
  821. }
  822. Object[] listeners = listenerList.getListenerList();
  823. for(int i = 0; i < listeners.length; i++)
  824. {
  825. if(listeners[i] == BrowserListener.class)
  826. {
  827. BrowserListener l = (BrowserListener)listeners[i+1];
  828. l.filesActivated(this,selectedFiles);
  829. }
  830. }
  831. } //}}}
  832. //{{{ Package-private members
  833. String currentEncoding;
  834. boolean autoDetectEncoding;
  835. //{{{ updateFilenameFilter() method
  836. void updateFilenameFilter()
  837. {
  838. try
  839. {
  840. String filter = filterField.getText();
  841. if(filter.length() == 0)
  842. filter = "*";
  843. filenameFilter = Pattern.compile(MiscUtilities.globToRE(filter),
  844. Pattern.CASE_INSENSITIVE);
  845. }
  846. catch(Exception e)
  847. {
  848. Log.log(Log.ERROR,VFSBrowser.this,e);
  849. String[] args = { filterField.getText(),
  850. e.getMessage() };
  851. GUIUtilities.error(this,"vfs.browser.bad-filter",args);
  852. }
  853. } //}}}
  854. //{{{ directoryLoaded() method
  855. void directoryLoaded(Object node, Object[] loadInfo,
  856. boolean addToHistory)
  857. {
  858. VFSManager.runInAWTThread(new DirectoryLoadedAWTRequest(
  859. node,loadInfo,addToHistory));
  860. } //}}}
  861. //{{{ filesSelected() method
  862. void filesSelected()
  863. {
  864. VFSFile[] selectedFiles = browserView.getSelectedFiles();
  865. if(mode == BROWSER)
  866. {
  867. for(int i = 0; i < selectedFiles.length; i++)
  868. {
  869. VFSFile file = selectedFiles[i];
  870. Buffer buffer = jEdit.getBuffer(file.getPath());
  871. if(buffer != null && view != null)
  872. view.setBuffer(buffer);
  873. }
  874. }
  875. Object[] listeners = listenerList.getListenerList();
  876. for(int i = 0; i < listeners.length; i++)
  877. {
  878. if(listeners[i] == BrowserListener.class)
  879. {
  880. BrowserListener l = (BrowserListener)listeners[i+1];
  881. l.filesSelected(this,selectedFiles);
  882. }
  883. }
  884. } //}}}
  885. //{{{ endRequest() method
  886. void endRequest()
  887. {
  888. requestRunning = false;
  889. } //}}}
  890. //}}}
  891. //{{{ Private members
  892. private static ActionContext actionContext;
  893. static
  894. {
  895. actionContext = new BrowserActionContext();
  896. ActionSet builtInActionSet = new ActionSet(null,null,null,
  897. jEdit.class.getResource("browser.actions.xml"));
  898. builtInActionSet.setLabel(jEdit.getProperty("action-set.browser"));
  899. builtInActionSet.load();
  900. actionContext.addActionSet(builtInActionSet);
  901. }
  902. //{{{ Instance variables
  903. private EventListenerList listenerList;
  904. private View view;
  905. private boolean horizontalLayout;
  906. private String path;
  907. private HistoryTextField pathField;
  908. private JCheckBox filterCheckbox;
  909. private HistoryTextField filterField;
  910. private Box toolbarBox;
  911. private FavoritesMenuButton favorites;
  912. private PluginsMenuButton plugins;
  913. private BrowserView browserView;
  914. private Pattern filenameFilter;
  915. private int mode;
  916. private boolean multipleSelection;
  917. private boolean showHiddenFiles;
  918. private boolean sortMixFilesAndDirs;
  919. private boolean sortIgnoreCase;
  920. private boolean doubleClickClose;
  921. private boolean requestRunning;
  922. private boolean maybeReloadRequestRunning;
  923. //}}}
  924. //{{{ createMenuBar() method
  925. private JPanel createMenuBar()
  926. {
  927. JPanel menuBar = new JPanel();
  928. menuBar.setLayout(new BoxLayout(menuBar,BoxLayout.X_AXIS));
  929. menuBar.setBorder(new EmptyBorder(0,1,0,3));
  930. menuBar.add(new CommandsMenuButton());
  931. menuBar.add(Box.createHorizontalStrut(3));
  932. menuBar.add(plugins = new PluginsMenuButton());
  933. menuBar.add(Box.createHorizontalStrut(3));
  934. menuBar.add(favorites = new FavoritesMenuButton());
  935. return menuBar;
  936. } //}}}
  937. //{{{ createToolBar() method
  938. private Box createToolBar()
  939. {
  940. if(mode == BROWSER)
  941. return GUIUtilities.loadToolBar(actionContext,
  942. "vfs.browser.toolbar-browser");
  943. else
  944. return GUIUtilities.loadToolBar(actionContext,
  945. "vfs.browser.toolbar-dialog");
  946. } //}}}
  947. //{{{ propertiesChanged() method
  948. private void propertiesChanged()
  949. {
  950. showHiddenFiles = jEdit.getBooleanProperty("vfs.browser.showHiddenFiles");
  951. sortMixFilesAndDirs = jEdit.getBooleanProperty("vfs.browser.sortMixFilesAndDirs");
  952. sortIgnoreCase = jEdit.getBooleanProperty("vfs.browser.sortIgnoreCase");
  953. doubleClickClose = jEdit.getBooleanProperty("vfs.browser.doubleClickClose");
  954. browserView.propertiesChanged();
  955. toolbarBox.removeAll();
  956. if(jEdit.getBooleanProperty("vfs.browser.showToolbar"))
  957. {
  958. Box toolbar = createToolBar();
  959. if(horizontalLayout)
  960. toolbarBox.add(toolbar);
  961. else
  962. {
  963. toolbar.add(Box.createGlue());
  964. toolbarBox.add(toolbar);
  965. }
  966. }
  967. if(jEdit.getBooleanProperty("vfs.browser.showMenubar"))
  968. {
  969. JPanel menubar = createMenuBar();
  970. if(horizontalLayout)
  971. {
  972. toolbarBox.add(Box.createHorizontalStrut(6));
  973. toolbarBox.add(menubar,0);
  974. }
  975. else
  976. {
  977. menubar.add(Box.createGlue());
  978. toolbarBox.add(menubar);
  979. }
  980. }
  981. else
  982. favorites = null;
  983. toolbarBox.add(Box.createGlue());
  984. revalidate();
  985. if(path != null)
  986. reloadDirectory();
  987. } //}}}
  988. /* We do this stuff because the browser is not able to handle
  989. * more than one request yet */
  990. //{{{ startRequest() method
  991. private boolean startRequest()
  992. {
  993. if(requestRunning)
  994. {
  995. // dump stack trace for debugging purposes
  996. Log.log(Log.DEBUG,this,new Throwable("For debugging purposes"));
  997. GUIUtilities.error(this,"browser-multiple-io",null);
  998. return false;
  999. }
  1000. else
  1001. {
  1002. requestRunning = true;
  1003. return true;
  1004. }
  1005. } //}}}
  1006. //{{{ updateFilterEnabled() method
  1007. private void updateFilterEnabled()
  1008. {
  1009. filterField.setEnabled(filterCheckbox.isSelected());
  1010. } //}}}
  1011. //{{{ maybeReloadDirectory() method
  1012. private void maybeReloadDirectory(String dir)
  1013. {
  1014. if(MiscUtilities.isURL(dir)
  1015. && MiscUtilities.getProtocolOfURL(dir).equals(
  1016. FavoritesVFS.PROTOCOL))
  1017. {
  1018. if(favorites != null)
  1019. favorites.popup = null;
  1020. }
  1021. // this is a dirty hack and it relies on the fact
  1022. // that updates for parents are sent before updates
  1023. // for the changed nodes themselves (if this was not
  1024. // the case, the browser wouldn't be updated properly
  1025. // on delete, etc).
  1026. //
  1027. // to avoid causing '> 1 request' errors, don't reload
  1028. // directory if request already active
  1029. if(maybeReloadRequestRunning)
  1030. {
  1031. //Log.log(Log.WARNING,this,"VFS update: request already in progress");
  1032. return;
  1033. }
  1034. // save a file -> sends vfs update. if a VFS file dialog box
  1035. // is shown from the same event frame as the save, the
  1036. // VFSUpdate will be delivered before the directory is loaded,
  1037. // and before the path is set.
  1038. if(path != null)
  1039. {
  1040. try
  1041. {
  1042. maybeReloadRequestRunning = true;
  1043. browserView.maybeReloadDirectory(dir);
  1044. }
  1045. finally
  1046. {
  1047. VFSManager.runInAWTThread(new Runnable()
  1048. {
  1049. public void run()
  1050. {
  1051. maybeReloadRequestRunning = false;
  1052. }
  1053. });
  1054. }
  1055. }
  1056. } //}}}
  1057. //}}}
  1058. //{{{ Inner classes
  1059. //{{{ ActionHandler class
  1060. class ActionHandler implements ActionListener
  1061. {
  1062. public void actionPerformed(ActionEvent evt)
  1063. {
  1064. Object source = evt.getSource();
  1065. if(source == pathField || source == filterField
  1066. || source == filterCheckbox)
  1067. {
  1068. updateFilterEnabled();
  1069. String path = pathField.getText();
  1070. if(path != null)
  1071. setDirectory(path);
  1072. browserView.focusOnFileView();
  1073. }
  1074. }
  1075. } //}}}
  1076. //{{{ CommandsMenuButton class
  1077. class CommandsMenuButton extends JButton
  1078. {
  1079. //{{{ CommandsMenuButton constructor
  1080. CommandsMenuButton()
  1081. {
  1082. setText(jEdit.getProperty("vfs.browser.commands.label"));
  1083. setIcon(GUIUtilities.loadIcon("ToolbarMenu.gif"));
  1084. setHorizontalTextPosition(SwingConstants.LEADING);
  1085. popup = new BrowserCommandsMenu(VFSBrowser.this,null);
  1086. CommandsMenuButton.this.setRequestFocusEnabled(false);
  1087. setMargin(new Insets(1,1,1,1));
  1088. CommandsMenuButton.this.addMouseListener(new MouseHandler());
  1089. if(OperatingSystem.isMacOSLF())
  1090. CommandsMenuButton.this.putClientProperty("JButton.buttonType","toolbar");
  1091. } //}}}
  1092. BrowserCommandsMenu popup;
  1093. //{{{ MouseHandler class
  1094. class MouseHandler extends MouseAdapter
  1095. {
  1096. public void mousePressed(MouseEvent evt)
  1097. {
  1098. if(!popup.isVisible())
  1099. {
  1100. popup.update();
  1101. GUIUtilities.showPopupMenu(
  1102. popup,CommandsMenuButton.this,0,
  1103. CommandsMenuButton.this.getHeight(),
  1104. false);
  1105. }
  1106. else
  1107. {
  1108. popup.setVisible(false);
  1109. }
  1110. }
  1111. } //}}}
  1112. } //}}}
  1113. //{{{ PluginsMenuButton class
  1114. class PluginsMenuButton extends JButton
  1115. {
  1116. //{{{ PluginsMenuButton constructor
  1117. PluginsMenuButton()
  1118. {
  1119. setText(jEdit.getProperty("vfs.browser.plugins.label"));
  1120. setIcon(GUIUtilities.loadIcon("ToolbarMenu.gif"));
  1121. setHorizontalTextPosition(SwingConstants.LEADING);
  1122. PluginsMenuButton.this.setRequestFocusEnabled(false);
  1123. setMargin(new Insets(1,1,1,1));
  1124. PluginsMenuButton.this.addMouseListener(new MouseHandler());
  1125. if(OperatingSystem.isMacOSLF())
  1126. PluginsMenuButton.this.putClientProperty("JButton.buttonType","toolbar");
  1127. } //}}}
  1128. JPopupMenu popup;
  1129. //{{{ updatePopupMenu() method
  1130. void updatePopupMenu()
  1131. {
  1132. popup = null;
  1133. } //}}}
  1134. //{{{ createPopupMenu() method
  1135. private void createPopupMenu()
  1136. {
  1137. if(popup != null)
  1138. return;
  1139. popup = (JPopupMenu)createPluginsMenu(new JPopupMenu(),true);
  1140. } //}}}
  1141. //{{{ ActionHandler class
  1142. class ActionHandler implements ActionListener
  1143. {
  1144. public void actionPerformed(ActionEvent evt)
  1145. {
  1146. VFS vfs = VFSManager.getVFSByName(evt.getActionCommand());
  1147. String directory = vfs.showBrowseDialog(null,
  1148. VFSBrowser.this);
  1149. if(directory != null)
  1150. setDirectory(directory);
  1151. }
  1152. } //}}}
  1153. //{{{ MouseHandler class
  1154. class MouseHandler extends MouseAdapter
  1155. {
  1156. public void mousePressed(MouseEvent evt)
  1157. {
  1158. createPopupMenu();
  1159. if(!popup.isVisible())
  1160. {
  1161. GUIUtilities.showPopupMenu(
  1162. popup,PluginsMenuButton.this,0,
  1163. PluginsMenuButton.this.getHeight(),
  1164. false);
  1165. }
  1166. else
  1167. {
  1168. popup.setVisible(false);
  1169. }
  1170. }
  1171. } //}}}
  1172. } //}}}
  1173. //{{{ FavoritesMenuButton class
  1174. class FavoritesMenuButton extends JButton
  1175. {
  1176. //{{{ FavoritesMenuButton constructor
  1177. FavoritesMenuButton()
  1178. {
  1179. setText(jEdit.getProperty("vfs.browser.favorites.label"));
  1180. setIcon(GUIUtilities.loadIcon("ToolbarMenu.gif"));
  1181. setHorizontalTextPosition(SwingConstants.LEADING);
  1182. FavoritesMenuButton.this.setRequestFocusEnabled(false);
  1183. setMargin(new Insets(1,1,1,1));
  1184. FavoritesMenuButton.this.addMouseListener(new MouseHandler());
  1185. if(OperatingSystem.isMacOSLF())
  1186. FavoritesMenuButton.this.putClientProperty("JButton.buttonType","toolbar");
  1187. } //}}}
  1188. JPopupMenu popup;
  1189. //{{{ createPopupMenu() method
  1190. void createPopupMenu()
  1191. {
  1192. popup = new JPopupMenu();
  1193. ActionHandler actionHandler = new ActionHandler();
  1194. JMenuItem mi = new JMenuItem(
  1195. jEdit.getProperty(
  1196. "vfs.browser.favorites"
  1197. + ".add-to-favorites.label"));
  1198. mi.setActionCommand("add-to-favorites");
  1199. mi.addActionListener(actionHandler);
  1200. popup.add(mi);
  1201. mi = new JMenuItem(
  1202. jEdit.getProperty(
  1203. "vfs.browser.favorites"
  1204. + ".edit-favorites.label"));
  1205. mi.setActionCommand("dir@favorites:");
  1206. mi.addActionListener(actionHandler);
  1207. popup.add(mi);
  1208. popup.addSeparator();
  1209. VFSFile[] favorites = FavoritesVFS.getFavorites();
  1210. if(favorites.length == 0)
  1211. {
  1212. mi = new JMenuItem(
  1213. jEdit.getProperty(
  1214. "vfs.browser.favorites"
  1215. + ".no-favorites.label"));
  1216. mi.setEnabled(false);
  1217. popup.add(mi);
  1218. }
  1219. else
  1220. {
  1221. Arrays.sort(favorites,
  1222. new VFS.DirectoryEntryCompare(
  1223. sortMixFilesAndDirs,
  1224. sortIgnoreCase));
  1225. for(int i = 0; i < favorites.length; i++)
  1226. {
  1227. VFSFile favorite = favorites[i];
  1228. mi = new JMenuItem(favorite.getPath());
  1229. mi.setIcon(FileCellRenderer
  1230. .getIconForFile(
  1231. favorite,false));
  1232. String cmd = (favorite.getType() ==
  1233. VFSFile.FILE
  1234. ? "file@" : "dir@")
  1235. + favorite.getPath();
  1236. mi.setActionCommand(cmd);
  1237. mi.addActionListener(actionHandler);
  1238. popup.add(mi);
  1239. }
  1240. }
  1241. } //}}}
  1242. //{{{ ActionHandler class
  1243. class ActionHandler implements ActionListener
  1244. {
  1245. public void actionPerformed(ActionEvent evt)
  1246. {
  1247. String actionCommand = evt.getActionCommand();
  1248. if(actionCommand.equals("add-to-favorites"))
  1249. {
  1250. // if any directories are selected, add
  1251. // them, otherwise add current directory
  1252. VFSFile[] selected = getSelectedFiles();
  1253. if(selected == null || selected.length == 0)
  1254. {
  1255. if(path.equals(FavoritesVFS.PROTOCOL + ":"))
  1256. {
  1257. GUIUtilities.error(VFSBrowser.this,
  1258. "vfs.browser.recurse-favorites",
  1259. null);
  1260. }
  1261. else
  1262. {
  1263. FavoritesVFS.addToFavorites(path,
  1264. VFSFile.DIRECTORY);
  1265. }
  1266. }
  1267. else
  1268. {
  1269. for(int i = 0; i < selected.length; i++)
  1270. {
  1271. VFSFile file = selected[i];
  1272. FavoritesVFS.addToFavorites(file.getPath(),
  1273. file.getType());
  1274. }
  1275. }
  1276. }
  1277. else if(actionCommand.startsWith("dir@"))
  1278. {
  1279. setDirectory(actionCommand.substring(4));
  1280. }
  1281. else if(actionCommand.startsWith("file@"))
  1282. {
  1283. switch(getMode())
  1284. {
  1285. case BROWSER:
  1286. jEdit.openFile(view,actionCommand.substring(5));
  1287. break;
  1288. default:
  1289. locateFile(actionCommand.substring(5));
  1290. break;
  1291. }
  1292. }
  1293. }
  1294. } //}}}
  1295. //{{{ MouseHandler class
  1296. class MouseHandler extends MouseAdapter
  1297. {
  1298. public void mousePressed(MouseEvent evt)
  1299. {
  1300. if(popup != null && popup.isVisible())
  1301. {
  1302. popup.setVisible(false);
  1303. return;
  1304. }
  1305. if(popup == null)
  1306. createPopupMenu();
  1307. GUIUtilities.showPopupMenu(
  1308. popup,FavoritesMenuButton.this,0,
  1309. FavoritesMenuButton.this.getHeight(),
  1310. false);
  1311. }
  1312. } //}}}
  1313. } //}}}
  1314. //{{{ DirectoryLoadedAWTRequest class
  1315. class DirectoryLoadedAWTRequest implements Runnable
  1316. {
  1317. private Object node;
  1318. private Object[] loadInfo;
  1319. private boolean addToHistory;
  1320. DirectoryLoadedAWTRequest(Object node, Object[] loadInfo,
  1321. boolean addToHistory)
  1322. {
  1323. this.node = node;
  1324. this.loadInfo = loadInfo;
  1325. this.addToHistory = addToHistory;
  1326. }
  1327. public void run()
  1328. {
  1329. String path = (String)loadInfo[0];
  1330. if(path == null)
  1331. {
  1332. // there was an error
  1333. return;
  1334. }
  1335. VFSFile[] list = (VFSFile[])loadInfo[1];
  1336. if(node == null)
  1337. {
  1338. // This is the new, canonical path
  1339. VFSBrowser.this.path = path;
  1340. if(!pathField.getText().equals(path))
  1341. pathField.setText(path);
  1342. if(path.endsWith("/") ||
  1343. path.endsWith(File.separator))
  1344. {
  1345. // ensure consistent history;
  1346. // eg we don't want both
  1347. // foo/ and foo
  1348. path = path.substring(0,
  1349. path.length() - 1);
  1350. }
  1351. if(addToHistory)
  1352. {
  1353. HistoryModel.getModel("vfs.browser.path")
  1354. .addItem(path);
  1355. }
  1356. }
  1357. boolean filterEnabled = filterCheckbox.isSelected();
  1358. ArrayList directoryVector = new ArrayList();
  1359. int directories = 0;
  1360. int files = 0;
  1361. int invisible = 0;
  1362. if(list != null)
  1363. {
  1364. for(int i = 0; i < list.length; i++)
  1365. {
  1366. VFSFile file = list[i];
  1367. if(file.isHidden() && !showHiddenFiles)
  1368. {
  1369. invisible++;
  1370. continue;
  1371. }
  1372. if(file.getType() == VFSFile.FILE
  1373. && filterEnabled
  1374. && filenameFilter != null
  1375. && !filenameFilter.matcher(file.getName()).matches())
  1376. {
  1377. invisible++;
  1378. continue;
  1379. }
  1380. if(file.getType() == VFSFile.FILE)
  1381. files++;
  1382. else
  1383. directories++;
  1384. directoryVector.add(file);
  1385. }
  1386. Collections.sort(directoryVector,
  1387. new VFS.DirectoryEntryCompare(
  1388. sortMixFilesAndDirs,
  1389. sortIgnoreCase));
  1390. }
  1391. browserView.directoryLoaded(node,path,
  1392. directoryVector);
  1393. // to notify listeners that any existing
  1394. // selection has been deactivated
  1395. // turns out under some circumstances this
  1396. // method can switch the current buffer in
  1397. // BROWSER mode.
  1398. // in any case, this is only needed for the
  1399. // directory chooser (why?), so we add a
  1400. // check. otherwise poor Rick will go insane.
  1401. if(mode == CHOOSE_DIRECTORY_DIALOG)
  1402. filesSelected();
  1403. }
  1404. public String toString()
  1405. {
  1406. return (String)loadInfo[0];
  1407. }
  1408. } //}}}
  1409. //{{{ BrowserActionContext class
  1410. static class BrowserActionContext extends ActionContext
  1411. {
  1412. /**
  1413. * If event source hierarchy contains a VFSDirectoryEntryTable,
  1414. * this is the currently selected files there. Otherwise, this
  1415. * is the currently selected item in the parent directory list.
  1416. */
  1417. private VFSFile[] getSelectedFiles(EventObject evt,
  1418. VFSBrowser browser)
  1419. {
  1420. Component source = (Component)evt.getSource();
  1421. if(GUIUtilities.getComponentParent(source,JList.class)
  1422. != null)
  1423. {
  1424. Object[] selected = browser.getBrowserView()
  1425. .getParentDirectoryList()
  1426. .getSelectedValues();
  1427. VFSFile[] returnValue = new VFSFile[
  1428. selected.length];
  1429. System.arraycopy(selected,0,returnValue,0,
  1430. selected.length);
  1431. return returnValue;
  1432. }
  1433. else
  1434. {
  1435. return browser.getSelectedFiles();
  1436. }
  1437. }
  1438. public void invokeAction(EventObject evt, EditAction action)
  1439. {
  1440. VFSBrowser browser = (VFSBrowser)
  1441. GUIUtilities.getComponentParent(
  1442. (Component)evt.getSource(),
  1443. VFSBrowser.class);
  1444. VFSFile[] files = getSelectedFiles(evt,browser);
  1445. // in the future we will want something better,
  1446. // eg. having an 'evt' object passed to
  1447. // EditAction.invoke().
  1448. // for now, since all browser actions are
  1449. // written in beanshell we set the 'browser'
  1450. // variable directly.
  1451. NameSpace global = BeanShell.getNameSpace();
  1452. try
  1453. {
  1454. global.setVariable("browser",browser);
  1455. global.setVariable("files",files);
  1456. View view = browser.getView();
  1457. // I guess ideally all browsers
  1458. // should have views, but since they
  1459. // don't, we just use the active view
  1460. // in that case, since some actions
  1461. // depend on a view being there and
  1462. // I don't want to add checks to
  1463. // them all
  1464. if(view == null)
  1465. view = jEdit.getActiveView();
  1466. action.invoke(view);
  1467. }
  1468. catch(UtilEvalError err)
  1469. {
  1470. Log.log(Log.ERROR,this,err);
  1471. }
  1472. finally
  1473. {
  1474. try
  1475. {
  1476. global.setVariable("browser",null);
  1477. global.setVariable("files",null);
  1478. }
  1479. catch(UtilEvalError err)
  1480. {
  1481. Log.log(Log.ERROR,this,err);
  1482. }
  1483. }
  1484. }
  1485. } //}}}
  1486. //}}}
  1487. }