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