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.sequece;
042:
043: import java.awt.BorderLayout;
044: import java.awt.Color;
045: import java.awt.Component;
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.util.EventObject;
051: import javax.swing.JPanel;
052: import javax.swing.JRadioButton;
053: import javax.swing.JTable;
054: import javax.swing.SwingConstants;
055: import javax.swing.UIManager;
056: import javax.swing.border.Border;
057: import javax.swing.border.EmptyBorder;
058: import javax.swing.event.CellEditorListener;
059: import javax.swing.table.TableCellEditor;
060: import javax.swing.table.TableCellRenderer;
061:
062: /**
063: *
064: * @author kherink
065: */
066: public class SequenceDefaultTableEditor extends JRadioButton implements
067: TableCellRenderer, TableCellEditor, ActionListener,
068: ItemListener {
069:
070: //Without this panel the border doesn't show up correcly when jbutton is selected and has focus
071: private JPanel panel = new JPanel();
072:
073: protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
074: private Color unselectedForeground;
075: private Color unselectedBackground;
076:
077: //protected EventListenerList listenerList = new EventListenerList();
078:
079: /** Creates a new instance of SequenceDefaultTableEditor */
080: public SequenceDefaultTableEditor() {
081: this .addActionListener(this );
082: this .addItemListener(this );
083: this .setHorizontalAlignment(SwingConstants.CENTER);
084: this .setBackground(Color.WHITE);
085: this .panel.setBackground(Color.WHITE);
086: this .panel.setLayout(new BorderLayout());
087: this .panel.add(this , BorderLayout.CENTER);
088: }
089:
090: //------------ TableCellRenderer -----------------
091: public Component getTableCellRendererComponent(JTable table,
092: Object value, boolean isSelected, boolean hasFocus,
093: int row, int column) {
094: if (value instanceof Boolean) {
095: Boolean b = (Boolean) value;
096: this .setSelected(b);
097: if (isSelected) {
098: super .setForeground(table.getSelectionForeground());
099: super .setBackground(table.getSelectionBackground());
100: this .panel
101: .setForeground(table.getSelectionForeground());
102: this .panel
103: .setBackground(table.getSelectionBackground());
104: } else {
105: super
106: .setForeground((unselectedForeground != null) ? unselectedForeground
107: : table.getForeground());
108: super
109: .setBackground((unselectedBackground != null) ? unselectedBackground
110: : table.getBackground());
111: this .panel
112: .setForeground((unselectedForeground != null) ? unselectedForeground
113: : table.getForeground());
114: this .panel
115: .setBackground((unselectedBackground != null) ? unselectedBackground
116: : table.getBackground());
117: }
118:
119: setFont(table.getFont());
120:
121: if (hasFocus) {
122: Border border = null;
123: if (isSelected) {
124: border = UIManager
125: .getBorder("Table.focusSelectedCellHighlightBorder"); // NOI18N
126: }
127: if (border == null) {
128: border = UIManager
129: .getBorder("Table.focusCellHighlightBorder"); // NOI18N
130: }
131: this .panel.setBorder(border);
132:
133: if (!isSelected && table.isCellEditable(row, column)) {
134: Color col;
135: col = UIManager
136: .getColor("Table.focusCellForeground"); // NOI18N
137: if (col != null) {
138: super .setForeground(col);
139: }
140: col = UIManager
141: .getColor("Table.focusCellBackground"); // NOI18N
142: if (col != null) {
143: super .setBackground(col);
144: }
145: }
146: } else {
147: this .panel.setBorder(noFocusBorder);
148: }
149: return this .panel;
150: }
151: throw new IllegalArgumentException(
152: "Only Boolean can be rendered!"); // NOI18N
153: }
154:
155: //------------ TableCellEditor -------------------
156:
157: public Component getTableCellEditorComponent(JTable table,
158: Object value, boolean isSelected, int row, int column) {
159: if (value instanceof Boolean) {
160: this .setSelected((Boolean) value);
161: return this ;
162: }
163: throw new IllegalArgumentException(
164: "Only Boolean can be edited."); // NOI18N
165: }
166:
167: public void addCellEditorListener(CellEditorListener listener) {
168: listenerList.add(CellEditorListener.class, listener);
169: }
170:
171: public void removeCellEditorListener(CellEditorListener listener) {
172: listenerList.remove(CellEditorListener.class, listener);
173: }
174:
175: protected void fireEditingStopped() {
176: CellEditorListener listener;
177: Object[] listeners = listenerList.getListenerList();
178: for (int i = 0; i < listeners.length; i++) {
179: if (listeners[i] == CellEditorListener.class) {
180: listener = (CellEditorListener) listeners[i + 1];
181: listener.editingStopped(changeEvent);
182: }
183: }
184: }
185:
186: protected void fireEditingCanceled() {
187: CellEditorListener listener;
188: Object[] listeners = listenerList.getListenerList();
189: for (int i = 0; i < listeners.length; i++) {
190: if (listeners[i] == CellEditorListener.class) {
191: listener = (CellEditorListener) listeners[i + 1];
192: listener.editingCanceled(changeEvent);
193: }
194: }
195: }
196:
197: public void cancelCellEditing() {
198: fireEditingCanceled();
199: }
200:
201: public boolean stopCellEditing() {
202: fireEditingStopped();
203: return true;
204: }
205:
206: public Object getCellEditorValue() {
207: return new Boolean(this .isSelected());
208: }
209:
210: public boolean isCellEditable(EventObject anEvent) {
211: return true;
212: }
213:
214: public boolean shouldSelectCell(EventObject anEvent) {
215: return true;
216: }
217:
218: public void actionPerformed(ActionEvent e) {
219: this .fireEditingStopped();
220: }
221:
222: public void itemStateChanged(ItemEvent e) {
223: this .fireEditingStopped();
224: }
225:
226: public void setForeground(Color c) {
227: super .setForeground(c);
228: unselectedForeground = c;
229: }
230:
231: public void setBackground(Color c) {
232: super.setBackground(c);
233: unselectedBackground = c;
234: }
235:
236: }
|