KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > syncview > SyncFileNode


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
20 package org.netbeans.modules.versioning.system.cvss.ui.syncview;
21
22 import org.openide.*;
23 import org.openide.nodes.*;
24 import org.openide.util.*;
25 import org.openide.util.lookup.Lookups;
26 import org.openide.util.actions.SystemAction;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.FileUtil;
29 import org.openide.loaders.DataObject;
30 import org.openide.loaders.DataObjectNotFoundException;
31 import org.netbeans.modules.versioning.system.cvss.*;
32 import org.netbeans.modules.versioning.system.cvss.util.Utils;
33 import org.netbeans.modules.versioning.system.cvss.ui.actions.diff.DiffAction;
34 import org.netbeans.modules.versioning.system.cvss.ui.actions.diff.ResolveConflictsAction;
35
36 import javax.swing.*;
37 import java.lang.reflect.InvocationTargetException JavaDoc;
38 import java.io.File JavaDoc;
39
40 /**
41  * The node that is rendered in the SyncTable view. It gets values to display from the
42  * CvsFileNode which serves as the 'data' node for this 'visual' node.
43  *
44  * @author Maros Sandor
45  */

46 public class SyncFileNode extends AbstractNode {
47     
48     private CvsFileNode node;
49
50     static final String JavaDoc COLUMN_NAME_NAME = "name"; // NOI18N
51
static final String JavaDoc COLUMN_NAME_PATH = "path"; // NOI18N
52
static final String JavaDoc COLUMN_NAME_STATUS = "status"; // NOI18N
53
static final String JavaDoc COLUMN_NAME_STICKY = "sticky"; // NOI18N
54

55     private String JavaDoc htmlDisplayName;
56     private String JavaDoc sticky;
57
58     public SyncFileNode(CvsFileNode node) {
59         this(Children.LEAF, node);
60     }
61
62     private SyncFileNode(Children children, CvsFileNode node) {
63         super(children, Lookups.fixed(node.getLookupObjects()));
64         this.node = node;
65         initProperties();
66         refreshHtmlDisplayName();
67     }
68     
69     public File JavaDoc getFile() {
70         return node.getFile();
71     }
72
73     public FileInformation getFileInformation() {
74         return node.getInformation();
75     }
76     
77     public String JavaDoc getName() {
78         return node.getName();
79     }
80
81     public Action getPreferredAction() {
82         if (node.getInformation().getStatus() == FileInformation.STATUS_VERSIONED_CONFLICT) {
83             return SystemAction.get(ResolveConflictsAction.class);
84         }
85         return SystemAction.get(DiffAction.class);
86     }
87
88     /**
89      * Provide cookies to actions.
90      * If a node represents primary file of a DataObject
91      * it has respective DataObject cookies.
92      */

93     public Cookie getCookie(Class JavaDoc klass) {
94         FileObject fo = FileUtil.toFileObject(getFile());
95         if (fo != null) {
96             try {
97                 DataObject dobj = DataObject.find(fo);
98                 if (fo.equals(dobj.getPrimaryFile())) {
99                     return dobj.getCookie(klass);
100                 }
101             } catch (DataObjectNotFoundException e) {
102                 // ignore file without data objects
103
}
104         }
105         return super.getCookie(klass);
106     }
107
108     private void initProperties() {
109         if (node.getFile().isDirectory()) setIconBaseWithExtension("org/openide/loaders/defaultFolder.gif"); // NOI18N
110

111         Sheet sheet = Sheet.createDefault();
112         Sheet.Set ps = Sheet.createPropertiesSet();
113         
114         ps.put(new NameProperty());
115         ps.put(new PathProperty());
116         ps.put(new StatusProperty());
117         ps.put(new StickyProperty());
118         
119         sheet.put(ps);
120         setSheet(sheet);
121     }
122
123     private void refreshHtmlDisplayName() {
124         FileInformation info = node.getInformation();
125         int status = info.getStatus();
126         // Special treatment: Mergeable status should be annotated as Conflict in Versioning view according to UI spec
127
if (status == FileInformation.STATUS_VERSIONED_MERGE) {
128             status = FileInformation.STATUS_VERSIONED_CONFLICT;
129         }
130         htmlDisplayName = CvsVersioningSystem.getInstance().getAnnotator().annotateNameHtml(node.getFile().getName(), info, null);
131         fireDisplayNameChange(node.getName(), node.getName());
132     }
133
134     public String JavaDoc getHtmlDisplayName() {
135         return htmlDisplayName;
136     }
137
138     public void refresh() {
139         refreshHtmlDisplayName();
140     }
141
142     /**
143      * Gets sticky information for the given file. Sticky info does not include any leading specifier (T, D).
144      *
145      * @return String branch,tag,date sticky info or an empty String, never returns null
146      */

147     public String JavaDoc getSticky() {
148         if (sticky == null) {
149             if ((sticky = Utils.getSticky(node.getFile())) == null) {
150                 sticky = ""; // NOI18N
151
} else {
152                 sticky = sticky.substring(1);
153             }
154         }
155         return sticky == null || sticky.length() == 0 ? "" : sticky; // NOI18N
156
}
157
158     private abstract class SyncFileProperty extends PropertySupport.ReadOnly {
159
160         protected SyncFileProperty(String JavaDoc name, Class JavaDoc type, String JavaDoc displayName, String JavaDoc shortDescription) {
161             super(name, type, displayName, shortDescription);
162         }
163
164         public String JavaDoc toString() {
165             try {
166                 return getValue().toString();
167             } catch (Exception JavaDoc e) {
168                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
169                 return e.getLocalizedMessage();
170             }
171         }
172     }
173     
174     private class StickyProperty extends SyncFileProperty {
175
176         public StickyProperty() {
177             super(COLUMN_NAME_STICKY, String JavaDoc.class, NbBundle.getMessage(SyncFileNode.class, "BK2001"), NbBundle.getMessage(SyncFileNode.class, "BK2002"));
178         }
179
180         public Object JavaDoc getValue() {
181             return getSticky();
182         }
183     }
184     
185     private class PathProperty extends SyncFileProperty {
186
187         private String JavaDoc shortPath;
188
189         public PathProperty() {
190             super(COLUMN_NAME_PATH, String JavaDoc.class, NbBundle.getMessage(SyncFileNode.class, "BK2003"), NbBundle.getMessage(SyncFileNode.class, "BK2004"));
191             shortPath = Utils.getRelativePath(node.getFile());
192             setValue("sortkey", shortPath + "\t" + SyncFileNode.this.getName()); // NOI18N
193
}
194
195         public Object JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
196             return shortPath;
197         }
198     }
199     
200     private class NameProperty extends SyncFileProperty {
201
202         public NameProperty() {
203             super(COLUMN_NAME_NAME, String JavaDoc.class, NbBundle.getMessage(SyncFileNode.class, "BK2005"), NbBundle.getMessage(SyncFileNode.class, "BK2006"));
204             setValue("sortkey", SyncFileNode.this.getName()); // NOI18N
205
}
206
207         public Object JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
208             return SyncFileNode.this.getDisplayName();
209         }
210     }
211
212     private static final String JavaDoc [] zeros = new String JavaDoc [] { "", "00", "0", "" }; // NOI18N
213

214     private class StatusProperty extends SyncFileProperty {
215         
216         public StatusProperty() {
217             super(COLUMN_NAME_STATUS, String JavaDoc.class, NbBundle.getMessage(SyncFileNode.class, "BK2007"), NbBundle.getMessage(SyncFileNode.class, "BK2008"));
218             String JavaDoc shortPath = Utils.getRelativePath(node.getFile());
219             String JavaDoc sortable = Integer.toString(Utils.getComparableStatus(node.getInformation().getStatus()));
220             setValue("sortkey", zeros[sortable.length()] + sortable + "\t" + shortPath + "\t" + SyncFileNode.this.getName()); // NOI18N
221
}
222
223         public Object JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
224             return node.getInformation().getStatusText();
225         }
226     }
227 }
228
Popular Tags