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.view.main;
042:
043: import org.netbeans.modules.vmd.game.editor.grid.ResourceImageEditor;
044: import org.netbeans.modules.vmd.game.editor.tiledlayer.TiledLayerEditor;
045: import org.netbeans.modules.vmd.game.model.*;
046: import javax.swing.*;
047: import java.awt.*;
048: import java.awt.Frame;
049: import java.util.HashMap;
050: import java.util.Map;
051: import javax.swing.event.EventListenerList;
052: import org.netbeans.modules.vmd.game.model.Editable.ImageResourceInfo;
053:
054: public class MainView implements GlobalRepositoryListener,
055: EditorManager {
056:
057: public static final boolean DEBUG = false;
058:
059: EventListenerList listenerList = new EventListenerList();
060:
061: private GlobalRepository gameDesign;
062: private Editable currentEditable;
063:
064: private JPanel rootPanel;
065: private JSplitPane mainSplit;
066: private JSplitPane previewExplorerSplit;
067:
068: //the right panel contains editor and optional resource grid
069: private JPanel mainEditorPanel;
070:
071: Map<Editable, JSplitPane> splits = new HashMap<Editable, JSplitPane>();
072:
073: public MainView(GlobalRepository gameDesign) {
074: this .gameDesign = gameDesign;
075: this .initComponents();
076: this .initLayout();
077: }
078:
079: private void initComponents() {
080: this .rootPanel = new JPanel();
081: this .rootPanel.setLayout(new BorderLayout());
082: this .rootPanel.setBackground(Color.WHITE);
083:
084: //editor components
085: this .mainEditorPanel = new JPanel();
086: this .mainEditorPanel.setBorder(null);
087: this .mainEditorPanel.setBackground(Color.WHITE);
088: }
089:
090: private void initLayout() {
091: this .mainEditorPanel.setLayout(new BorderLayout());
092:
093: //layout the main window
094: this .mainSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
095: false, this .previewExplorerSplit, this .mainEditorPanel);
096: this .mainSplit.setDividerLocation(230);
097: this .mainSplit.setDividerSize(5);
098: this .mainSplit.setOneTouchExpandable(false);
099: this .mainSplit.setResizeWeight(0.0);
100:
101: //TODO: here implement the toolbar panel
102: //this.rootPanel.add(this.toolbarPanel);
103:
104: this .rootPanel.add(this .mainEditorPanel, BorderLayout.CENTER);
105:
106: }
107:
108: private class EditorDisposer implements Runnable {
109: public void run() {
110: MainView.this .currentEditable = null;
111: MainView.this .mainEditorPanel.removeAll();
112: MainView.this .mainEditorPanel.repaint();
113: MainView.this .mainEditorPanel.validate();
114: }
115: }
116:
117: public void paintTileChanged(Tile tile) {
118: if (DEBUG)
119: System.out.println("MainView - paint tile changed " + tile); // NOI18N
120: if (tile == null) {
121: return;
122: }
123: Editable editable = this .currentEditable;
124: if (editable == null) {
125: return;
126: }
127: JComponent editor = editable.getEditor();
128: if (editor instanceof TiledLayerEditor) {
129: TiledLayerEditor tiledLayerEditor = (TiledLayerEditor) editor;
130: tiledLayerEditor.setPaintTile(tile);
131: }
132: }
133:
134: //--------GlobalRepositoryListener
135: public void sceneAdded(Scene scene, int index) {
136: this .requestEditing(scene);
137: }
138:
139: public void sceneRemoved(Scene scene, int index) {
140: this .requestEditing(this .gameDesign);
141: }
142:
143: public void tiledLayerAdded(final TiledLayer tiledLayer, int index) {
144: this .requestEditing(tiledLayer);
145: }
146:
147: public void tiledLayerRemoved(TiledLayer tiledLayer, int index) {
148: this .requestEditing(this .gameDesign);
149: }
150:
151: public void spriteAdded(Sprite sprite, int index) {
152: if (this .rootPanel.isShowing())
153: this .requestEditing(sprite);
154: }
155:
156: public void spriteRemoved(Sprite sprite, int index) {
157: this .requestEditing(this .gameDesign);
158: }
159:
160: public void imageResourceAdded(ImageResource imageResource) {
161: }
162:
163: public void requestEditing(Editable editable) {
164: if (this .currentEditable == editable) {
165: return;
166: }
167:
168: this .currentEditable = editable;
169:
170: ImageResourceInfo resource = this .currentEditable
171: .getImageResourceInfo();
172: JComponent editor = this .currentEditable.getEditor();
173:
174: this .mainEditorPanel.removeAll();
175:
176: if (resource != null) {
177: JSplitPane split = this .splits.get(this .currentEditable);
178: if (split == null) {
179: split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, false);
180: split.setDividerLocation(400);
181: split.setResizeWeight(1.0);
182: split.setOneTouchExpandable(false);
183: split.setDividerSize(5);
184:
185: split.setTopComponent(editor);
186:
187: ResourceImageEditor resourceImageView = new ResourceImageEditor();
188: resourceImageView.setImageResourceInfo(resource);
189: split.setBottomComponent(resourceImageView);
190:
191: split.setOneTouchExpandable(false);
192: split.setDividerSize(5);
193: this .splits.put(this .currentEditable, split);
194: }
195: this .mainEditorPanel.add(split);
196: } else {
197: this .mainEditorPanel.add(editor);
198: }
199: this .mainEditorPanel.repaint();
200: this .mainEditorPanel.validate();
201:
202: this .fireEditingChanged(this .currentEditable);
203: }
204:
205: private void fireEditingChanged(Editable editable) {
206: Object[] listeners = listenerList.getListenerList();
207: for (int i = listeners.length - 2; i >= 0; i -= 2) {
208: if (listeners[i] == EditorManagerListener.class) {
209: ((EditorManagerListener) listeners[i + 1])
210: .editing(editable);
211: }
212: }
213: }
214:
215: public void requestPreview(Previewable previewable) { // NOI18N
216: if (DEBUG)
217: System.out.println("Setting preview for: " + previewable);
218: JComponent previewComponent = previewable.getPreview();
219: if (previewComponent != null) {
220: if (DEBUG)
221: System.out.println("PREVIEW REQUESTED COMPONENT FOR: "
222: + previewable.getClass().getName()); // NOI18N
223: return;
224: }
225: if (DEBUG)
226: System.out
227: .println("!!! CREATE PREVIEW COMPONENT FOR: "
228: + previewable.getClass().getName()
229: + " by implementing Previewable.getPreview() RIGHT NOW !!!"); // NOI18N
230: }
231:
232: public void closeEditor(Editable editable) {
233: if (currentEditable == editable) {
234: SwingUtilities.invokeLater(new EditorDisposer());
235: }
236: }
237:
238: public static void center(JFrame frame) {
239: GraphicsEnvironment ge = GraphicsEnvironment
240: .getLocalGraphicsEnvironment();
241: Point center = ge.getCenterPoint();
242: Rectangle bounds = ge.getMaximumWindowBounds();
243: int w = Math.min(frame.getWidth(), bounds.width);
244: int h = Math.min(frame.getHeight(), bounds.height);
245: int x = center.x - w / 2, y = center.y - h / 2;
246: frame.setBounds(x, y, w, h);
247: if (w == bounds.width && h == bounds.height)
248: frame.setExtendedState(Frame.MAXIMIZED_BOTH);
249: frame.validate();
250: }
251:
252: public JComponent getRootComponent() {
253: return this .rootPanel;
254: }
255:
256: public void addEditorManagerListener(EditorManagerListener l) {
257: this .listenerList.add(EditorManagerListener.class, l);
258: }
259:
260: public void removeEditorManagerListener(EditorManagerListener l) {
261: this .listenerList.remove(EditorManagerListener.class, l);
262: }
263:
264: public Editable getCurrentEditable() {
265: return this.currentEditable;
266: }
267:
268: }
|