KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > history > RevisionNode


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.versioning.system.cvss.ui.history;
21
22 import org.openide.*;
23 import org.openide.nodes.*;
24 import org.openide.util.lookup.Lookups;
25 import org.openide.util.NbBundle;
26 import org.netbeans.lib.cvsclient.command.log.LogInformation;
27 import org.netbeans.modules.versioning.system.cvss.ui.actions.log.SearchHistoryAction;
28 import org.netbeans.modules.versioning.system.cvss.ui.actions.update.GetCleanAction;
29 import org.netbeans.modules.versioning.system.cvss.util.Utils;
30 import org.netbeans.modules.versioning.system.cvss.util.Context;
31 import org.netbeans.modules.versioning.system.cvss.VersionsCache;
32 import org.netbeans.api.project.Project;
33 import org.netbeans.api.project.ProjectUtils;
34 import org.netbeans.api.project.ui.OpenProjects;
35
36 import javax.swing.*;
37 import java.lang.reflect.InvocationTargetException JavaDoc;
38 import java.awt.event.ActionEvent JavaDoc;
39 import java.awt.*;
40 import java.io.File JavaDoc;
41 import java.beans.PropertyEditor JavaDoc;
42 import java.beans.PropertyEditorSupport JavaDoc;
43 import java.util.*;
44 import java.util.List JavaDoc;
45 import java.text.DateFormat JavaDoc;
46
47 /**
48  * Visible in the Search History Diff view.
49  *
50  * @author Maros Sandor
51  */

