1 19 package org.netbeans.modules.timers; 20 21 import java.awt.BorderLayout ; 22 import java.awt.Component ; 23 import java.awt.Dialog ; 24 import java.awt.Point ; 25 import java.awt.event.ActionEvent ; 26 import java.awt.event.MouseEvent ; 27 import java.beans.PropertyChangeEvent ; 28 import java.beans.PropertyChangeListener ; 29 import java.io.IOException ; 30 import java.util.Collection ; 31 import java.util.Collections ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.Map ; 35 import javax.swing.*; 36 import javax.swing.event.ChangeEvent ; 37 import javax.swing.event.ChangeListener ; 38 import javax.swing.table.DefaultTableModel ; 39 import org.netbeans.insane.live.LiveReferences; 40 import org.openide.DialogDescriptor; 41 import org.openide.explorer.view.NodeRenderer; 42 import org.openide.filesystems.FileObject; 43 import org.openide.loaders.DataObject; 44 import org.openide.loaders.DataObjectNotFoundException; 45 import org.openide.nodes.Node; 46 import org.openide.DialogDisplayer; 47 48 52 public class TimeComponentPanel extends javax.swing.JPanel implements PropertyChangeListener { 53 54 55 public TimeComponentPanel() { 56 initComponents(); 57 times.addMouseListener(new PopupAdapter()); 58 jList1.addMouseListener(new ListPopupAdapter()); 59 key2RowNumber = new HashMap <String , Integer >(); 60 TimesCollectorPeer.getDefault().addPropertyChangeListener(this); 61 fillIn(); 62 } 63 64 69 private void initComponents() { 71 java.awt.GridBagConstraints gridBagConstraints; 72 73 jSplitPane1 = new javax.swing.JSplitPane (); 74 jScrollPane1 = new javax.swing.JScrollPane (); 75 jList1 = new javax.swing.JList (); 76 jScrollPane2 = new javax.swing.JScrollPane (); 77 times = new javax.swing.JTable (); 78 79 setLayout(new java.awt.GridBagLayout ()); 80 81 jSplitPane1.setDividerLocation(100); 82 jSplitPane1.setDividerSize(8); 83 jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); 84 jList1.setModel(new DefaultListModel()); 85 jList1.setCellRenderer(new FileObjectListRenderer()); 86 jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener () { 87 public void valueChanged(javax.swing.event.ListSelectionEvent evt) { 88 jList1ValueChanged(evt); 89 } 90 }); 91 92 jScrollPane1.setViewportView(jList1); 93 94 jSplitPane1.setLeftComponent(jScrollPane1); 95 96 times.setModel(new javax.swing.table.DefaultTableModel ( 97 new Object [][] { 98 99 }, 100 new String [] { 101 "Name", "Time" 102 } 103 ) { 104 Class [] types = new Class [] { 105 java.lang.Object .class, java.lang.Long .class 106 }; 107 boolean[] canEdit = new boolean [] { 108 false, false 109 }; 110 111 public Class getColumnClass(int columnIndex) { 112 return types [columnIndex]; 113 } 114 115 public boolean isCellEditable(int rowIndex, int columnIndex) { 116 return canEdit [columnIndex]; 117 } 118 }); 119 jScrollPane2.setViewportView(times); 120 121 jSplitPane1.setRightComponent(jScrollPane2); 122 123 gridBagConstraints = new java.awt.GridBagConstraints (); 124 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 125 gridBagConstraints.weightx = 1.0; 126 gridBagConstraints.weighty = 1.0; 127 add(jSplitPane1, gridBagConstraints); 128 129 } 130 132 private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) { fillTimeTable(); 135 } 137 138 private javax.swing.JList jList1; 140 private javax.swing.JScrollPane jScrollPane1; 141 private javax.swing.JScrollPane jScrollPane2; 142 private javax.swing.JSplitPane jSplitPane1; 143 private javax.swing.JTable times; 144 146 private Map <String , Integer > key2RowNumber; 147 148 private void fillTimeTable() { 149 FileObject fo = (FileObject) jList1.getSelectedValue(); 150 DefaultTableModel model = (DefaultTableModel ) times.getModel(); 151 152 while (model.getRowCount() > 0) { 153 model.removeRow(0); 154 } 155 156 key2RowNumber.clear(); 157 158 if (fo != null) { 159 Collection <String > keys = TimesCollectorPeer.getDefault().getKeysForFile(fo); 160 161 for (String key : keys) { 162 changeRow(fo, key); 163 } 164 } 165 166 Collection <String > keys = TimesCollectorPeer.getDefault().getKeysForFile(fo); 167 168 for (String key : keys) { 169 changeRow(fo, key); 170 } 171 } 172 173 private TimesCollectorPeer.Description getDescForRow(FileObject fo, int row) { 174 Collection <String > keys = TimesCollectorPeer.getDefault().getKeysForFile(fo); 175 Iterator <String > it = keys.iterator(); 176 String key = null; 177 for (int i= 0; i<=row; i++) { 178 assert (it.hasNext()); 179 key = it.next(); 180 } 181 return TimesCollectorPeer.getDefault().getDescription(fo, key); 182 } 183 184 185 private void changeRow(FileObject fo, String key) { 186 Integer row = key2RowNumber.get(key); 187 DefaultTableModel model = (DefaultTableModel ) times.getModel(); 188 189 if (row != null) { 190 model.removeRow(row); 191 } 192 193 TimesCollectorPeer.Description desc = TimesCollectorPeer.getDefault().getDescription(fo, key); 194 195 if (desc == null) { 196 return ; 197 } 198 199 if (row == null) { 200 key2RowNumber.put(key, row = model.getRowCount()); 201 } 202 203 model.insertRow(row, new Object [] {desc.getMessage(), desc.getTime()}); 204 } 205 206 private void fillIn() { 207 DefaultListModel model = (DefaultListModel) jList1.getModel(); 208 209 model.removeAllElements(); 210 211 for (FileObject f : TimesCollectorPeer.getDefault().getFiles()) { 212 model.addElement(f); 213 } 214 } 215 216 public void propertyChange(final PropertyChangeEvent evt) { 217 SwingUtilities.invokeLater(new Runnable () { 218 public void run() { 219 if ("fos".equals(evt.getPropertyName())) { 220 DefaultListModel model = (DefaultListModel) jList1.getModel(); 221 222 if (evt.getNewValue() != null) { 223 model.addElement(evt.getNewValue()); 224 } else { 225 fillIn(); 226 } 227 } 228 229 if ("PROP".equals(evt.getPropertyName())) { 230 FileObject fo = (FileObject) evt.getOldValue(); 231 String key = (String ) evt.getNewValue(); 232 233 if (fo == jList1.getSelectedValue() || fo == null) { 234 changeRow(fo, key); 235 } 236 } 237 238 if ("selected".equals(evt.getPropertyName())) { 239 FileObject fo = (FileObject) evt.getNewValue(); 240 241 jList1.setSelectedValue(fo, true); 242 } 243 } 244 }); 245 } 246 247 248 private static void dumpRoots(Collection objs) { 249 JPanel inner = new JPanel(); 250 inner.setLayout(new BorderLayout ()); 251 JProgressBar bar = new JProgressBar(); 252 inner.add(new JLabel("Computing object reachability"), BorderLayout.CENTER); 253 inner.add(bar, BorderLayout.SOUTH); 254 Dialog d = DialogDisplayer.getDefault().createDialog(new DialogDescriptor( 255 inner, "Please wait")); 256 d.pack(); 257 d.setModal(false); 258 d.setVisible(true); 259 260 String report = getRoots(objs, bar, inner); 261 262 inner.removeAll(); 263 JScrollPane pane = new JScrollPane(); 264 JTextArea editor = new JTextArea(report); 265 editor.setColumns(80); 266 editor.setEditable(false); 267 pane.setViewportView(editor); 268 inner.add(pane, BorderLayout.CENTER); 269 d.setSize(Math.min(600, editor.getPreferredSize().width+30), Math.min(400, editor.getPreferredSize().height + 70)); 270 d.invalidate(); 271 d.validate(); 272 d.repaint(); 273 } 274 275 private static class FileObjectListRenderer extends DefaultListCellRenderer { 276 private NodeRenderer r; 277 278 private FileObjectListRenderer() { 279 r = new NodeRenderer(); 280 } 281 282 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 283 if (value instanceof FileObject) { 284 try { 285 FileObject fo = (FileObject) value; 286 DataObject od = DataObject.find(fo); 287 Node node = od.getNodeDelegate(); 288 289 return r.getListCellRendererComponent(list, node, index, isSelected, cellHasFocus); 290 } catch (IOException e) { 291 value = "<error>"; 292 } 293 } 294 295 return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 296 } 297 298 } 299 300 class PopupAdapter extends org.openide.awt.MouseUtils.PopupMouseAdapter { 301 PopupAdapter() { 302 } 303 304 protected void showPopup(MouseEvent e) { 305 int selRow = times.rowAtPoint(e.getPoint()); 306 307 if (!times.isRowSelected(selRow)) { 308 times.getSelectionModel().setSelectionInterval(selRow, selRow); 311 } 312 313 if (selRow != -1) { 314 Point p = SwingUtilities.convertPoint(e.getComponent(), e.getX(), e.getY(), TimeComponentPanel.this); 315 createPopup((int) p.getX(), (int) p.getY(), selRow); 316 } 317 } 318 319 void createPopup(int x, int y, int row) { 320 TimesCollectorPeer.Description desc = getDescForRow((FileObject) jList1.getSelectedValue(), row); 321 if (!(desc instanceof TimesCollectorPeer.ObjectCountDescripton)) return; 322 323 final TimesCollectorPeer.ObjectCountDescripton oc = (TimesCollectorPeer.ObjectCountDescripton) desc; 324 JPopupMenu popup = new JPopupMenu(); 325 popup.add(new AbstractAction("Find refs") { 326 327 public void actionPerformed(ActionEvent arg0) { 328 dumpRoots(oc.getInstances()); 329 } 330 }); 331 popup.show(TimeComponentPanel.this, x, y); 332 } 333 } 334 335 class ListPopupAdapter extends org.openide.awt.MouseUtils.PopupMouseAdapter { 336 ListPopupAdapter() { 337 } 338 339 protected void showPopup(MouseEvent e) { 340 int selRow = jList1.locationToIndex(e.getPoint()); 341 342 if (!jList1.isSelectedIndex(selRow)) { 343 jList1.getSelectionModel().setSelectionInterval(selRow, selRow); 345 } 346 347 if (selRow != -1) { 348 Point p = SwingUtilities.convertPoint(e.getComponent(), e.getX(), e.getY(), TimeComponentPanel.this); 349 createPopup((int) p.getX(), (int) p.getY(), selRow); 350 } 351 } 352 353 void createPopup(int x, int y, int row) { 354 final FileObject[] fo = new FileObject[] {(FileObject) jList1.getSelectedValue()}; 355 JPopupMenu popup = new JPopupMenu(); 356 popup.add(new AbstractAction("Find refs") { 357 358 public void actionPerformed(ActionEvent arg0) { 359 try { 360 FileObject f = fo[0]; 361 362 fo[0] = null; 363 dumpRoots(Collections.singleton(DataObject.find(f))); 367 } 368 catch (DataObjectNotFoundException ex) { 369 java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, 370 ex.getMessage(), 371 ex); 372 } 373 } 374 }); 375 popup.show(TimeComponentPanel.this, x, y); 376 } 377 } 378 379 380 private static String getRoots(Collection objects, JProgressBar bar, final JPanel inner) { 381 bar.getModel().addChangeListener(new ChangeListener () { 383 public void stateChanged(ChangeEvent arg0) { 384 inner.paintImmediately(inner.getBounds()); 385 } 386 }); 387 Map traces = LiveReferences.fromRoots(objects, null, bar.getModel()); 388 StringBuffer sb = new StringBuffer (); 389 390 for (Object inst : traces.keySet()) { 391 sb.append(inst); 392 sb.append(":\n"); 393 sb.append(traces.get(inst)); 394 sb.append("\n\n"); 395 } 396 397 return sb.toString(); 398 } 399 } 400 | Popular Tags |