KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > spi > impl > 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.spi.impl;
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.openide.util.NbBundle;
37
38
39 /** Class containing various utility methods and inner classes
40  * useful when creating refactoring UI.
41  *
42  * @author Martin Matula
43  */

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

59     public static String JavaDoc getDisplayText(Object JavaDoc element) {
60 //[retouche] if (element == null || !element.isValid()) {
61
//[retouche] return NbBundle.getMessage(UIUtilities.class, "LBL_NotAvailable");
62
//[retouche] } else if (element instanceof JavaClass) {
63
//[retouche] return ((JavaClass) element).getSimpleName();
64
//[retouche] } else if (element instanceof CallableFeature) {
65
//[retouche] CallableFeature cf = (CallableFeature) element;
66
//[retouche] StringBuffer sb = new StringBuffer(cf.getName());
67
//[retouche] sb.append('('); // NOI18N
68
//[retouche] for (Iterator it = cf.getParameters().iterator(); it.hasNext();) {
69
//[retouche] Parameter param = (Parameter) it.next();
70
//[retouche] sb.append(getDisplayText(param.getType()));
71
//[retouche] if (it.hasNext()) {
72
//[retouche] sb.append(", "); // NOI18N
73
//[retouche]//[retouche] } else if (param.isVarArg()) {
74
//[retouche] sb.append("..."); // NOI18N
75
//[retouche] }
76
//[retouche] }
77
//[retouche] sb.append(')');
78
//[retouche] return sb.toString();
79
//[retouche] } else {
80
//[retouche] return element.getName();
81
//[retouche] }
82
return "getDisplayText not implemented";
83     }
84     
85     /** Returns icon base (path to the standard icon file without ".gif" extension) for a given
86      * Java element or <code>null</code> if the element does not have a standard icon associated.
87      * @param element Java element.
88      * @return Base name of the icon for the element (or <code>null</code> if no icon associated).
89      */

90     public static String JavaDoc getIconBase(Object JavaDoc element) {
91         return "";
92 //[retouche] if (element instanceof Feature) {
93
//[retouche] return IconResolver.getIconBase((Feature) element);
94
//[retouche] } else if (element instanceof Resource) {
95
//[retouche] return IconStrings.RESOURCE;
96
//[retouche] } else if (element instanceof JavaPackage) {
97
//[retouche] return IconStrings.JAVA_PACKAGE;
98
//[retouche] } else {
99
//[retouche] return null;
100
//[retouche] }
101
}
102     
103     /** Returns the same string as passed in or " " if the passed string was an empty string.
104      * This method is used as a workaround for issue #58302.
105      * @param name Original table column name.
106      * @return "Fixed" column name.
107      */

108     public static String JavaDoc getColumnName(String JavaDoc name) {
109         return name == null || name.length() == 0 ? " " : name; // NOI18N
110
}
111     
112     /** Initializes preferred (and eventually maximum) width of a table column based on
113      * the size of its header and the estimated longest value.
114      * @param table Table to adjust the column width for.
115      * @param index Index of the column.
116      * @param longValue Estimated long value for the column.
117      * @param padding Number of pixes for padding.
118      */

119     public static void initColumnWidth(JTable JavaDoc table, int index, Object JavaDoc longValue, int padding) {
120         TableColumn JavaDoc column = table.getColumnModel().getColumn(index);
121         
122         // get preferred size of the header
123
TableCellRenderer JavaDoc headerRenderer = column.getHeaderRenderer();
124         if (headerRenderer == null) {
125             headerRenderer = table.getTableHeader().getDefaultRenderer();
126         }
127         Component JavaDoc comp = headerRenderer.getTableCellRendererComponent(
128                 null, column.getHeaderValue(), false, false, 0, 0);
129         int width = comp.getPreferredSize().width;
130         
131         // get preferred size of the long value (remeber max of the pref. size for header and long value)
132
comp = table.getDefaultRenderer(table.getModel().getColumnClass(index)).getTableCellRendererComponent(
133                 table, longValue, false, false, 0, index);
134         width = Math.max(width, comp.getPreferredSize().width) + 2 * padding;
135         
136         // set preferred width of the column
137
column.setPreferredWidth(width);
138         // if the column contains boolean values, the preferred width
139
// should also be its max width
140
if (longValue instanceof Boolean JavaDoc) {
141             column.setMaxWidth(width);
142         }
143     }
144
145     /** Table cell renderer that renders Java elements (instances of NamedElement and its subtypes).
146      * When rendering the elements it displays element's icon (if available) and display text.
147      */

