KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > timers > TimeComponentPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.timers;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.Component JavaDoc;
23 import java.awt.Dialog JavaDoc;
24 import java.awt.Point JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.awt.event.MouseEvent JavaDoc;
27 import java.beans.PropertyChangeEvent JavaDoc;
28 import java.beans.PropertyChangeListener JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Collections JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Map JavaDoc;
35 import javax.swing.*;
36 import javax.swing.event.ChangeEvent JavaDoc;
37 import javax.swing.event.ChangeListener JavaDoc;
38 import javax.swing.table.DefaultTableModel JavaDoc;
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 /**
49  *
50  * @author Jan Lahoda
51  */

52 public class TimeComponentPanel extends javax.swing.JPanel JavaDoc implements PropertyChangeListener JavaDoc {
53     
54     /** Creates new form TimeComponentPanel */
55     public TimeComponentPanel() {
56         initComponents();
57         times.addMouseListener(new PopupAdapter());
58         jList1.addMouseListener(new ListPopupAdapter());
59         key2RowNumber = new HashMap JavaDoc<String JavaDoc, Integer JavaDoc>();
60         TimesCollectorPeer.getDefault().addPropertyChangeListener(this);
61         fillIn();
62     }
63     
64     /** This method is called from within the constructor to
65      * initialize the form.
66      * WARNING: Do NOT modify this code. The content of this method is
67      * always regenerated by the Form Editor.
68      */

69 // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
70
private void initComponents() {
71         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
72         
73         jSplitPane1 = new javax.swing.JSplitPane JavaDoc();
74         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
75         jList1 = new javax.swing.JList JavaDoc();
76         jScrollPane2 = new javax.swing.JScrollPane JavaDoc();
77         times = new javax.swing.JTable JavaDoc();
78         
79         setLayout(new java.awt.GridBagLayout JavaDoc());
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 JavaDoc() {
87             public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc 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 JavaDoc(
97                 new Object JavaDoc [][] {
98             
99         },
100                 new String JavaDoc [] {
101             "Name", "Time"
102         }
103         ) {
104             Class JavaDoc[] types = new Class JavaDoc [] {
105                 java.lang.Object JavaDoc.class, java.lang.Long JavaDoc.class
106             };
107             boolean[] canEdit = new boolean [] {
108                 false, false
109             };
110             
111             public Class JavaDoc 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 JavaDoc();
124         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
125         gridBagConstraints.weightx = 1.0;
126         gridBagConstraints.weighty = 1.0;
127         add(jSplitPane1, gridBagConstraints);
128         
129     }
130 // </editor-fold>//GEN-END:initComponents
131

132     private void jList1ValueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {//GEN-FIRST:event_jList1ValueChanged
133
// TODO add your handling code here:
134
fillTimeTable();
135     }//GEN-LAST:event_jList1ValueChanged
136

137     
138 // Variables declaration - do not modify//GEN-BEGIN:variables
139
private javax.swing.JList JavaDoc jList1;
140     private javax.swing.JScrollPane JavaDoc jScrollPane1;
141     private javax.swing.JScrollPane JavaDoc jScrollPane2;
142     private javax.swing.JSplitPane JavaDoc jSplitPane1;
143     private javax.swing.JTable JavaDoc times;
144 // End of variables declaration//GEN-END:variables
145

146     private Map JavaDoc<String JavaDoc, Integer JavaDoc> key2RowNumber;
147     
148     private void fillTimeTable() {
149         FileObject fo = (FileObject) jList1.getSelectedValue();
150         DefaultTableModel JavaDoc model = (DefaultTableModel JavaDoc) times.getModel();
151         
152         while (model.getRowCount() > 0) {
153             model.removeRow(0);
154         }
155         
156         key2RowNumber.clear();
157         
158         if (fo != null) {
159             Collection JavaDoc<String JavaDoc> keys = TimesCollectorPeer.getDefault().getKeysForFile(fo);
160             
161             for (String JavaDoc key : keys) {
162                 changeRow(fo, key);
163             }
164         }
165         
166         Collection JavaDoc<String JavaDoc> keys = TimesCollectorPeer.getDefault().getKeysForFile(fo);
167         
168         for (String JavaDoc key : keys) {
169             changeRow(fo, key);
170         }
171     }
172
173     private TimesCollectorPeer.Description getDescForRow(FileObject fo, int row) {
174         Collection JavaDoc<String JavaDoc> keys = TimesCollectorPeer.getDefault().getKeysForFile(fo);
175         Iterator JavaDoc<String JavaDoc> it = keys.iterator();
176         String JavaDoc 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 JavaDoc key) {
186         Integer JavaDoc row = key2RowNumber.get(key);
187         DefaultTableModel JavaDoc model = (DefaultTableModel JavaDoc) 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 JavaDoc[] {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 JavaDoc evt) {
217         SwingUtilities.invokeLater(new Runnable JavaDoc() {
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 JavaDoc key = (String JavaDoc) 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 JavaDoc objs) {
249         JPanel inner = new JPanel();
250         inner.setLayout(new BorderLayout JavaDoc());
251         JProgressBar bar = new JProgressBar();
252         inner.add(new JLabel("Computing object reachability"), BorderLayout.CENTER);
253         inner.add(bar, BorderLayout.SOUTH);
254         Dialog JavaDoc d = DialogDisplayer.getDefault().createDialog(new DialogDescriptor(
255                 inner, "Please wait"));
256         d.pack();
257         d.setModal(false);
258         d.setVisible(true);
259         
260         String JavaDoc 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 JavaDoc getListCellRendererComponent(JList list, Object JavaDoc 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 JavaDoc 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 JavaDoc e) {
305             int selRow = times.rowAtPoint(e.getPoint());
306
307             if (!times.isRowSelected(selRow)) {
308                 // This will set ExplorerManager selection as well.
309
// If selRow == -1 the selection will be cleared.
310
times.getSelectionModel().setSelectionInterval(selRow, selRow);
311             }
312
313             if (selRow != -1) {
314                 Point JavaDoc 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 JavaDoc 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 JavaDoc e) {
340             int selRow = jList1.locationToIndex(e.getPoint());
341
342             if (!jList1.isSelectedIndex(selRow)) {
343                 // If selRow == -1 the selection will be cleared.
344
jList1.getSelectionModel().setSelectionInterval(selRow, selRow);
345             }
346
347             if (selRow != -1) {
348                 Point JavaDoc 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 JavaDoc arg0) {
359                     try {
360                         FileObject f = fo[0];
361
362                         fo[0] = null;
363                         // hack - DO.find, we'not really interrested in
364
// the FileObject reachability
365
// This will go away for general key type
366
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 JavaDoc getRoots(Collection JavaDoc objects, JProgressBar bar, final JPanel inner) {
381         // scanning intentionally blocks AWT, force repaints
382
bar.getModel().addChangeListener(new ChangeListener JavaDoc() {
383             public void stateChanged(ChangeEvent JavaDoc arg0) {
384                 inner.paintImmediately(inner.getBounds());
385             }
386         });
387         Map JavaDoc/*<Object,Path>*/ traces = LiveReferences.fromRoots(objects, null, bar.getModel());
388         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
389         
390         for (Object JavaDoc 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