KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > configbean > customizers > common > MethodTablePanel


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 /*
20  * MethodTablePanel.java
21  *
22  * Created on February 8, 2005, 8:47 AM
23  */

24
25 package org.netbeans.modules.j2ee.sun.share.configbean.customizers.common;
26
27 import java.awt.Color JavaDoc;
28 import java.awt.Component JavaDoc;
29 import java.awt.Dimension JavaDoc;
30 import java.awt.event.MouseEvent JavaDoc;
31 import java.awt.event.MouseMotionAdapter JavaDoc;
32 import java.awt.FontMetrics JavaDoc;
33 import java.awt.GridBagConstraints JavaDoc;
34 import java.awt.GridBagLayout JavaDoc;
35 import java.awt.Insets JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.lang.Math JavaDoc;
40
41 import javax.swing.JComboBox JavaDoc;
42 import javax.swing.JComponent JavaDoc;
43 import javax.swing.JLabel JavaDoc;
44 import javax.swing.JPanel JavaDoc;
45 import javax.swing.JTable JavaDoc;
46 import javax.swing.ListSelectionModel JavaDoc;
47 import javax.swing.DefaultCellEditor JavaDoc;
48 import javax.swing.table.JTableHeader JavaDoc;
49 import javax.swing.table.TableCellEditor JavaDoc;
50 import javax.swing.table.TableCellRenderer JavaDoc;
51 import javax.swing.table.TableColumnModel JavaDoc;
52 import javax.swing.table.TableColumn JavaDoc;
53 import javax.swing.table.TableModel JavaDoc;
54
55
56 /**
57  *
58  * @author Rajeshwar Patil
59  */