148     public static class JavaElementTableCellRenderer extends DefaultTableCellRenderer JavaDoc {
149         public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value, boolean isSelected, boolean hasFocus, int row, int column) {
150             super.getTableCellRendererComponent(table, extractText(value), isSelected, hasFocus, row, column);
151             String JavaDoc iconBase = extractIconBase(value);
152             setIcon(iconBase == null ? null : new ImageIcon JavaDoc(org.openide.util.Utilities.loadImage(iconBase + ".gif"))); // NOI18N
153
return this;
154         }
155         
156         /** Can be overriden to return non-standard icons or to return icons for
157          * non-standard elements.
158          * @param value Cell value.
159          * @return Base name of the icon file (without the extension).
160          */

161         protected String JavaDoc extractIconBase(Object JavaDoc value) {
162 //[retouche] if (value instanceof NamedElement) {
163
//[retouche] return getIconBase((NamedElement) value);
164
//[retouche] } else {
165
return null;
166 //[retouche] }
167
}
168         
169         /** Can be overriden to return alter the standard display text returned for elements.
170          * @param value Cell value.
171          * @return Display text.
172          */

173         protected String JavaDoc extractText(Object JavaDoc value) {
174             if (value==null)
175                 return null;
176             if (value instanceof Object JavaDoc) {
177                 return getDisplayText((Object JavaDoc) value);
178             } else {
179                 return value.toString();
180             }
181         }
182     }
183     
184     /** Table cell renderer that renders Java elements (instances of NamedElement and its subtypes).
185      * When rendering the elements it displays element's icon (if available) and display text.
186      */

187     public static class JavaElementListCellRenderer extends DefaultListCellRenderer JavaDoc {
188         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
189             super.getListCellRendererComponent(list, extractText(value), index, isSelected, cellHasFocus);
190             String JavaDoc iconBase = extractIconBase(value);
191             if (iconBase != null) {
192                 setIcon(new ImageIcon JavaDoc(org.openide.util.Utilities.loadImage(iconBase + ".gif"))); // NOI18N
193
}
194             return this;
195         }
196
197         /** Can be overriden to return non-standard icons or to return icons for
198          * non-standard elements.
199          * @param value Cell value.
200          * @return Base name of the icon file (without the extension).
201          */

202         protected String JavaDoc extractIconBase(Object JavaDoc value) {
203             if (value instanceof Object JavaDoc) {
204                 return getIconBase((Object JavaDoc) value);
205             } else {
206                 return null;
207             }
208         }
209         
210         /** Can be overriden to return alter the standard display text returned for elements.
211          * @param value Cell value.
212          * @return Display text.
213          */

214         protected String JavaDoc extractText(Object JavaDoc value) {
215             if (value instanceof Object JavaDoc) {
216                 return getDisplayText((Object JavaDoc) value);
217             } else {
218                 return value.toString();
219             }
220         }
221     }
222
223     /** Table cell renderer for boolean values (a little more advanced that the
224      * standard one). Enables hiding the combo box in case the value is <code>null</code>
225      * rather than <code>Boolean.TRUE</code> or <code>Boolean.FALSE</code>
226      * and disables the combo box for read-only cells to give a better visual feedback
227      * that the cells cannot be edited.
228      */

229     public static class BooleanTableCellRenderer extends JCheckBox JavaDoc implements TableCellRenderer JavaDoc {
230         private static final Border JavaDoc noFocusBorder = new EmptyBorder JavaDoc(1, 1, 1, 1);
231         private final JLabel JavaDoc emptyLabel = new JLabel JavaDoc();
232
233     public BooleanTableCellRenderer() {
234         super();
235         setHorizontalAlignment(JLabel.CENTER);
236             setBorderPainted(true);
237             emptyLabel.setBorder(noFocusBorder);
238             emptyLabel.setOpaque(true);
239     }
240
241         public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value, boolean isSelected, boolean hasFocus, int row, int column) {
242             JComponent JavaDoc result;
243             if (value == null) {
244                 result = emptyLabel;
245             } else {
246                 setSelected(((Boolean JavaDoc)value).booleanValue());
247                 setEnabled(table.getModel().isCellEditable(row, column));
248                 result = this;
249             }
250
251             result.setForeground(isSelected ? table.getSelectionForeground() : table.getForeground());
252             result.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
253             result.setBorder(hasFocus ? UIManager.getBorder("Table.focusCellHighlightBorder") : noFocusBorder); // NOI18N
254

255             return result;
256         }
257     }
258 }
259
Popular Tags