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.editor.grid;
043:
044: import java.awt.Point;
045: import java.awt.dnd.DnDConstants;
046: import java.awt.dnd.DragGestureEvent;
047: import java.awt.dnd.DragGestureListener;
048: import java.awt.dnd.DragGestureRecognizer;
049: import java.awt.dnd.DragSource;
050: import java.awt.dnd.DragSourceAdapter;
051: import java.awt.dnd.DragSourceDropEvent;
052: import java.awt.event.FocusAdapter;
053: import java.awt.event.FocusEvent;
054: import javax.swing.JList;
055: import javax.swing.event.ListSelectionEvent;
056: import javax.swing.event.ListSelectionListener;
057: import org.netbeans.modules.vmd.game.editor.common.TileCellRenderer;
058: import org.netbeans.modules.vmd.game.model.ImageResource;
059: import org.netbeans.modules.vmd.game.model.StaticTile;
060: import org.netbeans.modules.vmd.game.model.Tile;
061: import org.netbeans.modules.vmd.game.model.TileTransferable;
062:
063: /**
064: *
065: * @author kherink
066: */
067: class ResourceImageList extends JList {
068:
069: public static final boolean DEBUG = false;
070:
071: private int padX = 4;
072: private int padY = 4;
073:
074: /** Creates a new instance of ResourceImageList */
075: public ResourceImageList() {
076: this .setModel(new ResourceImageListModel());
077: this .setCellRenderer(new TileCellRenderer(padX, padY));
078: this .setLayoutOrientation(JList.HORIZONTAL_WRAP);
079: this .setVisibleRowCount(-1);
080: this
081: .addListSelectionListener(new ResourceImageListSelectionListener());
082: this .addFocusListener(new FocusAdapter() {
083: public void focusGained(FocusEvent e) {
084: int index = ResourceImageList.this .getSelectedIndex();
085: if (index != -1) {
086: StaticTile tile = (StaticTile) ResourceImageList.this
087: .getModel().getElementAt(index);
088: tile.getImageResource().getGameDesign()
089: .getMainView().requestPreview(tile);
090: tile.getImageResource().getGameDesign()
091: .getMainView().paintTileChanged(tile);
092: }
093: }
094: });
095: DragSource dragSource = new DragSource();
096: DragGestureRecognizer dgr = dragSource
097: .createDefaultDragGestureRecognizer(this ,
098: DnDConstants.ACTION_COPY, new DGL());
099: }
100:
101: public void setImageResource(ImageResource imageResource,
102: int tileWidth, int tileHeight, boolean zeroBasedIndex) {
103: ((ResourceImageListModel) this .getModel()).update(
104: imageResource, tileWidth, tileHeight, zeroBasedIndex);
105: }
106:
107: private class DGL extends DragSourceAdapter implements
108: DragGestureListener {
109:
110: public void dragGestureRecognized(DragGestureEvent dge) {
111: Point dragOrigin = dge.getDragOrigin();
112: TileTransferable payload = new TileTransferable();
113: //Dragging a single tile
114: if (ResourceImageList.this .getSelectedValues().length == 0) {
115: if (DEBUG)
116: System.out.println("selection Empty"); // NOI18N
117: int index = ResourceImageList.this
118: .locationToIndex(dragOrigin);
119: Tile tile = (Tile) ResourceImageList.this .getModel()
120: .getElementAt(index);
121: payload.getTiles().add(tile);
122: ResourceImageList.this .hiliteTileAtPoint(dragOrigin);
123: }
124: //Dragging multiple tiles
125: else {
126: if (DEBUG)
127: System.out.println("selection Not empty"); // NOI18N
128: Object[] values = ResourceImageList.this
129: .getSelectedValues();
130: for (int i = 0; i < values.length; i++) {
131: payload.getTiles().add((Tile) values[i]);
132: }
133: }
134: dge.startDrag(null, payload, this );
135: }
136:
137: public void dragDropEnd(DragSourceDropEvent dsde) {
138: super .dragDropEnd(dsde);
139: if (dsde.getDropSuccess()) {
140: if (DEBUG)
141: System.out.println("Drop successful"); // NOI18N
142: } else {
143: if (DEBUG)
144: System.out.println("Drop unsuccessful"); // NOI18N
145: }
146: }
147:
148: }
149:
150: private class ResourceImageListSelectionListener implements
151: ListSelectionListener {
152: public void valueChanged(ListSelectionEvent e) {
153: if (DEBUG)
154: System.out
155: .println("GridTableSelectionListener.valueChanged()"); // NOI18N
156: int index = ResourceImageList.this .getSelectedIndex();
157: this .handleTileSelection(index);
158: }
159:
160: private void handleTileSelection(int index) {
161: if (DEBUG)
162: System.out.println("Tile selected: " + index); // NOI18N
163: StaticTile tile = (StaticTile) ResourceImageList.this
164: .getModel().getElementAt(index);
165: tile.getImageResource().getGameDesign().getMainView()
166: .requestPreview(tile);
167: tile.getImageResource().getGameDesign().getMainView()
168: .paintTileChanged(tile);
169: }
170: }
171:
172: private void hiliteTileAtPoint(Point point) {
173: if (DEBUG)
174: System.out.println("HILITE tile"); // NOI18N
175: int hilite = this .locationToIndex(point);
176: this .setSelectedIndex(hilite);
177: }
178:
179: public Tile getSelectedTile() {
180: int selection = this .getSelectedIndex();
181: return (Tile) this .getModel().getElementAt(selection);
182: }
183:
184: public java.awt.Dimension getPreferredSize() {
185: return this.getMaximumSize();
186: }
187:
188: }
|