60 public abstract class MethodTablePanel extends JPanel JavaDoc {
61
62     private MethodTableModel model ;
63
64     protected TableWithToolTips methodTable;
65     protected javax.swing.JScrollPane JavaDoc tablePane;
66     protected javax.swing.JPanel JavaDoc tablePanel;
67
68
69     /** Creates new form MethodTablePanel */
70     public MethodTablePanel(MethodTableModel model){
71         this.model = model;
72         initComponents();
73     }
74
75
76     /** Creates a new instance of MethodTablePanel */
77     public MethodTablePanel() {
78         this.model = getMethodTableModel();
79         initComponents();
80     }
81
82
83     protected void setData(){
84         methodTable.setModel(model);
85     }
86
87
88     protected abstract MethodTableModel getMethodTableModel();
89     protected abstract String JavaDoc getTablePaneAcsblName();
90     protected abstract String JavaDoc getTablePaneAcsblDesc();
91     protected abstract String JavaDoc getTablePaneToolTip();
92
93
94     //Get the tooltip for the table cell
95
protected String JavaDoc getToolTip(int row, int col){
96         return null;
97     }
98
99
100     //Get the tooltip for the table column header
101
protected String JavaDoc getToolTip(int col){
102         return null;
103     }
104
105
106     protected void initComponents() {
107         //assert(model != null);
108
if(model == null) return;
109         setLayout(new GridBagLayout JavaDoc());
110
111         methodTable = new TableWithToolTips();
112
113         JTableHeader JavaDoc header = methodTable.getTableHeader();
114         ColumnHeaderToolTips tips = new ColumnHeaderToolTips();
115
116         // Assign a tooltip for each of the columns
117
header.addMouseMotionListener(tips);
118
119         methodTable.setModel(model);
120         methodTable.setRowSelectionAllowed(false);
121         methodTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
122
123         GridBagConstraints JavaDoc gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
124         javax.swing.JPanel JavaDoc panel = getPanel();
125         if(panel != null){
126             gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
127             gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
128             gridBagConstraints.insets = new java.awt.Insets JavaDoc(6,6,0,5);
129             add(panel, gridBagConstraints);
130         }
131
132         //adjustColumnWidth(1, false);
133
//adjustColumnWidth(methodTable, 2, "description field template", false); //NOI18N
134
tablePane = new javax.swing.JScrollPane JavaDoc(methodTable);
135         tablePane.setOpaque(true);
136         tablePane.setToolTipText(getTablePaneToolTip());
137         add(tablePane, getTableGridBagConstraints());
138         tablePane.getAccessibleContext().setAccessibleName(getTablePaneAcsblName());
139         tablePane.getAccessibleContext().setAccessibleDescription(getTablePaneAcsblDesc());
140      }
141
142      protected javax.swing.JPanel JavaDoc getPanel() {
143         return null;
144     }
145
146     protected GridBagConstraints JavaDoc getTableGridBagConstraints(){
147         GridBagConstraints JavaDoc gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
148         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
149         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
150         gridBagConstraints.weightx = 1.0;
151         gridBagConstraints.weighty = 1.0;
152         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6,6,5,5);
153         return gridBagConstraints;
154     }
155
156
157     protected void setColumnRenderer(TableCellRenderer JavaDoc renderer, int col){
158         TableColumnModel JavaDoc columnModel = methodTable.getColumnModel();
159         TableColumn JavaDoc column = columnModel.getColumn(col);
160         column.setCellRenderer(renderer);
161     }
162
163
164     protected void setColumnEditor(TableCellEditor JavaDoc editor, int col){
165         TableColumnModel JavaDoc columnModel = methodTable.getColumnModel();
166         TableColumn JavaDoc column = columnModel.getColumn(col);
167         column.setCellEditor(editor);
168     }
169
170
171     //get the column at the given index in the given table
172
protected TableColumn JavaDoc getColumn(int n, JTable JavaDoc table)
173     {
174     return ((n >= 0) && (n < table.getColumnCount()))?
175         table.getColumnModel().getColumn(n) :
176         null;
177     }
178
179
180     /* set column to resonable preferred width */
181     protected void adjustColumnWidth(int c, boolean includeEditor)
182     {
183     adjustColumnWidth(methodTable, c, null, includeEditor);
184     }
185
186
187     protected void adjustColumnWidth(JTable JavaDoc table, int c, Object JavaDoc templateValue, boolean includeEditor)
188     {
189     /* table column */
190     TableColumn JavaDoc tc = getColumn(c, table);
191     if (tc == null) {
192         System.out.println("Invalid column index: " + c); //NOI18N
193
return;
194     }
195
196     /* actual width */
197     int actualWidth = tc.getWidth();
198
199     /* initial preferred column width */
200     //int prefWidth = tc.getPreferredWidth(); // default is 75
201
TableCellRenderer JavaDoc hr = tc.getHeaderRenderer();
202     if (hr == null) { hr = table.getTableHeader().getDefaultRenderer(); }
203     Component JavaDoc hc =
204             hr.getTableCellRendererComponent(table, tc.getHeaderValue(), false, false, 0, c);
205     int prefWidth = hc.getPreferredSize().width;
206
207     /* template width */
208     if (templateValue != null) {
209         TableCellRenderer JavaDoc rend = table.getCellRenderer(0, c);
210             Component JavaDoc comp =
211                 getTableCellRendererComponent(rend, table, templateValue, false, false, 0, c, false);
212         Dimension JavaDoc ps = comp.getPreferredSize();
213         if (ps.width > prefWidth) { prefWidth = ps.width; }
214     }
215
216     /* adjust preferred column width */
217     for (int r = 0; r < methodTable.getRowCount(); r++) {
218
219         /* cell editor */
220         if (includeEditor && table.getModel().isCellEditable(r, c)) {
221             TableCellEditor JavaDoc tce = table.getCellEditor(r, c);
222             if (tce instanceof DefaultCellEditor JavaDoc) {
223                 Component JavaDoc comp = ((DefaultCellEditor JavaDoc)tce).getComponent();
224                 if (comp instanceof JComboBox JavaDoc) {
225                     Dimension JavaDoc ps = comp.getPreferredSize();
226                     if (ps.width > prefWidth) { prefWidth = ps.width; }
227                 }
228         }
229         }
230
231         /* cell value */
232         Object JavaDoc v = table.getValueAt(r, c);
233         if (v != null) {
234             TableCellRenderer JavaDoc rend = table.getCellRenderer(r, c);
235                 Component JavaDoc comp =
236                     getTableCellRendererComponent(rend, table, v, false, false, r, c, false);
237             Dimension JavaDoc ps = comp.getPreferredSize();
238             if (ps.width > prefWidth) { prefWidth = ps.width; }
239         }
240     }
241
242     /* set column width */
243     prefWidth += 4; // right margin offset
244
//prefWidth = prefWidth + 25; // right margin offset
245
tc.setMinWidth(prefWidth);
246         tc.setMaxWidth(prefWidth);
247         tc.setPreferredWidth(prefWidth);
248     //this.sizeColumnsToFit(-1); // <== reset to preferred widths
249
}
250
251
252     public Component JavaDoc getTableCellRendererComponent(TableCellRenderer JavaDoc rend,
253                 JTable JavaDoc tbl, Object JavaDoc val, boolean isSel, boolean focus, int r,
254                     int c, boolean rightAlign) {
255         Component JavaDoc renderer =
256                 rend.getTableCellRendererComponent(tbl, val, isSel, focus, r, c);
257
258         if (renderer instanceof JComponent JavaDoc) {
259         JComponent JavaDoc comp = (JComponent JavaDoc)renderer;
260                 if (comp instanceof JLabel JavaDoc) {
261                     String JavaDoc str = (val == null) ? "" : val.toString();
262                     FontMetrics JavaDoc fm = getFontMetrics(((JLabel JavaDoc)comp).getFont());
263                     String JavaDoc displayedVal = null;
264                     if (rightAlign) {
265                         displayedVal = rightAlignLongText(
266                                             str,
267                                             fm,
268                                             cellWidth(tbl,(JLabel JavaDoc)comp, r, c));
269                     }
270                     else {
271                         displayedVal = leftAlignLongText(
272                                             str,
273                                             fm,
274                                             cellWidth(tbl,(JLabel JavaDoc)comp, r, c));
275                     }
276                     ((JLabel JavaDoc)comp).setText(displayedVal);
277                     if (!displayedVal.equals(str)) {
278                         ((JLabel JavaDoc)comp).setToolTipText(str);
279                     }
280                     else {
281                         ((JLabel JavaDoc)comp).setToolTipText(null);
282                     }
283                 }
284         if (isSel) {
285             comp.setBackground(tbl.getSelectionBackground());
286             comp.setOpaque(true);
287         } else {
288             TableModel JavaDoc model = tbl.getModel();
289             Color JavaDoc bg = model.isCellEditable(r, c)?
290             EnabledBackgroundColor : DisabledBackgroundColor;
291             ///if (((r & 1) == 1) && (model.getColumnCount() > 1)) { bg = brighter(bg, -0.03); }
292
comp.setBackground(bg);
293             comp.setOpaque(true);
294         }
295         } else {
296         System.out.println("Not a JComponent: " + renderer);
297         }
298         return renderer;
299         }
300
301     
302     public int cellWidth(JTable JavaDoc tbl, JLabel JavaDoc label, int r, int c) {
303         int cellWidth = tbl.getCellRect(r, c, false).width;
304         Insets JavaDoc insets = label.getInsets();
305         cellWidth -= insets.left + insets.right;
306         return cellWidth;
307     }
308             
309     
310     
311      /**
312      * Determines if a string is longer than the width of the component that
313      * contains it. If the string is longer, a substring of the original string
314      * will be returned and will appear right aligned in the component
315      * with leading '...'.
316      *
317      * @param str the original text
318      * fm the FontMetrics to use for calculating stringWidth()
319      * width the width of the component that contains original text
320      * @return the substring prepended with '...' that will fit inside
321      * the component
322      * @see Image
323      */

