KickJava   Java API By Example, From Geeks To Geeks.

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


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.openide.explorer.ExplorerManager;
43 import org.openide.explorer.view.TreeTableView;
44 import org.openide.nodes.Node;
45 import org.openide.nodes.PropertySupport;
46 import org.openide.util.NbBundle;
47 import org.openide.windows.TopComponent;
48
49 /**
50  *
51  * @author Tomas Stupka
52  */

53 public class LocalHistoryFolderView implements PropertyChangeListener JavaDoc {
54        
55     private TopComponent tc;
56     private LocalHistoryFolderPanel folderPanel;
57     private FolderTablePanel tablePanel;
58     private File JavaDoc[] files;
59     
60     /** Creates a new instance of LocalHistoryView */
61     public LocalHistoryFolderView(File JavaDoc[] files, TopComponent tc) {
62         this.tc = tc;
63         
64         tablePanel = new FolderTablePanel();
65         tablePanel.getExplorerManager().addPropertyChangeListener(this);
66         folderPanel = new LocalHistoryFolderPanel();
67         folderPanel.tablePanel.add(tablePanel, BorderLayout.CENTER);
68                 
69         LocalHistory.getInstance().getLocalHistoryStore().addPropertyChangeListener(this); // XXX remove listener
70

71         this.files = files;
72         refreshTablePanel(true);
73     }
74         
75     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
76         if(ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) {
77             selectionChanged(evt);
78         } else if(LocalHistoryStore.PROPERTY_CHANGED.equals(evt.getPropertyName())) {
79             storeChanged(evt);
80         }
81     }
82     
83     public ExplorerManager getExplorerManager() {
84         return tablePanel.getExplorerManager();
85     }
86     
87     public JPanel JavaDoc getPanel() {
88         return folderPanel;
89     }
90     
91     public void close() {
92         LocalHistory.getInstance().getLocalHistoryStore().removePropertyChangeListener(this);
93     }
94     
95     private void selectionChanged(PropertyChangeEvent JavaDoc evt) {
96         Node[] newSelection = ((Node[]) evt.getNewValue());
97         if(!acceptSelection(newSelection)) { // XXX get selection
98
return;
99         }
100         tc.setActivatedNodes(newSelection);
101     }
102     
103     private boolean acceptSelection(Node[] newSelection) {
104         return newSelection != null && newSelection.length == 0;
105     }
106     
107     private void storeChanged(PropertyChangeEvent JavaDoc evt) {
108         Object JavaDoc newValue = evt.getNewValue();
109         Object JavaDoc oldValue = evt.getOldValue();
110         if( newValue != null && contains( (File JavaDoc) newValue ) ||
111             oldValue != null && contains( (File JavaDoc) oldValue ) )
112         {
113             refreshTablePanel(false);
114         }
115     }
116
117     private boolean contains(File JavaDoc file) {
118         for(File JavaDoc f : files) {
119             if(f.equals(file)) {
120                 return true;
121             }
122         }
123         return false;
124     }
125         
126     private void refreshTablePanel(final boolean selectFirst) {
127         
128     }
129     
130     private Node[] getEqualNodes(Node root, Node[] oldNodes) {
131         List JavaDoc<Node> ret = new ArrayList JavaDoc<Node>();
132         for(Node on : oldNodes) {
133             Node node = findEqualInChildren(root, on);
134             if(node != null) {
135                 ret.add(node);
136             }
137         }
138         return ret.toArray(new Node[ret.size()]);
139     }
140         
141     private Node findEqualInChildren(Node node, Node toFind) {
142         Node[] children = node.getChildren().getNodes();
143         for(Node child : children) {
144             if(toFind.getName().equals(child.getName())) {
145                 return child;
146             }
147             Node n = findEqualInChildren(child, toFind);
148             if(n != null) {
149                 return n;
150             }
151         }
152         return null;
153     }
154
155     private void selectFirstNode(Node root) {
156         Node[] dateFolders = root.getChildren().getNodes();
157         if (dateFolders != null && dateFolders.length > 0) {
158             final Node[] nodes = dateFolders[0].getChildren().getNodes();
159             if (nodes != null && nodes.length > 0) {
160                 selectNodes(new Node[]{ nodes[0] });
161             }
162         }
163     }
164     
165     private void selectFirstNeighborNode(Node context, Node oldSelection) {
166         tablePanel.getExplorerManager().setExploredContext(context);
167         Node[] children = context.getChildren().getNodes();
168         if(children.length > 0 && children[0] instanceof Comparable JavaDoc) {
169             Node[] newSelection = new Node[] { children[0] } ;
170             for(int i = 1; i < children.length; i++) {
171                 Comparable JavaDoc c = (Comparable JavaDoc) children[i];
172                 if( c.compareTo(oldSelection) < 0 ) {
173                    newSelection[0] = children[i];
174                 }
175             }
176             selectNodes(newSelection);
177         }
178     }
179     
180     private void selectNodes(final Node[] nodes) {
181         SwingUtilities.invokeLater(new Runnable JavaDoc() {
182             public void run() {
183                 try {
184                     tablePanel.getExplorerManager().setSelectedNodes(nodes);
185                 } catch (PropertyVetoException JavaDoc ex) {
186                     // ignore
187
}
188             }
189         });
190     }
191
192     // XXX reuse in folder view
193
private class FolderTablePanel extends JPanel JavaDoc implements ExplorerManager.Provider {
194
195         private final BrowserTreeTableView treeView;
196         private final ExplorerManager manager;
197
198         public FolderTablePanel() {
199             manager = new ExplorerManager();
200
201             setLayout(new GridBagLayout JavaDoc());
202
203             treeView = new BrowserTreeTableView();
204             setLayout(new BorderLayout JavaDoc());
205             add(treeView, BorderLayout.CENTER);
206         }
207
208         public ExplorerManager getExplorerManager() {
209             return manager;
210         }
211
212         private class BrowserTreeTableView extends TreeTableView {
213             BrowserTreeTableView() {
214                 setupColumns();
215
216                 tree.setShowsRootHandles(true);
217                 tree.setRootVisible(false);
218                 setDefaultActionAllowed (false);
219                 setBorder(BorderFactory.createEtchedBorder());
220         // treeView.getAccessibleContext().setAccessibleDescription(browserAcsd);
221
// treeView.getAccessibleContext().setAccessibleName(browserAcsn);
222
setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); // XXX DISCONTIGUOUS_TREE_SELECTION
223
setPopupAllowed(true);
224
225                 DefaultTreeCellRenderer JavaDoc renderer = new DefaultTreeCellRenderer JavaDoc();
226                 renderer.setLeafIcon(null);
227                 tree.setCellRenderer(renderer);
228
229             }
230
231             JTree JavaDoc getTree() {
232                 return tree;
233             }
234
235             JTable JavaDoc getTable() {
236                 return treeTable;
237             }
238
239             public void startEditingAtPath(TreePath JavaDoc path) {
240                 tree.startEditingAtPath(path);
241             }
242
243             public void addNotify() {
244                 super.addNotify();
245                 setDefaultColumnSizes();
246             }
247
248             private void setupColumns() {
249                 ResourceBundle JavaDoc loc = NbBundle.getBundle(FolderTablePanel.class);
250
251                 Node.Property [] columns = new Node.Property[1];
252                 columns = new Node.Property[1];
253                 columns[0] = new ColumnDescriptor<String JavaDoc>(
254                                 StoreEntryNode.PROPERTY_NAME_LABEL,
255                                 String JavaDoc.class,
256                                 loc.getString("LBL_LocalHistory_Column_Label"), // NOI18N
257
loc.getString("LBL_LocalHistory_Column_Version_Desc")); // NOI18N
258
setProperties(columns);
259             }
260
261             private void setDefaultColumnSizes() {
262                 SwingUtilities.invokeLater(new Runnable JavaDoc() {
263                     public void run() {
264                         int width = getWidth();
265                             treeTable.getColumnModel().getColumn(0).setPreferredWidth(width * 35 / 100);
266                             treeTable.getColumnModel().getColumn(1).setPreferredWidth(width * 65 / 100);
267                     }
268                 });
269             }
270         }
271
272         int rowAtPoint(Point JavaDoc point) {
273             return treeView.getTable().rowAtPoint(point);
274         }
275     }
276     
277     private static class ColumnDescriptor<T> extends PropertySupport.ReadOnly<T> {
278         public ColumnDescriptor(String JavaDoc name, Class JavaDoc<T> type, String JavaDoc displayName, String JavaDoc shortDescription) {
279             super(name, type, displayName, shortDescription);
280         }
281         public T getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
282             return null;
283         }
284     }
285 }
286
Popular Tags