52 class RevisionNode extends AbstractNode {
53     
54     static final String JavaDoc COLUMN_NAME_NAME = "name"; // NOI18N
55
static final String JavaDoc COLUMN_NAME_LOCATION = "location"; // NOI18N
56
static final String JavaDoc COLUMN_NAME_DATE = "date"; // NOI18N
57
static final String JavaDoc COLUMN_NAME_USERNAME = "username"; // NOI18N
58
static final String JavaDoc COLUMN_NAME_TAGS = "tags"; // NOI18N
59
static final String JavaDoc COLUMN_NAME_MESSAGE = "message"; // NOI18N
60

61     private SearchHistoryPanel.DispRevision revision;
62     private SearchHistoryPanel.ResultsContainer container;
63     private String JavaDoc path;
64
65     public RevisionNode(SearchHistoryPanel.ResultsContainer container) {
66         super(new RevisionNodeChildren(container), Lookups.singleton(container));
67         this.container = container;
68         this.revision = null;
69         this.path = container.getPath();
70         setName(((SearchHistoryPanel.DispRevision) container.getRevisions().get(0)).getRevision().getLogInfoHeader().getFile().getName());
71         initProperties();
72     }
73
74     public RevisionNode(SearchHistoryPanel.DispRevision revision) {
75         super(revision.getChildren() == null ? Children.LEAF : new RevisionNodeChildren(revision), Lookups.fixed(revision));
76         this.path = revision.getPath();
77         this.revision = revision;
78         if (revision.getRevision().getNumber() == VersionsCache.REVISION_CURRENT) {
79             setName(NbBundle.getMessage(DiffResultsView.class, "LBL_DiffPanel_LocalCopy")); // NOI18N
80
} else if (revision.isBranchRoot()) {
81             if (revision.getBranchName() != null) {
82                 setName(revision.getRevision().getNumber() + " - " + revision.getBranchName()); // NOI18N
83
} else {
84                 setName(revision.getRevision().getNumber());
85             }
86         } else {
87             setName(revision.getRevision().getNumber());
88         }
89         initProperties();
90     }
91
92     SearchHistoryPanel.DispRevision getDispRevision() {
93         return revision;
94     }
95
96     LogInformation.Revision getRevision() {
97         return revision.getRevision();
98     }
99
100     SearchHistoryPanel.ResultsContainer getContainer() {
101         return container;
102     }
103
104     public String JavaDoc getShortDescription() {
105         return path;
106     }
107
108     public Action[] getActions(boolean context) {
109         if (context) return null;
110         // TODO: reuse action code from SummaryView
111
if (revision == null || revision.getRevision().getNumber() == VersionsCache.REVISION_CURRENT) {
112             return new Action [0];
113         } else {
114             return new Action [] {
115                 new RollbackAction(),
116                 new RollbackChangeAction(),
117                 new FindCommitAction(false),
118                 new FindCommitAction(true),
119             };
120         }
121     }
122     
123     private void initProperties() {
124         Sheet sheet = Sheet.createDefault();
125         Sheet.Set ps = Sheet.createPropertiesSet();
126         
127         ps.put(new LocationProperty());
128         ps.put(new DateProperty());
129         ps.put(new UsernameProperty());
130         ps.put(new TagsProperty());
131         ps.put(new MessageProperty());
132         
133         sheet.put(ps);
134         setSheet(sheet);
135     }
136
137     private abstract class CommitNodeProperty extends PropertySupport.ReadOnly {
138
139         protected CommitNodeProperty(String JavaDoc name, Class JavaDoc type, String JavaDoc displayName, String JavaDoc shortDescription) {
140             super(name, type, displayName, shortDescription);
141         }
142
143         public String JavaDoc toString() {
144             try {
145                 return getValue().toString();
146             } catch (Exception JavaDoc e) {
147                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
148                 return e.getLocalizedMessage();
149             }
150         }
151
152         public PropertyEditor JavaDoc getPropertyEditor() {
153             try {
154                 return new RevisionPropertyEditor((String JavaDoc) getValue());
155             } catch (Exception JavaDoc e) {
156                 return super.getPropertyEditor();
157             }
158         }
159     }
160     
161     private class LocationProperty extends CommitNodeProperty {
162
163         public LocationProperty() {
164             super(COLUMN_NAME_LOCATION, String JavaDoc.class, COLUMN_NAME_LOCATION, COLUMN_NAME_LOCATION);
165         }
166
167         public Object JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
168             if (container != null) {
169                 return path;
170             } else {
171                 return ""; // NOI18N
172
}
173         }
174     }
175     
176     private class UsernameProperty extends CommitNodeProperty {
177
178         public UsernameProperty() {
179             super(COLUMN_NAME_USERNAME, String JavaDoc.class, COLUMN_NAME_USERNAME, COLUMN_NAME_USERNAME);
180         }
181
182         public Object JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
183             if (revision != null) {
184                 return revision.getRevision().getAuthor();
185             } else {
186                 return ""; // NOI18N
187
}
188         }
189     }
190
191     private class DateProperty extends CommitNodeProperty {
192         
193         private String JavaDoc dateString;
194
195         public DateProperty() {
196             super(COLUMN_NAME_DATE, String JavaDoc.class, COLUMN_NAME_DATE, COLUMN_NAME_DATE);
197             dateString = (revision == null || revision.getRevision().getDate() == null) ? "" : DateFormat.getDateTimeInstance().format(revision.getRevision().getDate()); // NOI18N
198
}
199
200         public Object JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
201             return dateString;
202         }
203     }
204
205     private class TagsProperty extends CommitNodeProperty {
206
207         public TagsProperty() {
208             super(COLUMN_NAME_TAGS, List JavaDoc.class, COLUMN_NAME_TAGS, COLUMN_NAME_TAGS);
209             if (revision != null) setValue("dispRevision", revision); // NOI18N
210
}
211
212         public Object JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
213             return null; // nobody reads this, the custom editor handles painting, see below
214
}
215
216         public PropertyEditor JavaDoc getPropertyEditor() {
217             try {
218                 return new TagsPropertyEditor(revision);
219             } catch (Exception JavaDoc e) {
220                 return super.getPropertyEditor();
221             }
222         }
223     }
224     
225     private class MessageProperty extends CommitNodeProperty {
226
227         public MessageProperty() {
228             super(COLUMN_NAME_MESSAGE, String JavaDoc.class, COLUMN_NAME_MESSAGE, COLUMN_NAME_MESSAGE);
229         }
230
231         public Object JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
232             if (revision != null) {
233                 return revision.getRevision().getMessage();
234             } else {
235                 return ""; // NOI18N
236
}
237         }
238     }
239
240     private class FindCommitAction extends AbstractAction {
241
242         private boolean allProjects;
243
244         public FindCommitAction(boolean allProjects) {
245             this.allProjects = allProjects;
246             if (allProjects) {
247                 putValue(Action.NAME, NbBundle.getMessage(RevisionNode.class, "CTL_Action_FindCommitInProjects")); // NOI18N
248
} else {
249                 File JavaDoc file = revision.getRevision().getLogInfoHeader().getFile();
250                 Project project = Utils.getProject(file);
251                 if (project != null) {
252                     String JavaDoc prjName = ProjectUtils.getInformation(project).getDisplayName();
253                     putValue(Action.NAME, NbBundle.getMessage(RevisionNode.class, "CTL_Action_FindCommitInProject", prjName)); // NOI18N
254
} else {
255                     putValue(Action.NAME, NbBundle.getMessage(RevisionNode.class, "CTL_Action_FindCommit")); // NOI18N
256
setEnabled(false);
257                 }
258             }
259         }
260
261         public void actionPerformed(ActionEvent JavaDoc e) {
262             File JavaDoc file = revision.getRevision().getLogInfoHeader().getFile();
263             if (allProjects) {
264                 Project [] projects = OpenProjects.getDefault().getOpenProjects();
265                 int n = projects.length;
266                 SearchHistoryAction.openSearch(
267                         (n == 1) ? ProjectUtils.getInformation(projects[0]).getDisplayName() :
268                         NbBundle.getMessage(SummaryView.class, "CTL_FindAssociateChanges_OpenProjects_Title", Integer.toString(n)), // NOI18N
269
revision.getRevision().getMessage().trim(), revision.getRevision().getAuthor(), revision.getRevision().getDate());
270             } else {
271                 Project project = Utils.getProject(file);
272                 Context context = Utils.getProjectsContext(new Project[] { project });
273                 SearchHistoryAction.openSearch(
274                         context,
275                         ProjectUtils.getInformation(project).getDisplayName(),
276                         revision.getRevision().getMessage().trim(), revision.getRevision().getAuthor(), revision.getRevision().getDate());
277             }
278         }
279     }
280
281     private class RollbackAction extends AbstractAction {
282
283         public RollbackAction() {
284             putValue(Action.NAME, NbBundle.getMessage(RevisionNode.class, "CTL_Action_RollbackTo", revision.getRevision().getNumber())); // NOI18N
285
}
286
287         public void actionPerformed(ActionEvent JavaDoc e) {
288             File JavaDoc file = revision.getRevision().getLogInfoHeader().getFile();
289             GetCleanAction.rollback(file, revision.getRevision().getNumber());
290         }
291     }
292
293     private class RollbackChangeAction extends AbstractAction {
294
295         public RollbackChangeAction() {
296             putValue(Action.NAME, NbBundle.getMessage(RevisionNode.class, "CTL_Action_RollbackChange")); // NOI18N
297
setEnabled(Utils.previousRevision(revision.getRevision().getNumber()) != null);
298         }
299
300         public void actionPerformed(ActionEvent JavaDoc e) {
301             SummaryView.rollbackChanges(new LogInformation.Revision [] { revision.getRevision() });
302         }
303     }
304     
305     private static class RevisionPropertyEditor extends PropertyEditorSupport JavaDoc {
306
307         private static final JLabel renderer = new JLabel();
308
309         static {
310             renderer.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2));
311         }
312
313         public RevisionPropertyEditor(String JavaDoc value) {
314             setValue(value);
315         }
316
317         public void paintValue(Graphics gfx, Rectangle box) {
318             renderer.setForeground(gfx.getColor());
319             renderer.setText((String JavaDoc) getValue());
320             renderer.setBounds(box);
321             renderer.paint(gfx);
322         }
323
324         public boolean isPaintable() {
325             return true;
326         }
327     }
328     
329     private static class TagsPropertyEditor extends PropertyEditorSupport JavaDoc {
330
331         private static final JLabel renderer = new JLabel();
332         
333         private SearchHistoryPanel.DispRevision dispRevision;
334
335         static {
336             renderer.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2));
337         }
338         
339         public TagsPropertyEditor(SearchHistoryPanel.DispRevision revision) {
340             this.dispRevision = revision;
341         }
342
343         public boolean isPaintable() {
344             return true;
345         }
346
347         public void paintValue(Graphics gfx, Rectangle box) {
348             renderer.setForeground(gfx.getColor());
349             renderer.setBounds(box);
350
351             if (dispRevision == null || dispRevision.getBranches() == null) {
352                 renderer.setText(""); // NOI18N
353
} else {
354                 List JavaDoc<String JavaDoc> tags = new ArrayList<String JavaDoc>(dispRevision.getBranches());
355                 tags.addAll(dispRevision.getTags());
356                 if (tags.size() > 0) {
357                     String JavaDoc tagInfo = "<html>" + tags.get(0); // NOI18N
358
if (tags.size() > 1) {
359                         tagInfo += ","; // NOI18N
360
Color foreground = UIManager.getColor("List.selectionForeground"); // NOI18N
361
if (!gfx.getColor().equals(foreground)) {
362                             tagInfo += " <a HREF=\"\">...</a>"; // NOI18N
363
} else {
364                             StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
365                             sb.append(" <a HREF=\"\" style=\"color:"); // NOI18N
366
sb.append("rgb("); // NOI18N
367
sb.append(foreground.getRed());
368                             sb.append(","); // NOI18N
369
sb.append(foreground.getGreen());
370                             sb.append(","); // NOI18N
371
sb.append(foreground.getBlue());
372                             sb.append(")"); // NOI18N
373
sb.append("\">"); // NOI18N
374
sb.append("..."); // NOI18N
375
sb.append("</a>"); // NOI18N
376
tagInfo += sb.toString();
377                         }
378                     }
379                     renderer.setText(tagInfo);
380                 } else {
381                     renderer.setText(""); // NOI18N
382
}
383             }
384             renderer.paint(gfx);
385         }
386     }
387 }
388
Popular Tags