KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > java > ui > UIUtilities


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.refactoring.java.ui;
20
21 import java.awt.Component JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import javax.swing.DefaultListCellRenderer JavaDoc;
24 import javax.swing.ImageIcon JavaDoc;
25 import javax.swing.JCheckBox JavaDoc;
26 import javax.swing.JComponent JavaDoc;
27 import javax.swing.JLabel JavaDoc;
28 import javax.swing.JList JavaDoc;
29 import javax.swing.JTable JavaDoc;
30 import javax.swing.UIManager JavaDoc;
31 import javax.swing.border.Border JavaDoc;
32 import javax.swing.border.EmptyBorder JavaDoc;
33 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
34 import javax.swing.table.TableCellRenderer JavaDoc;
35 import javax.swing.table.TableColumn JavaDoc;
36 import org.netbeans.api.java.source.TreePathHandle;
37 import org.netbeans.api.java.source.UiUtils;
38 import org.openide.util.NbBundle;
39
40
41 /** Class containing various utility methods and inner classes
42  * useful when creating refactoring UI.
43  *
44  * @author Martin Matula, Jan Becicka
45  */

46 public final class UIUtilities {
47     // not to be instantiated
48
private UIUtilities() {
49     }
50
51     /** Returns display text for a given Java element.
52      * Covers:
53      * <ul>
54      * <li>class, interface, annotation, enum - returns simple name</li>
55      * <li>method, constructor - returns signature (<code>method(paramType1, paramType2, ...)</code>)</li>
56      * <li>other subclasses of NamedElement - returns name</li>
57      * </ul>
58      * @param element Java element.
59      * @return Display text.
60      */

61     public static String JavaDoc getDisplayText(TreePathHandle element) {
62         return null;
63     }
64     
65     /** Returns icon base (path to the standard icon file without ".gif" extension) for a given
66      * Java element or <code>null</code> if the element does not have a standard icon associated.
67      * @param element Java element.
68      * @return Base name of the icon for the element (or <code>null</code> if no icon associated).
69      */

70     public static String JavaDoc getIconBase(TreePathHandle element) {
71         return null;
72     }
73     
74     /** Returns the same string as passed in or " " if the passed string was an empty string.
75      * This method is used as a workaround for issue #58302.
76      * @param name Original table column name.
77      * @return "Fixed" column name.
78      */

79     public static String JavaDoc getColumnName(String JavaDoc name) {
80         return name == null || name.length() == 0 ? " " : name; // NOI18N
81
}
82     
83     /** Initializes preferred (and eventually maximum) width of a table column based on
84      * the size of its header and the estimated longest value.
85      * @param table Table to adjust the column width for.
86      * @param index Index of the column.
87      * @param longValue Estimated long value for the column.
88      * @param padding Number of pixes for padding.
89      */

90     public static void initColumnWidth(JTable JavaDoc table, int index, Object JavaDoc longValue, int padding) {
91         TableColumn JavaDoc column = table.getColumnModel().getColumn(index);
92         
93         // get preferred size of the header
94
TableCellRenderer JavaDoc headerRenderer = column.getHeaderRenderer();
95         if (headerRenderer == null) {
96             headerRenderer = table.getTableHeader().getDefaultRenderer();
97         }
98         Component JavaDoc comp = headerRenderer.getTableCellRendererComponent(
99                 null, column.getHeaderValue(), false, false, 0, 0);
100         int width = comp.getPreferredSize().width;
101         
102         // get preferred size of the long value (remeber max of the pref. size for header and long value)
103
comp = table.getDefaultRenderer(table.getModel().getColumnClass(index)).getTableCellRendererComponent(
104                 table, longValue, false, false, 0, index);
105         width = Math.max(width, comp.getPreferredSize().width) + 2 * padding;
106         
107         // set preferred width of the column
108
column.setPreferredWidth(width);
109         // if the column contains boolean values, the preferred width
110
// should also be its max width
111
if (longValue instanceof Boolean JavaDoc) {
112             column.setMaxWidth(width);
113         }
114     }
115
116     /** Table cell renderer that renders Java elements (instances of NamedElement and its subtypes).
117      * When rendering the elements it displays element's icon (if available) and display text.
118      */

119     public static class JavaElementTableCellRenderer extends DefaultTableCellRenderer JavaDoc {
120         public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value, boolean isSelected, boolean hasFocus, int row, int column) {
121             super.getTableCellRendererComponent(table, extractText(value), isSelected, hasFocus, row, column);
122             String JavaDoc iconBase = extractIconBase(value);
123             setIcon(iconBase == null ? null : new ImageIcon JavaDoc(org.openide.util.Utilities.loadImage(iconBase + ".gif"))); // NOI18N
124
return this;
125         }
126         
127         /** Can be overriden to return non-standard icons or to return icons for
128          * non-standard elements.
129          * @param value Cell value.
130          * @return Base name of the icon file (without the extension).
131          */

132         protected String JavaDoc extractIconBase(Object JavaDoc value) {
133             if (value instanceof TreePathHandle) {
134                 return getIconBase((TreePathHandle) value);
135             } else {
136                 return null;
137             }
138         }
139         
140         /** Can be overriden to return alter the standard display text returned for elements.
141          * @param value Cell value.
142          * @return Display text.
143          */

144         protected String JavaDoc extractText(Object JavaDoc value) {
145             if (value==null)
146                 return null;
147             if (value instanceof TreePathHandle) {
148                 return getDisplayText((TreePathHandle) value);
149             } else {
150                 return value.toString();
151             }
152         }
153     }
154     
155     /** Table cell renderer that renders Java elements (instances of NamedElement and its subtypes).
156      * When rendering the elements it displays element's icon (if available) and display text.
157      */

