001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.refactoring.java.ui;
042:
043: import java.awt.Component;
044: import javax.swing.DefaultListCellRenderer;
045: import javax.swing.Icon;
046: import javax.swing.JCheckBox;
047: import javax.swing.JComponent;
048: import javax.swing.JLabel;
049: import javax.swing.JList;
050: import javax.swing.JTable;
051: import javax.swing.UIManager;
052: import javax.swing.border.Border;
053: import javax.swing.border.EmptyBorder;
054: import javax.swing.table.DefaultTableCellRenderer;
055: import javax.swing.table.TableCellRenderer;
056: import javax.swing.table.TableColumn;
057: import org.netbeans.modules.refactoring.java.api.MemberInfo;
058:
059: /** Class containing various utility methods and inner classes
060: * useful when creating refactoring UI.
061: *
062: * @author Martin Matula, Jan Becicka
063: */
064: public final class UIUtilities {
065: // not to be instantiated
066: private UIUtilities() {
067: }
068:
069: /** Returns the same string as passed in or " " if the passed string was an empty string.
070: * This method is used as a workaround for issue #58302.
071: * @param name Original table column name.
072: * @return "Fixed" column name.
073: */
074: public static String getColumnName(String name) {
075: return name == null || name.length() == 0 ? " " : name; // NOI18N
076: }
077:
078: /** Initializes preferred (and eventually maximum) width of a table column based on
079: * the size of its header and the estimated longest value.
080: * @param table Table to adjust the column width for.
081: * @param index Index of the column.
082: * @param longValue Estimated long value for the column.
083: * @param padding Number of pixes for padding.
084: */
085: public static void initColumnWidth(JTable table, int index,
086: Object longValue, int padding) {
087: TableColumn column = table.getColumnModel().getColumn(index);
088:
089: // get preferred size of the header
090: TableCellRenderer headerRenderer = column.getHeaderRenderer();
091: if (headerRenderer == null) {
092: headerRenderer = table.getTableHeader()
093: .getDefaultRenderer();
094: }
095: Component comp = headerRenderer.getTableCellRendererComponent(
096: null, column.getHeaderValue(), false, false, 0, 0);
097: int width = comp.getPreferredSize().width;
098:
099: // get preferred size of the long value (remeber max of the pref. size for header and long value)
100: comp = table.getDefaultRenderer(
101: table.getModel().getColumnClass(index))
102: .getTableCellRendererComponent(table, longValue, false,
103: false, 0, index);
104: width = Math.max(width, comp.getPreferredSize().width) + 2
105: * 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) {
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
120: DefaultTableCellRenderer {
121: public Component getTableCellRendererComponent(JTable table,
122: Object value, boolean isSelected, boolean hasFocus,
123: int row, int column) {
124: super .getTableCellRendererComponent(table,
125: extractText(value), isSelected, hasFocus, row,
126: column);
127: if (value instanceof MemberInfo) {
128: Icon i = ((MemberInfo) value).getIcon();
129: setIcon(i);
130: }
131: return this ;
132: }
133:
134: /** Can be overriden to return alter the standard display text returned for elements.
135: * @param value Cell value.
136: * @return Display text.
137: */
138: protected String extractText(Object value) {
139: if (value == null)
140: return null;
141: if (value instanceof MemberInfo) {
142: return ((MemberInfo) value).getHtmlText();
143: } else {
144: return value.toString();
145: }
146: }
147: }
148:
149: /** Table cell renderer that renders Java elements (instances of NamedElement and its subtypes).
150: * When rendering the elements it displays element's icon (if available) and display text.
151: */
152: public static class JavaElementListCellRenderer extends
153: DefaultListCellRenderer {
154: public Component getListCellRendererComponent(JList list,
155: Object value, int index, boolean isSelected,
156: boolean cellHasFocus) {
157: super
158: .getListCellRendererComponent(list,
159: extractText(value), index, isSelected,
160: cellHasFocus);
161: if (value instanceof MemberInfo) {
162: Icon i = ((MemberInfo) value).getIcon();
163: setIcon(i);
164: }
165: return this ;
166: }
167:
168: /** Can be overriden to return alter the standard display text returned for elements.
169: * @param value Cell value.
170: * @return Display text.
171: */
172: protected String extractText(Object value) {
173: if (value instanceof MemberInfo) {
174: return ((MemberInfo) value).getHtmlText();
175: } else {
176: return value.toString();
177: }
178: }
179: }
180:
181: /** Table cell renderer for boolean values (a little more advanced that the
182: * standard one). Enables hiding the combo box in case the value is <code>null</code>
183: * rather than <code>Boolean.TRUE</code> or <code>Boolean.FALSE</code>
184: * and disables the combo box for read-only cells to give a better visual feedback
185: * that the cells cannot be edited.
186: */
187: public static class BooleanTableCellRenderer extends JCheckBox
188: implements TableCellRenderer {
189: private static final Border noFocusBorder = new EmptyBorder(1,
190: 1, 1, 1);
191: private final JLabel emptyLabel = new JLabel();
192:
193: public BooleanTableCellRenderer() {
194: super ();
195: setHorizontalAlignment(JLabel.CENTER);
196: setBorderPainted(true);
197: emptyLabel.setBorder(noFocusBorder);
198: emptyLabel.setOpaque(true);
199: }
200:
201: public Component getTableCellRendererComponent(JTable table,
202: Object value, boolean isSelected, boolean hasFocus,
203: int row, int column) {
204: JComponent result;
205: if (value == null) {
206: result = emptyLabel;
207: } else {
208: setSelected(((Boolean) value).booleanValue());
209: setEnabled(table.getModel().isCellEditable(row, column));
210: result = this ;
211: }
212:
213: result.setForeground(isSelected ? table
214: .getSelectionForeground() : table.getForeground());
215: result.setBackground(isSelected ? table
216: .getSelectionBackground() : table.getBackground());
217: result.setBorder(hasFocus ? UIManager
218: .getBorder("Table.focusCellHighlightBorder")
219: : noFocusBorder); // NOI18N
220:
221: return result;
222: }
223: }
224: }
|