Adding Git source, NetBeans project files, GPL v2 LICENSE, ant build file.
[nbgit.git] / src / org / netbeans / modules / git / ui / status / SyncFileNode.java
blob5c2f1a92321ba88fef661b2e35c18078b52bccee
1 package org.netbeans.modules.git.ui.status;
3 import java.io.File;
4 import java.io.IOException;
5 import java.lang.reflect.InvocationTargetException;
6 import java.util.logging.Level;
7 import javax.swing.Action;
8 import org.netbeans.modules.git.FileInformation;
9 import org.netbeans.modules.git.Git;
10 import org.netbeans.modules.git.GitFileNode;
11 import org.netbeans.modules.git.ui.diff.DiffAction;
12 import org.netbeans.modules.git.util.GitUtils;
13 import org.openide.filesystems.FileObject;
14 import org.openide.filesystems.FileUtil;
15 import org.openide.loaders.DataObject;
16 import org.openide.loaders.DataObjectNotFoundException;
17 import org.openide.nodes.AbstractNode;
18 import org.openide.nodes.Children;
19 import org.openide.nodes.Sheet;
20 import org.openide.util.NbBundle;
21 import org.openide.util.RequestProcessor;
22 import org.openide.util.lookup.Lookups;
24 /**
25 * The node that is rendered in the SyncTable view. It gets values to display
26 * from the GitFileNode which serves as the 'data' node for this 'visual' node.
28 * @author Maros Sandor
30 public class SyncFileNode extends AbstractNode {
32 private GitFileNode node;
34 static final String COLUMN_NAME_NAME = "name"; // NOI18N;
35 static final String COLUMN_NAME_PATH = "path"; // NOI18N;
36 static final String COLUMN_NAME_STATUS = "status"; // NOI18N;
37 static final String COLUMN_NAME_BRANCH = "branch"; // NOI18N;
39 private String htmlDisplayName;
41 private RequestProcessor.Task repoload;
43 private final VersioningPanel panel;
45 public SyncFileNode(GitFileNode node, VersioningPanel _panel) {
46 this(Children.LEAF, node, _panel);
49 private SyncFileNode(Children children, GitFileNode node, VersioningPanel _panel) {
50 super(children, Lookups.fixed(node.getLookupObjects()));
51 this.node = node;
52 this.panel = _panel;
53 initProperties();
54 refreshHtmlDisplayName();
57 public File getFile() {
58 return node.getFile();
61 public FileInformation getFileInformation() {
62 return node.getInformation();
65 @Override
66 public String getName() {
67 return node.getName();
70 public Action getPreferredAction() {
71 // TODO: getPreferedAction
72 if (node.getInformation().getStatus() == FileInformation.STATUS_VERSIONED_CONFLICT) {
73 return null; //SystemAction.get(ResolveConflictsAction.class);
75 return new DiffAction(null, GitUtils.getCurrentContext(null));
78 /**
79 * Provide cookies to actions.
80 * If a node represents primary file of a DataObject
81 * it has respective DataObject cookies.
83 @SuppressWarnings("unchecked") // Adding getCookie(Class<Cookie> klass) results in name clash
84 @Override
85 public Cookie getCookie(Class klass) {
86 FileObject fo = FileUtil.toFileObject(getFile());
87 if (fo != null) {
88 try {
89 DataObject dobj = DataObject.find(fo);
90 if (fo.equals(dobj.getPrimaryFile())) {
91 return dobj.getCookie(klass);
93 } catch (DataObjectNotFoundException e) {
94 // ignore file without data objects
97 return super.getCookie(klass);
100 private void initProperties() {
101 if (node.getFile().isDirectory()) setIconBaseWithExtension("org/openide/loaders/defaultFolder.gif"); // NOI18N
103 Sheet sheet = Sheet.createDefault();
104 Sheet.Set ps = Sheet.createPropertiesSet();
106 ps.put(new NameProperty());
107 ps.put(new PathProperty());
108 ps.put(new StatusProperty());
109 ps.put(new BranchProperty());
111 sheet.put(ps);
112 setSheet(sheet);
115 private void refreshHtmlDisplayName() {
116 FileInformation info = node.getInformation();
117 int status = info.getStatus();
118 // Special treatment: Mergeable status should be annotated as Conflict in Versioning view according to UI spec
119 if (status == FileInformation.STATUS_VERSIONED_MERGE) {
120 status = FileInformation.STATUS_VERSIONED_CONFLICT;
122 htmlDisplayName = Git.getInstance().getGitAnnotator().annotateNameHtml(node.getFile().getName(), info, null);
123 fireDisplayNameChange(node.getName(), node.getName());
126 public String getHtmlDisplayName() {
127 return htmlDisplayName;
130 public void refresh() {
131 refreshHtmlDisplayName();
134 private abstract class SyncFileProperty extends org.openide.nodes.PropertySupport.ReadOnly {
135 @SuppressWarnings("unchecked")
136 protected SyncFileProperty(String name, Class type, String displayName, String shortDescription) {
137 super(name, type, displayName, shortDescription);
140 @Override
141 public String toString() {
142 try {
143 return getValue().toString();
144 } catch (Exception e) {
145 Git.LOG.log(Level.INFO, null, e);
146 return e.getLocalizedMessage();
151 private class BranchProperty extends SyncFileProperty {
153 public BranchProperty() {
154 super(COLUMN_NAME_BRANCH, String.class, NbBundle.getMessage(SyncFileNode.class, "BK2001"), NbBundle.getMessage(SyncFileNode.class, "BK2002")); // NOI18N
157 public Object getValue() {
158 String branchInfo = panel.getDisplayBranchInfo();
159 return branchInfo == null ? "" : branchInfo; // NOI18N
163 private class PathProperty extends SyncFileProperty {
165 private String shortPath;
167 public PathProperty() {
168 super(COLUMN_NAME_PATH, String.class, NbBundle.getMessage(SyncFileNode.class, "BK2003"), NbBundle.getMessage(SyncFileNode.class, "BK2004")); // NOI18N
169 shortPath = GitUtils.getRelativePath(node.getFile());
170 setValue("sortkey", shortPath + "\t" + SyncFileNode.this.getName()); // NOI18N
173 public Object getValue() throws IllegalAccessException, InvocationTargetException {
174 return shortPath;
178 // XXX it's not probably called, are there another Node lifecycle events
179 public void destroy() throws IOException {
180 super.destroy();
181 if (repoload != null) {
182 repoload.cancel();
186 private class NameProperty extends SyncFileProperty {
188 public NameProperty() {
189 super(COLUMN_NAME_NAME, String.class, NbBundle.getMessage(SyncFileNode.class, "BK2005"), NbBundle.getMessage(SyncFileNode.class, "BK2006")); // NOI18N
190 setValue("sortkey", SyncFileNode.this.getName()); // NOI18N
193 public Object getValue() throws IllegalAccessException, InvocationTargetException {
194 return SyncFileNode.this.getDisplayName();
198 private static final String [] zeros = new String [] { "", "00", "0", "" }; // NOI18N
200 private class StatusProperty extends SyncFileProperty {
202 public StatusProperty() {
203 super(COLUMN_NAME_STATUS, String.class, NbBundle.getMessage(SyncFileNode.class, "BK2007"), NbBundle.getMessage(SyncFileNode.class, "BK2008")); // NOI18N
204 String shortPath = GitUtils.getRelativePath(node.getFile()); // NOI18N
205 String sortable = Integer.toString(GitUtils.getComparableStatus(node.getInformation().getStatus()));
206 setValue("sortkey", zeros[sortable.length()] + sortable + "\t" + shortPath + "\t" + SyncFileNode.this.getName()); // NOI18N
209 public Object getValue() throws IllegalAccessException, InvocationTargetException {
210 FileInformation finfo = node.getInformation();
211 //TODO: finfo.getEntry(node.getFile()); // XXX not interested in return value, side effect loads ISVNStatus structure
212 int mask = panel.getDisplayStatuses();
213 return finfo.getStatusText(mask);