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:
042: package org.netbeans.modules.vmd.game;
043:
044: import org.netbeans.modules.vmd.api.io.DataEditorView;
045: import org.netbeans.modules.vmd.api.io.DataObjectContext;
046: import org.netbeans.modules.vmd.api.properties.common.PropertiesSupport;
047: import org.netbeans.modules.vmd.game.dialog.NewSceneDialog;
048: import org.netbeans.modules.vmd.game.dialog.NewSimpleTiledLayerDialog;
049: import org.netbeans.modules.vmd.game.model.Editable;
050: import org.netbeans.modules.vmd.game.model.EditorManagerListener;
051: import org.netbeans.modules.vmd.game.model.GlobalRepository;
052: import org.netbeans.modules.vmd.game.model.adapter.GlobalRepositoryComboBoxModel;
053: import org.netbeans.modules.vmd.game.nbdialog.SpriteDialog;
054: import org.netbeans.modules.vmd.game.nbdialog.TiledLayerDialog;
055: import org.openide.DialogDescriptor;
056: import org.openide.DialogDisplayer;
057: import org.openide.util.NbBundle;
058:
059: import javax.swing.*;
060: import java.awt.*;
061: import java.awt.event.ActionEvent;
062: import java.awt.event.ActionListener;
063: import java.io.IOException;
064: import java.util.Collection;
065: import java.util.Collections;
066: import org.netbeans.modules.vmd.api.io.providers.IOSupport;
067:
068: /**
069: *
070: * @author Karel Herink
071: */
072: public class GameEditorView implements DataEditorView,
073: EditorManagerListener {
074:
075: private static final long serialVersionUID = 3317521472849153199L;
076:
077: public transient static final boolean DEBUG = false;
078:
079: private DataObjectContext context;
080: private transient GameController controller;
081: private transient GlobalRepository gameDesign;
082:
083: private transient JComponent toolBarRepresentation;
084: private transient JComboBox comboGlobal;
085:
086: private transient boolean firstTime = false;
087:
088: /** Creates a new instance of GameEditorView */
089: public GameEditorView(DataObjectContext context) {
090: this .context = context;
091: init();
092: }
093:
094: private void init() {
095: this .controller = new GameController(context, this );
096: if (this .controller.getGameDesign() != null) {
097: this .controller.getGameDesign().getMainView()
098: .addEditorManagerListener(this );
099: }
100: }
101:
102: public void setGameDesign(GlobalRepository gameDesign) {
103: if (this .gameDesign == gameDesign) {
104: return;
105: }
106: if (this .gameDesign != null) {
107: this .gameDesign.getMainView().removeEditorManagerListener(
108: this );
109: }
110:
111: this .gameDesign = gameDesign;
112:
113: if (this .gameDesign != null) {
114: if (this .comboGlobal == null) {
115: this .comboGlobal = new JComboBox();
116: }
117: GlobalRepositoryComboBoxModel model = new GlobalRepositoryComboBoxModel();
118: model.setGameDesign(gameDesign);
119: this .comboGlobal.setModel(model);
120: }
121:
122: }
123:
124: public DataObjectContext getContext() {
125: return this .context;
126: }
127:
128: public DataEditorView.Kind getKind() {
129: return DataEditorView.Kind.MODEL;
130: }
131:
132: public boolean canShowSideWindows() {
133: return false;
134: }
135:
136: public Collection<String> getTags() {
137: return Collections
138: .singleton(PropertiesSupport.DO_NOT_OPEN_PROPERTIES_WINDOW_TAG);
139: }
140:
141: public String preferredID() {
142: return GameController.PROJECT_TYPE_GAME;
143: }
144:
145: public String getDisplayName() {
146: return NbBundle.getMessage(GameEditorView.class,
147: "GameEditorView.DisplayName");
148: }
149:
150: public org.openide.util.HelpCtx getHelpCtx() {
151: return null;
152: }
153:
154: public JComponent getVisualRepresentation() {
155: return this .controller.getVisualRepresentation();
156: }
157:
158: public JComponent getToolbarRepresentation() {
159: if (this .toolBarRepresentation == null) {
160: JToolBar tool = new JToolBar();
161: tool.setFloatable(false);
162: tool.setRollover(true);
163:
164: tool.addSeparator();
165:
166: if (this .comboGlobal == null) {
167: this .comboGlobal = new JComboBox();
168: }
169:
170: comboGlobal.setMaximumRowCount(16);
171: comboGlobal.setRenderer(new DefaultListCellRenderer() {
172: public Component getListCellRendererComponent(
173: JList list, Object value, int index,
174: boolean isSelected, boolean cellHasFocus) {
175: DefaultListCellRenderer retValue = (DefaultListCellRenderer) super
176: .getListCellRendererComponent(list, value,
177: index, isSelected, cellHasFocus);
178: if (value instanceof GlobalRepository) {
179: retValue.setFont(retValue.getFont().deriveFont(
180: Font.BOLD));
181: } else if (value instanceof String) {
182: retValue.setFont(retValue.getFont().deriveFont(
183: Font.BOLD));
184: retValue.setText(" " + retValue.getText());
185: } else {
186: retValue.setText(" " + retValue.getText());
187: }
188: return retValue;
189: }
190: });
191: comboGlobal.addActionListener(new ActionListener() {
192: public void actionPerformed(ActionEvent e) {
193: Object item = comboGlobal.getSelectedItem();
194: if (item instanceof Editable) {
195: if (DEBUG)
196: System.out.println("Request editing for: "
197: + item); // NOI18N
198: gameDesign.getMainView().requestEditing(
199: (Editable) item);
200: } else if (item instanceof String) {
201: gameDesign.getMainView().requestEditing(
202: gameDesign);
203: }
204: }
205: });
206: if (gameDesign != null) {
207: comboGlobal.setSelectedItem(gameDesign.getMainView()
208: .getCurrentEditable());
209: }
210:
211: tool.add(comboGlobal);
212: tool.addSeparator();
213:
214: JButton buttonCreateScene = new JButton(new ImageIcon(this
215: .getClass().getResource("res/new_scene_16.png"))); // NOI18N
216: buttonCreateScene.setToolTipText(NbBundle.getMessage(
217: GameEditorView.class,
218: "GameEditorView.buttonCreateScene.tooltip"));
219: buttonCreateScene.setBorderPainted(false);
220: buttonCreateScene.setRolloverEnabled(true);
221: buttonCreateScene.setSize(14, 14);
222: buttonCreateScene.addActionListener(new ActionListener() {
223: public void actionPerformed(ActionEvent e) {
224: NewSceneDialog dialog = new NewSceneDialog(
225: gameDesign);
226: DialogDescriptor dd = new DialogDescriptor(dialog,
227: NbBundle.getMessage(NewSceneDialog.class,
228: "NewSceneDialog.title.text"));
229: dd.setButtonListener(dialog);
230: dd.setValid(false);
231: dialog.setDialogDescriptor(dd);
232: Dialog d = DialogDisplayer.getDefault()
233: .createDialog(dd);
234: d.setVisible(true);
235: }
236: });
237:
238: JButton buttonCreateTiledLayer = new JButton(new ImageIcon(
239: this .getClass().getResource(
240: "res/new_tiled_layer_16.png"))); // NOI18N
241: buttonCreateTiledLayer.setToolTipText(NbBundle.getMessage(
242: GameEditorView.class,
243: "GameEditorView.buttonCreateTiledLayer.tooltip"));
244: buttonCreateTiledLayer.setBorderPainted(false);
245: buttonCreateTiledLayer.setRolloverEnabled(true);
246: buttonCreateTiledLayer.setSize(14, 14);
247: buttonCreateTiledLayer
248: .addActionListener(new ActionListener() {
249: public void actionPerformed(ActionEvent e) {
250: TiledLayerDialog nld = new TiledLayerDialog(
251: gameDesign);
252: DialogDescriptor dd = new DialogDescriptor(
253: nld,
254: NbBundle
255: .getMessage(
256: NewSimpleTiledLayerDialog.class,
257: "NewSimpleTiledLayerDialog.title.text"));
258: dd.setButtonListener(nld);
259: dd.setValid(false);
260: nld.setDialogDescriptor(dd);
261: Dialog d = DialogDisplayer.getDefault()
262: .createDialog(dd);
263: d.setVisible(true);
264: }
265: });
266:
267: JButton buttonCreateSprite = new JButton(new ImageIcon(this
268: .getClass().getResource("res/new_sprite_16.png"))); // NOI18N
269: buttonCreateSprite.setToolTipText(NbBundle.getMessage(
270: GameEditorView.class,
271: "GameEditorView.buttonCreateSprite.tooltip"));
272: buttonCreateSprite.setBorderPainted(false);
273: buttonCreateSprite.setRolloverEnabled(true);
274: buttonCreateSprite.setSize(14, 14);
275: buttonCreateSprite.addActionListener(new ActionListener() {
276: public void actionPerformed(ActionEvent e) {
277: SpriteDialog nld = new SpriteDialog(gameDesign);
278: DialogDescriptor dd = new DialogDescriptor(
279: nld,
280: NbBundle
281: .getMessage(GameEditorView.class,
282: "GameEditorView.buttonCreateSprite.txt"));
283: dd.setButtonListener(nld);
284: dd.setValid(false);
285: nld.setDialogDescriptor(dd);
286: Dialog d = DialogDisplayer.getDefault()
287: .createDialog(dd);
288: d.setVisible(true);
289: }
290: });
291:
292: tool.add(buttonCreateScene);
293: tool.add(buttonCreateTiledLayer);
294: tool.add(buttonCreateSprite);
295:
296: this .toolBarRepresentation = tool;
297: }
298: return this .toolBarRepresentation;
299: }
300:
301: public org.openide.awt.UndoRedo getUndoRedo() {
302: if (firstTime) {
303: IOSupport.getDocumentSerializer(context.getDataObject())
304: .getUndoRedoManager().setLimit(100);
305: }
306: return null;
307: }
308:
309: public void discardAllEdits() {
310: IOSupport.getDocumentSerializer(context.getDataObject())
311: .getUndoRedoManager().discardAllEdits();
312: }
313:
314: public void componentOpened() {
315: }
316:
317: public void componentClosed() {
318: }
319:
320: public void componentShowing() {
321: }
322:
323: public void componentHidden() {
324: }
325:
326: public void componentActivated() {
327: }
328:
329: public void componentDeactivated() {
330: }
331:
332: public int getOpenPriority() {
333: return getOrder();
334: }
335:
336: public int getEditPriority() {
337: return -getOrder();
338: }
339:
340: public int getOrder() {
341: return 500;
342: }
343:
344: //EditorManagerListener
345: public void editing(Editable e) {
346: if (DEBUG)
347: System.out.println("EDITING: " + e); // NOI18N
348: if (comboGlobal != null) {
349: comboGlobal.setSelectedItem(e);
350: comboGlobal.repaint();
351: }
352: }
353:
354: private void writeObject(java.io.ObjectOutputStream out)
355: throws IOException {
356: out.writeObject(context);
357: }
358:
359: private void readObject(java.io.ObjectInputStream in)
360: throws IOException, ClassNotFoundException {
361: Object object = in.readObject();
362: if (!(object instanceof DataObjectContext))
363: throw new ClassNotFoundException(
364: "DataObjectContext expected but not found"); // NOI18N
365: context = (DataObjectContext) object;
366: init();
367: }
368:
369: }
|