KickJava   Java API By Example, From Geeks To Geeks.

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


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.Image JavaDoc;
22 import java.beans.BeanInfo JavaDoc;
23 import java.io.File JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Calendar JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import javax.swing.Action JavaDoc;
29 import org.openide.nodes.AbstractNode;
30 import org.openide.nodes.Children;
31 import java.util.HashMap JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import javax.swing.UIManager JavaDoc;
35 import org.netbeans.modules.localhistory.LocalHistory;
36 import org.netbeans.modules.localhistory.store.StoreEntry;
37 import org.openide.nodes.Node;
38 import org.openide.util.NbBundle;
39
40 /**
41  *
42  * The toplevel Node in the LocalHistoryView
43  *
44  * <ul>
45  * <li>
46  * In case the LocalHistoryView was invoked for a 1 file Node
47  *
48  * LocalHistoryViewRootNode ( uvisible root node )
49  * |
50  * +-------- DateFolderNode ( Today )
51  * |
52  * +---------- StoreEntryNode ( 29.01.2007 02:15:07 PM )
53  * +---------- StoreEntryNode ( 29.01.2007 02:14:59 PM )
54  *
55  * +-------- DateFolderNode ( One day ago )
56  * |
57  * +---------- StoreEntryNode ( 28.01.2007 07:50:59 PM )
58  * +---------- StoreEntryNode ( 28.01.2007 03:11:50 PM )
59  * +---------- StoreEntryNode ( 28.01.2007 01:15:04 PM )
60  *
61  * </li>
62  *
63  * <li>
64  * In case the LocalHistoryView was invoked for a multifile Node
65  *
66  * LocalHistoryViewRootNode ( uvisible root node )
67  * |
68  * +-------- DateFolderNode ( Today )
69  * |
70  * +---------- StoreEntryNode ( 29.01.2007 02:15:07 PM )
71  * |
72  * +--------- FileNode ( MyPanel.java )
73  * +--------- FileNode ( MyPanel.form )
74  *
75  * +---------- StoreEntryNode ( 29.01.2007 01:11:23 PM )
76  * |
77  * +--------- FileNode ( MyPanel.java )
78  * +--------- FileNode ( MyPanel.form )
79  *
80  * +-------- One day ago ( DateFolderNode )
81  * |
82  * +-------- Two days ago ( DateFolderNode )
83  *
84  * </li>
85  * </ul>
86  *
87  *
88  * @author Tomas Stupka
89  *
90  */

