1 19 package org.netbeans.modules.localhistory.ui.view; 20 21 import java.awt.Image ; 22 import java.beans.BeanInfo ; 23 import java.io.File ; 24 import java.util.ArrayList ; 25 import java.util.Calendar ; 26 import java.util.Date ; 27 import java.util.Iterator ; 28 import javax.swing.Action ; 29 import org.openide.nodes.AbstractNode; 30 import org.openide.nodes.Children; 31 import java.util.HashMap ; 32 import java.util.List ; 33 import java.util.Map ; 34 import javax.swing.UIManager ; 35 import org.netbeans.modules.localhistory.LocalHistory; 36 import org.netbeans.modules.localhistory.store.StoreEntry; 37 import org.openide.nodes.Node; 38 import org.openide.util.NbBundle; 39 40 91 public class LocalHistoryRootNode extends AbstractNode { 92 93 static final Action [] NO_ACTION = new Action [0]; 94 95 private LocalHistoryRootNode(Children children) { 96 super(children); 97 } 98 99 106 static Node createRootNode(File [] files) { 107 Children.SortedArray children = new Children.SortedArray(); 108 children.add(createDateFolders(files)); 109 return new LocalHistoryRootNode(children); 110 } 111 112 119 private static DateFolderNode[] createDateFolders(File [] files) { 120 121 Map <Long , List <StoreEntry>> entriesMap = new HashMap <Long , List <StoreEntry>>(); 124 for (File f : files) { 125 StoreEntry[] ses = LocalHistory.getInstance().getLocalHistoryStore().getStoreEntries(f); 126 for(StoreEntry se : ses) { 127 List <StoreEntry> storeEntries = entriesMap.get(se.getTimestamp()); 128 if(storeEntries == null) { 129 storeEntries = new ArrayList <StoreEntry>(); 130 entriesMap.put(se.getTimestamp(), storeEntries); 131 } 132 storeEntries.add(se); 133 } 134 } 135 136 Map <Integer , List <Node>> storeEntryNodesToDays = new HashMap <Integer , List <Node>>(); 137 138 for (Long ts : entriesMap.keySet()) { 140 int day = getDay(ts); 141 List <Node> nodesFromADay = storeEntryNodesToDays.get(day); 142 if(nodesFromADay == null) { 143 nodesFromADay = new ArrayList <Node>(); 144 storeEntryNodesToDays.put(day, nodesFromADay); 145 } 146 List <StoreEntry> storeEntries = entriesMap.get(ts); 147 nodesFromADay.add(createStoreEntryNode(storeEntries, files)); 148 } 149 150 List <DateFolderNode> dateFolderNodes = new ArrayList <DateFolderNode>( storeEntryNodesToDays.keySet().size()); 152 for (Iterator <Integer > it = storeEntryNodesToDays.keySet().iterator(); it.hasNext();) { 153 int key = it.next(); 154 List <Node> l = storeEntryNodesToDays.get(key); 155 Children.SortedArray children = new Children.SortedArray(); 156 children.add(l.toArray(new Node[l.size()])); 157 158 dateFolderNodes.add(new DateFolderNode(key, children)); 159 } 160 161 return dateFolderNodes.toArray(new DateFolderNode[dateFolderNodes.size()]); 162 } 163 164 private static int getDay(long ts) { 165 Date date = new Date (ts); 166 167 Calendar c = Calendar.getInstance(); 168 c.setTime(new Date ()); 169 170 int todayMillis = c.get(Calendar.HOUR_OF_DAY) * 60 * 60 * 1000 + 172 c.get(Calendar.MINUTE) * 60 * 1000 + 173 c.get(Calendar.SECOND) * 1000 + 174 c.get(Calendar.MILLISECOND); 175 c.add(Calendar.MILLISECOND, -1 * todayMillis); 176 177 if(c.getTime().compareTo(date) < 0) { 178 return 0; 179 } 180 181 return (int) ( (c.getTimeInMillis() - ts) / (24 * 60 * 60 * 1000) ) + 1; 182 183 } 184 185 190 private static Node createStoreEntryNode(List <StoreEntry> entries, File [] files) { 191 if(files.length == 1) { 192 193 return StoreEntryNode.create(entries); 195 196 } else { 197 199 long ts = entries.get(0).getTimestamp(); 201 202 List <StoreEntry> entriesList = new ArrayList <StoreEntry>(); 205 for(File f : files) { 206 boolean fileInEntries = false; 207 for(StoreEntry se : entries) { 209 if(f.equals(se.getFile())) { 210 entriesList.add(se); 211 fileInEntries = true; 212 break; 213 } 214 } 215 if(fileInEntries) { 216 continue; 218 } 219 220 StoreEntry e = LocalHistory.getInstance().getLocalHistoryStore().getStoreEntry(f, ts); 222 if(e != null) { 223 224 226 entriesList.add(e); 228 } else { 229 entriesList.add(StoreEntry.createFakeStoreEntry(f, ts)); 231 } 232 } 233 return StoreEntryNode.create(entriesList); 234 } 235 } 236 237 public String getName() { 238 return "rootnode"; } 240 241 public String getDisplayName() { 242 return NbBundle.getMessage(LocalHistoryRootNode.class, "LBL_LocalHistory_Column_Version"); } 244 245 public Action [] getActions(boolean context) { 246 return NO_ACTION; 247 } 248 249 static class DateFolderNode extends AbstractNode implements Comparable { 250 private final int day; 251 252 DateFolderNode(int day, Children children) { 253 super(children); 254 this.day = day; 255 } 256 257 int getDay() { 258 return day; 259 } 260 261 public Image getIcon(int type) { 262 Image img = null; 263 if (type == BeanInfo.ICON_COLOR_16x16) { 264 img = (Image ) UIManager.get("Nb.Explorer.Folder.icon"); } 266 if (img == null) { 267 img = super.getIcon(type); 268 } 269 return img; 270 } 271 272 public Image getOpenedIcon(int type) { 273 Image img = null; 274 if (type == BeanInfo.ICON_COLOR_16x16) { 275 img = (Image ) UIManager.get("Nb.Explorer.Folder.openedIcon"); } 277 if (img == null) { 278 img = super.getIcon(type); 279 } 280 return img; 281 } 282 283 public Action [] getActions(boolean context) { 284 return NO_ACTION; 285 } 286 287 public String getName() { 288 switch (day) { 289 case 0: return NbBundle.getMessage(LocalHistoryRootNode.class, "DateFolderName_0"); 290 case 1: return NbBundle.getMessage(LocalHistoryRootNode.class, "DateFolderName_1"); 291 case 2: return NbBundle.getMessage(LocalHistoryRootNode.class, "DateFolderName_2"); 292 case 3: return NbBundle.getMessage(LocalHistoryRootNode.class, "DateFolderName_3"); 293 case 4: return NbBundle.getMessage(LocalHistoryRootNode.class, "DateFolderName_4"); 294 case 5: return NbBundle.getMessage(LocalHistoryRootNode.class, "DateFolderName_5"); 295 case 6: return NbBundle.getMessage(LocalHistoryRootNode.class, "DateFolderName_6"); 296 } 297 return NbBundle.getMessage(LocalHistoryRootNode.class, "DateFolderName_other"); 298 } 299 300 public int compareTo(Object obj) { 301 if( !(obj instanceof DateFolderNode) || obj == null) { 302 return -1; 303 } 304 DateFolderNode lhNode = (DateFolderNode) obj; 305 306 if(lhNode.getDay() > getDay()) { 307 return -1; 308 } else if(lhNode.getDay() < getDay()) { 309 return 1; 310 } else { 311 return 0; 312 } 313 } 314 } 315 } 316 317 | Popular Tags |