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.vmd.game.editor.scene;
042:
043: import java.awt.Color;
044: import java.awt.Component;
045: import java.awt.Dimension;
046: import java.awt.event.ActionEvent;
047: import java.awt.event.ActionListener;
048: import java.awt.event.ItemEvent;
049: import java.awt.event.ItemListener;
050: import java.net.MalformedURLException;
051: import java.util.EventObject;
052:
053: import javax.swing.ImageIcon;
054: import javax.swing.JCheckBox;
055: import javax.swing.JTable;
056: import javax.swing.SwingConstants;
057: import javax.swing.UIManager;
058: import javax.swing.border.Border;
059: import javax.swing.border.EmptyBorder;
060: import javax.swing.event.CellEditorListener;
061: import javax.swing.event.EventListenerList;
062: import javax.swing.table.TableCellEditor;
063: import javax.swing.table.TableCellRenderer;
064:
065: import org.netbeans.modules.vmd.game.model.adapter.SceneLayerTableAdapter;
066:
067: public class BooleanTableCellRenderer extends JCheckBox implements
068: TableCellRenderer, TableCellEditor, ActionListener,
069: ItemListener {
070:
071: protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
072: private Color unselectedForeground;
073: private Color unselectedBackground;
074:
075: private ImageIcon iconVisible;
076: private ImageIcon iconInvisible;
077: private ImageIcon iconLocked;
078: private ImageIcon iconUnlocked;
079:
080: protected EventListenerList listenerList = new EventListenerList();
081:
082: public BooleanTableCellRenderer(int padX, int padY) {
083: try {
084: this .initIcons();
085: } catch (MalformedURLException e) {
086: e.printStackTrace();
087: }
088: this .addActionListener(this );
089: this .addItemListener(this );
090: this .setBackground(Color.WHITE);
091: Dimension d = new Dimension(this .iconVisible.getIconWidth()
092: + padX, this .iconVisible.getIconHeight() + padY);
093: this .setPreferredSize(d);
094: this .setHorizontalAlignment(SwingConstants.CENTER);
095: // this.setMaximumSize(d);
096: }
097:
098: private void initIcons() throws MalformedURLException {
099: this .iconVisible = new ImageIcon(this .getClass().getResource(
100: "res/visible.png")); // NOI18N
101: this .iconInvisible = new ImageIcon(this .getClass().getResource(
102: "res/invisible.png")); // NOI18N
103: this .iconLocked = new ImageIcon(this .getClass().getResource(
104: "res/lock.png")); // NOI18N
105: this .iconUnlocked = new ImageIcon(this .getClass().getResource(
106: "res/unlock.png")); // NOI18N
107: }
108:
109: public Component getTableCellRendererComponent(JTable table,
110: final Object value, boolean isSelected, boolean hasFocus,
111: int row, int column) {
112: if (value instanceof Boolean) {
113: this .setImages(column);
114: this .setSelected(((Boolean) value).booleanValue());
115: if (isSelected) {
116: super .setForeground(table.getSelectionForeground());
117: super .setBackground(table.getSelectionBackground());
118: } else {
119: super
120: .setForeground((unselectedForeground != null) ? unselectedForeground
121: : table.getForeground());
122: super
123: .setBackground((unselectedBackground != null) ? unselectedBackground
124: : table.getBackground());
125: }
126:
127: setFont(table.getFont());
128:
129: if (hasFocus) {
130: setBorder(UIManager
131: .getBorder("Table.focusCellHighlightBorder")); // NOI18N
132: if (!isSelected && table.isCellEditable(row, column)) {
133: Color col;
134: col = UIManager
135: .getColor("Table.focusCellForeground"); // NOI18N
136: if (col != null) {
137: super .setForeground(col);
138: }
139: col = UIManager
140: .getColor("Table.focusCellBackground"); // NOI18N
141: if (col != null) {
142: super .setBackground(col);
143: }
144: }
145: } else {
146: setBorder(noFocusBorder);
147: }
148: return this ;
149: } else {
150: throw new IllegalArgumentException(
151: "Only Boolean can be rendered."); // NOI18N
152: }
153: }
154:
155: public Component getTableCellEditorComponent(final JTable table,
156: final Object value, boolean isSelected, final int row,
157: final int column) {
158: if (value instanceof Boolean) {
159: this .setImages(column);
160: this .setSelected(((Boolean) value).booleanValue());
161: return this ;
162: }
163: throw new IllegalArgumentException(
164: "Only Boolean can be edited."); // NOI18N
165: }
166:
167: private void setImages(int col) {
168: if (col == SceneLayerTableAdapter.COL_INDEX_LAYER_LOCK_INDICATOR) {
169: this .setSelectedIcon(iconLocked);
170: this .setIcon(iconUnlocked);
171: } else {
172: this .setSelectedIcon(iconVisible);
173: this .setIcon(iconInvisible);
174: }
175: }
176:
177: public void addCellEditorListener(CellEditorListener listener) {
178: listenerList.add(CellEditorListener.class, listener);
179: }
180:
181: public void removeCellEditorListener(CellEditorListener listener) {
182: listenerList.remove(CellEditorListener.class, listener);
183: }
184:
185: protected void fireEditingStopped() {
186: CellEditorListener listener;
187: Object[] listeners = listenerList.getListenerList();
188: for (int i = 0; i < listeners.length; i++) {
189: if (listeners[i] == CellEditorListener.class) {
190: listener = (CellEditorListener) listeners[i + 1];
191: listener.editingStopped(changeEvent);
192: }
193: }
194: }
195:
196: protected void fireEditingCanceled() {
197: CellEditorListener listener;
198: Object[] listeners = listenerList.getListenerList();
199: for (int i = 0; i < listeners.length; i++) {
200: if (listeners[i] == CellEditorListener.class) {
201: listener = (CellEditorListener) listeners[i + 1];
202: listener.editingCanceled(changeEvent);
203: }
204: }
205: }
206:
207: public void cancelCellEditing() {
208: fireEditingCanceled();
209: }
210:
211: public boolean stopCellEditing() {
212: fireEditingStopped();
213: return true;
214: }
215:
216: public Object getCellEditorValue() {
217: return new Boolean(this .isSelected());
218: }
219:
220: public boolean isCellEditable(EventObject anEvent) {
221: return true;
222: }
223:
224: public boolean shouldSelectCell(EventObject anEvent) {
225: return true;
226: }
227:
228: public void actionPerformed(ActionEvent e) {
229: this .fireEditingStopped();
230: }
231:
232: public void itemStateChanged(ItemEvent e) {
233: this .fireEditingStopped();
234: }
235:
236: public void setForeground(Color c) {
237: super .setForeground(c);
238: unselectedForeground = c;
239: }
240:
241: public void setBackground(Color c) {
242: super.setBackground(c);
243: unselectedBackground = c;
244: }
245:
246: }
|