91 public class LocalHistoryRootNode extends AbstractNode {
92     
93     static final Action JavaDoc[] NO_ACTION = new Action JavaDoc[0];
94     
95     private LocalHistoryRootNode(Children children) {
96         super(children);
97     }
98     
99     /**
100      *
101      * Creates the LocalHistoryViewRootNode with the whole node hierarchy
102      *
103      * @param files files represented by the node on which the LocalHistoryView was invoked - e.g. Main.java, or MyForm.form, MyForm.java, ...
104      * @return a node to be applyied as a roo tnode in the LocalHistoryView
105      */

106     static Node createRootNode(File JavaDoc[] files) {
107         Children.SortedArray children = new Children.SortedArray();
108         children.add(createDateFolders(files));
109         return new LocalHistoryRootNode(children);
110     }
111     
112     /**
113      *
114      * Creates the DateFolderNodes
115      *
116      * @param files files represented by the node on which the LocalHistoryView was invoked - e.g. Main.java, or MyForm.form, MyForm.java, ...
117      * @return an array of nodes segmenting all entries for the invoked output in separate folders - one for each day
118      */

119     private static DateFolderNode[] createDateFolders(File JavaDoc[] files) {
120         
121         // get all StoreEntries for all files and keep them in entriesMap, where
122
// for each timestamp retrieved from the storage there is an array of StoreEntries
123
Map JavaDoc<Long JavaDoc, List JavaDoc<StoreEntry>> entriesMap = new HashMap JavaDoc<Long JavaDoc, List JavaDoc<StoreEntry>>();
124         for (File JavaDoc f : files) {
125             StoreEntry[] ses = LocalHistory.getInstance().getLocalHistoryStore().getStoreEntries(f);
126             for(StoreEntry se : ses) {
127                 List JavaDoc<StoreEntry> storeEntries = entriesMap.get(se.getTimestamp());
128                 if(storeEntries == null) {
129                     storeEntries = new ArrayList JavaDoc<StoreEntry>();
130                     entriesMap.put(se.getTimestamp(), storeEntries);
131                 }
132                 storeEntries.add(se);
133             }
134         }
135                     
136         Map JavaDoc<Integer JavaDoc, List JavaDoc<Node>> storeEntryNodesToDays = new HashMap JavaDoc<Integer JavaDoc, List JavaDoc<Node>>();
137         
138         // segment the StoreEntries in day groups
139
for (Long JavaDoc ts : entriesMap.keySet()) {
140             int day = getDay(ts);
141             List JavaDoc<Node> nodesFromADay = storeEntryNodesToDays.get(day);
142             if(nodesFromADay == null) {
143                 nodesFromADay = new ArrayList JavaDoc<Node>();
144                  storeEntryNodesToDays.put(day, nodesFromADay);
145             }
146             List JavaDoc<StoreEntry> storeEntries = entriesMap.get(ts);
147             nodesFromADay.add(createStoreEntryNode(storeEntries, files));
148         }
149
150         // get a DateFolderNode for each day and the associated group of StoreEntryNode-s
151
List JavaDoc<DateFolderNode> dateFolderNodes = new ArrayList JavaDoc<DateFolderNode>( storeEntryNodesToDays.keySet().size());
152         for (Iterator JavaDoc<Integer JavaDoc> it = storeEntryNodesToDays.keySet().iterator(); it.hasNext();) {
153             int key = it.next();
154             List JavaDoc<Node> l = storeEntryNodesToDays.get(key);
155             Children.SortedArray children = new Children.SortedArray();
156             children.add(l.toArray(new Node[l.size()]));
157             
158             dateFolderNodes.add(new DateFolderNode(key, children));
159         }
160
161         return dateFolderNodes.toArray(new DateFolderNode[dateFolderNodes.size()]);
162     }
163     
164     private static int getDay(long ts) {
165         Date JavaDoc date = new Date JavaDoc(ts);
166                 
167         Calendar JavaDoc c = Calendar.getInstance();
168         c.setTime(new Date JavaDoc());
169         
170         // set the cal at today midnight
171
int todayMillis = c.get(Calendar.HOUR_OF_DAY) * 60 * 60 * 1000 +
172                           c.get(Calendar.MINUTE) * 60 * 1000 +
173                           c.get(Calendar.SECOND) * 1000 +
174                           c.get(Calendar.MILLISECOND);
175         c.add(Calendar.MILLISECOND, -1 * todayMillis);
176         
177         if(c.getTime().compareTo(date) < 0) {
178             return 0;
179         }
180         
181         return (int) ( (c.getTimeInMillis() - ts) / (24 * 60 * 60 * 1000) ) + 1;
182                 
183     }
184     
185     /**
186      *
187      * Creates a StoreEntryNode for a list of files, where the files are related to one DataObject - e.g. MyForm.java, MyForm.form
188      *
189      */

190     private static Node createStoreEntryNode(List JavaDoc<StoreEntry> entries, File JavaDoc[] files) {
191         if(files.length == 1) {
192             
193             // it's only 1 file, so we also already have the 1 StoreEntry
194
return StoreEntryNode.create(entries);
195             
196         } else {
197             // it's a multifile node ...
198

199             // the timestamp must be the same for all StoreEntries
200
long ts = entries.get(0).getTimestamp();
201             
202             // get the entries for every file -
203
// if there is no entry in the Storage then create a structural (fake) one
204
List JavaDoc<StoreEntry> entriesList = new ArrayList JavaDoc<StoreEntry>();
205             for(File JavaDoc f : files) {
206                 boolean fileInEntries = false;
207                 // check if we already have an entry for the file
208
for(StoreEntry se : entries) {
209                     if(f.equals(se.getFile())) {
210                         entriesList.add(se);
211                         fileInEntries = true;
212                         break;
213                     }
214                 }
215                 if(fileInEntries) {
216                     // continue if we already have an entry for the file
217
continue;
218                 }
219                                 
220                 // if there was no entry for the the file then try to get it ...
221
StoreEntry e = LocalHistory.getInstance().getLocalHistoryStore().getStoreEntry(f, ts);
222                 if(e != null) {
223                     
224                     // XXX we probably don't have to do this anymore - see in createDateFolders( ... )
225

226                     // ... either by retrieving them from the storage
227
entriesList.add(e);
228                 } else {
229                     // ... or by creating a structural (fake) one
230
entriesList.add(StoreEntry.createFakeStoreEntry(f, ts));
231                 }
232             }
233             return StoreEntryNode.create(entriesList);
234         }
235     }
236
237     public String JavaDoc getName() {
238         return "rootnode"; // NOI18N
239
}
240     
241     public String JavaDoc getDisplayName() {
242         return NbBundle.getMessage(LocalHistoryRootNode.class, "LBL_LocalHistory_Column_Version"); // NOI18N
243
}
244         
245     public Action JavaDoc[] getActions(boolean context) {
246         return NO_ACTION;
247     }
248         
249     static class DateFolderNode extends AbstractNode implements Comparable JavaDoc {
250         private final int day;
251
252         DateFolderNode(int day, Children children) {
253             super(children);
254             this.day = day;
255         }
256
257         int getDay() {
258             return day;
259         }
260
261         public Image JavaDoc getIcon(int type) {
262             Image JavaDoc img = null;
263             if (type == BeanInfo.ICON_COLOR_16x16) {
264                 img = (Image JavaDoc) UIManager.get("Nb.Explorer.Folder.icon"); // NOI18N
265
}
266             if (img == null) {
267                 img = super.getIcon(type);
268             }
269             return img;
270         }
271
272         public Image JavaDoc getOpenedIcon(int type) {
273             Image JavaDoc img = null;
274             if (type == BeanInfo.ICON_COLOR_16x16) {
275                 img = (Image JavaDoc) UIManager.get("Nb.Explorer.Folder.openedIcon"); // NOI18N
276
}
277             if (img == null) {
278                 img = super.getIcon(type);
279             }
280             return img;
281         }
282
283         public Action JavaDoc[] getActions(boolean context) {
284             return NO_ACTION;
285         }
286
287         public String JavaDoc getName() {
288             switch (day) {
289                 case 0: return NbBundle.getMessage(LocalHistoryRootNode.class, "DateFolderName_0");
290                 case 1: return NbBundle.getMessage(LocalHistoryRootNode.class, "DateFolderName_1");
291                 case 2: return NbBundle.getMessage(LocalHistoryRootNode.class, "DateFolderName_2");
292                 case 3: return NbBundle.getMessage(LocalHistoryRootNode.class, "DateFolderName_3");
293                 case 4: return NbBundle.getMessage(LocalHistoryRootNode.class, "DateFolderName_4");
294                 case 5: return NbBundle.getMessage(LocalHistoryRootNode.class, "DateFolderName_5");
295                 case 6: return NbBundle.getMessage(LocalHistoryRootNode.class, "DateFolderName_6");
296             }
297             return NbBundle.getMessage(LocalHistoryRootNode.class, "DateFolderName_other");
298         }
299
300         public int compareTo(Object JavaDoc obj) {
301             if( !(obj instanceof DateFolderNode) || obj == null) {
302                 return -1;
303             }
304             DateFolderNode lhNode = (DateFolderNode) obj;
305
306             if(lhNode.getDay() > getDay()) {
307                 return -1;
308             } else if(lhNode.getDay() < getDay()) {
309                 return 1;
310             } else {
311                 return 0;
312             }
313         }
314     }
315 }
316
317
Popular Tags