| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
package org.mozilla.javascript.tools.debugger.treetable; |
| 35 |
|
| 36 |
import javax.swing.*; |
| 37 |
import javax.swing.event.*; |
| 38 |
import javax.swing.tree.*; |
| 39 |
import javax.swing.table.*; |
| 40 |
|
| 41 |
import java.awt.Dimension; |
| 42 |
import java.awt.Component; |
| 43 |
import java.awt.Graphics; |
| 44 |
|
| 45 |
import java.awt.event.MouseEvent; |
| 46 |
|
| 47 |
import java.util.EventObject; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public class JTreeTable extends JTable { |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
private static final long serialVersionUID = -2103973006456695515L; |
| 64 |
|
| 65 |
protected TreeTableCellRenderer tree; |
| 66 |
|
| 67 |
public JTreeTable(TreeTableModel treeTableModel) { |
| 68 |
super(); |
| 69 |
|
| 70 |
|
| 71 |
tree = new TreeTableCellRenderer(treeTableModel); |
| 72 |
|
| 73 |
|
| 74 |
super.setModel(new TreeTableModelAdapter(treeTableModel, tree)); |
| 75 |
|
| 76 |
|
| 77 |
ListToTreeSelectionModelWrapper selectionWrapper = new |
| 78 |
ListToTreeSelectionModelWrapper(); |
| 79 |
tree.setSelectionModel(selectionWrapper); |
| 80 |
setSelectionModel(selectionWrapper.getListSelectionModel()); |
| 81 |
|
| 82 |
|
| 83 |
setDefaultRenderer(TreeTableModel.class, tree); |
| 84 |
setDefaultEditor(TreeTableModel.class, new TreeTableCellEditor()); |
| 85 |
|
| 86 |
|
| 87 |
setShowGrid(false); |
| 88 |
|
| 89 |
|
| 90 |
setIntercellSpacing(new Dimension(0, 0)); |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
if (tree.getRowHeight() < 1) { |
| 95 |
|
| 96 |
setRowHeight(18); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
@Override |
| 106 |
public void updateUI() { |
| 107 |
super.updateUI(); |
| 108 |
if(tree != null) { |
| 109 |
tree.updateUI(); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
LookAndFeel.installColorsAndFont(this, "Tree.background", |
| 114 |
"Tree.foreground", "Tree.font"); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
@Override |
| 124 |
public int getEditingRow() { |
| 125 |
return (getColumnClass(editingColumn) == TreeTableModel.class) ? -1 : |
| 126 |
editingRow; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
@Override |
| 133 |
public void setRowHeight(int rowHeight) { |
| 134 |
super.setRowHeight(rowHeight); |
| 135 |
if (tree != null && tree.getRowHeight() != rowHeight) { |
| 136 |
tree.setRowHeight(getRowHeight()); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public JTree getTree() { |
| 144 |
return tree; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public class TreeTableCellRenderer extends JTree implements TableCellRenderer { |
| 151 |
private static final long serialVersionUID = -193867880014600717L; |
| 152 |
|
| 153 |
protected int visibleRow; |
| 154 |
|
| 155 |
public TreeTableCellRenderer(TreeModel model) { |
| 156 |
super(model); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
@Override |
| 164 |
public void updateUI() { |
| 165 |
super.updateUI(); |
| 166 |
|
| 167 |
|
| 168 |
TreeCellRenderer tcr = getCellRenderer(); |
| 169 |
if (tcr instanceof DefaultTreeCellRenderer) { |
| 170 |
DefaultTreeCellRenderer dtcr = ((DefaultTreeCellRenderer)tcr); |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
dtcr.setTextSelectionColor(UIManager.getColor |
| 176 |
("Table.selectionForeground")); |
| 177 |
dtcr.setBackgroundSelectionColor(UIManager.getColor |
| 178 |
("Table.selectionBackground")); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
@Override |
| 187 |
public void setRowHeight(int rowHeight) { |
| 188 |
if (rowHeight > 0) { |
| 189 |
super.setRowHeight(rowHeight); |
| 190 |
if (JTreeTable.this != null && |
| 191 |
JTreeTable.this.getRowHeight() != rowHeight) { |
| 192 |
JTreeTable.this.setRowHeight(getRowHeight()); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
@Override |
| 201 |
public void setBounds(int x, int y, int w, int h) { |
| 202 |
super.setBounds(x, 0, w, JTreeTable.this.getHeight()); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
@Override |
| 210 |
public void paint(Graphics g) { |
| 211 |
g.translate(0, -visibleRow * getRowHeight()); |
| 212 |
super.paint(g); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public Component getTableCellRendererComponent(JTable table, |
| 219 |
Object value, |
| 220 |
boolean isSelected, |
| 221 |
boolean hasFocus, |
| 222 |
int row, int column) { |
| 223 |
if(isSelected) |
| 224 |
setBackground(table.getSelectionBackground()); |
| 225 |
else |
| 226 |
setBackground(table.getBackground()); |
| 227 |
|
| 228 |
visibleRow = row; |
| 229 |
return this; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public class TreeTableCellEditor extends AbstractCellEditor implements |
| 239 |
TableCellEditor { |
| 240 |
public Component getTableCellEditorComponent(JTable table, |
| 241 |
Object value, |
| 242 |
boolean isSelected, |
| 243 |
int r, int c) { |
| 244 |
return tree; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
@Override |
| 266 |
public boolean isCellEditable(EventObject e) { |
| 267 |
if (e instanceof MouseEvent) { |
| 268 |
for (int counter = getColumnCount() - 1; counter >= 0; |
| 269 |
counter--) { |
| 270 |
if (getColumnClass(counter) == TreeTableModel.class) { |
| 271 |
MouseEvent me = (MouseEvent)e; |
| 272 |
MouseEvent newME = new MouseEvent(tree, me.getID(), |
| 273 |
me.getWhen(), me.getModifiers(), |
| 274 |
me.getX() - getCellRect(0, counter, true).x, |
| 275 |
me.getY(), me.getClickCount(), |
| 276 |
me.isPopupTrigger()); |
| 277 |
tree.dispatchEvent(newME); |
| 278 |
break; |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
return false; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
public class ListToTreeSelectionModelWrapper |
| 294 |
extends DefaultTreeSelectionModel |
| 295 |
{ |
| 296 |
private static final long serialVersionUID = 8168140829623071131L; |
| 297 |
|
| 298 |
|
| 299 |
protected boolean updatingListSelectionModel; |
| 300 |
|
| 301 |
public ListToTreeSelectionModelWrapper() { |
| 302 |
super(); |
| 303 |
getListSelectionModel().addListSelectionListener |
| 304 |
(createListSelectionListener()); |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
public ListSelectionModel getListSelectionModel() { |
| 313 |
return listSelectionModel; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
@Override |
| 322 |
public void resetRowSelection() { |
| 323 |
if(!updatingListSelectionModel) { |
| 324 |
updatingListSelectionModel = true; |
| 325 |
try { |
| 326 |
super.resetRowSelection(); |
| 327 |
} |
| 328 |
finally { |
| 329 |
updatingListSelectionModel = false; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
protected ListSelectionListener createListSelectionListener() { |
| 343 |
return new ListSelectionHandler(); |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
protected void updateSelectedPathsFromSelectedRows() { |
| 352 |
if(!updatingListSelectionModel) { |
| 353 |
updatingListSelectionModel = true; |
| 354 |
try { |
| 355 |
|
| 356 |
|
| 357 |
int min = listSelectionModel.getMinSelectionIndex(); |
| 358 |
int max = listSelectionModel.getMaxSelectionIndex(); |
| 359 |
|
| 360 |
clearSelection(); |
| 361 |
if(min != -1 && max != -1) { |
| 362 |
for(int counter = min; counter <= max; counter++) { |
| 363 |
if(listSelectionModel.isSelectedIndex(counter)) { |
| 364 |
TreePath selPath = tree.getPathForRow |
| 365 |
(counter); |
| 366 |
|
| 367 |
if(selPath != null) { |
| 368 |
addSelectionPath(selPath); |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
finally { |
| 375 |
updatingListSelectionModel = false; |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
class ListSelectionHandler implements ListSelectionListener { |
| 385 |
public void valueChanged(ListSelectionEvent e) { |
| 386 |
updateSelectedPathsFromSelectedRows(); |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
} |