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

# · Java · 332 lines · 241 code · 46 blank · 45 comment · 29 complexity · 7d3b5deebeb459f132bcd60d89c978be MD5 · raw file

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