PageRenderTime 46ms CodeModel.GetById 14ms app.highlight 26ms RepoModel.GetById 2ms app.codeStats 0ms

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

#
Java | 417 lines | 311 code | 60 blank | 46 comment | 57 complexity | 3f73f4708891b930adc1e3e2fb0b9c8e MD5 | raw file
  1/*
  2 * FilesChangedDialog.java - Files changed on disk
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 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
 23package org.gjt.sp.jedit.gui;
 24
 25//{{{ Imports
 26import javax.swing.border.*;
 27import javax.swing.event.*;
 28import javax.swing.tree.*;
 29import javax.swing.*;
 30import java.awt.event.*;
 31import java.awt.*;
 32import org.gjt.sp.jedit.io.*;
 33import org.gjt.sp.jedit.*;
 34//}}}
 35
 36/**
 37 * Files changed on disk dialog.
 38 *
 39 * @author Slava Pestov
 40 * @version $Id: FilesChangedDialog.java 5067 2004-06-28 06:45:27Z spestov $
 41 */
 42public class FilesChangedDialog extends EnhancedDialog
 43{
 44	//{{{ FilesChangedDialog constructor
 45	public FilesChangedDialog(View view, int[] states,
 46		boolean alreadyReloaded)
 47	{
 48		super(view,jEdit.getProperty("files-changed.title"),false);
 49
 50		this.view = view;
 51
 52		JPanel content = new JPanel(new BorderLayout(12,12));
 53		content.setBorder(new EmptyBorder(12,12,12,12));
 54		setContentPane(content);
 55
 56		Box iconBox = new Box(BoxLayout.Y_AXIS);
 57		iconBox.add(new JLabel(UIManager.getIcon("OptionPane.warningIcon")));
 58		iconBox.add(Box.createGlue());
 59		content.add(BorderLayout.WEST,iconBox);
 60
 61		JPanel centerPanel = new JPanel(new BorderLayout());
 62
 63		JLabel label = new JLabel(jEdit.getProperty("files-changed.caption"));
 64		label.setBorder(new EmptyBorder(0,0,6,0));
 65		centerPanel.add(BorderLayout.NORTH,label);
 66
 67		DefaultMutableTreeNode deleted = new DefaultMutableTreeNode(
 68			jEdit.getProperty("files-changed.deleted"),true);
 69		DefaultMutableTreeNode changed = new DefaultMutableTreeNode(
 70			jEdit.getProperty("files-changed.changed"
 71			+ (alreadyReloaded ? "-auto" : "")),true);
 72		DefaultMutableTreeNode changedDirty = new DefaultMutableTreeNode(
 73			jEdit.getProperty("files-changed.changed-dirty"
 74			+ (alreadyReloaded ? "-auto" : "")),true);
 75		Buffer[] buffers = jEdit.getBuffers();
 76		for(int i = 0; i < states.length; i++)
 77		{
 78			Buffer buffer = buffers[i];
 79			DefaultMutableTreeNode addTo;
 80			switch(states[i])
 81			{
 82			case Buffer.FILE_DELETED:
 83				addTo = deleted;
 84				break;
 85			case Buffer.FILE_CHANGED:
 86				addTo = (buffer.isDirty() ? changedDirty : changed);
 87				break;
 88			default:
 89				addTo = null;
 90				break;
 91			}
 92
 93			if(addTo != null)
 94			{
 95				addTo.add(new DefaultMutableTreeNode(
 96					buffer.getPath()));
 97			}
 98		}
 99
100		root = new DefaultMutableTreeNode("",true);
101		if(deleted.getChildCount() != 0)
102		{
103			root.add(deleted);
104		}
105		if(changed.getChildCount() != 0)
106		{
107			root.add(changed);
108		}
109		if(changedDirty.getChildCount() != 0)
110		{
111			root.add(changedDirty);
112		}
113
114		bufferTreeModel = new DefaultTreeModel(root);
115		bufferTree = new JTree(bufferTreeModel);
116		bufferTree.setRootVisible(false);
117		bufferTree.setVisibleRowCount(10);
118		bufferTree.setCellRenderer(new Renderer());
119		bufferTree.getSelectionModel().addTreeSelectionListener(new TreeHandler());
120		bufferTree.getSelectionModel().setSelectionMode(
121			TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
122
123		centerPanel.add(BorderLayout.CENTER,new JScrollPane(bufferTree));
124
125		content.add(BorderLayout.CENTER,centerPanel);
126
127		Box buttons = new Box(BoxLayout.X_AXIS);
128		buttons.add(Box.createGlue());
129
130		if(!alreadyReloaded)
131		{
132			selectAll = new JButton(jEdit.getProperty(
133				"files-changed.select-all"));
134			selectAll.setMnemonic(jEdit.getProperty(
135				"files-changed.select-all.mnemonic").charAt(0));
136			buttons.add(selectAll);
137			selectAll.addActionListener(new ActionHandler());
138
139			buttons.add(Box.createHorizontalStrut(12));
140
141			reload = new JButton(jEdit.getProperty(
142				"files-changed.reload"));
143			reload.setMnemonic(jEdit.getProperty(
144				"files-changed.reload.mnemonic").charAt(0));
145			buttons.add(reload);
146			reload.addActionListener(new ActionHandler());
147
148			buttons.add(Box.createHorizontalStrut(12));
149		}
150
151		close = new JButton(jEdit.getProperty("common.close"));
152		getRootPane().setDefaultButton(close);
153		buttons.add(close);
154		close.addActionListener(new ActionHandler());
155
156		buttons.add(Box.createGlue());
157
158		content.add(BorderLayout.SOUTH,buttons);
159
160		bufferTree.expandPath(new TreePath(
161			new Object[] {
162				root,
163				deleted
164			}));
165		bufferTree.expandPath(new TreePath(
166			new Object[] {
167				root,
168				changed
169			}));
170		bufferTree.expandPath(new TreePath(
171			new Object[] {
172				root,
173				changedDirty
174			}));
175
176		GUIUtilities.requestFocus(this,bufferTree);
177
178		updateEnabled();
179
180		pack();
181		setLocationRelativeTo(view);
182		setVisible(true);
183	} //}}}
184
185	//{{{ ok() method
186	public void ok()
187	{
188		dispose();
189	} //}}}
190
191	//{{{ cancel() method
192	public void cancel()
193	{
194		dispose();
195	} //}}}
196
197	//{{{ Private members
198	private View view;
199	private JTree bufferTree;
200	private DefaultTreeModel bufferTreeModel;
201	private DefaultMutableTreeNode root;
202	private JButton selectAll;
203
204	// hack so that 'select all' does not change current buffer
205	private boolean selectAllInProgress;
206
207	private JButton reload;
208	private JButton close;
209
210	//{{{ updateEnabled() method
211	private void updateEnabled()
212	{
213		TreePath[] paths = bufferTree
214			.getSelectionPaths();
215		boolean enabled = false;
216		if(paths != null)
217		{
218			for(int i = 0; i < paths.length; i++)
219			{
220				Object[] path = paths[i].getPath();
221				if(path.length == 3)
222					enabled = true;
223			}
224		}
225
226		if(reload != null)
227			reload.setEnabled(enabled);
228	} //}}}
229
230	//{{{ selectAll() method
231	private void selectAll()
232	{
233		selectAllInProgress = true;
234
235		TreeNode[] path = new TreeNode[3];
236		path[0] = root;
237		for(int i = 0; i < root.getChildCount(); i++)
238		{
239			DefaultMutableTreeNode node =
240				(DefaultMutableTreeNode)
241				root.getChildAt(i);
242			path[1] = node;
243			for(int j = 0; j < node.getChildCount(); j++)
244			{
245				DefaultMutableTreeNode node2 =
246					(DefaultMutableTreeNode)
247					node.getChildAt(j);
248				path[2] = node2;
249				bufferTree.getSelectionModel()
250					.addSelectionPath(
251					new TreePath(path));
252			}
253		}
254
255		selectAllInProgress = false;
256
257		updateEnabled();
258	} //}}}
259
260	//{{{ reload() method
261	private void reload()
262	{
263		TreePath[] paths = bufferTree
264			.getSelectionPaths();
265		if(paths == null || paths.length == 0)
266			return;
267
268		int row = bufferTree.getRowForPath(paths[0]);
269
270		for(int i = 0; i < paths.length; i++)
271		{
272			TreePath path = paths[i];
273
274			// is it a header?
275			if(path.getPathCount() == 2)
276				continue;
277
278			DefaultMutableTreeNode node = (DefaultMutableTreeNode)
279				path.getLastPathComponent();
280			if(!(node.getUserObject() instanceof String))
281			{
282				return;
283			}
284
285			Buffer buffer = jEdit.getBuffer(
286				(String)node.getUserObject());
287			if(buffer == null)
288				return;
289
290			buffer.reload(view);
291			DefaultMutableTreeNode parent =
292				(DefaultMutableTreeNode)
293				node.getParent();
294			parent.remove(node);
295		}
296
297		bufferTreeModel.reload(root);
298
299		// we expand those that are non-empty, and
300		// remove those that are empty
301		TreeNode[] nodes = { root, null };
302
303		// remove empty category branches
304		for(int j = 0; j < root.getChildCount(); j++)
305		{
306			DefaultMutableTreeNode node
307				= (DefaultMutableTreeNode)
308				root.getChildAt(j);
309			if(root.getChildAt(j)
310				.getChildCount() == 0)
311			{
312				root.remove(j);
313				j--;
314			}
315			else
316			{
317				nodes[1] = node;
318				bufferTree.expandPath(
319					new TreePath(nodes));
320			}
321		}
322
323		if(root.getChildCount() == 0)
324			dispose();
325		else
326		{
327			if(row >= bufferTree.getRowCount())
328				row = bufferTree.getRowCount() - 1;
329			TreePath path = bufferTree.getPathForRow(row);
330			if(path.getPathCount() == 2)
331			{
332				// selected a header; skip to the next row
333				bufferTree.setSelectionRow(row + 1);
334			}
335			else
336				bufferTree.setSelectionPath(path);
337		}
338	} //}}}
339
340	//}}}
341
342	//{{{ ActionHandler class
343	class ActionHandler implements ActionListener
344	{
345		public void actionPerformed(ActionEvent evt)
346		{
347			Object source = evt.getSource();
348			if(source == selectAll)
349				selectAll();
350			else if(source == reload)
351				reload();
352			else if(source == close)
353				dispose();
354		}
355	} //}}}
356
357	//{{{ TreeHandler class
358	class TreeHandler implements TreeSelectionListener
359	{
360		public void valueChanged(TreeSelectionEvent evt)
361		{
362			if(selectAllInProgress)
363				return;
364
365			updateEnabled();
366
367			TreePath[] paths = bufferTree
368				.getSelectionPaths();
369			if(paths == null || paths.length == 0)
370				return;
371			TreePath path = paths[paths.length - 1];
372			DefaultMutableTreeNode node = (DefaultMutableTreeNode)
373				path.getLastPathComponent();
374			if(node.getUserObject() instanceof String)
375			{
376				Buffer buffer = jEdit.getBuffer(
377					(String)node.getUserObject());
378				if(buffer != null)
379					view.goToBuffer(buffer);
380			}
381		}
382	} //}}}
383
384	//{{{ Renderer class
385	class Renderer extends DefaultTreeCellRenderer
386	{
387		public Renderer()
388		{
389			entryFont = UIManager.getFont("Tree.font");
390			if(entryFont == null)
391				entryFont = jEdit.getFontProperty("metal.secondary.font");
392			groupFont = entryFont.deriveFont(Font.BOLD);
393		}
394
395		public Component getTreeCellRendererComponent(JTree tree,
396			Object value, boolean selected, boolean expanded,
397			boolean leaf, int row, boolean hasFocus)
398		{
399			super.getTreeCellRendererComponent(tree,value,
400				selected,expanded,leaf,row,hasFocus);
401
402			DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
403
404			if(node.getParent() == tree.getModel().getRoot())
405				this.setFont(groupFont);
406			else
407				this.setFont(entryFont);
408
409			setIcon(null);
410
411			return this;
412		}
413
414		private Font entryFont;
415		private Font groupFont;
416	} //}}}
417}