1 19 package org.netbeans.modules.subversion.ui.history; 20 21 import org.openide.explorer.view.TreeTableView; 22 import org.openide.explorer.ExplorerManager; 23 import org.openide.nodes.Node; 24 import org.openide.nodes.PropertySupport; 25 import org.openide.nodes.AbstractNode; 26 import org.openide.nodes.Children; 27 import org.openide.util.NbBundle; 28 import org.openide.util.lookup.Lookups; 29 import org.openide.ErrorManager; 30 31 import javax.swing.*; 32 import javax.swing.tree.DefaultTreeCellRenderer ; 33 import java.util.*; 34 import java.beans.PropertyVetoException ; 35 import java.lang.reflect.InvocationTargetException ; 36 37 42 class DiffTreeTable extends TreeTableView { 43 44 private RevisionsRootNode rootNode; 45 private List results; 46 private final SearchHistoryPanel master; 47 48 public DiffTreeTable(SearchHistoryPanel master) { 49 this.master = master; 50 treeTable.setShowHorizontalLines(true); 51 treeTable.setShowVerticalLines(false); 52 setRootVisible(false); 53 setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 54 setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 55 setupColumns(); 56 57 DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer (); 58 renderer.setOpenIcon(null); 59 renderer.setClosedIcon(null); 60 renderer.setLeafIcon(null); 61 tree.setCellRenderer(renderer); 62 } 63 64 private void setupColumns() { 65 Node.Property [] columns = new Node.Property[4]; 66 ResourceBundle loc = NbBundle.getBundle(DiffTreeTable.class); 67 columns[0] = new ColumnDescriptor(RevisionNode.COLUMN_NAME_NAME, String .class, "", ""); columns[0].setValue("TreeColumnTTV", Boolean.TRUE); columns[1] = new ColumnDescriptor(RevisionNode.COLUMN_NAME_DATE, String .class, loc.getString("LBL_DiffTree_Column_Time"), loc.getString("LBL_DiffTree_Column_Time_Desc")); 70 columns[2] = new ColumnDescriptor(RevisionNode.COLUMN_NAME_USERNAME, String .class, loc.getString("LBL_DiffTree_Column_Username"), loc.getString("LBL_DiffTree_Column_Username_Desc")); 71 columns[3] = new ColumnDescriptor(RevisionNode.COLUMN_NAME_MESSAGE, String .class, loc.getString("LBL_DiffTree_Column_Message"), loc.getString("LBL_DiffTree_Column_Message_Desc")); 72 setProperties(columns); 73 } 74 75 private void setDefaultColumnSizes() { 76 SwingUtilities.invokeLater(new Runnable () { 77 public void run() { 78 int width = getWidth(); 79 treeTable.getColumnModel().getColumn(0).setPreferredWidth(width * 25 / 100); 80 treeTable.getColumnModel().getColumn(1).setPreferredWidth(width * 15 / 100); 81 treeTable.getColumnModel().getColumn(2).setPreferredWidth(width * 10 / 100); 82 treeTable.getColumnModel().getColumn(3).setPreferredWidth(width * 50 / 100); 83 } 84 }); 85 } 86 87 void setSelection(int idx) { 88 treeTable.getSelectionModel().setValueIsAdjusting(false); 89 treeTable.scrollRectToVisible(treeTable.getCellRect(idx, 1, true)); 90 treeTable.getSelectionModel().setSelectionInterval(idx, idx); 91 } 92 93 void setSelection(RepositoryRevision container) { 94 RevisionNode node = (RevisionNode) getNode(rootNode, container); 95 if (node == null) return; 96 ExplorerManager em = ExplorerManager.find(this); 97 try { 98 em.setSelectedNodes(new Node [] { node }); 99 } catch (PropertyVetoException e) { 100 ErrorManager.getDefault().notify(e); 101 } 102 } 103 104 void setSelection(RepositoryRevision.Event revision) { 105 RevisionNode node = (RevisionNode) getNode(rootNode, revision); 106 if (node == null) return; 107 ExplorerManager em = ExplorerManager.find(this); 108 try { 109 em.setSelectedNodes(new Node [] { node }); 110 } catch (PropertyVetoException e) { 111 ErrorManager.getDefault().notify(e); 112 } 113 } 114 115 private Node getNode(Node node, Object obj) { 116 Object object = node.getLookup().lookup(obj.getClass()); 117 if (obj.equals(object)) return node; 118 Enumeration children = node.getChildren().nodes(); 119 while (children.hasMoreElements()) { 120 Node child = (Node) children.nextElement(); 121 Node result = getNode(child, obj); 122 if (result != null) return result; 123 } 124 return null; 125 } 126 127 public int [] getSelection() { 128 return treeTable.getSelectedRows(); 129 } 130 131 public int getRowCount() { 132 return treeTable.getRowCount(); 133 } 134 135 private static class ColumnDescriptor<T> extends PropertySupport.ReadOnly<T> { 136 137 public ColumnDescriptor(String name, Class <T> type, String displayName, String shortDescription) { 138 super(name, type, displayName, shortDescription); 139 } 140 141 public T getValue() throws IllegalAccessException , InvocationTargetException { 142 return null; 143 } 144 } 145 146 public void addNotify() { 147 super.addNotify(); 148 ExplorerManager em = ExplorerManager.find(this); 149 em.setRootContext(rootNode); 150 setDefaultColumnSizes(); 151 } 152 153 public void setResults(List results) { 154 this.results = results; 155 rootNode = new RevisionsRootNode(); 156 ExplorerManager em = ExplorerManager.find(this); 157 if (em != null) { 158 em.setRootContext(rootNode); 159 } 160 } 161 162 private class RevisionsRootNode extends AbstractNode { 163 164 public RevisionsRootNode() { 165 super(new RevisionsRootNodeChildren(), Lookups.singleton(results)); 166 } 167 168 public String getName() { 169 return "revision"; } 171 172 public String getDisplayName() { 173 return NbBundle.getMessage(DiffTreeTable.class, "LBL_DiffTree_Column_Name"); } 175 176 public String getShortDescription() { 177 return NbBundle.getMessage(DiffTreeTable.class, "LBL_DiffTree_Column_Name_Desc"); } 179 } 180 181 private class RevisionsRootNodeChildren extends Children.Keys { 182 183 public RevisionsRootNodeChildren() { 184 } 185 186 protected void addNotify() { 187 refreshKeys(); 188 } 189 190 protected void removeNotify() { 191 setKeys(Collections.EMPTY_SET); 192 } 193 194 private void refreshKeys() { 195 setKeys(results); 196 } 197 198 protected Node[] createNodes(Object key) { 199 RevisionNode node; 200 if (key instanceof RepositoryRevision) { 201 node = new RevisionNode((RepositoryRevision) key, master); 202 } else { node = new RevisionNode(((RepositoryRevision.Event) key), master); 204 } 205 return new Node[] { node }; 206 } 207 } 208 } 209 | Popular Tags |