PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/browser/VFSFileChooserDialog.java

#
Java | 528 lines | 421 code | 51 blank | 56 comment | 86 complexity | e1df42666f7896d3fa8f7b0f923a49a6 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. * VFSFileChooserDialog.java - VFS file chooser
  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 javax.swing.border.EmptyBorder;
  25. import javax.swing.*;
  26. import java.awt.event.*;
  27. import java.awt.*;
  28. import java.io.File;
  29. import java.io.IOException;
  30. import java.util.*;
  31. import org.gjt.sp.jedit.gui.EnhancedDialog;
  32. import org.gjt.sp.jedit.io.*;
  33. import org.gjt.sp.jedit.*;
  34. import org.gjt.sp.util.*;
  35. //}}}
  36. /**
  37. * Wraps the VFS browser in a modal dialog.
  38. * @author Slava Pestov
  39. * @version $Id: VFSFileChooserDialog.java 4957 2004-01-18 19:01:02Z spestov $
  40. */
  41. public class VFSFileChooserDialog extends EnhancedDialog
  42. {
  43. //{{{ VFSFileChooserDialog constructor
  44. public VFSFileChooserDialog(View view, String path,
  45. int mode, boolean multipleSelection)
  46. {
  47. super(view,jEdit.getProperty("vfs.browser.title"),true);
  48. JPanel content = new JPanel(new BorderLayout());
  49. content.setBorder(new EmptyBorder(12,12,12,12));
  50. setContentPane(content);
  51. String name;
  52. if(mode == VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
  53. name = null;
  54. else if(path == null || path.endsWith(File.separator)
  55. || path.endsWith("/"))
  56. {
  57. name = null;
  58. }
  59. else
  60. {
  61. VFS vfs = VFSManager.getVFSForPath(path);
  62. name = vfs.getFileName(path);
  63. path = vfs.getParentOfPath(path);
  64. }
  65. browser = new VFSBrowser(view,path,mode,multipleSelection,null);
  66. browser.getBrowserView().getTable().setRequestFocusEnabled(false);
  67. browser.getBrowserView().getParentDirectoryList()
  68. .setRequestFocusEnabled(false);
  69. /* browser.getBrowserView().getTable().addKeyListener(new KeyHandler()); */
  70. browser.addBrowserListener(new BrowserHandler());
  71. content.add(BorderLayout.CENTER,browser);
  72. JPanel panel = new JPanel();
  73. panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
  74. panel.setBorder(new EmptyBorder(12,0,0,0));
  75. filenameField = new VFSFileNameField(browser,null);
  76. filenameField.setText(name);
  77. filenameField.selectAll();
  78. Box box = new Box(BoxLayout.Y_AXIS);
  79. box.add(Box.createGlue());
  80. box.add(filenameField);
  81. box.add(Box.createGlue());
  82. JLabel label = new JLabel(jEdit.getProperty("vfs.browser.dialog.filename"));
  83. label.setDisplayedMnemonic(jEdit.getProperty(
  84. "vfs.browser.dialog.filename.mnemonic").charAt(0));
  85. label.setLabelFor(filenameField);
  86. panel.add(label);
  87. panel.add(Box.createHorizontalStrut(12));
  88. panel.add(box);
  89. panel.add(Box.createHorizontalStrut(12));
  90. GUIUtilities.requestFocus(this,filenameField);
  91. ok = new JButton();
  92. getRootPane().setDefaultButton(ok);
  93. switch(mode)
  94. {
  95. case VFSBrowser.OPEN_DIALOG:
  96. case VFSBrowser.BROWSER_DIALOG:
  97. ok.setText(jEdit.getProperty("vfs.browser.dialog.open"));
  98. break;
  99. case VFSBrowser.CHOOSE_DIRECTORY_DIALOG:
  100. ok.setText(jEdit.getProperty("vfs.browser.dialog.choose-dir"));
  101. // so that it doesn't resize...
  102. Dimension dim = ok.getPreferredSize();
  103. ok.setPreferredSize(dim);
  104. break;
  105. case VFSBrowser.SAVE_DIALOG:
  106. ok.setText(jEdit.getProperty("vfs.browser.dialog.save"));
  107. break;
  108. }
  109. ok.addActionListener(new ActionHandler());
  110. panel.add(ok);
  111. panel.add(Box.createHorizontalStrut(6));
  112. cancel = new JButton(jEdit.getProperty("common.cancel"));
  113. cancel.addActionListener(new ActionHandler());
  114. panel.add(cancel);
  115. content.add(BorderLayout.SOUTH,panel);
  116. VFSManager.getIOThreadPool().addProgressListener(
  117. workThreadHandler = new WorkThreadHandler());
  118. pack();
  119. GUIUtilities.loadGeometry(this,"vfs.browser.dialog");
  120. show();
  121. } //}}}
  122. //{{{ dispose() method
  123. public void dispose()
  124. {
  125. GUIUtilities.saveGeometry(this,"vfs.browser.dialog");
  126. VFSManager.getIOThreadPool().removeProgressListener(workThreadHandler);
  127. super.dispose();
  128. } //}}}
  129. //{{{ ok() method
  130. public void ok()
  131. {
  132. VFS.DirectoryEntry[] files = browser.getSelectedFiles();
  133. filename = filenameField.getText();
  134. if(files.length != 0)
  135. {
  136. browser.filesActivated(VFSBrowser.M_OPEN,false);
  137. return;
  138. }
  139. else if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG
  140. && (filename == null || filename.length() == 0))
  141. {
  142. isOK = true;
  143. dispose();
  144. return;
  145. }
  146. else if(filename == null || filename.length() == 0)
  147. {
  148. getToolkit().beep();
  149. return;
  150. }
  151. String bufferDir = browser.getView().getBuffer()
  152. .getDirectory();
  153. if(filename.equals("-"))
  154. filename = bufferDir;
  155. else if(filename.startsWith("-/")
  156. || filename.startsWith("-" + File.separator))
  157. {
  158. filename = MiscUtilities.constructPath(
  159. bufferDir,filename.substring(2));
  160. }
  161. final int[] type = { -1 };
  162. final String path = MiscUtilities.constructPath(
  163. browser.getDirectory(),filename);
  164. final VFS vfs = VFSManager.getVFSForPath(path);
  165. Object session = vfs.createVFSSession(path,this);
  166. if(session == null)
  167. return;
  168. VFSManager.runInWorkThread(new GetFileTypeRequest(
  169. vfs,session,path,type));
  170. VFSManager.runInAWTThread(new Runnable()
  171. {
  172. public void run()
  173. {
  174. switch(type[0])
  175. {
  176. case VFS.DirectoryEntry.FILE:
  177. if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
  178. break;
  179. if(vfs instanceof FileVFS)
  180. {
  181. if(doFileExistsWarning(path))
  182. break;
  183. }
  184. isOK = true;
  185. if(browser.getMode() == VFSBrowser.BROWSER_DIALOG)
  186. {
  187. Hashtable props = new Hashtable();
  188. props.put(Buffer.ENCODING,browser.currentEncoding);
  189. jEdit.openFile(browser.getView(),
  190. browser.getDirectory(),
  191. path,false,props);
  192. }
  193. dispose();
  194. break;
  195. case VFS.DirectoryEntry.DIRECTORY:
  196. case VFS.DirectoryEntry.FILESYSTEM:
  197. browser.setDirectory(path);
  198. break;
  199. }
  200. }
  201. });
  202. } //}}}
  203. //{{{ cancel() method
  204. public void cancel()
  205. {
  206. dispose();
  207. } //}}}
  208. //{{{ getSelectedFiles() method
  209. public String[] getSelectedFiles()
  210. {
  211. if(!isOK)
  212. return null;
  213. //String filename = filenameField.getText();
  214. if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
  215. {
  216. return new String[] { browser.getDirectory() };
  217. }
  218. else if(filename != null && filename.length() != 0)
  219. {
  220. String path = browser.getDirectory();
  221. return new String[] { MiscUtilities.constructPath(
  222. path,filename) };
  223. }
  224. else
  225. {
  226. Vector vector = new Vector();
  227. VFS.DirectoryEntry[] selectedFiles = browser.getSelectedFiles();
  228. for(int i = 0; i < selectedFiles.length; i++)
  229. {
  230. VFS.DirectoryEntry file = selectedFiles[i];
  231. if(file.type == VFS.DirectoryEntry.FILE)
  232. vector.addElement(file.path);
  233. }
  234. String[] retVal = new String[vector.size()];
  235. vector.copyInto(retVal);
  236. return retVal;
  237. }
  238. } //}}}
  239. //{{{ Private members
  240. //{{{ Instance variables
  241. private VFSBrowser browser;
  242. private VFSFileNameField filenameField;
  243. private String filename;
  244. private JButton ok;
  245. private JButton cancel;
  246. private boolean isOK;
  247. private WorkThreadHandler workThreadHandler;
  248. //}}}
  249. //{{{ doFileExistsWarning() method
  250. private boolean doFileExistsWarning(String filename)
  251. {
  252. if(browser.getMode() == VFSBrowser.SAVE_DIALOG
  253. && new File(filename).exists())
  254. {
  255. String[] args = { MiscUtilities.getFileName(filename) };
  256. int result = GUIUtilities.confirm(browser,
  257. "fileexists",args,
  258. JOptionPane.YES_NO_OPTION,
  259. JOptionPane.WARNING_MESSAGE);
  260. if(result != JOptionPane.YES_OPTION)
  261. return true;
  262. }
  263. return false;
  264. } //}}}
  265. //}}}
  266. //{{{ Inner classes
  267. //{{{ ActionHandler class
  268. class ActionHandler implements ActionListener
  269. {
  270. public void actionPerformed(ActionEvent evt)
  271. {
  272. if(evt.getSource() == ok)
  273. {
  274. if(!browser.getDirectory().equals(
  275. browser.getDirectoryField().getText()))
  276. {
  277. browser.setDirectory(browser.getDirectoryField().getText());
  278. }
  279. else
  280. ok();
  281. }
  282. else if(evt.getSource() == cancel)
  283. cancel();
  284. }
  285. } //}}}
  286. //{{{ BrowserHandler class
  287. class BrowserHandler implements BrowserListener
  288. {
  289. //{{{ filesSelected() method
  290. public void filesSelected(VFSBrowser browser, VFS.DirectoryEntry[] files)
  291. {
  292. if(files.length == 0)
  293. {
  294. if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
  295. {
  296. ok.setText(jEdit.getProperty(
  297. "vfs.browser.dialog.choose-dir"));
  298. }
  299. return;
  300. }
  301. else if(files.length == 1)
  302. {
  303. if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
  304. {
  305. ok.setText(jEdit.getProperty(
  306. "vfs.browser.dialog.open"));
  307. }
  308. VFS.DirectoryEntry file = files[0];
  309. if(file.type == VFS.DirectoryEntry.FILE)
  310. {
  311. String path = file.path;
  312. String directory = browser.getDirectory();
  313. String parent = MiscUtilities
  314. .getParentOfPath(path);
  315. if(VFSBrowser.pathsEqual(parent,directory))
  316. path = file.name;
  317. filenameField.setText(path);
  318. filenameField.selectAll();
  319. }
  320. }
  321. else
  322. {
  323. if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
  324. {
  325. ok.setText(jEdit.getProperty(
  326. "vfs.browser.dialog.open"));
  327. }
  328. filenameField.setText(null);
  329. }
  330. } //}}}
  331. //{{{ filesActivated() method
  332. public void filesActivated(VFSBrowser browser, VFS.DirectoryEntry[] files)
  333. {
  334. filenameField.selectAll();
  335. if(files.length == 0)
  336. {
  337. // user pressed enter when the vfs table or
  338. // file name field has focus, with nothing
  339. // selected.
  340. ok();
  341. return;
  342. }
  343. for(int i = 0; i < files.length; i++)
  344. {
  345. if(files[i].type == VFS.DirectoryEntry.FILE)
  346. {
  347. String path = files[i].path;
  348. VFS vfs = VFSManager.getVFSForPath(path);
  349. if(browser.getMode() == VFSBrowser.SAVE_DIALOG
  350. && vfs instanceof FileVFS)
  351. {
  352. if(doFileExistsWarning(path))
  353. return;
  354. }
  355. isOK = true;
  356. filenameField.setText(null);
  357. if(browser.getMode() != VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
  358. {
  359. dispose();
  360. }
  361. return;
  362. }
  363. else
  364. return;
  365. }
  366. } //}}}
  367. } //}}}
  368. //{{{ KeyHandler class
  369. class KeyHandler extends KeyAdapter
  370. {
  371. public void keyTyped(KeyEvent evt)
  372. {
  373. switch(evt.getKeyChar())
  374. {
  375. case '/':
  376. case '-':
  377. case '~':
  378. filenameField.processKeyEvent(evt);
  379. filenameField.requestFocus();
  380. break;
  381. }
  382. }
  383. } //}}}
  384. //{{{ WorkThreadListener class
  385. class WorkThreadHandler implements WorkThreadProgressListener
  386. {
  387. //{{{ statusUpdate() method
  388. public void statusUpdate(final WorkThreadPool threadPool,
  389. final int threadIndex)
  390. {
  391. SwingUtilities.invokeLater(new Runnable()
  392. {
  393. public void run()
  394. {
  395. int requestCount = threadPool.getRequestCount();
  396. if(requestCount == 0)
  397. {
  398. getContentPane().setCursor(
  399. Cursor.getDefaultCursor());
  400. }
  401. else if(requestCount >= 1)
  402. {
  403. getContentPane().setCursor(
  404. Cursor.getPredefinedCursor(
  405. Cursor.WAIT_CURSOR));
  406. }
  407. }
  408. });
  409. } //}}}
  410. //{{{ progressUpdate() method
  411. public void progressUpdate(WorkThreadPool threadPool, int threadIndex)
  412. {
  413. } //}}}
  414. } //}}}
  415. //{{{ GetFileTypeRequest class
  416. class GetFileTypeRequest implements Runnable
  417. {
  418. VFS vfs;
  419. Object session;
  420. String path;
  421. int[] type;
  422. GetFileTypeRequest(VFS vfs, Object session,
  423. String path, int[] type)
  424. {
  425. this.vfs = vfs;
  426. this.session = session;
  427. this.path = path;
  428. this.type = type;
  429. }
  430. public void run()
  431. {
  432. try
  433. {
  434. VFS.DirectoryEntry entry
  435. = vfs._getDirectoryEntry(
  436. session,
  437. path,
  438. browser);
  439. if(entry == null)
  440. {
  441. // non-existent file
  442. type[0] = VFS.DirectoryEntry.FILE;
  443. }
  444. else
  445. type[0] = entry.type;
  446. }
  447. catch(IOException e)
  448. {
  449. Log.log(Log.ERROR,this,e);
  450. VFSManager.error(browser,path,
  451. "ioerror",
  452. new String[]
  453. { e.toString() });
  454. return;
  455. }
  456. finally
  457. {
  458. try
  459. {
  460. vfs._endVFSSession(
  461. session,
  462. browser);
  463. }
  464. catch(IOException e)
  465. {
  466. Log.log(Log.ERROR,this,e);
  467. VFSManager.error(browser,path,
  468. "ioerror",
  469. new String[]
  470. { e.toString() });
  471. return;
  472. }
  473. }
  474. }
  475. } //}}}
  476. //}}}
  477. }