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.util.ArrayList;
047: import java.util.List;
048: import javax.swing.AbstractAction;
049: import javax.swing.Action;
050: import javax.swing.JComponent;
051: import org.netbeans.modules.vmd.game.dialog.RenameSpriteDialog;
052: import org.netbeans.modules.vmd.game.editor.sequece.SequenceContainerEditor;
053: import org.netbeans.modules.vmd.game.editor.sequece.SequenceContainerNavigator;
054: import org.netbeans.modules.vmd.game.preview.SequenceContainerPreview;
055: import org.openide.DialogDescriptor;
056: import org.openide.DialogDisplayer;
057: import org.openide.util.NbBundle;
058:
059: public class Sprite extends Layer implements SequenceContainer {
060:
061: private SequenceContainerEditor editor;
062: private JComponent navigator;
063: private SequenceContainerImpl sequenceContainer;
064:
065: /**
066: * Creates a new animated Sprite using frames contained in the provided Image.
067: */
068: Sprite(GlobalRepository gameDesign, String name,
069: ImageResource imageResource, int numberFrames,
070: int frameWidth, int frameHeight) {
071: super (gameDesign, name, imageResource, frameWidth, frameHeight);
072: this .sequenceContainer = new SequenceContainerImpl(this , null,
073: super .propertyChangeSupport, imageResource, frameWidth,
074: frameHeight, true);
075: String seqName = this .getNextSequenceName(name + "seq"); // NOI18N
076: Sequence defaultSequence = this .createSequence(seqName,
077: numberFrames, frameWidth, frameHeight);
078: this .setDefaultSequence(defaultSequence);
079: }
080:
081: Sprite(GlobalRepository gameDesign, String name,
082: ImageResource imageResource, Sequence defaultSequence) {
083: super (gameDesign, name, imageResource, defaultSequence
084: .getFrameWidth(), defaultSequence.getFrameHeight());
085: this .sequenceContainer = new SequenceContainerImpl(this , null,
086: super .propertyChangeSupport, imageResource,
087: defaultSequence.getFrameWidth(), defaultSequence
088: .getFrameHeight(), true);
089: this .setDefaultSequence(defaultSequence);
090: }
091:
092: public ImageResourceInfo getImageResourceInfo() {
093: return new ImageResourceInfo(this .getImageResource(), this
094: .getTileWidth(), this .getTileHeight(), true);
095: }
096:
097: public String getNextSequenceName(String prefix) {
098: return this .sequenceContainer.getNextSequenceName(prefix);
099: }
100:
101: public void addSequenceContainerListener(
102: SequenceContainerListener listener) {
103: this .sequenceContainer.addSequenceContainerListener(listener);
104: }
105:
106: public void removeSequenceContainerListener(
107: SequenceContainerListener listener) {
108: this .sequenceContainer
109: .removeSequenceContainerListener(listener);
110: }
111:
112: //------SequenceContainer-------
113:
114: public Sequence createSequence(String name, int numberFrames,
115: int frameWidth, int frameHeight) {
116: return this .sequenceContainer.createSequence(name,
117: numberFrames, frameWidth, frameHeight);
118: }
119:
120: public Sequence createSequence(String name, Sequence s) {
121: return this .sequenceContainer.createSequence(name, s);
122: }
123:
124: public boolean append(Sequence sequence) {
125: return this .sequenceContainer.append(sequence);
126: }
127:
128: public boolean insert(Sequence sequence, int index) {
129: return this .sequenceContainer.insert(sequence, index);
130: }
131:
132: public boolean remove(Sequence sequence) {
133: return this .sequenceContainer.remove(sequence);
134: }
135:
136: public void move(Sequence sequence, int newIndex) {
137: this .sequenceContainer.move(sequence, newIndex);
138: }
139:
140: public List<Sequence> getSequences() {
141: return this .sequenceContainer.getSequences();
142: }
143:
144: public int getSequenceCount() {
145: return this .sequenceContainer.getSequenceCount();
146: }
147:
148: public Sequence getSequenceByName(String name) {
149: return this .sequenceContainer.getSequenceByName(name);
150: }
151:
152: public void setDefaultSequence(Sequence defaultSequence) {
153: this .sequenceContainer.setDefaultSequence(defaultSequence);
154: }
155:
156: public Sequence getDefaultSequence() {
157: return this .sequenceContainer.getDefaultSequence();
158: }
159:
160: public int indexOf(Sequence sequence) {
161: return this .sequenceContainer.indexOf(sequence);
162: }
163:
164: public Sequence getSequenceAt(int index) {
165: return this .sequenceContainer.getSequenceAt(index);
166: }
167:
168: public List<Action> getActionsForSequence(Sequence sequence) {
169: return this .sequenceContainer.getActionsForSequence(sequence);
170: }
171:
172: //------Editable-------
173:
174: public JComponent getEditor() {
175: return this .editor == null ? this .editor = new SequenceContainerEditor(
176: this )
177: : this .editor;
178: }
179:
180: public JComponent getNavigator() {
181: return this .navigator == null ? this .navigator = new SequenceContainerNavigator(
182: this )
183: : this .navigator;
184: }
185:
186: public int getHeight() {
187: return super .getTileHeight();
188: }
189:
190: public int getWidth() {
191: return super .getTileWidth();
192: }
193:
194: public List<Action> getActions() {
195: List<Action> super Actions = super .getActions();
196: List<Action> actions = new ArrayList<Action>();
197: actions.addAll(super Actions);
198: actions.add(new RenameAction());
199: // actions.add(new AddSequenceAction());
200: return actions;
201: }
202:
203: // private class AddSequenceAction extends AbstractAction {
204: // {
205: // this.putValue(NAME, "Add sequence");
206: // }
207: // public void actionPerformed(ActionEvent e) {
208: // NewSequenceDialog dialog = new NewSequenceDialog(Sprite.this, Sprite.this.getTileWidth(), Sprite.this.getTileHeight());
209: // DialogDescriptor dd = new DialogDescriptor(dialog, "Add Sequence");
210: // dd.setButtonListener(dialog);
211: // dd.setValid(false);
212: // dialog.setDialogDescriptor(dd);
213: // Dialog d = DialogDisplayer.getDefault().createDialog(dd);
214: // d.setVisible(true);
215: // }
216: // }
217:
218: private class RenameAction extends AbstractAction {
219: {
220: this .putValue(NAME, NbBundle.getMessage(Sprite.class,
221: "Sprite.RenameAction.text"));
222: }
223:
224: public void actionPerformed(ActionEvent e) {
225: RenameSpriteDialog dialog = new RenameSpriteDialog(
226: Sprite.this );
227: DialogDescriptor dd = new DialogDescriptor(dialog, NbBundle
228: .getMessage(Sprite.class,
229: "Sprite.RenameAction.text"));
230: dd.setButtonListener(dialog);
231: dd.setValid(false);
232: dialog.setDialogDescriptor(dd);
233: Dialog d = DialogDisplayer.getDefault().createDialog(dd);
234: d.setVisible(true);
235: }
236: }
237:
238: public String getDisplayableTypeName() {
239: return NbBundle.getMessage(Sprite.class, "Sprite.text");
240: }
241:
242: public JComponent getPreview() {
243: return new SequenceContainerPreview(NbBundle.getMessage(
244: Sprite.class, "Sprite.preview.title"), this );
245: }
246:
247: public void paint(Graphics2D g, int x, int y) {
248: this .getDefaultSequence().getFrame(0).paint(g, x, y);
249: }
250:
251: public void paint(Graphics2D g) {
252: this .getDefaultSequence().getFrame(0).paint(g, 0, 0);
253: }
254:
255: }
|