KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > localhistory > ui > view > LocalHistoryFileView


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.localhistory.ui.view;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.GridBagLayout JavaDoc;
23 import java.awt.Point JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.beans.PropertyVetoException JavaDoc;
27 import java.io.File JavaDoc;
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.ResourceBundle JavaDoc;
32 import javax.swing.BorderFactory JavaDoc;
33 import javax.swing.JPanel JavaDoc;
34 import javax.swing.JTable JavaDoc;
35 import javax.swing.JTree JavaDoc;
36 import javax.swing.SwingUtilities JavaDoc;
37 import javax.swing.tree.DefaultTreeCellRenderer JavaDoc;
38 import javax.swing.tree.TreePath JavaDoc;
39 import javax.swing.tree.TreeSelectionModel JavaDoc;
40 import org.netbeans.modules.localhistory.LocalHistory;
41 import org.netbeans.modules.localhistory.store.LocalHistoryStore;
42 import org.netbeans.modules.localhistory.store.StoreEntry;
43 import org.openide.explorer.ExplorerManager;
44 import org.openide.explorer.view.TreeTableView;
45 import org.openide.nodes.Node;
46 import org.openide.nodes.PropertySupport;
47 import org.openide.util.NbBundle;
48 import org.openide.util.RequestProcessor;
49
50 /**
51  *
52  * @author Tomas Stupka
53  */

54 public class LocalHistoryFileView implements PropertyChangeListener JavaDoc {
55            
56     private FileTablePanel tablePanel;
57     private File JavaDoc[] files;
58         
59     private RequestProcessor.Task refreshTask;
60     private RefreshTable refreshTable;
61
62     public LocalHistoryFileView(File JavaDoc[] files) {
63         this(files, -1);
64     }
65     
66     public LocalHistoryFileView(File JavaDoc[] files, long toSelect) {
67         tablePanel = new FileTablePanel();
68         LocalHistory.getInstance().getLocalHistoryStore().addPropertyChangeListener(this); // XXX remove listener
69
this.files = files;
70         refreshTablePanel(toSelect);
71     }
72         
73     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
74         if(LocalHistoryStore.PROPERTY_CHANGED.equals(evt.getPropertyName())) {
75             storeChanged(evt);
76         }
77     }
78     
79     public ExplorerManager getExplorerManager() {
80         return tablePanel.getExplorerManager();
81     }
82     
83     public StoreEntry[] getSelectedEntries() {
84         Node[] nodes = tablePanel.getExplorerManager().getSelectedNodes();
85         if(nodes != null && nodes.length > 0) {
86             List JavaDoc<StoreEntry> entries = new ArrayList JavaDoc<StoreEntry>();
87             for(Node node : nodes) {
88                 entries.add(node.getLookup().lookup(StoreEntry.class));
89             }
90             return entries.toArray(new StoreEntry[entries.size()]);
91         }
92         return new StoreEntry[0];
93     }
94     
95     public JPanel JavaDoc getPanel() {
96         return tablePanel;
97     }
98     
99     public void close() {
100         LocalHistory.getInstance().getLocalHistoryStore().removePropertyChangeListener(this);
101     }
102     
103     private Node getNode(long ts) {
104         if(ts == -1) return null;
105         Node root = tablePanel.getExplorerManager().getRootContext();
106         Node[] dayNodes = root.getChildren().getNodes();
107         if(dayNodes != null && dayNodes.length > 0) {
108             for(Node dayNode : dayNodes) {
109                 Node[] entryNodes = dayNode.getChildren().getNodes();
110                 if(entryNodes != null && entryNodes.length > 0) {
111                     for(Node entryNode : entryNodes) {
112                         StoreEntry se = entryNode.getLookup().lookup(StoreEntry.class);
113                         if(se != null && se.getTimestamp() == ts) {
114                             return entryNode;
115                         }
116                     }
117                 }
118             }
119         }
120         return null;
121     }
122     
123     private void storeChanged(PropertyChangeEvent JavaDoc evt) {
124         Object JavaDoc newValue = evt.getNewValue();
125         Object JavaDoc oldValue = evt.getOldValue();
126         if( newValue != null && contains( (File JavaDoc) newValue ) ||
127             oldValue != null && contains( (File JavaDoc) oldValue ) )
128         {
129             refreshTablePanel(-1);
130         }
131     }
132
133     private boolean contains(File JavaDoc file) {
134         for(File JavaDoc f : files) {
135             if(f.equals(file)) {
136                 return true;
137             }
138         }
139         return false;
140     }
141     
142     private void refreshTablePanel(long toSelect) {
143         if(refreshTask == null) {
144             refreshTable = new RefreshTable();
145             RequestProcessor rp = new RequestProcessor();
146             refreshTask = rp.create(refreshTable);
147         }
148         refreshTable.setup(toSelect);
149         refreshTask.schedule(100);
150     }
151     
152     private void selectNodes(final Node[] nodes) {
153         SwingUtilities.invokeLater(new Runnable JavaDoc() {
154             public void run() {
155                 try {
156                     tablePanel.getExplorerManager().setSelectedNodes(nodes);
157                 } catch (PropertyVetoException JavaDoc ex) {
158                     // ignore
159
}
160             }
161         });
162     }
163
164     /**
165      * Selects a node with the timestamp = toSelect, otherwise the selection stays.
166      * If there wasn't a selection set yet then the first node will be selected.
167      */

