1 19 package org.netbeans.modules.localhistory.ui.view; 20 21 import java.awt.BorderLayout ; 22 import java.awt.GridBagLayout ; 23 import java.awt.Point ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.beans.PropertyVetoException ; 27 import java.io.File ; 28 import java.lang.reflect.InvocationTargetException ; 29 import java.util.ArrayList ; 30 import java.util.List ; 31 import java.util.ResourceBundle ; 32 import javax.swing.BorderFactory ; 33 import javax.swing.JPanel ; 34 import javax.swing.JTable ; 35 import javax.swing.JTree ; 36 import javax.swing.SwingUtilities ; 37 import javax.swing.tree.DefaultTreeCellRenderer ; 38 import javax.swing.tree.TreePath ; 39 import javax.swing.tree.TreeSelectionModel ; 40 import org.netbeans.modules.localhistory.LocalHistory; 41 import org.netbeans.modules.localhistory.store.LocalHistoryStore; 42 import org.openide.explorer.ExplorerManager; 43 import org.openide.explorer.view.TreeTableView; 44 import org.openide.nodes.Node; 45 import org.openide.nodes.PropertySupport; 46 import org.openide.util.NbBundle; 47 import org.openide.windows.TopComponent; 48 49 53 public class LocalHistoryFolderView implements PropertyChangeListener { 54 55 private TopComponent tc; 56 private LocalHistoryFolderPanel folderPanel; 57 private FolderTablePanel tablePanel; 58 private File [] files; 59 60 61 public LocalHistoryFolderView(File [] files, TopComponent tc) { 62 this.tc = tc; 63 64 tablePanel = new FolderTablePanel(); 65 tablePanel.getExplorerManager().addPropertyChangeListener(this); 66 folderPanel = new LocalHistoryFolderPanel(); 67 folderPanel.tablePanel.add(tablePanel, BorderLayout.CENTER); 68 69 LocalHistory.getInstance().getLocalHistoryStore().addPropertyChangeListener(this); 71 this.files = files; 72 refreshTablePanel(true); 73 } 74 75 public void propertyChange(PropertyChangeEvent evt) { 76 if(ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) { 77 selectionChanged(evt); 78 } else if(LocalHistoryStore.PROPERTY_CHANGED.equals(evt.getPropertyName())) { 79 storeChanged(evt); 80 } 81 } 82 83 public ExplorerManager getExplorerManager() { 84 return tablePanel.getExplorerManager(); 85 } 86 87 public JPanel getPanel() { 88 return folderPanel; 89 } 90 91 public void close() { 92 LocalHistory.getInstance().getLocalHistoryStore().removePropertyChangeListener(this); 93 } 94 95 private void selectionChanged(PropertyChangeEvent evt) { 96 Node[] newSelection = ((Node[]) evt.getNewValue()); 97 if(!acceptSelection(newSelection)) { return; 99 } 100 tc.setActivatedNodes(newSelection); 101 } 102 103 private boolean acceptSelection(Node[] newSelection) { 104 return newSelection != null && newSelection.length == 0; 105 } 106 107 private void storeChanged(PropertyChangeEvent evt) { 108 Object newValue = evt.getNewValue(); 109 Object oldValue = evt.getOldValue(); 110 if( newValue != null && contains( (File ) newValue ) || 111 oldValue != null && contains( (File ) oldValue ) ) 112 { 113 refreshTablePanel(false); 114 } 115 } 116 117 private boolean contains(File file) { 118 for(File f : files) { 119 if(f.equals(file)) { 120 return true; 121 } 122 } 123 return false; 124 } 125 126 private void refreshTablePanel(final boolean selectFirst) { 127 128 } 129 130 private Node[] getEqualNodes(Node root, Node[] oldNodes) { 131 List <Node> ret = new ArrayList <Node>(); 132 for(Node on : oldNodes) { 133 Node node = findEqualInChildren(root, on); 134 if(node != null) { 135 ret.add(node); 136 } 137 } 138 return ret.toArray(new Node[ret.size()]); 139 } 140 141 private Node findEqualInChildren(Node node, Node toFind) { 142 Node[] children = node.getChildren().getNodes(); 143 for(Node child : children) { 144 if(toFind.getName().equals(child.getName())) { 145 return child; 146 } 147 Node n = findEqualInChildren(child, toFind); 148 if(n != null) { 149 return n; 150 } 151 } 152 return null; 153 } 154 155 private void selectFirstNode(Node root) { 156 Node[] dateFolders = root.getChildren().getNodes(); 157 if (dateFolders != null && dateFolders.length > 0) { 158 final Node[] nodes = dateFolders[0].getChildren().getNodes(); 159 if (nodes != null && nodes.length > 0) { 160 selectNodes(new Node[]{ nodes[0] }); 161 } 162 } 163 } 164 165 private void selectFirstNeighborNode(Node context, Node oldSelection) { 166 tablePanel.getExplorerManager().setExploredContext(context); 167 Node[] children = context.getChildren().getNodes(); 168 if(children.length > 0 && children[0] instanceof Comparable ) { 169 Node[] newSelection = new Node[] { children[0] } ; 170 for(int i = 1; i < children.length; i++) { 171 Comparable c = (Comparable ) children[i]; 172 if( c.compareTo(oldSelection) < 0 ) { 173 newSelection[0] = children[i]; 174 } 175 } 176 selectNodes(newSelection); 177 } 178 } 179 180 private void selectNodes(final Node[] nodes) { 181 SwingUtilities.invokeLater(new Runnable () { 182 public void run() { 183 try { 184 tablePanel.getExplorerManager().setSelectedNodes(nodes); 185 } catch (PropertyVetoException ex) { 186 } 188 } 189 }); 190 } 191 192 private class FolderTablePanel extends JPanel implements ExplorerManager.Provider { 194 195 private final BrowserTreeTableView treeView; 196 private final ExplorerManager manager; 197 198 public FolderTablePanel() { 199 manager = new ExplorerManager(); 200 201 setLayout(new GridBagLayout ()); 202 203 treeView = new BrowserTreeTableView(); 204 setLayout(new BorderLayout ()); 205 add(treeView, BorderLayout.CENTER); 206 } 207 208 public ExplorerManager getExplorerManager() { 209 return manager; 210 } 211 212 private class BrowserTreeTableView extends TreeTableView { 213 BrowserTreeTableView() { 214 setupColumns(); 215 216 tree.setShowsRootHandles(true); 217 tree.setRootVisible(false); 218 setDefaultActionAllowed (false); 219 setBorder(BorderFactory.createEtchedBorder()); 220 setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); setPopupAllowed(true); 224 225 DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer (); 226 renderer.setLeafIcon(null); 227 tree.setCellRenderer(renderer); 228 229 } 230 231 JTree getTree() { 232 return tree; 233 } 234 235 JTable getTable() { 236 return treeTable; 237 } 238 239 public void startEditingAtPath(TreePath path) { 240 tree.startEditingAtPath(path); 241 } 242 243 public void addNotify() { 244 super.addNotify(); 245 setDefaultColumnSizes(); 246 } 247 248 private void setupColumns() { 249 ResourceBundle loc = NbBundle.getBundle(FolderTablePanel.class); 250 251 Node.Property [] columns = new Node.Property[1]; 252 columns = new Node.Property[1]; 253 columns[0] = new ColumnDescriptor<String >( 254 StoreEntryNode.PROPERTY_NAME_LABEL, 255 String .class, 256 loc.getString("LBL_LocalHistory_Column_Label"), loc.getString("LBL_LocalHistory_Column_Version_Desc")); setProperties(columns); 259 } 260 261 private void setDefaultColumnSizes() { 262 SwingUtilities.invokeLater(new Runnable () { 263 public void run() { 264 int width = getWidth(); 265 treeTable.getColumnModel().getColumn(0).setPreferredWidth(width * 35 / 100); 266 treeTable.getColumnModel().getColumn(1).setPreferredWidth(width * 65 / 100); 267 } 268 }); 269 } 270 } 271 272 int rowAtPoint(Point point) { 273 return treeView.getTable().rowAtPoint(point); 274 } 275 } 276 277 private static class ColumnDescriptor<T> extends PropertySupport.ReadOnly<T> { 278 public ColumnDescriptor(String name, Class <T> type, String displayName, String shortDescription) { 279 super(name, type, displayName, shortDescription); 280 } 281 public T getValue() throws IllegalAccessException , InvocationTargetException { 282 return null; 283 } 284 } 285 } 286 | Popular Tags |