KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > 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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.subversion.ui.history;
20
21 import org.openide.nodes.*;
22 import org.openide.util.lookup.Lookups;
23 import org.openide.util.NbBundle;
24 import org.openide.util.HelpCtx;
25 import org.openide.util.actions.SystemAction;
26 import org.openide.util.actions.NodeAction;
27 import org.openide.ErrorManager;
28
29 import javax.swing.*;
30 import java.beans.PropertyEditor JavaDoc;
31 import java.beans.PropertyEditorSupport JavaDoc;
32 import java.lang.reflect.InvocationTargetException JavaDoc;
33 import java.awt.event.ActionEvent JavaDoc;
34 import java.awt.Graphics JavaDoc;
35 import java.awt.Rectangle JavaDoc;
36 import java.text.DateFormat JavaDoc;
37 import java.util.*;
38
39 /**
40  * Visible in the Search History Diff view.
41  *
42  * @author Maros Sandor
43  */

44 class RevisionNode extends AbstractNode {
45     
46     static final String JavaDoc COLUMN_NAME_NAME = "name"; // NOI18N
47
static final String JavaDoc COLUMN_NAME_DATE = "date"; // NOI18N
48
static final String JavaDoc COLUMN_NAME_USERNAME = "username"; // NOI18N
49
static final String JavaDoc COLUMN_NAME_MESSAGE = "message"; // NOI18N
50

51     private RepositoryRevision.Event event;
52     private RepositoryRevision container;
53     private String JavaDoc path;
54
55     public RevisionNode(RepositoryRevision container, SearchHistoryPanel master) {
56         super(new RevisionNodeChildren(container, master), Lookups.fixed(master, container));
57         this.container = container;
58         this.event = null;
59         this.path = null;
60         setName(container.getLog().getRevision().getNumber() +
61                 NbBundle.getMessage(RevisionNode.class, "LBL_NumberOfChangedPaths", container.getLog().getChangedPaths().length));
62         initProperties();
63     }
64
65     public RevisionNode(RepositoryRevision.Event revision, SearchHistoryPanel master) {
66         super(Children.LEAF, Lookups.fixed(master, revision));
67         this.path = revision.getChangedPath().getPath();
68         this.event = revision;
69         setName(revision.getName());
70         initProperties();
71     }
72
73     RepositoryRevision.Event getRevision() {
74         return event;
75     }
76
77     RepositoryRevision getContainer() {
78         return container;
79     }
80
81     RepositoryRevision.Event getEvent() {
82         return event;
83     }
84
85     public String JavaDoc getShortDescription() {
86         return path;
87     }
88
89     public Action[] getActions(boolean context) {
90         if (context) return null;
91         // TODO: reuse action code from SummaryView
92
if (event == null) {
93             return new Action [] {
94                 SystemAction.get(RevertModificationsAction.class)
95             };
96         } else {
97             return new Action [] {
98                 new RollbackAction(),
99                 SystemAction.get(RevertModificationsAction.class)
100             };
101         }
102     }
103     
104     private void initProperties() {
105         Sheet sheet = Sheet.createDefault();
106         Sheet.Set ps = Sheet.createPropertiesSet();
107         
108         ps.put(new DateProperty());
109         ps.put(new UsernameProperty());
110         ps.put(new MessageProperty());
111         
112         sheet.put(ps);
113         setSheet(sheet);
114     }
115
116     private abstract class CommitNodeProperty<T> extends PropertySupport.ReadOnly<T> {
117
118         protected CommitNodeProperty(String JavaDoc name, Class JavaDoc<T> type, String JavaDoc displayName, String JavaDoc shortDescription) {
119             super(name, type, displayName, shortDescription);
120         }
121
122         public String JavaDoc toString() {
123             try {
124                 return getValue().toString();
125             } catch (Exception JavaDoc e) {
126                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
127                 return e.getLocalizedMessage();
128             }
129         }
130
131         public PropertyEditor JavaDoc getPropertyEditor() {
132             try {
133                 return new RevisionPropertyEditor((String JavaDoc) getValue());
134             } catch (Exception JavaDoc e) {
135                 return super.getPropertyEditor();
136             }
137         }
138     }
139     
140     private class UsernameProperty extends CommitNodeProperty {
141
142         public UsernameProperty() {
143             super(COLUMN_NAME_USERNAME, String JavaDoc.class, COLUMN_NAME_USERNAME, COLUMN_NAME_USERNAME);
144         }
145
146         public Object JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
147             if (event == null) {
148                 return container.getLog().getAuthor();
149             } else {
150                 return ""; // NOI18N
151
}
152         }
153     }
154
155     private class DateProperty extends CommitNodeProperty {
156
157         public DateProperty() {
158             super(COLUMN_NAME_DATE, String JavaDoc.class, COLUMN_NAME_DATE, COLUMN_NAME_DATE);
159         }
160
161         public Object JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
162             if (event == null) {
163                 return DateFormat.getDateTimeInstance().format(container.getLog().getDate());
164             } else {
165                 return ""; // NOI18N
166
}
167         }
168     }
169
170     private class MessageProperty extends CommitNodeProperty {
171
172         public MessageProperty() {
173             super(COLUMN_NAME_MESSAGE, String JavaDoc.class, COLUMN_NAME_MESSAGE, COLUMN_NAME_MESSAGE);
174         }
175
176         public Object JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
177             if (event == null) {
178                 return container.getLog().getMessage();
179             } else {
180                 return ""; // NOI18N
181
}
182         }
183     }
184
185     private class RollbackAction extends AbstractAction {
186
187         public RollbackAction() {
188             putValue(Action.NAME, NbBundle.getMessage(RevisionNode.class, "CTL_Action_RollbackTo", // NOI18N
189
event.getLogInfoHeader().getLog().getRevision().getNumber()));
190         }
191
192         public void actionPerformed(ActionEvent JavaDoc e) {
193             SummaryView.rollback(event);
194         }
195     }
196
197     private static class RevertModificationsAction extends NodeAction {
198
199         protected void performAction(Node[] activatedNodes) {
200             Set<RepositoryRevision.Event> events = new HashSet<RepositoryRevision.Event>();
201             Set<RepositoryRevision> revisions = new HashSet<RepositoryRevision>();
202             for (Node n : activatedNodes) {
203                 RevisionNode node = (RevisionNode) n;
204                 if (node.event != null) {
205                     events.add(node.event);
206                 } else {
207                     revisions.add(node.container);
208                 }
209             }
210             SearchHistoryPanel master = (SearchHistoryPanel) activatedNodes[0].getLookup().lookup(SearchHistoryPanel.class);
211             SummaryView.revert(master, revisions.toArray(new RepositoryRevision[revisions.size()]), events.toArray(new RepositoryRevision.Event[events.size()]));
212         }
213
214         protected boolean enable(Node[] activatedNodes) {
215             return true;
216         }
217
218         public String JavaDoc getName() {
219             return NbBundle.getMessage(RevisionNode.class, "CTL_Action_RollbackChange"); // NOI18N
220
}
221
222         public HelpCtx getHelpCtx() {
223             return new HelpCtx(RevertModificationsAction.class);
224         }
225     }
226     
227     private static class RevisionPropertyEditor extends PropertyEditorSupport JavaDoc {
228
229         private static final JLabel renderer = new JLabel();
230
231         static {
232             renderer.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2));
233         }
234
235         public RevisionPropertyEditor(String JavaDoc value) {
236             setValue(value);
237         }
238
239         public void paintValue(Graphics JavaDoc gfx, Rectangle JavaDoc box) {
240             renderer.setForeground(gfx.getColor());
241             renderer.setText((String JavaDoc) getValue());
242             renderer.setBounds(box);
243             renderer.paint(gfx);
244         }
245
246         public boolean isPaintable() {
247             return true;
248         }
249     }
250 }
251
Popular Tags