PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/FilePropertiesDialog.java

#
Java | 362 lines | 268 code | 47 blank | 47 comment | 35 complexity | 8736e52b406fb15841562f5ee12b0d9c 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. * FilePropertiesDialog.java - A File property dialog
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2008 VladimirR
  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.gui;
  23. //{{{ Imports
  24. import java.io.File;
  25. import java.awt.BorderLayout;
  26. import java.awt.GridLayout;
  27. import java.awt.event.ActionEvent;
  28. import java.awt.event.ActionListener;
  29. import java.text.SimpleDateFormat;
  30. import java.util.Date;
  31. import javax.swing.*;
  32. import javax.swing.border.Border;
  33. import javax.swing.border.EmptyBorder;
  34. import org.gjt.sp.jedit.GUIUtilities;
  35. import org.gjt.sp.jedit.OperatingSystem;
  36. import org.gjt.sp.jedit.io.FavoritesVFS;
  37. import org.gjt.sp.jedit.io.VFS;
  38. import org.gjt.sp.jedit.jEdit;
  39. import org.gjt.sp.jedit.View;
  40. import org.gjt.sp.jedit.browser.VFSBrowser;
  41. import org.gjt.sp.jedit.io.VFSFile;
  42. import org.gjt.sp.jedit.io.FileVFS.LocalFile;
  43. import org.gjt.sp.util.IOUtilities;
  44. import org.gjt.sp.util.StandardUtilities;
  45. //}}}
  46. /**
  47. * File's Properties dialog. This class create and show a window from the selected file or files.
  48. */
  49. public class FilePropertiesDialog extends EnhancedDialog
  50. {
  51. private final VFSBrowser browser;
  52. private final VFSFile[] selectedFiles;
  53. private final VFSFile local;
  54. //{{{ FilePropertiesDialog(View view, VFSBrowser browser) constructor
  55. /**
  56. * The FilePropertiesDialog's constructor
  57. * @param view The view
  58. * @param browser The VFSBrowser
  59. */
  60. public FilePropertiesDialog(View view, VFSBrowser browser, VFSFile[] files)
  61. {
  62. super(view,jEdit.getProperty("vfs.browser.properties.title"),true);
  63. GUIUtilities.loadGeometry(this,"propdialog");
  64. this.browser = browser;
  65. if (files.length > 0)
  66. selectedFiles = files;
  67. else
  68. selectedFiles = browser.getSelectedFiles();
  69. local = selectedFiles[0];
  70. createAndShowGUI();
  71. } //}}}
  72. //{{{ addComponentsToPane() method
  73. public void addComponentsToPane()
  74. {
  75. JPanel content = new JPanel(new BorderLayout());
  76. content.setBorder(new EmptyBorder(12,5,0,5));
  77. setContentPane(content);
  78. if (selectedFiles.length == 1)
  79. {
  80. content.add(BorderLayout.NORTH, createNorthPanel());
  81. content.add(BorderLayout.CENTER, createCenterPanel());
  82. content.add(BorderLayout.SOUTH, createSouthPanel());
  83. }
  84. else if(selectedFiles.length > 1)
  85. {
  86. content.add(BorderLayout.NORTH, createNorthPanelAll());
  87. content.add(BorderLayout.CENTER, createCenterPanelAll());
  88. content.add(BorderLayout.SOUTH, createSouthPanelAll());
  89. }
  90. } //}}}
  91. //{{{createNorthPanelAll() method
  92. public JPanel createNorthPanelAll()
  93. {
  94. JPanel northPanel = new JPanel(new BorderLayout());
  95. infoIcon = new JLabel();
  96. infoIcon.setIcon(UIManager.getIcon("OptionPane.informationIcon"));
  97. northPanel.add(BorderLayout.WEST, infoIcon);
  98. int filesCounter = 0;
  99. int directoriesCounter = 0;
  100. for(int i=0;i<selectedFiles.length;i++)
  101. {
  102. if(selectedFiles[i].getType() == VFSFile.DIRECTORY)
  103. {
  104. directoriesCounter++;
  105. }
  106. else if(selectedFiles[i].getType() == VFSFile.FILE)
  107. {
  108. filesCounter++;
  109. }
  110. }
  111. JPanel nameField = new JPanel();
  112. nameField.add(new JLabel(jEdit.getProperty("fileprop.selectedFiles")+": "+filesCounter+", "+
  113. jEdit.getProperty("fileprop.selectedDirectories")+": "+directoriesCounter));
  114. northPanel.add(BorderLayout.CENTER, nameField);
  115. northPanel.add(BorderLayout.SOUTH, new JPanel());
  116. return northPanel;
  117. } //}}}
  118. //{{{createCenterPanelAll() method
  119. public JPanel createCenterPanelAll()
  120. {
  121. long filesSize = 0L;
  122. JPanel centerPanel = new JPanel(new BorderLayout());
  123. for (int i=0;i<selectedFiles.length;i++)
  124. {
  125. if(selectedFiles[i].getType() == VFSFile.DIRECTORY)
  126. {
  127. File ioFile = new File(selectedFiles[i].getPath());
  128. filesSize += IOUtilities.fileLength(ioFile);
  129. }
  130. else if(selectedFiles[i].getType() == VFSFile.FILE)
  131. {
  132. filesSize += selectedFiles[i].getLength();
  133. }
  134. }
  135. JPanel propField = new JPanel();
  136. propField.setLayout(new GridLayout(2, 1));
  137. String path = local.getPath();
  138. if(OperatingSystem.isWindows() || OperatingSystem.isWindows9x() || OperatingSystem.isWindowsNT())
  139. {
  140. path = path.substring(0, path.lastIndexOf(92)); // 92 = '\'
  141. }
  142. else
  143. {
  144. path = path.substring(0, path.lastIndexOf('/'));
  145. }
  146. propField.add(new JLabel(jEdit.getProperty("fileprop.path")+": "+path));
  147. propField.add(new JLabel(jEdit.getProperty("fileprop.size")+": "+
  148. StandardUtilities.formatFileSize(filesSize)));
  149. Border etch = BorderFactory.createEtchedBorder();
  150. propField.setBorder(BorderFactory.createTitledBorder(etch, jEdit.getProperty("fileprop.properties")));
  151. centerPanel.add(BorderLayout.CENTER, propField);
  152. return centerPanel;
  153. } //}}}
  154. //{{{ createSouthPanelAll() method
  155. public JPanel createSouthPanelAll()
  156. {
  157. ButtonActionHandler actionHandler = new ButtonActionHandler();
  158. JPanel southPanel = new JPanel(new BorderLayout());
  159. JPanel buttonsField = new JPanel();
  160. okButton = new JButton(jEdit.getProperty("fileprop.okBtn"));
  161. buttonsField.add(okButton);
  162. okButton.addActionListener(actionHandler);
  163. cancelButton = new JButton(jEdit.getProperty("fileprop.cancelBtn"));
  164. buttonsField.add(cancelButton);
  165. cancelButton.addActionListener(actionHandler);
  166. southPanel.add(BorderLayout.EAST, buttonsField);
  167. return southPanel;
  168. } //}}}
  169. //{{{ createNorthPanel() method
  170. public JPanel createNorthPanel()
  171. {
  172. JPanel northPanel = new JPanel(new BorderLayout());
  173. infoIcon = new JLabel();
  174. infoIcon.setIcon(UIManager.getIcon("OptionPane.informationIcon"));
  175. northPanel.add(BorderLayout.WEST, infoIcon);
  176. JPanel nameField = new JPanel();
  177. nameField.add(new JLabel(jEdit.getProperty("fileprop.name")+": "));
  178. String filename;
  179. if (local instanceof FavoritesVFS.Favorite)
  180. {
  181. FavoritesVFS.Favorite favorite = (FavoritesVFS.Favorite) local;
  182. filename = favorite.getLabel();
  183. }
  184. else
  185. {
  186. filename = local.getName();
  187. }
  188. nameTextField = new JTextField(filename, 20);
  189. if ((local.getVFS().getCapabilities() & VFS.RENAME_CAP) == 0)
  190. {
  191. // If the VFS cannot rename, the nameTextField is non editable
  192. nameTextField.setEditable(false);
  193. }
  194. nameField.add(nameTextField);
  195. northPanel.add(BorderLayout.CENTER, nameField);
  196. northPanel.add(BorderLayout.SOUTH, new JPanel());
  197. return northPanel;
  198. } //}}}
  199. //{{{ createCenterPanel() method
  200. public JPanel createCenterPanel()
  201. {
  202. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
  203. JPanel centerPanel = new JPanel(new BorderLayout());
  204. JPanel propField = new JPanel();
  205. propField.setLayout(new GridLayout(4, 1));
  206. propField.add(new JLabel(jEdit.getProperty("fileprop.name")+": "+local.getName()));
  207. propField.add(new JLabel(jEdit.getProperty("fileprop.path")+": "+local.getPath()));
  208. // Show last modified property only for LocalFile
  209. if (local instanceof LocalFile)
  210. {
  211. propField.add(new JLabel(jEdit.getProperty("fileprop.lastmod")+": "+
  212. sdf.format(new Date(((LocalFile)local).getModified()))));
  213. }
  214. if(local.getType() == VFSFile.DIRECTORY)
  215. {
  216. File ioFile = new File(local.getPath());
  217. propField.add(new JLabel(jEdit.getProperty("fileprop.size")+": "+
  218. StandardUtilities.formatFileSize(IOUtilities.fileLength(ioFile))));
  219. }
  220. else
  221. {
  222. propField.add(new JLabel(jEdit.getProperty("fileprop.size")+": "+
  223. StandardUtilities.formatFileSize(local.getLength())));
  224. }
  225. Border etch = BorderFactory.createEtchedBorder();
  226. propField.setBorder(BorderFactory.createTitledBorder(etch, jEdit.getProperty("fileprop.properties")));
  227. centerPanel.add(BorderLayout.CENTER, propField);
  228. JPanel attributeField = new JPanel();
  229. attributeField.setLayout(new GridLayout(1, 2));
  230. readable = new JCheckBox(jEdit.getProperty("fileprop.readable"));
  231. readable.setSelected(local.isReadable());
  232. readable.setEnabled(false);
  233. attributeField.add(readable);
  234. write = new JCheckBox(jEdit.getProperty("fileprop.writeable"));
  235. write.setSelected(local.isWriteable());
  236. write.setEnabled(false);
  237. attributeField.add(write);
  238. attributeField.setBorder(BorderFactory.createTitledBorder(etch, jEdit.getProperty("fileprop.attribute")));
  239. centerPanel.add(BorderLayout.SOUTH, attributeField);
  240. return centerPanel;
  241. } //}}}
  242. //{{{ createSouthPanel() method
  243. public JPanel createSouthPanel()
  244. {
  245. ButtonActionHandler actionHandler = new ButtonActionHandler();
  246. JPanel southPanel = new JPanel(new BorderLayout());
  247. JPanel buttonsField = new JPanel();
  248. okButton = new JButton(jEdit.getProperty("fileprop.okBtn"));
  249. buttonsField.add(okButton);
  250. okButton.addActionListener(actionHandler);
  251. cancelButton = new JButton(jEdit.getProperty("fileprop.cancelBtn"));
  252. buttonsField.add(cancelButton);
  253. cancelButton.addActionListener(actionHandler);
  254. southPanel.add(BorderLayout.EAST, buttonsField);
  255. return southPanel;
  256. } //}}}
  257. //{{{ ok() method
  258. @Override
  259. public void ok()
  260. {
  261. if(nameTextField != null)
  262. {
  263. VFSFile vfsFile = browser.getSelectedFiles()[0];
  264. if ((vfsFile.getVFS().getCapabilities() & VFS.RENAME_CAP) != 0)
  265. {
  266. browser.rename(vfsFile, nameTextField.getText());
  267. }
  268. }
  269. GUIUtilities.saveGeometry(this,"propdialog");
  270. setVisible(false);
  271. } //}}}
  272. //{{{ cancel() method
  273. @Override
  274. public void cancel()
  275. {
  276. GUIUtilities.saveGeometry(this,"propdialog");
  277. setVisible(false);
  278. } //}}}
  279. //{{{ Private members
  280. private JButton okButton;
  281. private JButton cancelButton;
  282. private JTextField nameTextField;
  283. private JLabel infoIcon;
  284. private JCheckBox readable;
  285. private JCheckBox write;
  286. //{{{ createAndShowGUI() method
  287. private void createAndShowGUI()
  288. {
  289. addComponentsToPane();
  290. pack();
  291. setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  292. setFocusable(true);
  293. toFront();
  294. requestFocus();
  295. setResizable(false);
  296. setVisible(true);
  297. } //}}}
  298. //{{{ ButtonActionHandler class
  299. private class ButtonActionHandler implements ActionListener
  300. {
  301. public void actionPerformed(ActionEvent evt)
  302. {
  303. Object source = evt.getSource();
  304. if(source == okButton)
  305. {
  306. ok();
  307. }
  308. else if(source == cancelButton)
  309. {
  310. cancel();
  311. }
  312. }
  313. } //}}}
  314. //}}}
  315. }