1 19 package org.netbeans.modules.localhistory.ui.view; 20 21 import java.beans.PropertyEditor ; 22 import java.beans.PropertyEditorSupport ; 23 import java.util.ArrayList ; 24 import javax.swing.Action ; 25 import org.openide.nodes.AbstractNode; 26 import org.openide.nodes.Children; 27 import java.lang.reflect.InvocationTargetException ; 28 import java.text.DateFormat ; 29 import java.util.List ; 30 import org.netbeans.modules.localhistory.LocalHistory; 31 import org.netbeans.modules.localhistory.store.StoreEntry; 32 import org.openide.nodes.PropertySupport; 33 import org.openide.nodes.Sheet; 34 import org.openide.util.Lookup; 35 import org.openide.util.NbBundle; 36 import org.openide.util.actions.SystemAction; 37 import org.openide.util.lookup.Lookups; 38 39 44 public class StoreEntryNode extends AbstractNode implements Comparable { 45 46 static final String PROPERTY_NAME_LABEL = "label"; private List <StoreEntry> entries; 48 private static DateFormat defaultFormat = DateFormat.getDateTimeInstance(); 49 50 private StoreEntryNode(List <StoreEntry> childrenEntries) { 51 super(createChildren(childrenEntries)); 52 this.entries = childrenEntries; 53 initProperties(); 54 } 55 56 private StoreEntryNode(List <StoreEntry> childrenEntries, Lookup l) { 57 super(Children.LEAF, l); 58 this.entries = childrenEntries; 59 initProperties(); 60 } 61 62 static StoreEntryNode create(List <StoreEntry> childrenEntries) { 63 64 assert childrenEntries != null && childrenEntries.size() > 0; 65 66 if(childrenEntries.size() > 1) { 67 return new StoreEntryNode(childrenEntries); 68 } else { 69 return new StoreEntryNode(childrenEntries, Lookups.fixed(new Object [] { childrenEntries.get(0) })); 70 } 71 } 72 73 private static Children createChildren(List <StoreEntry> childrenEntries) { 74 FileNode[] nodes = new FileNode[childrenEntries.size()]; 75 int i = 0; 76 for (StoreEntry se : childrenEntries) { 77 nodes[i++] = new FileNode(se); 78 } 79 Children.SortedArray children = new Children.SortedArray(); 80 children.add(nodes); 81 return children; 82 } 83 84 private void initProperties() { 85 Sheet sheet = Sheet.createDefault(); 86 Sheet.Set ps = Sheet.createPropertiesSet(); 87 88 ps.put(new LabelProperty()); 89 90 sheet.put(ps); 91 setSheet(sheet); 92 } 93 94 public String getDisplayName() { 95 return getName(); 96 } 97 98 public String getName() { 99 return getFormatedDate(entries.get(0)); 100 } 101 102 static String getFormatedDate(StoreEntry se) { 103 return defaultFormat.format(se.getDate()); 104 } 105 106 public Action [] getActions(boolean context) { 107 return new Action [] { 108 SystemAction.get(RevertFileAction.class), 109 SystemAction.get(DeleteAction.class) 110 }; 111 } 112 113 public int compareTo(Object obj) { 114 if( !(obj instanceof StoreEntryNode) || obj == null) { 115 return 1; 116 } 117 StoreEntryNode node = (StoreEntryNode) obj; 118 119 if(node.entries.get(0).getTimestamp() > entries.get(0).getTimestamp()) { 120 return 1; 121 } else if(node.entries.get(0).getTimestamp() < entries.get(0).getTimestamp()) { 122 return -1; 123 } else { 124 return 0; 125 } 126 } 127 128 private class LabelProperty extends PropertySupport.ReadWrite<String > { 129 public LabelProperty() { 130 super(PROPERTY_NAME_LABEL, String .class, NbBundle.getMessage(StoreEntryNode.class, "LBL_LabelProperty_Name"), NbBundle.getMessage(StoreEntryNode.class, "LBL_LabelProperty_Desc")); 131 } 132 public String getValue() throws IllegalAccessException , InvocationTargetException { 133 return entries.get(0).getLabel(); 134 } 135 public void setValue(String value) throws IllegalAccessException , IllegalArgumentException , InvocationTargetException 136 { 137 List <StoreEntry> newEntries = new ArrayList <StoreEntry>(entries.size()); 138 for(StoreEntry se : entries) { 139 LocalHistory.getInstance().getLocalHistoryStore().setLabel(se.getFile(), se.getTimestamp(), value); 140 newEntries.add(StoreEntry.createStoreEntry(se.getFile(), se.getStoreFile(), se.getTimestamp(), value)); 141 } 142 entries = newEntries; 143 } 144 public PropertyEditor getPropertyEditor() { 145 return new PropertyEditorSupport (); 146 } 147 } 148 149 private static class FileNode extends AbstractNode implements Comparable { 150 151 private final StoreEntry entry; 152 153 FileNode(StoreEntry entry) { 154 super(Children.LEAF, Lookups.fixed(new Object [] { entry })); 155 this.entry = entry; 156 } 157 158 public Action [] getActions(boolean context) { 159 List <StoreEntry> entries = new ArrayList <StoreEntry>(1); 160 entries.add(entry); 161 return new Action [] { 162 SystemAction.get(RevertFileAction.class), 163 SystemAction.get(DeleteAction.class) 164 }; 165 } 166 167 public String getName() { 168 return entry.getFile().getName(); 169 } 170 171 public int compareTo(Object obj) { 172 if( !(obj instanceof FileNode) || obj == null) { 173 return -1; 174 } 175 FileNode node = (FileNode) obj; 176 return getName().compareTo(node.getName()); 177 } 178 } 179 180 } 181 182 | Popular Tags |