KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.PropertyEditor JavaDoc;
22 import java.beans.PropertyEditorSupport JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import javax.swing.Action JavaDoc;
25 import org.openide.nodes.AbstractNode;
26 import org.openide.nodes.Children;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import java.text.DateFormat JavaDoc;
29 import java.util.List JavaDoc;
30 import org.netbeans.modules.localhistory.LocalHistory;
31 import org.netbeans.modules.localhistory.store.StoreEntry;
32 import org.openide.nodes.PropertySupport;
33 import org.openide.nodes.Sheet;
34 import org.openide.util.Lookup;
35 import org.openide.util.NbBundle;
36 import org.openide.util.actions.SystemAction;
37 import org.openide.util.lookup.Lookups;
38
39 /**
40  *
41  * @author Tomas Stupka
42  *
43  */

44 public class StoreEntryNode extends AbstractNode implements Comparable JavaDoc {
45     
46     static final String JavaDoc PROPERTY_NAME_LABEL = "label"; // NOI18N
47
private List JavaDoc<StoreEntry> entries;
48     private static DateFormat JavaDoc defaultFormat = DateFormat.getDateTimeInstance();
49
50     private StoreEntryNode(List JavaDoc<StoreEntry> childrenEntries) {
51         super(createChildren(childrenEntries));
52         this.entries = childrenEntries;
53         initProperties();
54     }
55
56     private StoreEntryNode(List JavaDoc<StoreEntry> childrenEntries, Lookup l) {
57         super(Children.LEAF, l);
58         this.entries = childrenEntries;
59         initProperties();
60     }
61
62     static StoreEntryNode create(List JavaDoc<StoreEntry> childrenEntries) {
63         
64         assert childrenEntries != null && childrenEntries.size() > 0;
65         
66         if(childrenEntries.size() > 1) {
67             return new StoreEntryNode(childrenEntries);
68         } else {
69             return new StoreEntryNode(childrenEntries, Lookups.fixed(new Object JavaDoc [] { childrenEntries.get(0) }));
70         }
71     }
72     
73     private static Children createChildren(List JavaDoc<StoreEntry> childrenEntries) {
74         FileNode[] nodes = new FileNode[childrenEntries.size()];
75         int i = 0;
76         for (StoreEntry se : childrenEntries) {
77             nodes[i++] = new FileNode(se);
78         }
79         Children.SortedArray children = new Children.SortedArray();
80         children.add(nodes);
81         return children;
82     }
83         
84     private void initProperties() {
85         Sheet sheet = Sheet.createDefault();
86         Sheet.Set ps = Sheet.createPropertiesSet();
87                 
88         ps.put(new LabelProperty());
89         
90         sheet.put(ps);
91         setSheet(sheet);
92     }
93     
94     public String JavaDoc getDisplayName() {
95         return getName();
96     }
97
98     public String JavaDoc getName() {
99         return getFormatedDate(entries.get(0));
100     }
101        
102     static String JavaDoc getFormatedDate(StoreEntry se) {
103         return defaultFormat.format(se.getDate());
104     }
105     
106     public Action JavaDoc[] getActions(boolean context) {
107         return new Action JavaDoc[] {
108             SystemAction.get(RevertFileAction.class),
109             SystemAction.get(DeleteAction.class)
110         };
111     }
112
113     public int compareTo(Object JavaDoc obj) {
114         if( !(obj instanceof StoreEntryNode) || obj == null) {
115             return 1;
116         }
117         StoreEntryNode node = (StoreEntryNode) obj;
118
119         if(node.entries.get(0).getTimestamp() > entries.get(0).getTimestamp()) {
120             return 1;
121         } else if(node.entries.get(0).getTimestamp() < entries.get(0).getTimestamp()) {
122             return -1;
123         } else {
124             return 0;
125         }
126     }
127                           
128     private class LabelProperty extends PropertySupport.ReadWrite<String JavaDoc> {
129         public LabelProperty() {
130             super(PROPERTY_NAME_LABEL, String JavaDoc.class, NbBundle.getMessage(StoreEntryNode.class, "LBL_LabelProperty_Name"), NbBundle.getMessage(StoreEntryNode.class, "LBL_LabelProperty_Desc"));
131         }
132         public String JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
133             return entries.get(0).getLabel();
134         }
135         public void setValue(String JavaDoc value) throws IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc
136         {
137             List JavaDoc<StoreEntry> newEntries = new ArrayList JavaDoc<StoreEntry>(entries.size());
138             for(StoreEntry se : entries) {
139                 LocalHistory.getInstance().getLocalHistoryStore().setLabel(se.getFile(), se.getTimestamp(), value);
140                 newEntries.add(StoreEntry.createStoreEntry(se.getFile(), se.getStoreFile(), se.getTimestamp(), value));
141             }
142             entries = newEntries;
143         }
144         public PropertyEditor JavaDoc getPropertyEditor() {
145             return new PropertyEditorSupport JavaDoc();
146         }
147     }
148     
149     private static class FileNode extends AbstractNode implements Comparable JavaDoc {
150
151         private final StoreEntry entry;
152         
153         FileNode(StoreEntry entry) {
154             super(Children.LEAF, Lookups.fixed(new Object JavaDoc [] { entry }));
155             this.entry = entry;
156         }
157     
158         public Action JavaDoc[] getActions(boolean context) {
159             List JavaDoc<StoreEntry> entries = new ArrayList JavaDoc<StoreEntry>(1);
160             entries.add(entry);
161             return new Action JavaDoc[] {
162                 SystemAction.get(RevertFileAction.class),
163                 SystemAction.get(DeleteAction.class)
164             };
165         }
166
167         public String JavaDoc getName() {
168             return entry.getFile().getName();
169         }
170         
171         public int compareTo(Object JavaDoc obj) {
172             if( !(obj instanceof FileNode) || obj == null) {
173                 return -1;
174             }
175             FileNode node = (FileNode) obj;
176             return getName().compareTo(node.getName());
177         }
178     }
179     
180 }
181
182
Popular Tags