168     private class RefreshTable implements Runnable JavaDoc {
169         private long toSelect;
170         void setup(long toSelect) {
171             this.toSelect = toSelect;
172         }
173         public void run() {
174             Node oldExploredContext = getExplorerManager().getExploredContext();
175             Node root = LocalHistoryRootNode.createRootNode(files);
176             
177             Node[] oldSelection = getExplorerManager().getSelectedNodes();
178             tablePanel.getExplorerManager().setRootContext(root);
179                         
180             if(toSelect > -1) {
181                 Node node = getNode(toSelect);
182                 if(node != null) {
183                     oldSelection = new Node[] { node };
184                 }
185             }
186             
187             if (oldSelection != null && oldSelection.length > 0) {
188                 Node[] newSelection = getEqualNodes(root, oldSelection);
189                 if(newSelection.length > 0) {
190                     selectNodes(newSelection);
191                 } else {
192                     if(oldExploredContext != null) {
193                         Node[] newExploredContext = getEqualNodes(root, new Node[] { oldExploredContext });
194                         if(newExploredContext.length > 0) {
195                             selectFirstNeighborNode(newExploredContext[0], oldSelection[0]);
196                         }
197                     }
198                 }
199             } else {
200                 selectFirstNode(root);
201             }
202             tablePanel.revalidate();
203             tablePanel.repaint();
204         }
205         
206         private Node[] getEqualNodes(Node root, Node[] oldNodes) {
207             List JavaDoc<Node> ret = new ArrayList JavaDoc<Node>();
208             for(Node on : oldNodes) {
209                 Node node = findEqualInChildren(root, on);
210                 if(node != null) {
211                     ret.add(node);
212                 }
213             }
214             return ret.toArray(new Node[ret.size()]);
215         }
216         
217         private Node findEqualInChildren(Node node, Node toFind) {
218             Node[] children = node.getChildren().getNodes();
219             for(Node child : children) {
220                 if(toFind.getName().equals(child.getName())) {
221                     return child;
222                 }
223                 Node n = findEqualInChildren(child, toFind);
224                 if(n != null) {
225                     return n;
226                 }
227             }
228             return null;
229         }
230
231         private void selectFirstNode(final Node root) {
232             Node[] dateFolders = root.getChildren().getNodes();
233             if (dateFolders != null && dateFolders.length > 0) {
234                 final Node[] nodes = dateFolders[0].getChildren().getNodes();
235                 if (nodes != null && nodes.length > 0) {
236                     selectNodes(new Node[]{ nodes[0] });
237                 }
238             }
239         }
240
241         private void selectFirstNeighborNode(Node context, Node oldSelection) {
242             tablePanel.getExplorerManager().setExploredContext(context);
243             Node[] children = context.getChildren().getNodes();
244             if(children.length > 0 && children[0] instanceof Comparable JavaDoc) {
245                 Node[] newSelection = new Node[] { children[0] } ;
246                 for(int i = 1; i < children.length; i++) {
247                     Comparable JavaDoc c = (Comparable JavaDoc) children[i];
248                     if( c.compareTo(oldSelection) < 0 ) {
249                        newSelection[0] = children[i];
250                     }
251                 }
252                 selectNodes(newSelection);
253             }
254         }
255     }
256     
257     // XXX reuse in folder view
258
private class FileTablePanel extends JPanel JavaDoc implements ExplorerManager.Provider {
259
260         private final BrowserTreeTableView treeView;
261         private final ExplorerManager manager;
262
263         public FileTablePanel() {
264             manager = new ExplorerManager();
265
266             setLayout(new GridBagLayout JavaDoc());
267
268             treeView = new BrowserTreeTableView();
269             setLayout(new BorderLayout JavaDoc());
270             add(treeView, BorderLayout.CENTER);
271         }
272
273         public ExplorerManager getExplorerManager() {
274             return manager;
275         }
276
277         private class BrowserTreeTableView extends TreeTableView {
278             BrowserTreeTableView() {
279                 setupColumns();
280
281                 tree.setShowsRootHandles(true);
282                 tree.setRootVisible(false);
283                 setDefaultActionAllowed (false);
284                 setBorder(BorderFactory.createEtchedBorder());
285         // treeView.getAccessibleContext().setAccessibleDescription(browserAcsd);
286
// treeView.getAccessibleContext().setAccessibleName(browserAcsn);
287
setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
288                 setPopupAllowed(true);
289
290                 DefaultTreeCellRenderer JavaDoc renderer = new DefaultTreeCellRenderer JavaDoc();
291                 renderer.setLeafIcon(null);
292                 tree.setCellRenderer(renderer);
293
294             }
295
296             JTree JavaDoc getTree() {
297                 return tree;
298             }
299
300             JTable JavaDoc getTable() {
301                 return treeTable;
302             }
303
304             public void startEditingAtPath(TreePath JavaDoc path) {
305                 tree.startEditingAtPath(path);
306             }
307
308             public void addNotify() {
309                 super.addNotify();
310                 setDefaultColumnSizes();
311             }
312
313             private void setupColumns() {
314                 ResourceBundle JavaDoc loc = NbBundle.getBundle(FileTablePanel.class);
315
316                 Node.Property [] columns = new Node.Property[1];
317                 columns = new Node.Property[1];
318                 columns[0] = new ColumnDescriptor<String JavaDoc>(
319                                 StoreEntryNode.PROPERTY_NAME_LABEL,
320                                 String JavaDoc.class,
321                                 loc.getString("LBL_LocalHistory_Column_Label"), // NOI18N
322
loc.getString("LBL_LocalHistory_Column_Label_Desc")); // NOI18N
323
setProperties(columns);
324             }
325
326             private void setDefaultColumnSizes() {
327                 SwingUtilities.invokeLater(new Runnable JavaDoc() {
328                     public void run() {
329                         int width = getWidth();
330                             treeTable.getColumnModel().getColumn(0).setPreferredWidth(width * 35 / 100);
331                             treeTable.getColumnModel().getColumn(1).setPreferredWidth(width * 65 / 100);
332                     }
333                 });
334             }
335         }
336
337         int rowAtPoint(Point JavaDoc point) {
338             return treeView.getTable().rowAtPoint(point);
339         }
340     }
341     
342     private static class ColumnDescriptor<T> extends PropertySupport.ReadOnly<T> {
343         public ColumnDescriptor(String JavaDoc name, Class JavaDoc<T> type, String JavaDoc displayName, String JavaDoc shortDescription) {
344             super(name, type, displayName, shortDescription);
345         }
346         public T getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
347             return null;
348         }
349     }
350 }
351
Popular Tags