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;
042:
043: import java.awt.Dialog;
044: import java.awt.Graphics2D;
045: import java.awt.event.ActionEvent;
046: import java.beans.PropertyChangeEvent;
047: import java.beans.PropertyChangeListener;
048: import java.beans.PropertyChangeSupport;
049: import java.util.ArrayList;
050: import java.util.Collections;
051: import java.util.List;
052: import javax.swing.AbstractAction;
053: import javax.swing.Action;
054: import javax.swing.JComponent;
055: import org.netbeans.modules.vmd.game.dialog.NewSequenceDialog;
056: import org.netbeans.modules.vmd.game.dialog.RenameAnimatedTileDialog;
057: import org.netbeans.modules.vmd.game.editor.sequece.SequenceContainerEditor;
058: import org.netbeans.modules.vmd.game.editor.sequece.SequenceContainerNavigator;
059: import org.netbeans.modules.vmd.game.preview.SequenceContainerPreview;
060: import org.openide.DialogDescriptor;
061: import org.openide.DialogDisplayer;
062: import org.openide.NotifyDescriptor;
063: import org.openide.util.NbBundle;
064:
065: public class AnimatedTile extends Tile implements SequenceContainer,
066: Editable, Identifiable {
067:
068: private long id = Identifiable.ID_UNKNOWN;
069:
070: public static final boolean DEBUG = false;
071:
072: private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
073: this );
074: private SequenceContainerImpl sequenceContainer;
075:
076: private SequenceContainerEditor editor;
077: private JComponent navigator;
078:
079: private String name;
080:
081: AnimatedTile(String name, ImageResource imageResource, int index,
082: Sequence sequence, int width, int height) {
083: super (imageResource, index, width, height);
084: this .sequenceContainer = new SequenceContainerImpl(this , null,
085: this .propertyChangeSupport, imageResource, width,
086: height, false);
087: this .name = name;
088: this .setDefaultSequence(sequence);
089: }
090:
091: AnimatedTile(String name, ImageResource imageResource, int index,
092: int width, int height) {
093: super (imageResource, index, width, height);
094: this .sequenceContainer = new SequenceContainerImpl(this , null,
095: this .propertyChangeSupport, imageResource, width,
096: height, false);
097: this .name = name;
098: String seqName = this .getNextSequenceName(this .name + "seq"); // NOI18N
099: Sequence sequence = this .createSequence(seqName, 1, width,
100: height);
101: this .setDefaultSequence(sequence);
102: }
103:
104: public String getNextSequenceName(String prefix) {
105: return this .sequenceContainer.getNextSequenceName(prefix);
106: }
107:
108: public GlobalRepository getGameDesign() {
109: return this .getImageResource().getGameDesign();
110: }
111:
112: public void addSequenceContainerListener(
113: SequenceContainerListener listener) {
114: this .sequenceContainer.addSequenceContainerListener(listener);
115: }
116:
117: public void removeSequenceContainerListener(
118: SequenceContainerListener listener) {
119: this .sequenceContainer
120: .removeSequenceContainerListener(listener);
121: }
122:
123: public void setName(String name) {
124: if (name == null) {
125: return;
126: }
127: if (this .getName().equals(name)) {
128: return;
129: }
130: if (!this .getGameDesign().isComponentNameAvailable(name)) {
131: throw new IllegalArgumentException(
132: "AnimatedTile cannot be renamed because component name '"
133: + name + "' already exists."); // NOI18N
134: }
135: String oldName = this .name;
136: this .name = name;
137: //System.out.println("old name: " + oldName + ", new name: " + name);
138: this .propertyChangeSupport.firePropertyChange(PROPERTY_NAME,
139: oldName, name);
140: }
141:
142: //------SequenceContainer-------
143:
144: public String getName() {
145: return this .name;
146: }
147:
148: public Sequence createSequence(String name, int numberFrames,
149: int frameWidth, int frameHeight) {
150: return this .sequenceContainer.createSequence(name,
151: numberFrames, frameWidth, frameHeight);
152: }
153:
154: public Sequence createSequence(String name, Sequence s) {
155: return this .sequenceContainer.createSequence(name, s);
156: }
157:
158: public boolean append(Sequence sequence) {
159: return this .sequenceContainer.append(sequence);
160: }
161:
162: public boolean insert(Sequence sequence, int index) {
163: return this .sequenceContainer.insert(sequence, index);
164: }
165:
166: public boolean remove(Sequence sequence) {
167: return this .sequenceContainer.remove(sequence);
168: }
169:
170: public void move(Sequence sequence, int newIndex) {
171: this .sequenceContainer.move(sequence, newIndex);
172: }
173:
174: public List<Sequence> getSequences() {
175: return this .sequenceContainer.getSequences();
176: }
177:
178: public int getSequenceCount() {
179: return this .sequenceContainer.getSequenceCount();
180: }
181:
182: public Sequence getSequenceByName(String name) {
183: return this .sequenceContainer.getSequenceByName(name);
184: }
185:
186: public void setDefaultSequence(Sequence defaultSequence) {
187: this .sequenceContainer.setDefaultSequence(defaultSequence);
188: }
189:
190: public Sequence getDefaultSequence() {
191: return this .sequenceContainer.getDefaultSequence();
192: }
193:
194: public int indexOf(Sequence sequence) {
195: return this .sequenceContainer.indexOf(sequence);
196: }
197:
198: public Sequence getSequenceAt(int index) {
199: return this .sequenceContainer.getSequenceAt(index);
200: }
201:
202: public List<Action> getActionsForSequence(Sequence sequence) {
203: return this .sequenceContainer.getActionsForSequence(sequence);
204: }
205:
206: public JComponent getEditor() {
207: return this .editor == null ? this .editor = new SequenceContainerEditor(
208: this )
209: : this .editor;
210: }
211:
212: public ImageResourceInfo getImageResourceInfo() {
213: return new ImageResourceInfo(this .getImageResource(), this
214: .getWidth(), this .getHeight(), false);
215: }
216:
217: public List<Action> getActions() {
218: List<Action> actions = new ArrayList<Action>();
219: actions.add(new RenameAction());
220: actions.add(new AddSequenceAction());
221: actions.add(new DeleteAction());
222: return Collections.unmodifiableList(actions);
223: }
224:
225: public class AddSequenceAction extends AbstractAction {
226: {
227: this .putValue(NAME, NbBundle.getMessage(AnimatedTile.class,
228: "AnimatedTile.AddSequenceAction.name")); // NOI18N
229: }
230:
231: public void actionPerformed(ActionEvent e) {
232: NewSequenceDialog dialog = new NewSequenceDialog(
233: AnimatedTile.this , getWidth(), getHeight());
234: DialogDescriptor dd = new DialogDescriptor(dialog, NbBundle
235: .getMessage(AnimatedTile.class,
236: "AnimatedTile.AddSequenceAction.name")); // NOI18N
237: dd.setButtonListener(dialog);
238: dd.setValid(false);
239: dialog.setDialogDescriptor(dd);
240: Dialog d = DialogDisplayer.getDefault().createDialog(dd);
241: d.setVisible(true);
242: }
243: }
244:
245: public class RenameAction extends AbstractAction {
246: {
247: this .putValue(NAME, NbBundle.getMessage(AnimatedTile.class,
248: "AnimatedTile.RenameAction.name")); // NOI18N
249: }
250:
251: public void actionPerformed(ActionEvent e) {
252: RenameAnimatedTileDialog dialog = new RenameAnimatedTileDialog(
253: AnimatedTile.this );
254: DialogDescriptor dd = new DialogDescriptor(dialog, NbBundle
255: .getMessage(AnimatedTile.class,
256: "AnimatedTile.RenameAction.name")); // NOI18N
257: dd.setButtonListener(dialog);
258: dd.setValid(false);
259: dialog.setDialogDescriptor(dd);
260: Dialog d = DialogDisplayer.getDefault().createDialog(dd);
261: d.setVisible(true);
262: }
263: }
264:
265: public class DeleteAction extends AbstractAction {
266: {
267: this .putValue(NAME, NbBundle.getMessage(AnimatedTile.class,
268: "AnimatedTile.DeleteAction.name")); // NOI18N
269: }
270:
271: public void actionPerformed(ActionEvent e) {
272: Object response = DialogDisplayer.getDefault().notify(
273: new NotifyDescriptor(NbBundle
274: .getMessage(AnimatedTile.class,
275: "AnimatedTile.DeleteDialog.text",
276: getName()), NbBundle.getMessage(
277: AnimatedTile.class,
278: "AnimatedTile.DeleteAnimatedTile.text"),
279: NotifyDescriptor.YES_NO_OPTION,
280: NotifyDescriptor.QUESTION_MESSAGE,
281: new Object[] { NotifyDescriptor.YES_OPTION,
282: NotifyDescriptor.NO_OPTION },
283: NotifyDescriptor.YES_OPTION));
284: if (response == NotifyDescriptor.YES_OPTION) {
285: if (DEBUG)
286: System.out
287: .println("said YES to delete AnimatedTile"); // NOI18N
288: getImageResource().removeAnimatedTile(getIndex());
289: }
290: }
291: }
292:
293: public JComponent getPreview() {
294: return new SequenceContainerPreview(NbBundle.getMessage(
295: AnimatedTile.class, "AnimatedTile.previewLabel.text"),
296: this ); // NOI18N
297: }
298:
299: public void addPropertyChangeListener(PropertyChangeListener l) {
300: propertyChangeSupport.addPropertyChangeListener(l);
301: }
302:
303: public void removePropertyChangeListener(PropertyChangeListener l) {
304: propertyChangeSupport.removePropertyChangeListener(l);
305: }
306:
307: //--------PropertyChangeListener
308:
309: public void propertyChange(PropertyChangeEvent evt) {
310: if (DEBUG)
311: System.out.println(this .getClass()
312: + "unimplemented propertyChange() from "
313: + evt.getSource()); // NOI18N
314: }
315:
316: public void paint(Graphics2D g, int x, int y) {
317: this .getDefaultSequence().getFrame(0).paint(g, x, y);
318: }
319:
320: public JComponent getNavigator() {
321: return this .navigator == null ? this .navigator = new SequenceContainerNavigator(
322: this )
323: : this .navigator;
324: }
325:
326: public long getId() {
327: return id;
328: }
329:
330: public void setId(long id) {
331: this.id = id;
332: }
333:
334: }
|