001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
007: *
008: * The contents of this file are subject to the terms of either the GNU
009: * General Public License Version 2 only ("GPL") or the Common
010: * Development and Distribution License("CDDL") (collectively, the
011: * "License"). You may not use this file except in compliance with the
012: * License. You can obtain a copy of the License at
013: * http://www.netbeans.org/cddl-gplv2.html
014: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
015: * specific language governing permissions and limitations under the
016: * License. When distributing the software, include this License Header
017: * Notice in each file and include the License file at
018: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
019: * particular file as subject to the "Classpath" exception as provided
020: * by Sun in the GPL Version 2 section of the License file that
021: * accompanied this code. If applicable, add the following below the
022: * License Header, with the fields enclosed by brackets [] replaced by
023: * your own identifying information:
024: * "Portions Copyrighted [year] [name of copyright owner]"
025: *
026: * Contributor(s):
027: *
028: * The Original Software is NetBeans. The Initial Developer of the Original
029: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
030: * Microsystems, Inc. All Rights Reserved.
031: *
032: * If you wish your version of this file to be governed by only the CDDL
033: * or only the GPL Version 2, indicate your decision by adding
034: * "[Contributor] elects to include this software in this distribution
035: * under the [CDDL or GPL Version 2] license." If you do not indicate a
036: * single choice of license, a recipient has the option to distribute
037: * your version of this file under either the CDDL, the GPL Version 2 or
038: * to extend the choice of license to its licensees as provided above.
039: * However, if you add GPL Version 2 code and therefore, elected the GPL
040: * Version 2 license, then the option applies only if the new code is
041: * made subject to such option by the copyright holder.
042: */
043: package org.netbeans.modules.vmd.game.model;
044:
045: import java.awt.Graphics2D;
046: import java.awt.datatransfer.DataFlavor;
047: import java.awt.datatransfer.Transferable;
048: import java.awt.datatransfer.UnsupportedFlavorException;
049: import java.awt.event.ActionEvent;
050: import java.beans.PropertyChangeListener;
051: import java.beans.PropertyChangeSupport;
052: import java.io.IOException;
053: import java.util.ArrayList;
054: import java.util.Collections;
055: import java.util.Comparator;
056: import java.util.List;
057: import javax.swing.AbstractAction;
058: import javax.swing.Action;
059: import javax.swing.JComponent;
060: import org.openide.DialogDisplayer;
061: import org.openide.NotifyDescriptor;
062: import org.openide.util.NbBundle;
063:
064: public abstract class Layer implements Previewable, Editable,
065: Transferable, Identifiable {
066:
067: private long id = Identifiable.ID_UNKNOWN;
068:
069: public static final String ACTION_PROP_SCENE = "layer.action.prop.scene"; // NOI18N
070:
071: protected PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
072: this );
073:
074: private GlobalRepository gameDesign;
075:
076: private String name;
077: private ImageResource imageResource;
078: private int cellWidth;
079: private int cellHeight;
080:
081: protected Layer(GlobalRepository gameDesign, String name,
082: ImageResource imageResource, int cellWidth, int cellHeight) {
083: assert (gameDesign != null);
084: this .gameDesign = gameDesign;
085: this .name = name;
086: this .imageResource = imageResource;
087: this .cellWidth = cellWidth;
088: this .cellHeight = cellHeight;
089: }
090:
091: public String getName() {
092: return this .name;
093: }
094:
095: public GlobalRepository getGameDesign() {
096: return this .gameDesign;
097: }
098:
099: public void setName(String name) {
100: if (name == null) {
101: return;
102: }
103: if (this .getName().equals(name)) {
104: return;
105: }
106: if (!this .gameDesign.isComponentNameAvailable(name)) {
107: throw new IllegalArgumentException(
108: "Layer cannot be renamed because component name '"
109: + name + "' already exists."); // NOI18N
110: }
111: String oldName = this .name;
112: this .name = name;
113: this .propertyChangeSupport.firePropertyChange(PROPERTY_NAME,
114: oldName, name);
115: }
116:
117: public ImageResource getImageResource() {
118: return this .imageResource;
119: }
120:
121: public String toString() {
122: return this .getName();
123: }
124:
125: public void addPropertyChangeListener(PropertyChangeListener l) {
126: propertyChangeSupport.addPropertyChangeListener(l);
127: }
128:
129: public void removePropertyChangeListener(PropertyChangeListener l) {
130: propertyChangeSupport.removePropertyChangeListener(l);
131: }
132:
133: public abstract void paint(Graphics2D g);
134:
135: public abstract int getHeight();
136:
137: public abstract int getWidth();
138:
139: //Transferable Interface
140:
141: public Object getTransferData(DataFlavor flavor)
142: throws UnsupportedFlavorException, IOException {
143: return this ;
144: }
145:
146: public DataFlavor[] getTransferDataFlavors() {
147: DataFlavor[] flavors = new DataFlavor[1];
148: try {
149: flavors[0] = new LayerDataFlavor();
150: } catch (ClassNotFoundException e) {
151: e.printStackTrace();
152: }
153: return flavors;
154: }
155:
156: public boolean isDataFlavorSupported(DataFlavor flavor) {
157: if (flavor instanceof LayerDataFlavor)
158: return true;
159: return false;
160: }
161:
162: public int getTileHeight() {
163: return this .cellHeight;
164: }
165:
166: public int getTileWidth() {
167: return this .cellWidth;
168: }
169:
170: public abstract String getDisplayableTypeName();
171:
172: public static class NameComparator implements Comparator {
173:
174: public int compare(Object arg0, Object arg1) {
175: if (!(arg0 instanceof Layer) || !(arg1 instanceof Layer))
176: throw new ClassCastException(
177: "Compared object not instance of Layer"); // NOI18N
178: Layer l0 = (Layer) arg0;
179: Layer l1 = (Layer) arg1;
180: return (l0.getName().compareTo(l1.getName()));
181: }
182: }
183:
184: public List<Action> getActions() {
185: List<Action> actions = new ArrayList<Action>();
186: actions.add(new EditLayerAction());
187: actions.add(new RemoveAction());
188: return Collections.unmodifiableList(actions);
189: }
190:
191: public JComponent getNavigator() {
192: //TODO : implement this
193: return null;
194: }
195:
196: public class EditLayerAction extends AbstractAction {
197: {
198: this .putValue(NAME, NbBundle.getMessage(Layer.class,
199: "Layer.editAction", getDisplayableTypeName()));
200: }
201:
202: public void actionPerformed(ActionEvent e) {
203: Layer.this .gameDesign.getMainView().requestEditing(
204: Layer.this );
205: }
206: }
207:
208: public class RemoveAction extends AbstractAction {
209: {
210: this .putValue(NAME, NbBundle.getMessage(Layer.class,
211: "Layer.removeAction", getDisplayableTypeName()));
212: }
213:
214: public void actionPerformed(ActionEvent e) {
215: Scene scene = (Scene) this
216: .getValue(Layer.ACTION_PROP_SCENE);
217: //if we were called in the context of a scene we remove the layer from a scene
218: if (scene != null) {
219: scene.remove(Layer.this );
220: }
221: //else delete completely
222: else {
223: Object response = DialogDisplayer.getDefault().notify(
224: new NotifyDescriptor(NbBundle.getMessage(
225: Layer.class, "Layer.removeDialog.text",
226: new Object[] {
227: getDisplayableTypeName(),
228: getName() }), NbBundle
229: .getMessage(Layer.class,
230: "Layer.removeDialog.title"),
231: NotifyDescriptor.YES_NO_OPTION,
232: NotifyDescriptor.QUESTION_MESSAGE,
233: new Object[] {
234: NotifyDescriptor.YES_OPTION,
235: NotifyDescriptor.NO_OPTION },
236: NotifyDescriptor.YES_OPTION));
237: if (response == NotifyDescriptor.YES_OPTION) {
238: Layer.this .gameDesign.removeLayer(Layer.this );
239: }
240: }
241: }
242: }
243:
244: public long getId() {
245: return id;
246: }
247:
248: public void setId(long id) {
249: this.id = id;
250: }
251:
252: }
|