KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > actions > update > UpdateResultNode


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 package org.netbeans.modules.versioning.system.cvss.ui.actions.update;
20
21 import org.openide.nodes.*;
22 import org.openide.util.NbBundle;
23 import org.openide.filesystems.FileObject;
24 import org.openide.filesystems.FileUtil;
25 import org.openide.loaders.DataObject;
26 import org.openide.loaders.DataObjectNotFoundException;
27 import org.openide.ErrorManager;
28 import org.netbeans.modules.versioning.system.cvss.util.Utils;
29 import org.netbeans.lib.cvsclient.command.DefaultFileInfoContainer;
30
31 import java.lang.reflect.InvocationTargetException JavaDoc;
32 import java.text.MessageFormat JavaDoc;
33
34 /**
35  * The node that is rendered in the Update Results view.
36  *
37  * @author Maros Sandor
38  */

39 class UpdateResultNode extends AbstractNode {
40     
41     private DefaultFileInfoContainer info;
42
43     static final String JavaDoc COLUMN_NAME_NAME = "name"; // NOI18N
44
static final String JavaDoc COLUMN_NAME_PATH = "path"; // NOI18N
45
static final String JavaDoc COLUMN_NAME_STATUS = "status"; // NOI18N
46

47     private final MessageFormat JavaDoc conflictFormat = new MessageFormat JavaDoc("<font color=\"#FF0000\">{0}</font>"); // NOI18N
48
private final MessageFormat JavaDoc updatedFormat = new MessageFormat JavaDoc("<font color=\"#0000FF\">{0}</font>"); // NOI18N
49

50     private String JavaDoc statusDisplayName;
51     private String JavaDoc htmlDisplayName;
52     
53     public UpdateResultNode(DefaultFileInfoContainer info) {
54         super(Children.LEAF);
55         this.info = info;
56         initProperties();
57         refreshHtmlDisplayName();
58     }
59
60     public DefaultFileInfoContainer getInfo() {
61         return info;
62     }
63
64     public String JavaDoc getName() {
65         return info.getFile().getName();
66     }
67
68     /**
69      * Provide cookies to actions.
70      * If a node represents primary file of a DataObject
71      * it has respective DataObject cookies.
72      */

73     public <T extends Node.Cookie> T getCookie(Class JavaDoc<T> klass) {
74         FileObject fo = FileUtil.toFileObject(info.getFile());
75         if (fo != null) {
76             try {
77                 DataObject dobj = DataObject.find(fo);
78                 if (fo.equals(dobj.getPrimaryFile())) {
79                     return dobj.getCookie(klass);
80                 }
81             } catch (DataObjectNotFoundException e) {
82                 // ignore file without data objects
83
}
84         }
85         return super.getCookie(klass);
86     }
87
88     private void initProperties() {
89         Sheet sheet = Sheet.createDefault();
90         Sheet.Set ps = Sheet.createPropertiesSet();
91         
92         ps.put(new UpdateResultNode.NameProperty());
93         ps.put(new UpdateResultNode.PathProperty());
94         ps.put(new UpdateResultNode.StatusProperty());
95         
96         sheet.put(ps);
97         setSheet(sheet);
98     }
99     
100     private void refreshHtmlDisplayName() {
101         if (isConflict()) {
102             htmlDisplayName = conflictFormat.format(new Object JavaDoc [] { getName() });
103             statusDisplayName = NbBundle.getMessage(UpdateResultNode.class, "LBL_Status_Conflict"); // NOI18N
104
} else if (isRemoved()) {
105             htmlDisplayName = getName();
106             statusDisplayName = NbBundle.getMessage(UpdateResultNode.class, "LBL_Status_Removed"); // NOI18N
107
} else if (isUpdated()) {
108             htmlDisplayName = updatedFormat.format(new Object JavaDoc [] { getName() });
109             statusDisplayName = NbBundle.getMessage(UpdateResultNode.class, "LBL_Status_Updated"); // NOI18N
110
} else {
111             throw new IllegalStateException JavaDoc("Unhandled update type: " + info.getType()); // NOI18N
112
}
113         fireDisplayNameChange(htmlDisplayName, htmlDisplayName);
114     }
115
116     public String JavaDoc getHtmlDisplayName() {
117         return htmlDisplayName;
118     }
119
120     private boolean isUpdated() {
121         return "UPG".indexOf(info.getType()) != -1; // NOI18N
122
}
123
124     private boolean isRemoved() {
125         return DefaultFileInfoContainer.PERTINENT_STATE.equals(info.getType());
126     }
127
128     private boolean isConflict() {
129         return "C".equals(info.getType()); // NOI18N
130
}
131
132     public void refresh() {
133         refreshHtmlDisplayName();
134     }
135
136     private abstract class SyncFileProperty extends PropertySupport.ReadOnly<String JavaDoc> {
137
138         protected SyncFileProperty(String JavaDoc name, Class JavaDoc<String JavaDoc> type, String JavaDoc displayName, String JavaDoc shortDescription) {
139             super(name, type, displayName, shortDescription);
140         }
141
142         public String JavaDoc toString() {
143             try {
144                 return getValue();
145             } catch (Exception JavaDoc e) {
146                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
147                 return e.getLocalizedMessage();
148             }
149         }
150     }
151     
152     private class PathProperty extends UpdateResultNode.SyncFileProperty {
153
154         private String JavaDoc shortPath;
155
156         public PathProperty() {
157             super(COLUMN_NAME_PATH, String JavaDoc.class, NbBundle.getMessage(UpdateResultNode.class, "LBL_Path_Name"), NbBundle.getMessage(UpdateResultNode.class, "LBL_Path_Desc")); // NOI18N
158
shortPath = Utils.getRelativePath(info.getFile());
159             setValue("sortkey", shortPath + "\t" + UpdateResultNode.this.getName()); // NOI18N
160
}
161
162         public String JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
163             return shortPath;
164         }
165     }
166     
167     private class NameProperty extends UpdateResultNode.SyncFileProperty {
168
169         public NameProperty() {
170             super(COLUMN_NAME_NAME, String JavaDoc.class, NbBundle.getMessage(UpdateResultNode.class, "LBL_Name_Name"), NbBundle.getMessage(UpdateResultNode.class, "LBL_Name_Desc")); // NOI18N
171
setValue("sortkey", UpdateResultNode.this.getName()); // NOI18N
172
}
173
174         public String JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
175             return UpdateResultNode.this.getDisplayName();
176         }
177     }
178
179     private final String JavaDoc [] zeros = new String JavaDoc [] { "", "00", "0", "" }; // NOI18N
180

181     private class StatusProperty extends UpdateResultNode.SyncFileProperty {
182         
183         public StatusProperty() {
184             super(COLUMN_NAME_STATUS, String JavaDoc.class, NbBundle.getMessage(UpdateResultNode.class, "LBL_Status_Name"), NbBundle.getMessage(UpdateResultNode.class, "LBL_Status_Desc")); // NOI18N
185
String JavaDoc shortPath = Utils.getRelativePath(info.getFile());
186             String JavaDoc sortable = info.getType();
187             setValue("sortkey", zeros[sortable.length()] + sortable + "\t" + shortPath + "\t" + UpdateResultNode.this.getName()); // NOI18N
188
}
189
190         public String JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
191             return statusDisplayName;
192         }
193     }
194 }
195
Popular Tags