324     public static String JavaDoc rightAlignLongText(String JavaDoc str, FontMetrics JavaDoc fm, int width) {
325         if (str.length() > 0) {
326             int swidth = fm.stringWidth(str);
327             if (width > 0 && swidth > width) {
328                 int i = 0;
329                 while (swidth > width && i < str.length()) {
330                     i += 1;
331                     String JavaDoc test = "..." + str.substring(i); //NOI18N
332
swidth = fm.stringWidth(test);
333                 }
334                 str = "..." + str.substring(i);
335             }
336         }
337         return str;
338     }
339
340
341     /**
342      * Determines if a string is longer than the width of the component that
343      * contains it. If the string is longer, a substring of the original string
344      * will be returned and will appear left aligned in the component
345      * with trailing '...'.
346      *
347      * @param str the original text
348      * fm the FontMetrics to use for calculating stringWidth()
349      * width the width of the component that contains original text
350      * @return the substring with '...' appendage that will fit inside
351      * the component
352      * @see Image
353      */

354     public static String JavaDoc leftAlignLongText(String JavaDoc str, FontMetrics JavaDoc fm, int width) {
355         if (str.length() > 0) {
356             int swidth = fm.stringWidth(str);
357             if (width > 0 && swidth > width) {
358                 int i = 0;
359                 while (swidth > width && i < str.length()) {
360                     i += 1;
361                     String JavaDoc test = str.substring(0, str.length() - i) + "..."; //NOI18N
362
swidth = fm.stringWidth(test);
363                 }
364                 str = str.substring(0, str.length() - i) + "..."; //NOI18N
365
}
366         }
367         return str;
368     }
369     
370
371     public static Color JavaDoc brighter(Color JavaDoc color, double factor) {
372     int r = color.getRed(), g = color.getGreen(), b = color.getBlue();
373     if ((factor > 0.0) && (factor < 1.0)) { // brighter
374
factor = 1.0 - factor;
375         int f = (int)(1.0 / (1.0 - factor));
376         if ((r == 0) && (g == 0) && (b == 0)) {
377         return new Color JavaDoc(f, f, f);
378         } else {
379             if ((r > 0) && (r < f)) { r = f; }
380             if ((g > 0) && (g < f)) { g = f; }
381             if ((b > 0) && (b < f)) { b = f; }
382             return new Color JavaDoc(
383             Math.min((int)(r / factor), 255),
384             Math.min((int)(g / factor), 255),
385             Math.min((int)(b / factor), 255));
386         }
387     } else
388     if ((factor < 0.0) && (factor > -1.0)) { // darker
389
factor = 1.0 + factor;
390         return new Color JavaDoc(
391         Math.max((int)(r * factor),0),
392         Math.max((int)(g * factor),0),
393         Math.max((int)(b * factor),0));
394     }
395     return color;
396     }
397
398
399     public void setTablePanePreferredSize(Dimension JavaDoc dimension){
400         tablePane.setMinimumSize(dimension);
401         tablePane.setPreferredSize(dimension);
402     }
403     
404     
405     public static final Color JavaDoc DisabledBackgroundColor = Color.lightGray;
406     public static final Color JavaDoc EnabledBackgroundColor = Color.white;
407     public static final Color JavaDoc DisabledTextColor = Color.darkGray;
408
409    
410     protected class ColumnHeaderToolTips extends MouseMotionAdapter JavaDoc {
411         // Current column whose tooltip is being displayed.
412
// This variable is used to minimize the calls to setToolTipText().
413
int curCol = -1;
414
415         // Maps TableColumn objects to tooltips
416
Map JavaDoc tips = new HashMap JavaDoc();
417
418         // If tooltip is null, removes any tooltip text.
419
public void setToolTip(TableColumn JavaDoc col, String JavaDoc tooltip) {
420             if (tooltip == null) {
421                 tips.remove(col);
422             } else {
423                 tips.put(col, tooltip);
424             }
425         }
426
427
428         public void mouseMoved(MouseEvent JavaDoc evt) {
429             int col = -1;
430             JTableHeader JavaDoc header = (JTableHeader JavaDoc)evt.getSource();
431             JTable JavaDoc table = header.getTable();
432             TableColumnModel JavaDoc colModel = table.getColumnModel();
433             col = colModel.getColumnIndexAtX(evt.getX());
434
435             // Return if not clicked on any column header
436
if (col >= 0) {
437                 if (col != curCol) {
438                     header.setToolTipText(getToolTip(col));
439                     curCol = col;
440                 }
441             }
442         }
443     }
444
445
446     protected class TableWithToolTips extends FixedHeightJTable {
447        public Component JavaDoc prepareRenderer(TableCellRenderer JavaDoc renderer,
448                                          int rowIndex, int vColIndex) {
449             Component JavaDoc c = super.prepareRenderer(renderer, rowIndex, vColIndex);
450             if (c instanceof JComponent JavaDoc) {
451                 JComponent JavaDoc jc = (JComponent JavaDoc)c;
452                 jc.setToolTipText(getToolTip(rowIndex, vColIndex));
453             }
454             return c;
455         }
456     }
457
458 }
459
Popular Tags