158     public static class JavaElementListCellRenderer extends DefaultListCellRenderer JavaDoc {
159         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
160             super.getListCellRendererComponent(list, extractText(value), index, isSelected, cellHasFocus);
161             String JavaDoc iconBase = extractIconBase(value);
162             if (iconBase != null) {
163                 setIcon(new ImageIcon JavaDoc(org.openide.util.Utilities.loadImage(iconBase + ".gif"))); // NOI18N
164
}
165             return this;
166         }
167
168         /** Can be overriden to return non-standard icons or to return icons for
169          * non-standard elements.
170          * @param value Cell value.
171          * @return Base name of the icon file (without the extension).
172          */

173         protected String JavaDoc extractIconBase(Object JavaDoc value) {
174             if (value instanceof TreePathHandle) {
175                 return getIconBase((TreePathHandle) value);
176             } else {
177                 return null;
178             }
179         }
180         
181         /** Can be overriden to return alter the standard display text returned for elements.
182          * @param value Cell value.
183          * @return Display text.
184          */

185         protected String JavaDoc extractText(Object JavaDoc value) {
186             if (value instanceof TreePathHandle) {
187                 return getDisplayText((TreePathHandle) value);
188             } else {
189                 return value.toString();
190             }
191         }
192     }
193
194     /** Table cell renderer for boolean values (a little more advanced that the
195      * standard one). Enables hiding the combo box in case the value is <code>null</code>
196      * rather than <code>Boolean.TRUE</code> or <code>Boolean.FALSE</code>
197      * and disables the combo box for read-only cells to give a better visual feedback
198      * that the cells cannot be edited.
199      */

200     public static class BooleanTableCellRenderer extends JCheckBox JavaDoc implements TableCellRenderer JavaDoc {
201         private static final Border JavaDoc noFocusBorder = new EmptyBorder JavaDoc(1, 1, 1, 1);
202         private final JLabel JavaDoc emptyLabel = new JLabel JavaDoc();
203
204     public BooleanTableCellRenderer() {
205         super();
206         setHorizontalAlignment(JLabel.CENTER);
207             setBorderPainted(true);
208             emptyLabel.setBorder(noFocusBorder);
209             emptyLabel.setOpaque(true);
210     }
211
212         public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value, boolean isSelected, boolean hasFocus, int row, int column) {
213             JComponent JavaDoc result;
214             if (value == null) {
215                 result = emptyLabel;
216             } else {
217                 setSelected(((Boolean JavaDoc)value).booleanValue());
218                 setEnabled(table.getModel().isCellEditable(row, column));
219                 result = this;
220             }
221
222             result.setForeground(isSelected ? table.getSelectionForeground() : table.getForeground());
223             result.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
224             result.setBorder(hasFocus ? UIManager.getBorder("Table.focusCellHighlightBorder") : noFocusBorder); // NOI18N
225

226             return result;
227         }
228     }
229 }
230
Popular Tags