KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > gui > FilesChangedDialog


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
23 package org.gjt.sp.jedit.gui;
24
25 //{{{ Imports
26
import javax.swing.border.*;
27 import javax.swing.event.*;
28 import javax.swing.tree.*;
29 import javax.swing.*;
30 import java.awt.event.*;
31 import java.awt.*;
32 import org.gjt.sp.jedit.*;
33 //}}}
34

35 /**
36  * Files changed on disk dialog.
37  *
38  * @author Slava Pestov
39  * @version $Id: FilesChangedDialog.java 8190 2006-12-07 07:58:34Z kpouer $
40  */

41 public class FilesChangedDialog extends EnhancedDialog
42 {
43     //{{{ FilesChangedDialog constructor
44
public FilesChangedDialog(View view, int[] states,
45         boolean alreadyReloaded)
46     {
47         super(view,jEdit.getProperty("files-changed.title"),false);
48
49         this.view = view;
50
51         JPanel content = new JPanel(new BorderLayout(12,12));
52         content.setBorder(new EmptyBorder(12,12,12,12));
53         setContentPane(content);
54
55         Box iconBox = new Box(BoxLayout.Y_AXIS);
56         iconBox.add(new JLabel(UIManager.getIcon("OptionPane.warningIcon")));
57         iconBox.add(Box.createGlue());
58         content.add(BorderLayout.WEST,iconBox);
59
60         JPanel centerPanel = new JPanel(new BorderLayout());
61
62         JLabel label = new JLabel(jEdit.getProperty("files-changed.caption"));
63         label.setBorder(new EmptyBorder(0,0,6,0));
64         centerPanel.add(BorderLayout.NORTH,label);
65
66         DefaultMutableTreeNode deleted = new DefaultMutableTreeNode(
67             jEdit.getProperty("files-changed.deleted"),true);
68         DefaultMutableTreeNode changed = new DefaultMutableTreeNode(
69             jEdit.getProperty("files-changed.changed"
70             + (alreadyReloaded ? "-auto" : "")),true);
71         DefaultMutableTreeNode changedDirty = new DefaultMutableTreeNode(
72             jEdit.getProperty("files-changed.changed-dirty"
73             + (alreadyReloaded ? "-auto" : "")),true);
74         Buffer[] buffers = jEdit.getBuffers();
75         for(int i = 0; i < states.length; i++)
76         {
77             Buffer buffer = buffers[i];
78             DefaultMutableTreeNode addTo;
79             switch(states[i])
80             {
81             case Buffer.FILE_DELETED:
82                 addTo = deleted;
83                 break;
84             case Buffer.FILE_CHANGED:
85                 addTo = buffer.isDirty() ? changedDirty : changed;
86                 break;
87             default:
88                 addTo = null;
89                 break;
90             }
91
92             if(addTo != null)
93             {
94                 addTo.add(new DefaultMutableTreeNode(
95                     buffer.getPath()));
96             }
97         }
98
99         root = new DefaultMutableTreeNode("",true);
100         if(deleted.getChildCount() != 0)
101         {
102             root.add(deleted);
103         }
104         if(changed.getChildCount() != 0)
105         {
106             root.add(changed);
107         }
108         if(changedDirty.getChildCount() != 0)
109         {
110             root.add(changedDirty);
111         }
112
113         bufferTreeModel = new DefaultTreeModel(root);
114         bufferTree = new JTree(bufferTreeModel);
115         bufferTree.setRootVisible(false);
116         bufferTree.setVisibleRowCount(10);
117         bufferTree.setCellRenderer(new Renderer());
118         bufferTree.getSelectionModel().addTreeSelectionListener(new TreeHandler());
119         bufferTree.getSelectionModel().setSelectionMode(
120             TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
121
122         centerPanel.add(BorderLayout.CENTER,new JScrollPane(bufferTree));
123
124         content.add(BorderLayout.CENTER,centerPanel);
125
126         Box buttons = new Box(BoxLayout.X_AXIS);
127         buttons.add(Box.createGlue());
128
129         if(!alreadyReloaded)
130         {
131             selectAll = new JButton(jEdit.getProperty(
132                 "files-changed.select-all"));
133             selectAll.setMnemonic(jEdit.getProperty(
134                 "files-changed.select-all.mnemonic").charAt(0));
135             buttons.add(selectAll);
136             selectAll.addActionListener(new ActionHandler());
137
138             buttons.add(Box.createHorizontalStrut(12));
139
140             reload = new JButton(jEdit.getProperty(
141                 "files-changed.reload"));
142             reload.setMnemonic(jEdit.getProperty(
143                 "files-changed.reload.mnemonic").charAt(0));
144             buttons.add(reload);
145             reload.addActionListener(new ActionHandler());
146
147             buttons.add(Box.createHorizontalStrut(12));
148
149             ignore = new JButton(jEdit.getProperty("files-changed.ignore"));
150             ignore.setMnemonic(jEdit.getProperty(
151                 "files-changed.ignore.mnemonic").charAt(0));
152             buttons.add(ignore);
153             ignore.addActionListener(new ActionHandler());
154
155             buttons.add(Box.createHorizontalStrut(12));
156         }
157
158         close = new JButton(jEdit.getProperty("common.close"));
159         getRootPane().setDefaultButton(close);
160         buttons.add(close);
161         close.addActionListener(new ActionHandler());
162
163         buttons.add(Box.createGlue());
164
165         content.add(BorderLayout.SOUTH,buttons);
166
167         bufferTree.expandPath(new TreePath(
168             new Object JavaDoc[] {
169                 root,
170                 deleted
171             }));
172         bufferTree.expandPath(new TreePath(
173             new Object JavaDoc[] {
174                 root,
175                 changed
176             }));
177         bufferTree.expandPath(new TreePath(
178             new Object JavaDoc[] {
179                 root,
180                 changedDirty
181             }));
182
183         GUIUtilities.requestFocus(this,bufferTree);
184
185         updateEnabled();
186
187         pack();
188         setLocationRelativeTo(view);
189         setVisible(true);
190     } //}}}
191

192     //{{{ ok() method
193
public void ok()
194     {
195         dispose();
196     } //}}}
197

198     //{{{ cancel() method
199
public void cancel()
200     {
201         dispose();
202     } //}}}
203

204     //{{{ Private members
205
private View view;
206     private JTree bufferTree;
207     private DefaultTreeModel bufferTreeModel;
208     private DefaultMutableTreeNode root;
209     private JButton selectAll;
210
211     // hack so that 'select all' does not change current buffer
212
private boolean selectAllInProgress;
213
214     private JButton reload;
215     private JButton ignore;
216     private JButton close;
217
218     //{{{ updateEnabled() method
219
private void updateEnabled()
220     {
221         TreePath[] paths = bufferTree
222             .getSelectionPaths();
223         boolean enabled = false;
224         if(paths != null)
225         {
226             for(int i = 0; i < paths.length; i++)
227             {
228                 Object JavaDoc[] path = paths[i].getPath();
229                 if(path.length == 3)
230                     enabled = true;
231             }
232         }
233
234         if(reload != null)
235             reload.setEnabled(enabled);
236
237         if (ignore != null)
238             ignore.setEnabled(enabled);
239     } //}}}
240

241     //{{{ selectAll() method
242
private void selectAll()
243     {
244         selectAllInProgress = true;
245
246         TreeNode[] path = new TreeNode[3];
247         path[0] = root;
248         for(int i = 0; i < root.getChildCount(); i++)
249         {
250             DefaultMutableTreeNode node =
251                 (DefaultMutableTreeNode)
252                 root.getChildAt(i);
253             path[1] = node;
254             for(int j = 0; j < node.getChildCount(); j++)
255             {
256                 DefaultMutableTreeNode node2 =
257                     (DefaultMutableTreeNode)
258                     node.getChildAt(j);
259                 path[2] = node2;
260                 bufferTree.getSelectionModel()
261                     .addSelectionPath(
262                     new TreePath(path));
263             }
264         }
265
266         selectAllInProgress = false;
267
268         updateEnabled();
269     } //}}}
270

271     //{{{ reload() method
272
private void action(String JavaDoc action)
273     {
274         TreePath[] paths = bufferTree
275             .getSelectionPaths();
276         if(paths == null || paths.length == 0)
277             return;
278
279         int row = bufferTree.getRowForPath(paths[0]);
280
281         for(int i = 0; i < paths.length; i++)
282         {
283             TreePath path = paths[i];
284
285             // is it a header?
286
if(path.getPathCount() == 2)
287                 continue;
288
289             DefaultMutableTreeNode node = (DefaultMutableTreeNode)
290                 path.getLastPathComponent();
291             if(!(node.getUserObject() instanceof String JavaDoc))
292             {
293                 return;
294             }
295
296             Buffer buffer = jEdit.getBuffer(
297                 (String JavaDoc)node.getUserObject());
298             if(buffer == null)
299                 return;
300
301             if ("RELOAD".equals(action))
302                 buffer.reload(view);
303             else
304             {
305                 buffer.setAutoReload(false);
306                 buffer.setAutoReloadDialog(false);
307             }
308
309             DefaultMutableTreeNode parent =
310                 (DefaultMutableTreeNode)
311                 node.getParent();
312             parent.remove(node);
313         }
314
315         bufferTreeModel.reload(root);
316
317         // we expand those that are non-empty, and
318
// remove those that are empty
319
TreeNode[] nodes = { root, null };
320
321         // remove empty category branches
322
for(int j = 0; j < root.getChildCount(); j++)
323         {
324             DefaultMutableTreeNode node
325                 = (DefaultMutableTreeNode)
326                 root.getChildAt(j);
327             if(root.getChildAt(j)
328                 .getChildCount() == 0)
329             {
330                 root.remove(j);
331                 j--;
332             }
333             else
334             {
335                 nodes[1] = node;
336                 bufferTree.expandPath(
337                     new TreePath(nodes));
338             }
339         }
340
341         if(root.getChildCount() == 0)
342             dispose();
343         else
344         {
345             if(row >= bufferTree.getRowCount())
346                 row = bufferTree.getRowCount() - 1;
347             TreePath path = bufferTree.getPathForRow(row);
348             if(path.getPathCount() == 2)
349             {
350                 // selected a header; skip to the next row
351
bufferTree.setSelectionRow(row + 1);
352             }
353             else
354                 bufferTree.setSelectionPath(path);
355         }
356     } //}}}
357

358     //}}}
359

360     //{{{ ActionHandler class
361
class ActionHandler implements ActionListener
362     {
363         public void actionPerformed(ActionEvent evt)
364         {
365             Object JavaDoc source = evt.getSource();
366             if(source == selectAll)
367                 selectAll();
368             else if(source == reload)
369                 action("RELOAD");
370             else if(source == close)
371                 dispose();
372             else if (source == ignore)
373                 action("IGNORE");
374         }
375     } //}}}
376

377     //{{{ TreeHandler class
378
class TreeHandler implements TreeSelectionListener
379     {
380         public void valueChanged(TreeSelectionEvent evt)
381         {
382             if(selectAllInProgress)
383                 return;
384
385             updateEnabled();
386
387             TreePath[] paths = bufferTree
388                 .getSelectionPaths();
389             if(paths == null || paths.length == 0)
390                 return;
391             TreePath path = paths[paths.length - 1];
392             DefaultMutableTreeNode node = (DefaultMutableTreeNode)
393                 path.getLastPathComponent();
394             if(node.getUserObject() instanceof String JavaDoc)
395             {
396                 Buffer buffer = jEdit.getBuffer(
397                     (String JavaDoc)node.getUserObject());
398                 if(buffer != null)
399                     view.goToBuffer(buffer);
400             }
401         }
402     } //}}}
403

404     //{{{ Renderer class
405
static class Renderer extends DefaultTreeCellRenderer
406     {
407         Renderer()
408         {
409             entryFont = UIManager.getFont("Tree.font");
410             if(entryFont == null)
411                 entryFont = jEdit.getFontProperty("metal.secondary.font");
412             groupFont = entryFont.deriveFont(Font.BOLD);
413         }
414
415         public Component getTreeCellRendererComponent(JTree tree,
416             Object JavaDoc value, boolean selected, boolean expanded,
417             boolean leaf, int row, boolean hasFocus)
418         {
419             super.getTreeCellRendererComponent(tree,value,
420                 selected,expanded,leaf,row,hasFocus);
421
422             DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
423
424             if(node.getParent() == tree.getModel().getRoot())
425                 setFont(groupFont);
426             else
427                 setFont(entryFont);
428
429             setIcon(null);
430
431             return this;
432         }
433
434         private Font entryFont;
435         private Font groupFont;
436     } //}}}
437
}
438
Popular Tags