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

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

#
Java | 520 lines | 413 code | 51 blank | 56 comment | 76 complexity | c15c7b35d16d427ea70f89d24d17d713 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 4808 2003-06-22 21:23:33Z 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. if(files.length != 0)
  134. {
  135. browser.filesActivated(VFSBrowser.M_OPEN,false);
  136. }
  137. else
  138. {
  139. if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
  140. {
  141. isOK = true;
  142. dispose();
  143. return;
  144. }
  145. String filename = filenameField.getText();
  146. 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(vfs instanceof FileVFS)
  178. {
  179. if(doFileExistsWarning(path))
  180. break;
  181. }
  182. isOK = true;
  183. if(browser.getMode() == VFSBrowser.BROWSER_DIALOG)
  184. {
  185. Hashtable props = new Hashtable();
  186. props.put(Buffer.ENCODING,browser.currentEncoding);
  187. jEdit.openFile(browser.getView(),
  188. browser.getDirectory(),
  189. path,false,props);
  190. }
  191. dispose();
  192. break;
  193. case VFS.DirectoryEntry.DIRECTORY:
  194. case VFS.DirectoryEntry.FILESYSTEM:
  195. browser.setDirectory(path);
  196. break;
  197. }
  198. }
  199. });
  200. }
  201. } //}}}
  202. //{{{ cancel() method
  203. public void cancel()
  204. {
  205. dispose();
  206. } //}}}
  207. //{{{ getSelectedFiles() method
  208. public String[] getSelectedFiles()
  209. {
  210. if(!isOK)
  211. return null;
  212. String filename = filenameField.getText();
  213. if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
  214. {
  215. return new String[] { browser.getDirectory() };
  216. }
  217. else if(filename != null && filename.length() != 0)
  218. {
  219. String path = browser.getDirectory();
  220. return new String[] { MiscUtilities.constructPath(
  221. path,filename) };
  222. }
  223. else
  224. {
  225. Vector vector = new Vector();
  226. VFS.DirectoryEntry[] selectedFiles = browser.getSelectedFiles();
  227. for(int i = 0; i < selectedFiles.length; i++)
  228. {
  229. VFS.DirectoryEntry file = selectedFiles[i];
  230. if(file.type == VFS.DirectoryEntry.FILE)
  231. vector.addElement(file.path);
  232. }
  233. String[] retVal = new String[vector.size()];
  234. vector.copyInto(retVal);
  235. return retVal;
  236. }
  237. } //}}}
  238. //{{{ Private members
  239. //{{{ Instance variables
  240. private VFSBrowser browser;
  241. private VFSFileNameField filenameField;
  242. private JButton ok;
  243. private JButton cancel;
  244. private boolean isOK;
  245. private WorkThreadHandler workThreadHandler;
  246. //}}}
  247. //{{{ doFileExistsWarning() method
  248. private boolean doFileExistsWarning(String filename)
  249. {
  250. if(browser.getMode() == VFSBrowser.SAVE_DIALOG
  251. && new File(filename).exists())
  252. {
  253. String[] args = { MiscUtilities.getFileName(filename) };
  254. int result = GUIUtilities.confirm(browser,
  255. "fileexists",args,
  256. JOptionPane.YES_NO_OPTION,
  257. JOptionPane.WARNING_MESSAGE);
  258. if(result != JOptionPane.YES_OPTION)
  259. return true;
  260. }
  261. return false;
  262. } //}}}
  263. //}}}
  264. //{{{ Inner classes
  265. //{{{ ActionHandler class
  266. class ActionHandler implements ActionListener
  267. {
  268. public void actionPerformed(ActionEvent evt)
  269. {
  270. if(evt.getSource() == ok)
  271. {
  272. if(!browser.getDirectory().equals(
  273. browser.getDirectoryField().getText()))
  274. {
  275. browser.setDirectory(browser.getDirectoryField().getText());
  276. }
  277. else
  278. ok();
  279. }
  280. else if(evt.getSource() == cancel)
  281. cancel();
  282. }
  283. } //}}}
  284. //{{{ BrowserHandler class
  285. class BrowserHandler implements BrowserListener
  286. {
  287. //{{{ filesSelected() method
  288. public void filesSelected(VFSBrowser browser, VFS.DirectoryEntry[] files)
  289. {
  290. if(files.length == 0)
  291. {
  292. if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
  293. {
  294. ok.setText(jEdit.getProperty(
  295. "vfs.browser.dialog.choose-dir"));
  296. }
  297. return;
  298. }
  299. else if(files.length == 1)
  300. {
  301. if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
  302. {
  303. ok.setText(jEdit.getProperty(
  304. "vfs.browser.dialog.open"));
  305. }
  306. VFS.DirectoryEntry file = files[0];
  307. if(file.type == VFS.DirectoryEntry.FILE)
  308. {
  309. String path = file.path;
  310. String directory = browser.getDirectory();
  311. String parent = MiscUtilities
  312. .getParentOfPath(path);
  313. if(VFSBrowser.pathsEqual(parent,directory))
  314. path = file.name;
  315. filenameField.setText(path);
  316. filenameField.selectAll();
  317. }
  318. }
  319. else
  320. {
  321. if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
  322. {
  323. ok.setText(jEdit.getProperty(
  324. "vfs.browser.dialog.open"));
  325. }
  326. filenameField.setText(null);
  327. }
  328. } //}}}
  329. //{{{ filesActivated() method
  330. public void filesActivated(VFSBrowser browser, VFS.DirectoryEntry[] files)
  331. {
  332. filenameField.selectAll();
  333. if(files.length == 0)
  334. {
  335. // user pressed enter when the vfs table or
  336. // file name field has focus, with nothing
  337. // selected.
  338. ok();
  339. return;
  340. }
  341. for(int i = 0; i < files.length; i++)
  342. {
  343. if(files[i].type == VFS.DirectoryEntry.FILE)
  344. {
  345. String path = files[i].path;
  346. VFS vfs = VFSManager.getVFSForPath(path);
  347. if(browser.getMode() == VFSBrowser.SAVE_DIALOG
  348. && vfs instanceof FileVFS)
  349. {
  350. if(doFileExistsWarning(path))
  351. return;
  352. }
  353. isOK = true;
  354. filenameField.setText(null);
  355. dispose();
  356. return;
  357. }
  358. else
  359. return;
  360. }
  361. } //}}}
  362. } //}}}
  363. //{{{ KeyHandler class
  364. class KeyHandler extends KeyAdapter
  365. {
  366. public void keyTyped(KeyEvent evt)
  367. {
  368. switch(evt.getKeyChar())
  369. {
  370. case '/':
  371. case '-':
  372. case '~':
  373. filenameField.processKeyEvent(evt);
  374. filenameField.requestFocus();
  375. break;
  376. }
  377. }
  378. } //}}}
  379. //{{{ WorkThreadListener class
  380. class WorkThreadHandler implements WorkThreadProgressListener
  381. {
  382. //{{{ statusUpdate() method
  383. public void statusUpdate(WorkThreadPool threadPool, int threadIndex)
  384. {
  385. // synchronize with hide/showWaitCursor()
  386. synchronized(VFSFileChooserDialog.this)
  387. {
  388. int requestCount = threadPool.getRequestCount();
  389. if(requestCount == 0)
  390. {
  391. getContentPane().setCursor(
  392. Cursor.getDefaultCursor());
  393. }
  394. else if(requestCount >= 1)
  395. {
  396. getContentPane().setCursor(
  397. Cursor.getPredefinedCursor(
  398. Cursor.WAIT_CURSOR));
  399. }
  400. }
  401. } //}}}
  402. //{{{ progressUpdate() method
  403. public void progressUpdate(WorkThreadPool threadPool, int threadIndex)
  404. {
  405. } //}}}
  406. } //}}}
  407. //{{{ GetFileTypeRequest class
  408. class GetFileTypeRequest implements Runnable
  409. {
  410. VFS vfs;
  411. Object session;
  412. String path;
  413. int[] type;
  414. GetFileTypeRequest(VFS vfs, Object session,
  415. String path, int[] type)
  416. {
  417. this.vfs = vfs;
  418. this.session = session;
  419. this.path = path;
  420. this.type = type;
  421. }
  422. public void run()
  423. {
  424. try
  425. {
  426. VFS.DirectoryEntry entry
  427. = vfs._getDirectoryEntry(
  428. session,
  429. path,
  430. browser);
  431. if(entry == null)
  432. {
  433. // non-existent file
  434. type[0] = VFS.DirectoryEntry.FILE;
  435. }
  436. else
  437. type[0] = entry.type;
  438. }
  439. catch(IOException e)
  440. {
  441. Log.log(Log.ERROR,this,e);
  442. VFSManager.error(browser,path,
  443. "ioerror",
  444. new String[]
  445. { e.toString() });
  446. return;
  447. }
  448. finally
  449. {
  450. try
  451. {
  452. vfs._endVFSSession(
  453. session,
  454. browser);
  455. }
  456. catch(IOException e)
  457. {
  458. Log.log(Log.ERROR,this,e);
  459. VFSManager.error(browser,path,
  460. "ioerror",
  461. new String[]
  462. { e.toString() });
  463. return;
  464. }
  465. }
  466. }
  467. } //}}}
  468. //}}}
  469. }