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-2007 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 com.sun.jsfcl.std.property;
042:
043: import java.awt.Color;
044: import java.awt.Component;
045: import java.awt.Dimension;
046: import java.awt.Font;
047: import javax.swing.Box;
048: import javax.swing.Box.Filler;
049: import javax.swing.BoxLayout;
050: import javax.swing.JLabel;
051: import javax.swing.JList;
052: import javax.swing.JPanel;
053: import javax.swing.ListCellRenderer;
054: import com.sun.jsfcl.std.reference.ReferenceDataItem;
055:
056: /**
057: * @author eric
058: *
059: * TODO To change the template for this generated type comment go to
060: * Window - Preferences - Java - Code Generation - Code and Comments
061: */
062: public class ReferenceDataTwoColumnListCellRenderer extends JPanel
063: implements ListCellRenderer {
064: protected static final int COLUMN_GAP = 5;
065: protected static final Dimension MAX_DIMENSION = new Dimension(
066: Short.MAX_VALUE, 0);
067: protected static final Dimension MIN_DIMENSION = new Dimension(0, 0);
068: protected static final Dimension COLUMN_GAP_DIMENSION = new Dimension(
069: COLUMN_GAP, 0);
070:
071: protected int leftColumnWidth = -1;
072: protected JLabel leftLabel;
073: protected boolean wantsSecondLabel = true;
074: protected JLabel rightLabel;
075:
076: public static String[] getLabels(ReferenceDataItem item) {
077:
078: String itemNameString = item.getName();
079: Object itemValue = item.getValue();
080: String itemValueString = " "; // NOI18N
081: if (itemValue instanceof String) {
082: itemValueString = (String) itemValue;
083: if (itemValueString.equals(itemNameString)) {
084: itemValueString = " "; //NOI18N
085: } else {
086: itemValueString = "[" + itemValueString + "]"; // NOI18N
087: }
088: }
089: return new String[] { itemNameString, itemValueString };
090: }
091:
092: public ReferenceDataTwoColumnListCellRenderer() {
093:
094: leftLabel = new JLabel();
095: leftLabel.setOpaque(true);
096: rightLabel = new JLabel();
097: rightLabel.setOpaque(true);
098:
099: setLayout(new BoxLayout(this , BoxLayout.LINE_AXIS));
100: // add(Box.createRigidArea(new Dimension(2, 0)));
101: add(leftLabel);
102: add(new Box.Filler(null, null, null) {
103: public Dimension getMinimumSize() {
104:
105: if (wantsSecondLabel) {
106: return COLUMN_GAP_DIMENSION;
107: }
108: return MIN_DIMENSION;
109: }
110:
111: public Dimension getPreferredSize() {
112:
113: if (wantsSecondLabel) {
114: if (leftColumnWidth != -1) {
115: return new Dimension(Math.max(leftColumnWidth
116: + COLUMN_GAP
117: - (int) leftLabel.getPreferredSize()
118: .getWidth(), COLUMN_GAP), 0);
119: }
120: return COLUMN_GAP_DIMENSION;
121: }
122: return MIN_DIMENSION;
123: }
124:
125: public Dimension getMaximumSize() {
126:
127: if (wantsSecondLabel) {
128: if (leftColumnWidth == -1) {
129: return MAX_DIMENSION;
130: }
131: return new Dimension(Math.max(leftColumnWidth
132: + COLUMN_GAP
133: - (int) leftLabel.getPreferredSize()
134: .getWidth(), COLUMN_GAP), 0);
135: }
136: return MIN_DIMENSION;
137: }
138: });
139: add(rightLabel);
140: // add(Box.createRigidArea(new Dimension(2, 0)));
141: setOpaque(true);
142: }
143:
144: public void adjustLeftColumnWidthIfNecessary() {
145:
146: int w = (int) leftLabel.getPreferredSize().getWidth();
147: if (w > leftColumnWidth) {
148: leftColumnWidth = w;
149: invalidate();
150: }
151: }
152:
153: /* (non-Javadoc)
154: * @see javax.swing.ListCellRenderer#getListCellRendererComponent(javax.swing.JList, java.lang.Object, int, boolean, boolean)
155: */
156: public Component getListCellRendererComponent(JList list,
157: Object value, int index, boolean isSelected,
158: boolean cellHasFocus) {
159: ReferenceDataItem item;
160:
161: //Get the selected index. (The index param isn't
162: //always valid, so just use the value.)
163: item = (ReferenceDataItem) value;
164: Color backgroundColor, foregroundColor;
165: if (isSelected) {
166: backgroundColor = list.getSelectionBackground();
167: foregroundColor = list.getSelectionForeground();
168: } else {
169: backgroundColor = list.getBackground();
170: foregroundColor = list.getForeground();
171: }
172: setBackground(backgroundColor);
173: leftLabel.setBackground(backgroundColor);
174: rightLabel.setBackground(backgroundColor);
175: setForeground(foregroundColor);
176: leftLabel.setForeground(foregroundColor);
177: rightLabel.setForeground(foregroundColor);
178:
179: Font font = list.getFont();
180: setFont(font);
181: leftLabel.setFont(font);
182: rightLabel.setFont(font);
183:
184: String[] labels = getLabels(item);
185: leftLabel.setText(labels[0]);
186: rightLabel.setText(labels[1]);
187: invalidate();
188: return this ;
189: }
190:
191: public void resetLeftColumnWidth() {
192:
193: leftColumnWidth = -1;
194: }
195:
196: }
|