KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > suggestions > ui > SuggestionsTable


1 package org.netbeans.modules.tasklist.suggestions.ui;
2
3 import java.awt.event.MouseAdapter JavaDoc;
4 import java.awt.event.MouseEvent JavaDoc;
5 import java.beans.PropertyVetoException JavaDoc;
6 import javax.swing.Action JavaDoc;
7 import javax.swing.JPopupMenu JavaDoc;
8 import javax.swing.JTable JavaDoc;
9 import javax.swing.event.ListSelectionListener JavaDoc;
10 import javax.swing.table.TableColumn JavaDoc;
11 import org.netbeans.modules.tasklist.client.Suggestion;
12 import org.netbeans.modules.tasklist.core.editors.PriorityTableCellRenderer;
13 import org.netbeans.modules.tasklist.suggestions.SuggestionImpl;
14 import org.netbeans.modules.tasklist.suggestions.SuggestionNode;
15 import org.openide.ErrorManager;
16 import org.openide.awt.MouseUtils;
17 import org.openide.explorer.ExplorerManager;
18 import org.openide.nodes.Node;
19 import org.openide.text.Line;
20 import org.openide.util.Utilities;
21
22 /**
23  * A table for suggestions.
24  *
25  * @author tl
26  */

27 public class SuggestionsTable extends JTable JavaDoc {
28     /**
29      * Creates a new instance of SuggestionsTable
30      */

31     public SuggestionsTable() {
32         super(new SuggestionsTableModel());
33         
34         addMouseListener(new MouseUtils.PopupMouseAdapter() {
35             public void showPopup(MouseEvent JavaDoc e) {
36                 int row = rowAtPoint(e.getPoint());
37                 int col = columnAtPoint(e.getPoint());
38                 Action JavaDoc[] actions;
39                 if (row < 0 || col < 0)
40                     return;
41
42                 if (!getSelectionModel().isSelectedIndex(row)) {
43                     setRowSelectionInterval(row, row);
44                 }
45                 Node n = createNode(row);
46
47                 actions = n.getActions(false);
48
49                 JPopupMenu JavaDoc pm = Utilities.actionsToPopup(actions,
50                     SuggestionsTable.this);
51                 if(pm != null)
52                     pm.show(SuggestionsTable.this, e.getX(), e.getY());
53             }
54         });
55         
56         getSelectionModel().addListSelectionListener(
57             new ListSelectionListener JavaDoc() {
58                 public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc e) {
59                     int row = getSelectedRow();
60                     ExplorerManager em = ExplorerManager.find(
61                         SuggestionsTable.this);
62                     if (em == null)
63                         return;
64                     try {
65                         if (row < 0)
66                             em.setSelectedNodes(new Node[0]);
67                         else {
68                             Node sn = createNode(row);
69                             em.setRootContext(sn);
70                             em.setSelectedNodes(new Node[] {sn});
71                         }
72                     } catch (PropertyVetoException JavaDoc ex) {
73                         ErrorManager.getDefault().notify(ex);
74                     }
75                 }
76             }
77         );
78         addMouseListener(new MouseAdapter JavaDoc() {
79             public void mouseClicked(MouseEvent JavaDoc e) {
80                 if (e.getClickCount() != 2)
81                     return;
82                 JTable JavaDoc tt = (JTable JavaDoc) e.getSource();
83                 int row = tt.rowAtPoint(e.getPoint());
84                 if (row < 0)
85                     return;
86                 Suggestion s =
87                     ((SuggestionsTableModel) tt.getModel()).getSuggestion(row);
88                 Line l = s.getLine();
89                 if (l != null)
90                     l.show(Line.SHOW_GOTO);
91             }
92         });
93         
94         setAutoResizeMode(AUTO_RESIZE_OFF);
95         
96         TableColumn JavaDoc tc = getColumnModel().getColumn(
97                 SuggestionsTableModel.Columns.TYPE.ordinal());
98         tc.setCellRenderer(new CategoryTableCellRenderer());
99         tc.setPreferredWidth(100);
100         
101         tc = getColumnModel().getColumn(
102                 SuggestionsTableModel.Columns.PRIORITY.ordinal());
103         tc.setPreferredWidth(60);
104         tc.setCellRenderer(new PriorityTableCellRenderer());
105
106         tc = getColumnModel().getColumn(
107                 SuggestionsTableModel.Columns.SUMMARY.ordinal());
108         tc.setPreferredWidth(400);
109         
110         tc = getColumnModel().getColumn(
111                 SuggestionsTableModel.Columns.DETAILS.ordinal());
112         tc.setPreferredWidth(300);
113         
114         tc = getColumnModel().getColumn(
115                 SuggestionsTableModel.Columns.FILE.ordinal());
116         tc.setPreferredWidth(200);
117         
118         tc = getColumnModel().getColumn(
119                 SuggestionsTableModel.Columns.LINE.ordinal());
120         tc.setPreferredWidth(40);
121     }
122     
123     /**
124      * Creates a Node for the specified row.
125      *
126      * @param row row number
127      * @return Node for this row
128      */

129     private Node createNode(int row) {
130         Suggestion s =
131             ((SuggestionsTableModel) getModel()).getSuggestion(row);
132         return new SuggestionNode((SuggestionImpl) s);
133     }
134 }
135
Popular Tags