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 javax.swing.event.EventListenerList;
047: import javax.swing.event.TreeModelEvent;
048: import javax.swing.event.TreeModelListener;
049: import javax.swing.tree.TreeModel;
050: import javax.swing.tree.TreePath;
051: import org.netbeans.modules.vmd.game.model.Editable;
052: import org.netbeans.modules.vmd.game.model.GlobalRepository;
053: import org.netbeans.modules.vmd.game.model.GlobalRepositoryListener;
054: import org.netbeans.modules.vmd.game.model.ImageResource;
055: import org.netbeans.modules.vmd.game.model.Layer;
056: import org.netbeans.modules.vmd.game.model.Scene;
057: import org.netbeans.modules.vmd.game.model.Scene.LayerInfo;
058: import org.netbeans.modules.vmd.game.model.SceneListener;
059: import org.netbeans.modules.vmd.game.model.Sprite;
060: import org.netbeans.modules.vmd.game.model.TiledLayer;
061:
062: /**
063: * @author Karel Herink
064: */
065: public class GlobalRepositoryTreeAdapter implements TreeModel,
066: GlobalRepositoryListener, SceneListener, PropertyChangeListener {
067:
068: private static final boolean DEBUG = false;
069:
070: private EventListenerList listenerList;
071:
072: private GlobalRepository globalRepository;
073:
074: public GlobalRepositoryTreeAdapter(GlobalRepository globalRepository) {
075: this .globalRepository = globalRepository;
076: this .listenerList = new EventListenerList();
077: this .registerListeners();
078: }
079:
080: private void registerListeners() {
081: this .globalRepository.addGlobalRepositoryListener(this );
082: for (Scene scene : this .globalRepository.getScenes()) {
083: scene.addSceneListener(this );
084: scene.addPropertyChangeListener(this );
085: for (Layer layer : scene.getLayers()) {
086: layer.addPropertyChangeListener(this );
087: }
088: }
089: }
090:
091: //------ TreeModel -----------
092:
093: public Object getRoot() {
094: return this .globalRepository;
095: }
096:
097: public Object getChild(Object parent, int index) {
098: if (parent == this .globalRepository) {
099: int numScenes = this .globalRepository.getScenes().size();
100: if (index >= numScenes) {
101: return null;
102: }
103: return this .globalRepository.getScenes().get(index);
104: }
105: if (parent instanceof Scene) {
106: return ((Scene) parent).getLayerAt(index);
107: }
108: return null;
109: }
110:
111: public int getChildCount(Object parent) {
112: if (parent == this .globalRepository) {
113: return this .globalRepository.getScenes().size();
114: }
115: if (parent instanceof Scene) {
116: return ((Scene) parent).getLayers().size();
117: }
118: return 0;
119: }
120:
121: public boolean isLeaf(Object node) {
122: if (node == this .globalRepository || node instanceof Scene) {
123: return false;
124: }
125: return true;
126: }
127:
128: public void valueForPathChanged(TreePath path, Object newValue) {
129: if (DEBUG)
130: System.out
131: .println("GlobalRepositoryTreeAdapter.valueForPathChanged"); // NOI18N
132: }
133:
134: public int getIndexOfChild(Object parent, Object child) {
135: if (parent == this .globalRepository) {
136: return this .globalRepository.getScenes().indexOf(
137: (Scene) child);
138: }
139: if (parent instanceof Scene) {
140: return ((Scene) parent).indexOf((Layer) child);
141: }
142: return -1;
143: }
144:
145: public void addTreeModelListener(TreeModelListener listener) {
146: this .listenerList.add(TreeModelListener.class, listener);
147: }
148:
149: public void removeTreeModelListener(TreeModelListener listener) {
150: this .listenerList.remove(TreeModelListener.class, listener);
151: }
152:
153: //------ GlobalRepositoryListener -----------
154:
155: public void sceneAdded(Scene scene, int index) {
156: scene.addPropertyChangeListener(this );
157: scene.addSceneListener(this );
158: TreePath path = new TreePath(this .getRoot());
159: this .fireNodeInserted(path, index, scene);
160: }
161:
162: public void sceneRemoved(Scene scene, int index) {
163: scene.removePropertyChangeListener(this );
164: scene.removeSceneListener(this );
165: TreePath path = new TreePath(this .getRoot());
166: this .fireNodeRemoved(path, index, scene);
167: }
168:
169: public void tiledLayerAdded(TiledLayer tiledLayer, int index) {
170: tiledLayer.addPropertyChangeListener(this );
171: }
172:
173: public void tiledLayerRemoved(TiledLayer tiledLayer, int index) {
174: tiledLayer.removePropertyChangeListener(this );
175: }
176:
177: public void spriteAdded(Sprite sprite, int index) {
178: sprite.addPropertyChangeListener(this );
179: }
180:
181: public void spriteRemoved(Sprite sprite, int index) {
182: sprite.removePropertyChangeListener(this );
183: }
184:
185: public void imageResourceAdded(ImageResource imageResource) {
186: //ignore
187: }
188:
189: //----------------------------------------
190:
191: //EVENTS firing
192:
193: private void fireNodeInserted(TreePath path, int index,
194: Object object) {
195: TreeModelEvent e = new TreeModelEvent(this , path,
196: new int[] { index }, new Object[] { object });
197: Object[] listeners = listenerList.getListenerList();
198: for (int i = listeners.length - 2; i >= 0; i -= 2) {
199: if (listeners[i] == TreeModelListener.class) {
200: ((TreeModelListener) listeners[i + 1])
201: .treeNodesInserted(e);
202: }
203: }
204: }
205:
206: private void fireNodeRemoved(TreePath path, int index, Object object) {
207: TreeModelEvent e = new TreeModelEvent(this , path,
208: new int[] { index }, new Object[] { object });
209: Object[] listeners = listenerList.getListenerList();
210: for (int i = listeners.length - 2; i >= 0; i -= 2) {
211: if (listeners[i] == TreeModelListener.class) {
212: ((TreeModelListener) listeners[i + 1])
213: .treeNodesRemoved(e);
214: }
215: }
216: }
217:
218: private void fireNodeChanged(TreePath path, int index, Object object) {
219: TreeModelEvent e = new TreeModelEvent(this , path,
220: new int[] { index }, new Object[] { object });
221: Object[] listeners = listenerList.getListenerList();
222: for (int i = listeners.length - 2; i >= 0; i -= 2) {
223: if (listeners[i] == TreeModelListener.class) {
224: ((TreeModelListener) listeners[i + 1])
225: .treeNodesChanged(e);
226: }
227: }
228: }
229:
230: //--------------- SceneListener -------------------
231:
232: public void layerAdded(Scene sourceScene, Layer layer, int index) {
233: TreePath path = new TreePath(new Object[] { this .getRoot(),
234: sourceScene });
235: this .fireNodeInserted(path, index, layer);
236: }
237:
238: public void layerRemoved(Scene sourceScene, Layer layer,
239: LayerInfo info, int index) {
240: TreePath path = new TreePath(new Object[] { this .getRoot(),
241: sourceScene });
242: this .fireNodeRemoved(path, index, layer);
243: }
244:
245: public void layerMoved(Scene sourceScene, Layer layer,
246: int indexOld, int indexNew) {
247: this .layerRemoved(sourceScene, layer, null, indexOld);
248: this .layerAdded(sourceScene, layer, indexNew);
249: }
250:
251: public void layerPositionChanged(Scene sourceScene, Layer layer,
252: Point oldPosition, Point newPosition, boolean inTransition) {
253: //ignore
254: }
255:
256: public void layerLockChanged(Scene sourceScene, Layer layer,
257: boolean locked) {
258: //ignore
259: }
260:
261: public void layerVisibilityChanged(Scene sourceScene, Layer layer,
262: boolean visible) {
263: //ignore
264: }
265:
266: //----------------- PropertyChangeListener -----------------
267:
268: public void propertyChange(PropertyChangeEvent e) {
269: Object src = e.getSource();
270: if (e.getPropertyName().equals(Editable.PROPERTY_NAME)) {
271: if (src instanceof Scene) {
272: Scene scene = (Scene) src;
273: TreePath path = new TreePath(this .getRoot());
274: this .fireNodeChanged(path, this .globalRepository
275: .getScenes().indexOf(scene), scene);
276: } else if (src instanceof Layer) {
277: Layer layer = (Layer) src;
278: for (Scene scene : this .globalRepository.getScenes()) {
279: if (scene.getLayers().contains(layer)) {
280: TreePath path = new TreePath(new Object[] {
281: this.getRoot(), scene });
282: this.fireNodeChanged(path, scene.getLayers()
283: .indexOf(layer), layer);
284: }
285: }
286: }
287: }
288: }
289:
290: }
|