KickJava   Java API By Example, From Geeks To Geeks.

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

49 public class SyncFileNode extends AbstractNode {
50     
51     private SvnFileNode node;
52
53     static final String JavaDoc COLUMN_NAME_NAME = "name"; // NOI18N
54
static final String JavaDoc COLUMN_NAME_PATH = "path"; // NOI18N
55
static final String JavaDoc COLUMN_NAME_STATUS = "status"; // NOI18N
56
static final String JavaDoc COLUMN_NAME_BRANCH = "branch"; // NOI18N
57

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

101     public Cookie getCookie(Class JavaDoc klass) {
102         FileObject fo = FileUtil.toFileObject(getFile());
103         if (fo != null) {
104             try {
105                 DataObject dobj = DataObject.find(fo);
106                 if (fo.equals(dobj.getPrimaryFile())) {
107                     return dobj.getCookie(klass);
108                 }
109             } catch (DataObjectNotFoundException e) {
110                 // ignore file without data objects
111
}
112         }
113         return super.getCookie(klass);
114     }
115
116     private void initProperties() {
117         if (node.getFile().isDirectory()) setIconBaseWithExtension("org/openide/loaders/defaultFolder.gif"); // NOI18N
118

119         Sheet sheet = Sheet.createDefault();
120         Sheet.Set ps = Sheet.createPropertiesSet();
121         
122         ps.put(new NameProperty());
123         ps.put(new PathProperty());
124         ps.put(new StatusProperty());
125         ps.put(new BranchProperty());
126         
127         sheet.put(ps);
128         setSheet(sheet);
129     }
130
131     private void refreshHtmlDisplayName() {
132         FileInformation info = node.getInformation();
133         int status = info.getStatus();
134         // Special treatment: Mergeable status should be annotated as Conflict in Versioning view according to UI spec
135
if (status == FileInformation.STATUS_VERSIONED_MERGE) {
136             status = FileInformation.STATUS_VERSIONED_CONFLICT;
137         }
138         htmlDisplayName = Subversion.getInstance().getAnnotator().annotateNameHtml(node.getFile().getName(), info, null);
139         fireDisplayNameChange(node.getName(), node.getName());
140     }
141
142     public String JavaDoc getHtmlDisplayName() {
143         return htmlDisplayName;
144     }
145
146     public void refresh() {
147         refreshHtmlDisplayName();
148     }
149
150     private abstract class SyncFileProperty extends org.openide.nodes.PropertySupport.ReadOnly {
151
152         protected SyncFileProperty(String JavaDoc name, Class JavaDoc type, String JavaDoc displayName, String JavaDoc shortDescription) {
153             super(name, type, displayName, shortDescription);
154         }
155
156         public String JavaDoc toString() {
157             try {
158                 return getValue().toString();
159             } catch (Exception JavaDoc e) {
160                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
161                 return e.getLocalizedMessage();
162             }
163         }
164     }
165     
166     private class BranchProperty extends SyncFileProperty {
167
168         public BranchProperty() {
169             super(COLUMN_NAME_BRANCH, String JavaDoc.class, NbBundle.getMessage(SyncFileNode.class, "BK2001"), NbBundle.getMessage(SyncFileNode.class, "BK2002")); // NOI18N
170
}
171
172         public Object JavaDoc getValue() {
173             String JavaDoc copyName = SvnUtils.getCopy(node.getFile());
174             return copyName == null ? "" : copyName;
175         }
176     }
177     
178     private class PathProperty extends SyncFileProperty {
179
180         private String JavaDoc shortPath;
181
182         public PathProperty() {
183             super(COLUMN_NAME_PATH, String JavaDoc.class, NbBundle.getMessage(SyncFileNode.class, "BK2003"), NbBundle.getMessage(SyncFileNode.class, "BK2004")); // NOI18N
184
setValue("sortkey", "\u65000\t" + SyncFileNode.this.getName()); // NOI18N
185
}
186
187         public Object JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
188             if (shortPath == null) {
189                 Runnable JavaDoc run = new Runnable JavaDoc() {
190                     public void run() {
191                         shortPath = SvnUtils.getRelativePath(node.getFile());
192                         if (shortPath == null) {
193                             shortPath = org.openide.util.NbBundle.getMessage(SyncFileNode.class, "LBL_Location_NotInRepository"); // NOI18N
194
}
195                         setValue("sortkey", shortPath + "\t" + SyncFileNode.this.getName()); // NOI18N
196
// Table sorter is not thread safe, use this as workaround
197
SwingUtilities.invokeLater(new Runnable JavaDoc() {
198                             public void run() {
199                                 firePropertyChange(COLUMN_NAME_PATH, null, null);
200                             }
201                         });
202                     }
203                 };
204                 repoload = Subversion.getInstance().getRequestProcessor().post(run);
205                 return org.openide.util.NbBundle.getMessage(SyncFileNode.class, "LBL_RepositoryPath_LoadingProgress"); // NOI18N
206
}
207             return shortPath;
208         }
209     }
210
211     // XXX it's not probably called, are there another Node lifecycle events
212
public void destroy() throws IOException JavaDoc {
213         super.destroy();
214         if (repoload != null) {
215             repoload.cancel();
216         }
217     }
218     
219     private class NameProperty extends SyncFileProperty {
220
221         public NameProperty() {
222             super(COLUMN_NAME_NAME, String JavaDoc.class, NbBundle.getMessage(SyncFileNode.class, "BK2005"), NbBundle.getMessage(SyncFileNode.class, "BK2006")); // NOI18N
223
setValue("sortkey", SyncFileNode.this.getName()); // NOI18N
224
}
225
226         public Object JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
227             return SyncFileNode.this.getDisplayName();
228         }
229     }
230
231     private static final String JavaDoc [] zeros = new String JavaDoc [] { "", "00", "0", "" }; // NOI18N
232

233     private class StatusProperty extends SyncFileProperty {
234         
235         public StatusProperty() {
236             super(COLUMN_NAME_STATUS, String JavaDoc.class, NbBundle.getMessage(SyncFileNode.class, "BK2007"), NbBundle.getMessage(SyncFileNode.class, "BK2008")); // NOI18N
237
String JavaDoc shortPath = "path";//SvnUtils.getRelativePath(node.getFile()); // NOI18N
238
String JavaDoc sortable = Integer.toString(SvnUtils.getComparableStatus(node.getInformation().getStatus()));
239             setValue("sortkey", zeros[sortable.length()] + sortable + "\t" + shortPath + "\t" + SyncFileNode.this.getName()); // NOI18N
240
}
241
242         public Object JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
243             FileInformation finfo = node.getInformation();
244             finfo.getEntry(node.getFile()); // XXX not interested in return value, side effect loads ISVNStatus structure
245
int mask = panel.getDisplayStatuses();
246             return finfo.getStatusText(mask);
247         }
248     }
249 }
250
Popular Tags