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.beans.PropertyChangeEvent;
044: import java.beans.PropertyChangeListener;
045: import java.util.ArrayList;
046: import java.util.Iterator;
047: import javax.swing.event.TableModelEvent;
048: import javax.swing.event.TableModelListener;
049: import javax.swing.table.TableModel;
050: import org.netbeans.modules.vmd.game.model.Sequence;
051: import org.netbeans.modules.vmd.game.model.SequenceContainer;
052: import org.netbeans.modules.vmd.game.model.SequenceContainerListener;
053: import org.netbeans.modules.vmd.game.model.SequenceListener;
054: import org.openide.DialogDescriptor;
055: import org.openide.DialogDisplayer;
056: import org.openide.util.NbBundle;
057:
058: /**
059: *
060: * @author kherink
061: */
062: public class SequenceContainerTableAdapter implements TableModel,
063: SequenceContainerListener, SequenceListener,
064: PropertyChangeListener {
065:
066: private static final int COLS = 4;
067:
068: private static final int COL_DEFAULT = 0;
069: private static final int COL_NAME = 1;
070: private static final int COL_FRAMES = 2;
071: private static final int COL_DELAY = 3;
072:
073: private SequenceContainer sequenceContainer;
074: private ArrayList listeners = new ArrayList();
075:
076: public SequenceContainerTableAdapter(
077: SequenceContainer sequenceContainer) {
078: this .sequenceContainer = sequenceContainer;
079: this .sequenceContainer.addSequenceContainerListener(this );
080: this .sequenceContainer.addPropertyChangeListener(this );
081: for (Sequence s : this .sequenceContainer.getSequences()) {
082: s.addSequenceListener(this );
083: s.addPropertyChangeListener(this );
084: }
085: }
086:
087: //------- TableModel
088:
089: public int getRowCount() {
090: return this .sequenceContainer.getSequenceCount();
091: }
092:
093: public int getColumnCount() {
094: return COLS;
095: }
096:
097: public String getColumnName(int columnIndex) {
098: switch (columnIndex) {
099: case COL_DEFAULT:
100: return NbBundle.getMessage(
101: SequenceContainerTableAdapter.class,
102: "SequenceContainerTableAdapter.columnDefault.txt");
103: case COL_NAME:
104: return NbBundle.getMessage(
105: SequenceContainerTableAdapter.class,
106: "SequenceContainerTableAdapter.columnName.txt");
107: case COL_FRAMES:
108: return NbBundle.getMessage(
109: SequenceContainerTableAdapter.class,
110: "SequenceContainerTableAdapter.columnFrames.txt");
111: case COL_DELAY:
112: return NbBundle.getMessage(
113: SequenceContainerTableAdapter.class,
114: "SequenceContainerTableAdapter.columnDelay.txt");
115: default:
116: return "???"; // NOI18N
117: }
118: }
119:
120: public Class<?> getColumnClass(int columnIndex) {
121: switch (columnIndex) {
122: case COL_DEFAULT:
123: return Boolean.class;
124: case COL_NAME:
125: return String.class;
126: case COL_FRAMES:
127: return Short.class;
128: case COL_DELAY:
129: return Integer.class;
130: default:
131: return Object.class;
132: }
133: }
134:
135: public boolean isCellEditable(int rowIndex, int columnIndex) {
136: switch (columnIndex) {
137: case COL_DEFAULT:
138: return true;
139: case COL_NAME:
140: return true;
141: case COL_FRAMES:
142: return false;
143: case COL_DELAY:
144: return true;
145: default:
146: return false;
147: }
148: }
149:
150: public Object getValueAt(int rowIndex, int columnIndex) {
151: Sequence s = this .sequenceContainer.getSequenceAt(rowIndex);
152: if (s == null) {
153: return null;
154: }
155: switch (columnIndex) {
156: case COL_DEFAULT:
157: return this .sequenceContainer.getDefaultSequence() == s;
158: case COL_NAME:
159: return s.getName();
160: case COL_FRAMES:
161: return s.getFrameCount();
162: case COL_DELAY:
163: return s.getFrameMs();
164: default:
165: return null;
166: }
167: }
168:
169: public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
170: Sequence s = this .sequenceContainer.getSequenceAt(rowIndex);
171: switch (columnIndex) {
172: case COL_DEFAULT:
173: this .sequenceContainer.setDefaultSequence(s);
174: break;
175: case COL_NAME:
176: String name = (String) aValue;
177: if (!this .sequenceContainer.getGameDesign()
178: .isComponentNameAvailable(name)) {
179: DialogDisplayer
180: .getDefault()
181: .notify(
182: new DialogDescriptor.Message(
183: NbBundle
184: .getMessage(
185: SceneLayerTableAdapter.class,
186: "SequenceContainerTableAdapter.noRenameDialog.txt",
187: name),
188: //"Sequence cannot be renamed because component name '" + name + "' already exists.",
189: DialogDescriptor.ERROR_MESSAGE));
190: } else {
191: s.setName(name);
192: }
193: break;
194: case COL_DELAY:
195: s.setFrameMs((Integer) aValue);
196: break;
197: }
198: }
199:
200: public void addTableModelListener(TableModelListener l) {
201: this .listeners.add(l);
202: }
203:
204: public void removeTableModelListener(TableModelListener l) {
205: this .listeners.remove(l);
206: }
207:
208: //-------- SequenceContainerListener
209:
210: public void sequenceAdded(SequenceContainer source,
211: Sequence sequence, int index) {
212: TableModelEvent e = new TableModelEvent(this , index, index,
213: TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT);
214: sequence.addPropertyChangeListener(this );
215: sequence.addSequenceListener(this );
216: this .fireTableChanged(e);
217: }
218:
219: public void sequenceRemoved(SequenceContainer source,
220: Sequence sequence, int index) {
221: TableModelEvent e = new TableModelEvent(this , index, index,
222: TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE);
223: sequence.removePropertyChangeListener(this );
224: sequence.removeSequenceListener(this );
225: this .fireTableChanged(e);
226: }
227:
228: public void sequenceMoved(SequenceContainer source,
229: Sequence sequence, int indexOld, int indexNew) {
230: this .sequenceRemoved(source, sequence, indexOld);
231: this .sequenceAdded(source, sequence, indexNew);
232: }
233:
234: private void fireTableChanged(TableModelEvent e) {
235: for (Iterator iter = this .listeners.iterator(); iter.hasNext();) {
236: TableModelListener l = (TableModelListener) iter.next();
237: l.tableChanged(e);
238: }
239:
240: }
241:
242: //--------- PropertyChangeListener
243:
244: public void propertyChange(PropertyChangeEvent evt) {
245: if (evt.getSource() instanceof Sequence) {
246: Sequence s = (Sequence) evt.getSource();
247: int index = this .sequenceContainer.indexOf(s);
248: TableModelEvent e = new TableModelEvent(this , index, index,
249: TableModelEvent.ALL_COLUMNS, TableModelEvent.UPDATE);
250: this .fireTableChanged(e);
251: }
252: if (evt.getSource() instanceof SequenceContainer) {
253: SequenceContainer sc = (SequenceContainer) evt.getSource();
254: if (evt.getPropertyName().equals(
255: SequenceContainer.PROPERTY_DEFAULT_SEQUENCE)) {
256: Sequence old = (Sequence) evt.getOldValue();
257: Sequence current = (Sequence) evt.getNewValue();
258: int oldIndex = this .sequenceContainer.indexOf(old);
259: int newIndex = this .sequenceContainer.indexOf(current);
260: //System.out.println("old = " + oldIndex + " new " + newIndex);
261: TableModelEvent e = new TableModelEvent(this , oldIndex,
262: oldIndex, TableModelEvent.ALL_COLUMNS,
263: TableModelEvent.UPDATE);
264: this .fireTableChanged(e);
265:
266: e = new TableModelEvent(this , newIndex, newIndex,
267: TableModelEvent.ALL_COLUMNS,
268: TableModelEvent.UPDATE);
269: this .fireTableChanged(e);
270: }
271: }
272: }
273:
274: public void frameAdded(Sequence sequence, int index) {
275: int seqIndex = this .sequenceContainer.indexOf(sequence);
276: TableModelEvent e = new TableModelEvent(this , seqIndex,
277: seqIndex, TableModelEvent.ALL_COLUMNS,
278: TableModelEvent.INSERT);
279: this .fireTableChanged(e);
280: }
281:
282: public void frameRemoved(Sequence sequence, int index) {
283: int seqIndex = this .sequenceContainer.indexOf(sequence);
284: TableModelEvent e = new TableModelEvent(this , seqIndex,
285: seqIndex, TableModelEvent.ALL_COLUMNS,
286: TableModelEvent.INSERT);
287: this .fireTableChanged(e);
288: }
289:
290: public void frameModified(Sequence sequence, int index) {
291: //don't care
292: }
293:
294: public void framesChanged(Sequence sequence) {
295: TableModelEvent e = new TableModelEvent(this);
296: this.fireTableChanged(e);
297: }
298:
299: }
|