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.model.adapter;
042:
043: import java.awt.Point;
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.util.ArrayList;
047: import java.util.Iterator;
048: import javax.swing.event.TableModelEvent;
049: import javax.swing.event.TableModelListener;
050: import javax.swing.table.TableModel;
051: import org.netbeans.modules.vmd.game.model.Layer;
052: import org.netbeans.modules.vmd.game.model.Scene;
053: import org.netbeans.modules.vmd.game.model.SceneListener;
054: import org.netbeans.modules.vmd.game.model.Scene.LayerInfo;
055: import org.openide.DialogDescriptor;
056: import org.openide.DialogDisplayer;
057: import org.openide.util.NbBundle;
058:
059: public class SceneLayerTableAdapter implements TableModel,
060: SceneListener, PropertyChangeListener {
061:
062: public static final boolean DEBUG = false;
063:
064: private final static int COLS = 7;
065:
066: public final static int COL_INDEX_LAYER_TYPE = 0;
067: public final static int COL_INDEX_LAYER_INDEX = 1;
068: public final static int COL_INDEX_LAYER_VISIBILITY_INDICATOR = 2;
069: public final static int COL_INDEX_LAYER_LOCK_INDICATOR = 3;
070: public final static int COL_INDEX_LAYER_NAME = 4;
071: public final static int COL_INDEX_LAYER_POS_X = 5;
072: public final static int COL_INDEX_LAYER_POS_Y = 6;
073:
074: private Scene scene;
075:
076: private ArrayList listeners = new ArrayList();
077:
078: public SceneLayerTableAdapter(Scene scene) {
079: this .scene = scene;
080: this .scene.addSceneListener(this );
081: }
082:
083: public int getColumnCount() {
084: return COLS;
085: }
086:
087: public int getRowCount() {
088: return this .scene.getLayerCount();
089: }
090:
091: public boolean isCellEditable(int rowIndex, int columnIndex) {
092: //cannot manually edit layer position (Z) - this is done only by using DnD
093: if (columnIndex == COL_INDEX_LAYER_INDEX)
094: return false;
095: //cannot edit layer type
096: if (columnIndex == COL_INDEX_LAYER_TYPE)
097: return false;
098: //if the layer is locked then disable editing of layer location (X, Y)
099: if (columnIndex == COL_INDEX_LAYER_POS_X
100: || columnIndex == COL_INDEX_LAYER_POS_Y) {
101: if (this .scene.isLayerLocked(this .scene
102: .getLayerAt(rowIndex))) {
103: return false;
104: }
105: }
106: return true;
107: }
108:
109: public Class getColumnClass(int columnIndex) {
110: if (columnIndex == COL_INDEX_LAYER_TYPE)
111: return Layer.class;
112: if (columnIndex == COL_INDEX_LAYER_INDEX)
113: return Integer.class;
114: if (columnIndex == COL_INDEX_LAYER_VISIBILITY_INDICATOR)
115: return Boolean.class;
116: if (columnIndex == COL_INDEX_LAYER_LOCK_INDICATOR)
117: return Boolean.class;
118: if (columnIndex == COL_INDEX_LAYER_NAME)
119: return String.class;
120: if (columnIndex == COL_INDEX_LAYER_POS_X)
121: return Integer.class;
122: if (columnIndex == COL_INDEX_LAYER_POS_Y)
123: return Integer.class;
124: return Object.class;
125: }
126:
127: public Object getValueAt(int rowIndex, int columnIndex) {
128: if (rowIndex < 0 || rowIndex >= this .scene.getLayerCount()
129: || columnIndex < 0 || columnIndex >= COLS)
130: return null;
131: Layer layer = this .scene.getLayerAt(rowIndex);
132: switch (columnIndex) {
133: case COL_INDEX_LAYER_TYPE:
134: return layer;
135: case COL_INDEX_LAYER_INDEX:
136: return new Integer(this .scene.indexOf(layer));
137: case COL_INDEX_LAYER_VISIBILITY_INDICATOR:
138: return new Boolean(this .scene.isLayerVisible(layer));
139: case COL_INDEX_LAYER_LOCK_INDICATOR:
140: return new Boolean(this .scene.isLayerLocked(layer));
141: case COL_INDEX_LAYER_NAME:
142: return layer.getName();
143: case COL_INDEX_LAYER_POS_X:
144: return new Integer(this .scene.getLayerPosition(layer).x);
145: case COL_INDEX_LAYER_POS_Y:
146: return new Integer(this .scene.getLayerPosition(layer).y);
147: }
148: return null;
149: }
150:
151: public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
152: if (rowIndex < 0 || rowIndex >= this .scene.getLayerCount()
153: || columnIndex < 0 || columnIndex >= COLS) {
154: throw new IllegalArgumentException("Arguments rowIndex = "
155: + rowIndex // NOI18N
156: + ", columnIndex = " + columnIndex
157: + " are illegal for table of "
158: + // NOI18N
159: this .scene.getLayerCount() + " rows and " + COLS
160: + " columns."); // NOI18N
161: }
162: Layer layer = this .scene.getLayerAt(rowIndex);
163: switch (columnIndex) {
164: //XXX this case has not been tested - this cell is not editable for now
165: case COL_INDEX_LAYER_INDEX:
166: Integer indexVal = (Integer) aValue;
167: int newIndex = indexVal.intValue();
168: this .scene.move(layer, newIndex);
169: break;
170: case COL_INDEX_LAYER_VISIBILITY_INDICATOR:
171: boolean visible = ((Boolean) aValue).booleanValue();
172: this .scene.setLayerVisible(layer, visible);
173: break;
174: case COL_INDEX_LAYER_LOCK_INDICATOR:
175: boolean locked = ((Boolean) aValue).booleanValue();
176: this .scene.setLayerLocked(layer, locked);
177: break;
178: case COL_INDEX_LAYER_NAME:
179: String name = (String) aValue;
180: if (name.equals(layer.getName())) {
181: return;
182: }
183: if (!this .scene.getGameDesign().isComponentNameAvailable(
184: name)) {
185: DialogDisplayer
186: .getDefault()
187: .notify(
188: new DialogDescriptor.Message(
189: NbBundle
190: .getMessage(
191: SceneLayerTableAdapter.class,
192: "SceneLayerTableAdapter.noRenameDialog.txt",
193: name),
194: //"Layer cannot be renamed because component name '" + name + "' already exists.",
195: DialogDescriptor.ERROR_MESSAGE));
196: } else {
197: layer.setName(name);
198: }
199: break;
200: case COL_INDEX_LAYER_POS_X:
201: Integer xPos = (Integer) aValue;
202: this .scene.setLayerPositionX(layer, xPos.intValue(), false);
203: break;
204: case COL_INDEX_LAYER_POS_Y:
205: Integer yPos = (Integer) aValue;
206: this .scene.setLayerPositionY(layer, yPos.intValue(), false);
207: break;
208: }
209: }
210:
211: public String getColumnName(int columnIndex) {
212: switch (columnIndex) {
213: case COL_INDEX_LAYER_TYPE:
214: return "Type";
215: case COL_INDEX_LAYER_INDEX:
216: return "Z";
217: case COL_INDEX_LAYER_VISIBILITY_INDICATOR:
218: return "View";
219: case COL_INDEX_LAYER_LOCK_INDICATOR:
220: return "Lock";
221: case COL_INDEX_LAYER_NAME:
222: return "Name";
223: case COL_INDEX_LAYER_POS_X:
224: return "X";
225: case COL_INDEX_LAYER_POS_Y:
226: return "Y";
227: default:
228: return "???";
229: }
230: }
231:
232: public void addTableModelListener(TableModelListener l) {
233: this .listeners.add(l);
234: }
235:
236: public void removeTableModelListener(TableModelListener l) {
237: this .listeners.remove(l);
238: }
239:
240: private void fireTableChanged(TableModelEvent e) {
241: for (Iterator iter = this .listeners.iterator(); iter.hasNext();) {
242: TableModelListener l = (TableModelListener) iter.next();
243: l.tableChanged(e);
244: }
245:
246: }
247:
248: //listens for name changes on all layers
249: public void propertyChange(PropertyChangeEvent evt) {
250: if (evt.getSource() instanceof Layer) {
251: Layer layer = (Layer) evt.getSource();
252: if (DEBUG)
253: System.out.println("layer changed: " + layer); // NOI18N
254: int index = this .scene.indexOf(layer);
255: TableModelEvent e = new TableModelEvent(this , index, index,
256: TableModelEvent.ALL_COLUMNS, TableModelEvent.UPDATE);
257: this .fireTableChanged(e);
258: }
259:
260: }
261:
262: public void layerAdded(Scene sourceScene, Layer layer, int index) {
263: TableModelEvent e = new TableModelEvent(this , index, index,
264: TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT);
265: layer.addPropertyChangeListener(this );
266: this .fireTableChanged(e);
267: }
268:
269: public void layerRemoved(Scene sourceScene, Layer layer,
270: LayerInfo info, int index) {
271: TableModelEvent e = new TableModelEvent(this , index, index,
272: TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE);
273: layer.removePropertyChangeListener(this );
274: this .fireTableChanged(e);
275: }
276:
277: public void layerModified(Scene sourceScene, Layer layer) {
278: int index = sourceScene.indexOf(layer);
279: TableModelEvent e = new TableModelEvent(this , index, index,
280: TableModelEvent.ALL_COLUMNS, TableModelEvent.UPDATE);
281: this .fireTableChanged(e);
282: }
283:
284: public void layerMoved(Scene sourceScene, Layer layer,
285: int indexOld, int indexNew) {
286: this .layerRemoved(sourceScene, layer, null, indexOld);
287: this .layerAdded(sourceScene, layer, indexNew);
288: }
289:
290: public void layerLockChanged(Scene sourceScene, Layer layer,
291: boolean locked) {
292: this .layerModified(sourceScene, layer);
293: }
294:
295: public void layerPositionChanged(Scene sourceScene, Layer layer,
296: Point oldPosition, Point newPosition, boolean inTransition) {
297: this .layerModified(sourceScene, layer);
298: }
299:
300: public void layerVisibilityChanged(Scene sourceScene, Layer layer,
301: boolean visible) {
302: this.layerModified(sourceScene, layer);
303: }
304:
305: }
|