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.editor.scene;
042:
043: import java.awt.Component;
044: import java.awt.Dimension;
045: import java.awt.Point;
046: import java.awt.datatransfer.Transferable;
047: import java.awt.datatransfer.UnsupportedFlavorException;
048: import java.awt.dnd.DnDConstants;
049: import java.awt.dnd.DragGestureEvent;
050: import java.awt.dnd.DragGestureListener;
051: import java.awt.dnd.DragGestureRecognizer;
052: import java.awt.dnd.DragSource;
053: import java.awt.dnd.DragSourceAdapter;
054: import java.awt.dnd.DragSourceDropEvent;
055: import java.awt.dnd.DropTarget;
056: import java.awt.dnd.DropTargetAdapter;
057: import java.awt.dnd.DropTargetDropEvent;
058: import java.awt.dnd.DropTargetEvent;
059: import java.io.IOException;
060: import javax.swing.JTable;
061: import javax.swing.ListSelectionModel;
062: import javax.swing.SwingConstants;
063: import javax.swing.event.ListSelectionEvent;
064: import javax.swing.event.ListSelectionListener;
065: import javax.swing.table.DefaultTableCellRenderer;
066: import javax.swing.table.TableColumn;
067: import org.netbeans.modules.vmd.game.model.Layer;
068: import org.netbeans.modules.vmd.game.model.LayerDataFlavor;
069: import org.netbeans.modules.vmd.game.model.Scene;
070: import org.netbeans.modules.vmd.game.model.adapter.SceneLayerTableAdapter;
071:
072: public class SceneLayerNavigator extends JTable {
073:
074: public static final boolean DEBUG = false;
075:
076: public static final int PAD_X = 4;
077: public static final int PAD_Y = 4;
078:
079: private static final int IMG_PREVIEW_WIDTH = 40;
080: private static final int IMG_PREVIEW_HEIGHT = 30;
081:
082: private Scene scene;
083:
084: public SceneLayerNavigator(Scene layerModel) {
085: this .scene = layerModel;
086: this .setModel(new SceneLayerTableAdapter(layerModel));
087: this .getColumnModel().setColumnMargin(0);
088:
089: //Dnd
090: DragSource dragSource = new DragSource();
091: DragGestureRecognizer dragGestureRecognizer = dragSource
092: .createDefaultDragGestureRecognizer(this ,
093: DnDConstants.ACTION_MOVE, new DGL());
094: DropTarget dropTarget = new DropTarget(this ,
095: new TableDropTarget());
096: dropTarget.setActive(true);
097: this .setDropTarget(dropTarget);
098:
099: this .getSelectionModel().addListSelectionListener(
100: new SceneTableSelectionListener());
101:
102: this .setRowHeight(IMG_PREVIEW_HEIGHT);
103: int width = IMG_PREVIEW_WIDTH /* + 2*PAD_X */
104: + this .getColumnModel().getColumnMargin();
105:
106: TableColumn typeColumn = this .getColumnModel().getColumn(
107: SceneLayerTableAdapter.COL_INDEX_LAYER_TYPE);
108: typeColumn.setPreferredWidth(width);
109: typeColumn.setMaxWidth(width);
110: typeColumn.setMinWidth(width);
111:
112: TableColumn indexColumn = this .getColumnModel().getColumn(
113: SceneLayerTableAdapter.COL_INDEX_LAYER_INDEX);
114: indexColumn.setPreferredWidth(width);
115: indexColumn.setMaxWidth(width);
116: indexColumn.setMinWidth(width);
117:
118: TableColumn visibilityColumn = this
119: .getColumnModel()
120: .getColumn(
121: SceneLayerTableAdapter.COL_INDEX_LAYER_VISIBILITY_INDICATOR);
122: visibilityColumn.setPreferredWidth(width);
123: visibilityColumn.setMaxWidth(width);
124: visibilityColumn.setMinWidth(width);
125:
126: TableColumn lockColumn = this .getColumnModel().getColumn(
127: SceneLayerTableAdapter.COL_INDEX_LAYER_LOCK_INDICATOR);
128: lockColumn.setPreferredWidth(width);
129: lockColumn.setMaxWidth(width);
130: lockColumn.setMinWidth(width);
131:
132: this .setDefaultEditor(Boolean.class,
133: new BooleanTableCellRenderer(PAD_X, PAD_Y));
134: this .setDefaultRenderer(Boolean.class,
135: new BooleanTableCellRenderer(PAD_X, PAD_Y));
136:
137: this .setDefaultRenderer(Layer.class,
138: new LayerTableCellRenderer());
139:
140: this .setDefaultRenderer(Integer.class,
141: new DefaultTableCellRenderer() {
142: public Component getTableCellRendererComponent(
143: JTable table, Object value,
144: boolean isSelected, boolean hasFocus,
145: int row, int column) {
146: this
147: .setHorizontalAlignment(SwingConstants.CENTER);
148: return super .getTableCellRendererComponent(
149: table, value, isSelected, hasFocus,
150: row, column);
151: }
152: });
153:
154: TableColumn posXColumn = this .getColumnModel().getColumn(
155: SceneLayerTableAdapter.COL_INDEX_LAYER_POS_X);
156: posXColumn.setPreferredWidth(width + 10);
157: posXColumn.setMaxWidth(width + 10);
158: posXColumn.setMinWidth(width + 10);
159:
160: TableColumn posYColumn = this .getColumnModel().getColumn(
161: SceneLayerTableAdapter.COL_INDEX_LAYER_POS_Y);
162: posYColumn.setPreferredWidth(width + 10);
163: posYColumn.setMaxWidth(width + 10);
164: posYColumn.setMinWidth(width + 10);
165:
166: this .setShowVerticalLines(false);
167: this .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
168:
169: }
170:
171: public Dimension getPreferredScrollableViewportSize() {
172: return super .getPreferredSize();
173: }
174:
175: private class SceneTableSelectionListener implements
176: ListSelectionListener {
177:
178: public void valueChanged(ListSelectionEvent e) {
179: if (e.getValueIsAdjusting()) {
180: return;
181: }
182: ListSelectionModel lsm = (ListSelectionModel) e.getSource();
183: if (lsm.isSelectionEmpty())
184: return;
185: int selectedLayerIndex = SceneLayerNavigator.this
186: .getSelectionModel().getAnchorSelectionIndex();
187: Layer selectedLayer = SceneLayerNavigator.this .scene
188: .getLayerAt(selectedLayerIndex);
189: if (DEBUG)
190: System.out.println(selectedLayer
191: + " has been selected."); // NOI18N
192: selectedLayer.getGameDesign().getMainView().requestPreview(
193: selectedLayer);
194: }
195: }
196:
197: //DnD implementation
198: private class DGL extends DragSourceAdapter implements
199: DragGestureListener {
200: public void dragGestureRecognized(DragGestureEvent dge) {
201: Point dragOrigin = dge.getDragOrigin();
202: int srcRow = SceneLayerNavigator.this
203: .rowAtPoint(dragOrigin);
204: if (DEBUG)
205: System.out.println("dragGestureRecognized @ "
206: + dragOrigin + " row: " + srcRow); // NOI18N
207: Layer payload = (Layer) SceneLayerNavigator.this .scene
208: .getLayerAt(srcRow);
209: //Cursor.getPredefinedCursor(Cursor.getSystemCustomCursor(null));
210: if (DEBUG)
211: System.out.println("payload = " + payload); // NOI18N
212: SceneLayerNavigator.this .setRowSelectionInterval(srcRow,
213: srcRow);
214: dge.startDrag(null, payload, this );
215: }
216:
217: public void dragDropEnd(DragSourceDropEvent dsde) {
218: super .dragDropEnd(dsde);
219: if (dsde.getDropSuccess()) {
220: if (DEBUG)
221: System.out.println("Drop End - success"); // NOI18N
222: } else {
223: if (DEBUG)
224: System.out.println("Drop End - failure!!!"); // NOI18N
225: }
226: }
227: }
228:
229: private class TableDropTarget extends DropTargetAdapter {
230: public void drop(DropTargetDropEvent dtde) {
231: Point dropPoint = dtde.getLocation();
232: if (DEBUG)
233: System.out.println("Start drop @: " + dropPoint); // NOI18N
234: int dropRow = SceneLayerNavigator.this
235: .rowAtPoint(dropPoint);
236: Transferable transferable = dtde.getTransferable();
237: try {
238: LayerDataFlavor layerFlavor = new LayerDataFlavor();
239: if (transferable.isDataFlavorSupported(layerFlavor)) {
240: dtde.acceptDrop(DnDConstants.ACTION_MOVE);
241: Layer layer = (Layer) transferable
242: .getTransferData(layerFlavor);
243: SceneLayerNavigator.this .scene.insert(layer,
244: dropRow);
245: dtde.dropComplete(true);
246: } else {
247: if (DEBUG)
248: System.out.println("NOT a Layer ... weird."); // NOI18N
249: }
250: } catch (ClassNotFoundException e) {
251: e.printStackTrace();
252: dtde.dropComplete(false);
253: } catch (UnsupportedFlavorException e) {
254: e.printStackTrace();
255: dtde.dropComplete(false);
256: } catch (IOException e) {
257: e.printStackTrace();
258: dtde.dropComplete(false);
259: }
260: }
261:
262: public void dragExit(DropTargetEvent dte) {
263: if (DEBUG)
264: System.out.println("dragExit"); // NOI18N
265: }
266: }
267: }
|