PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/jedit/browser/VFSFileChooserDialog.java

#
Java | 368 lines | 270 code | 45 blank | 53 comment | 60 complexity | c353b416b947913acdc1d30ccf2f1aad 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 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.util.Vector;
  30. import org.gjt.sp.jedit.gui.EnhancedDialog;
  31. import org.gjt.sp.jedit.io.*;
  32. import org.gjt.sp.jedit.*;
  33. import org.gjt.sp.util.*;
  34. //}}}
  35. /**
  36. * Wraps the VFS browser in a modal dialog.
  37. * @author Slava Pestov
  38. * @version $Id: VFSFileChooserDialog.java 3925 2001-11-30 11:40:16Z spestov $
  39. */
  40. public class VFSFileChooserDialog extends EnhancedDialog
  41. implements WorkThreadProgressListener
  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(path == null || path.endsWith(File.separator) || path.endsWith("/"))
  53. name = null;
  54. else
  55. {
  56. VFS vfs = VFSManager.getVFSForPath(path);
  57. name = vfs.getFileName(path);
  58. path = vfs.getParentOfPath(path);
  59. }
  60. browser = new VFSBrowser(view,path,mode,multipleSelection,true);
  61. browser.addBrowserListener(new BrowserHandler());
  62. content.add(BorderLayout.CENTER,browser);
  63. JPanel bottomPanel = new JPanel(new BorderLayout());
  64. JPanel panel = new JPanel();
  65. panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
  66. panel.setBorder(new EmptyBorder(12,0,0,0));
  67. JLabel label = new JLabel(jEdit.getProperty("vfs.browser.dialog.filename"));
  68. panel.add(label);
  69. panel.add(Box.createHorizontalStrut(12));
  70. filenameField = new JTextField();
  71. filenameField.setText(name);
  72. filenameField.addKeyListener(new KeyHandler());
  73. filenameField.selectAll();
  74. label.setDisplayedMnemonic(jEdit.getProperty(
  75. "vfs.browser.dialog.filename.mnemonic").charAt(0));
  76. label.setLabelFor(filenameField);
  77. Dimension dim = filenameField.getPreferredSize();
  78. dim.width = Integer.MAX_VALUE;
  79. filenameField.setMaximumSize(dim);
  80. Box box = new Box(BoxLayout.Y_AXIS);
  81. box.add(Box.createGlue());
  82. box.add(filenameField);
  83. box.add(Box.createGlue());
  84. panel.add(box);
  85. panel.add(Box.createHorizontalStrut(12));
  86. if(mode == VFSBrowser.BROWSER || mode == VFSBrowser.OPEN_DIALOG)
  87. {
  88. GUIUtilities.requestFocus(this,browser.getBrowserView()
  89. .getDefaultFocusComponent());
  90. }
  91. else
  92. {
  93. GUIUtilities.requestFocus(this,filenameField);
  94. }
  95. ok = new JButton(jEdit.getProperty("vfs.browser.dialog."
  96. + (mode == VFSBrowser.OPEN_DIALOG ? "open" : "save")));
  97. ok.addActionListener(new ActionHandler());
  98. getRootPane().setDefaultButton(ok);
  99. panel.add(ok);
  100. panel.add(Box.createHorizontalStrut(6));
  101. cancel = new JButton(jEdit.getProperty("common.cancel"));
  102. cancel.addActionListener(new ActionHandler());
  103. panel.add(cancel);
  104. bottomPanel.add(BorderLayout.NORTH,panel);
  105. status = new JLabel(" ");
  106. status.setBorder(new EmptyBorder(12,0,0,0));
  107. bottomPanel.add(BorderLayout.SOUTH,status);
  108. content.add(BorderLayout.SOUTH,bottomPanel);
  109. VFSManager.getIOThreadPool().addProgressListener(this);
  110. pack();
  111. GUIUtilities.loadGeometry(this,"vfs.browser.dialog");
  112. show();
  113. } //}}}
  114. //{{{ dispose() method
  115. public void dispose()
  116. {
  117. GUIUtilities.saveGeometry(this,"vfs.browser.dialog");
  118. VFSManager.getIOThreadPool().removeProgressListener(this);
  119. super.dispose();
  120. } //}}}
  121. //{{{ ok() method
  122. public void ok()
  123. {
  124. VFS.DirectoryEntry[] files = browser.getSelectedFiles();
  125. String directory = browser.getDirectory();
  126. if(files.length == 0)
  127. {
  128. filename = filenameField.getText();
  129. if(filename.length() == 0)
  130. {
  131. getToolkit().beep();
  132. return;
  133. }
  134. }
  135. else
  136. {
  137. for(int i = 0; i < files.length; i++)
  138. {
  139. VFS.DirectoryEntry file = files[i];
  140. if(file.type == VFS.DirectoryEntry.FILESYSTEM
  141. || file.type == VFS.DirectoryEntry.DIRECTORY)
  142. {
  143. browser.setDirectory(file.path);
  144. return;
  145. }
  146. else if(browser.getMode() == VFSBrowser.SAVE_DIALOG)
  147. filename = file.path;
  148. }
  149. }
  150. if(browser.getMode() == VFSBrowser.SAVE_DIALOG)
  151. {
  152. if(!MiscUtilities.isURL(directory)
  153. && !MiscUtilities.isURL(filename))
  154. {
  155. filename = MiscUtilities.constructPath(directory,
  156. MiscUtilities.canonPath(filename));
  157. if(doFileExistsWarning(filename))
  158. return;
  159. }
  160. }
  161. isOK = true;
  162. dispose();
  163. } //}}}
  164. //{{{ cancel() method
  165. public void cancel()
  166. {
  167. dispose();
  168. } //}}}
  169. //{{{ getSelectedFiles() method
  170. public String[] getSelectedFiles()
  171. {
  172. if(!isOK)
  173. return null;
  174. if(filename != null)
  175. {
  176. String path = browser.getDirectory();
  177. return new String[] { MiscUtilities.constructPath(
  178. path,filename) };
  179. }
  180. else
  181. {
  182. Vector vector = new Vector();
  183. VFS.DirectoryEntry[] selectedFiles = browser.getSelectedFiles();
  184. for(int i = 0; i < selectedFiles.length; i++)
  185. {
  186. VFS.DirectoryEntry file = selectedFiles[i];
  187. if(file.type == VFS.DirectoryEntry.FILE)
  188. vector.addElement(file.path);
  189. }
  190. String[] retVal = new String[vector.size()];
  191. vector.copyInto(retVal);
  192. return retVal;
  193. }
  194. } //}}}
  195. //{{{ WorkThreadListener implementation
  196. //{{{ statusUpdate() method
  197. public void statusUpdate(final WorkThreadPool threadPool, int threadIndex)
  198. {
  199. SwingUtilities.invokeLater(new Runnable()
  200. {
  201. public void run()
  202. {
  203. int requestCount = threadPool.getRequestCount();
  204. if(requestCount == 0)
  205. {
  206. status.setText(jEdit.getProperty(
  207. "view.status.io.done"));
  208. }
  209. else if(requestCount == 1)
  210. {
  211. status.setText(jEdit.getProperty(
  212. "view.status.io-1"));
  213. }
  214. else
  215. {
  216. Object[] args = { new Integer(requestCount) };
  217. status.setText(jEdit.getProperty(
  218. "view.status.io",args));
  219. }
  220. }
  221. });
  222. } //}}}
  223. //{{{ progressUpdate() method
  224. public void progressUpdate(WorkThreadPool threadPool, int threadIndex)
  225. {
  226. } //}}}
  227. //}}}
  228. //{{{ Private members
  229. //{{{ Instance variables
  230. private VFSBrowser browser;
  231. private JTextField filenameField;
  232. private JLabel status;
  233. private String filename;
  234. private JButton ok;
  235. private JButton cancel;
  236. private boolean isOK;
  237. //}}}
  238. //{{{ doFileExistsWarning() method
  239. private boolean doFileExistsWarning(String filename)
  240. {
  241. if(new File(filename).exists())
  242. {
  243. String[] args = { MiscUtilities.getFileName(filename) };
  244. int result = GUIUtilities.confirm(browser,
  245. "fileexists",args,
  246. JOptionPane.YES_NO_OPTION,
  247. JOptionPane.WARNING_MESSAGE);
  248. if(result != JOptionPane.YES_OPTION)
  249. return true;
  250. }
  251. return false;
  252. } //}}}
  253. //}}}
  254. //{{{ Inner classes
  255. //{{{ ActionHandler class
  256. class ActionHandler implements ActionListener
  257. {
  258. //{{{ actionPerformed() method
  259. public void actionPerformed(ActionEvent evt)
  260. {
  261. if(evt.getSource() == ok)
  262. ok();
  263. else if(evt.getSource() == cancel)
  264. cancel();
  265. } //}}}
  266. } //}}}
  267. //{{{ BrowserHandler class
  268. class BrowserHandler implements BrowserListener
  269. {
  270. //{{{ filesSelected() method
  271. public void filesSelected(VFSBrowser browser, VFS.DirectoryEntry[] files)
  272. {
  273. if(files.length == 0)
  274. return;
  275. else if(files.length == 1)
  276. {
  277. VFS.DirectoryEntry file = files[0];
  278. if(file.type == VFS.DirectoryEntry.FILE)
  279. {
  280. String path = file.path;
  281. String directory = browser.getDirectory();
  282. VFS vfs = VFSManager.getVFSForPath(directory);
  283. String parent = vfs.getParentOfPath(path);
  284. if(parent.endsWith("/") || parent.endsWith(File.separator))
  285. parent = parent.substring(0,parent.length() - 1);
  286. if(parent.equals(directory))
  287. path = file.name;
  288. filenameField.setText(path);
  289. }
  290. }
  291. else
  292. {
  293. filenameField.setText(null);
  294. }
  295. } //}}}
  296. //{{{ filesActivated() method
  297. public void filesActivated(VFSBrowser browser, VFS.DirectoryEntry[] files)
  298. {
  299. for(int i = 0; i < files.length; i++)
  300. {
  301. VFS.DirectoryEntry file = files[i];
  302. if(file.type == VFS.DirectoryEntry.FILESYSTEM
  303. || file.type == VFS.DirectoryEntry.DIRECTORY)
  304. {
  305. // the browser will list the directory
  306. // in question, so just return
  307. return;
  308. }
  309. }
  310. ok();
  311. } //}}}
  312. } //}}}
  313. //{{{ KeyHandler class
  314. class KeyHandler extends KeyAdapter
  315. {
  316. //{{{ keyPressed() method
  317. public void keyPressed(KeyEvent evt)
  318. {
  319. browser.getBrowserView().selectNone();
  320. } //}}}
  321. } //}}}
  322. //}}}
  323. }