001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.subversion.ui.update;
042:
043: import org.openide.nodes.*;
044: import org.openide.util.NbBundle;
045: import org.openide.filesystems.FileObject;
046: import org.openide.filesystems.FileUtil;
047: import org.openide.loaders.DataObject;
048: import org.openide.loaders.DataObjectNotFoundException;
049:
050: import java.lang.reflect.InvocationTargetException;
051: import java.text.MessageFormat;
052: import java.util.logging.Level;
053: import org.netbeans.modules.subversion.Subversion;
054: import org.netbeans.modules.subversion.client.SvnClientExceptionHandler;
055: import org.netbeans.modules.subversion.util.SvnUtils;
056: import org.openide.util.lookup.Lookups;
057: import org.tigris.subversion.svnclientadapter.SVNClientException;
058:
059: /**
060: * The node that is rendered in the Update Results view.
061: *
062: * @author Maros Sandor
063: */
064: class UpdateResultNode extends AbstractNode {
065:
066: private FileUpdateInfo info;
067:
068: static final String COLUMN_NAME_NAME = "name"; // NOI18N
069: static final String COLUMN_NAME_PATH = "path"; // NOI18N
070: static final String COLUMN_NAME_STATUS = "status"; // NOI18N
071:
072: private final MessageFormat conflictFormat = new MessageFormat(
073: "<font color=\"#FF0000\">{0}</font>"); // NOI18N
074: private final MessageFormat mergedFormat = new MessageFormat(
075: "<font color=\"#0000FF\">{0}</font>"); // NOI18N
076: private final MessageFormat removedFormat = new MessageFormat(
077: "<font color=\"#999999\">{0}</font>"); // NOI18N
078: private final MessageFormat addedFormat = new MessageFormat(
079: "<font color=\"#008000\">{0}</font>"); // NOI18N
080:
081: private String statusDisplayName;
082:
083: private String htmlDisplayName;
084:
085: public UpdateResultNode(FileUpdateInfo info) {
086: super (Children.LEAF, Lookups.fixed(new Object[] { info }));
087: this .info = info;
088: initProperties();
089: refreshHtmlDisplayName();
090: }
091:
092: public FileUpdateInfo getInfo() {
093: return info;
094: }
095:
096: public String getName() {
097: String name = info.getFile().getName()
098: + ((info.getAction() & FileUpdateInfo.ACTION_TYPE_PROPERTY) != 0 ? " - Property"
099: : "");
100: return name;
101: }
102:
103: /**
104: * Provide cookies to actions.
105: * If a node represents primary file of a DataObject
106: * it has respective DataObject cookies.
107: */
108: public <T extends Node.Cookie> T getCookie(Class<T> klass) {
109: FileObject fo = FileUtil.toFileObject(info.getFile());
110: if (fo != null) {
111: try {
112: DataObject dobj = DataObject.find(fo);
113: if (fo.equals(dobj.getPrimaryFile())) {
114: return dobj.getCookie(klass);
115: }
116: } catch (DataObjectNotFoundException e) {
117: // ignore file without data objects
118: }
119: }
120: return super .getCookie(klass);
121: }
122:
123: private void initProperties() {
124: Sheet sheet = Sheet.createDefault();
125: Sheet.Set ps = Sheet.createPropertiesSet();
126:
127: ps.put(new UpdateResultNode.NameProperty());
128: ps.put(new UpdateResultNode.PathProperty());
129: ps.put(new UpdateResultNode.FileStatusProperty());
130:
131: sheet.put(ps);
132: setSheet(sheet);
133: }
134:
135: private void refreshHtmlDisplayName() {
136: String name = getName();
137: if ((info.getAction() & FileUpdateInfo.ACTION_ADDED) != 0) {
138: htmlDisplayName = addedFormat.format(new Object[] { name });
139: statusDisplayName = NbBundle.getMessage(
140: UpdateResultNode.class, "LBL_Status_Added"); // NOI18N
141: } else if ((info.getAction() & FileUpdateInfo.ACTION_CONFLICTED) != 0) {
142: htmlDisplayName = conflictFormat
143: .format(new Object[] { name });
144: statusDisplayName = NbBundle.getMessage(
145: UpdateResultNode.class, "LBL_Status_Conflict"); // NOI18N
146: } else if ((info.getAction() & FileUpdateInfo.ACTION_DELETED) != 0) {
147: htmlDisplayName = removedFormat
148: .format(new Object[] { name });
149: statusDisplayName = NbBundle.getMessage(
150: UpdateResultNode.class, "LBL_Status_Removed"); // NOI18N
151: } else if ((info.getAction() & FileUpdateInfo.ACTION_MERGED) != 0) {
152: htmlDisplayName = mergedFormat
153: .format(new Object[] { name });
154: statusDisplayName = NbBundle.getMessage(
155: UpdateResultNode.class, "LBL_Status_Merged"); // NOI18N
156: } else if ((info.getAction() & FileUpdateInfo.ACTION_UPDATED) != 0) {
157: htmlDisplayName = name;
158: statusDisplayName = NbBundle.getMessage(
159: UpdateResultNode.class, "LBL_Status_Updated"); // NOI18N
160: } else if ((info.getAction() & FileUpdateInfo.ACTION_CONFLICTED_RESOLVED) != 0) {
161: htmlDisplayName = name;
162: statusDisplayName = NbBundle.getMessage(
163: UpdateResultNode.class,
164: "LBL_Status_Conflict_Resolved"); // NOI18N
165: } else {
166: throw new IllegalStateException("Unhandled update type: "
167: + info.getAction()); // NOI18N
168: }
169: fireDisplayNameChange(htmlDisplayName, htmlDisplayName);
170: }
171:
172: public String getHtmlDisplayName() {
173: return htmlDisplayName;
174: }
175:
176: public void refresh() {
177: refreshHtmlDisplayName();
178: }
179:
180: private abstract class SyncFileProperty extends
181: PropertySupport.ReadOnly<String> {
182: protected SyncFileProperty(String name, Class<String> type,
183: String displayName, String shortDescription) {
184: super (name, type, displayName, shortDescription);
185: }
186:
187: public String toString() {
188: try {
189: return getValue();
190: } catch (Exception e) {
191: Subversion.LOG.log(Level.INFO, null, e);
192: return e.getLocalizedMessage();
193: }
194: }
195: }
196:
197: private class PathProperty extends
198: UpdateResultNode.SyncFileProperty {
199: private String shortPath;
200:
201: public PathProperty() {
202: super (COLUMN_NAME_PATH, String.class, NbBundle.getMessage(
203: UpdateResultNode.class, "LBL_Path_Name"),
204: NbBundle.getMessage(UpdateResultNode.class,
205: "LBL_Path_Desc")); // NOI18N
206: try {
207: shortPath = SvnUtils.getRelativePath(info.getFile());
208: } catch (SVNClientException ex) {
209: SvnClientExceptionHandler.notifyException(ex, false,
210: false);
211: shortPath = "";
212: }
213: setValue("sortkey", shortPath + "\t"
214: + UpdateResultNode.this .getName()); // NOI18N
215: }
216:
217: public String getValue() throws IllegalAccessException,
218: InvocationTargetException {
219: return shortPath;
220: }
221: }
222:
223: private class NameProperty extends
224: UpdateResultNode.SyncFileProperty {
225: public NameProperty() {
226: super (COLUMN_NAME_NAME, String.class, NbBundle.getMessage(
227: UpdateResultNode.class, "LBL_Name_Name"),
228: NbBundle.getMessage(UpdateResultNode.class,
229: "LBL_Name_Desc")); // NOI18N
230: setValue("sortkey", UpdateResultNode.this .getName()); // NOI18N
231: }
232:
233: public String getValue() throws IllegalAccessException,
234: InvocationTargetException {
235: return UpdateResultNode.this .getDisplayName();
236: }
237: }
238:
239: private final String[] zeros = new String[] { "", "00", "0", "" }; // NOI18N
240:
241: private class FileStatusProperty extends
242: UpdateResultNode.SyncFileProperty {
243: private String shortPath;
244:
245: public FileStatusProperty() {
246: super (COLUMN_NAME_STATUS, String.class, NbBundle
247: .getMessage(UpdateResultNode.class,
248: "LBL_Status_Name"), NbBundle.getMessage(
249: UpdateResultNode.class, "LBL_Status_Desc"));
250: try {
251: shortPath = SvnUtils.getRelativePath(info.getFile());
252: } catch (SVNClientException ex) {
253: SvnClientExceptionHandler.notifyException(ex, false,
254: false);
255: shortPath = "";
256: }
257: String sortable = Integer.toString(info.getAction());
258: setValue("sortkey", zeros[sortable.length()] + sortable
259: + "\t" + shortPath + "\t"
260: + UpdateResultNode.this .getName());
261: }
262:
263: public String getValue() throws IllegalAccessException,
264: InvocationTargetException {
265: return statusDisplayName;
266: }
267: }
268: }
|