001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package wingset;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017: import org.wings.SBoxLayout;
018: import org.wings.SButton;
019: import org.wings.SComponent;
020: import org.wings.SConstants;
021: import org.wings.SDimension;
022: import org.wings.SGridLayout;
023: import org.wings.SIcon;
024: import org.wings.SLabel;
025: import org.wings.SPanel;
026: import org.wings.SURLIcon;
027: import org.wings.dnd.DragSource;
028: import org.wings.dnd.DropTarget;
029: import org.wings.event.SComponentDropListener;
030: import org.wings.style.CSSProperty;
031:
032: import java.awt.event.ActionEvent;
033: import java.util.ArrayList;
034: import java.util.List;
035: import java.util.Random;
036:
037: /**
038: * example for showing the drag and drop capabilities of wingS.
039: * And "it's got wingS ;)"
040: * @author ole
041: *
042: */
043: public class DragAndDropExample extends WingSetPane {
044: private final static Log log = LogFactory
045: .getLog(DragAndDropExample.class);
046:
047: private static final SURLIcon ICON_BEE1 = new SURLIcon(
048: "../icons/bee_1.jpg", 100, 100);
049: private static final SURLIcon ICON_BEE2 = new SURLIcon(
050: "../icons/bee_2.jpg", 100, 100);
051: private static final SURLIcon ICON_BEE3 = new SURLIcon(
052: "../icons/bee_3.jpg", 100, 100);
053: private static final SURLIcon ICON_BEE4 = new SURLIcon(
054: "../icons/bee_4.jpg", 100, 100);
055: private static final SURLIcon ICON_BEE5 = new SURLIcon(
056: "../icons/bee_5.jpg", 100, 100);
057: private static final SURLIcon ICON_BEE6 = new SURLIcon(
058: "../icons/bee_6.jpg", 100, 100);
059: private static final SURLIcon ICON_BEE7 = new SURLIcon(
060: "../icons/bee_7.jpg", 100, 100);
061: private static final SURLIcon ICON_BEE8 = new SURLIcon(
062: "../icons/bee_8.jpg", 100, 100);
063: private static final SURLIcon ICON_BEE9 = new SURLIcon(
064: "../icons/bee_9.jpg", 100, 100);
065:
066: private static final SURLIcon ICON_BEE1_SMALL = new SURLIcon(
067: "../icons/bee_1.jpg", 30, 30);
068: private static final SURLIcon ICON_BEE2_SMALL = new SURLIcon(
069: "../icons/bee_2.jpg", 30, 30);
070: private static final SURLIcon ICON_BEE3_SMALL = new SURLIcon(
071: "../icons/bee_3.jpg", 30, 30);
072: private static final SURLIcon ICON_BEE4_SMALL = new SURLIcon(
073: "../icons/bee_4.jpg", 30, 30);
074: private static final SURLIcon ICON_BEE5_SMALL = new SURLIcon(
075: "../icons/bee_5.jpg", 30, 30);
076: private static final SURLIcon ICON_BEE6_SMALL = new SURLIcon(
077: "../icons/bee_6.jpg", 30, 30);
078: private static final SURLIcon ICON_BEE7_SMALL = new SURLIcon(
079: "../icons/bee_7.jpg", 30, 30);
080: private static final SURLIcon ICON_BEE8_SMALL = new SURLIcon(
081: "../icons/bee_8.jpg", 30, 30);
082: private static final SURLIcon ICON_BEE9_SMALL = new SURLIcon(
083: "../icons/bee_9.jpg", 30, 30);
084:
085: private final SDragLabel dragIconOne = new SDragLabel();
086: private final SDragLabel dragIconTwo = new SDragLabel();
087: private final SDragLabel dragIconThree = new SDragLabel();
088: private final SDragLabel dragIconFour = new SDragLabel();
089: private final SDragLabel dragIconFive = new SDragLabel();
090: private final SDragLabel dragIconSix = new SDragLabel();
091: private final SDragLabel dragIconSeven = new SDragLabel();
092: private final SDragLabel dragIconEight = new SDragLabel();
093: private final SDragLabel dragIconNine = new SDragLabel();
094:
095: private final SDropLabel dropIconOne = new SDropLabel();
096: private final SDropLabel dropIconTwo = new SDropLabel();
097: private final SDropLabel dropIconThree = new SDropLabel();
098: private final SDropLabel dropIconFour = new SDropLabel();
099: private final SDropLabel dropIconFive = new SDropLabel();
100: private final SDropLabel dropIconSix = new SDropLabel();
101: private final SDropLabel dropIconSeven = new SDropLabel();
102: private final SDropLabel dropIconEight = new SDropLabel();
103: private final SDropLabel dropIconNine = new SDropLabel();
104:
105: private final SIcon[] beeIcons = new SIcon[] { ICON_BEE1,
106: ICON_BEE2, ICON_BEE3, ICON_BEE4, ICON_BEE5, ICON_BEE6,
107: ICON_BEE7, ICON_BEE8, ICON_BEE9 };
108: private final SIcon[] beeIconsSmall = new SIcon[] {
109: ICON_BEE1_SMALL, ICON_BEE2_SMALL, ICON_BEE3_SMALL,
110: ICON_BEE4_SMALL, ICON_BEE5_SMALL, ICON_BEE6_SMALL,
111: ICON_BEE7_SMALL, ICON_BEE8_SMALL, ICON_BEE9_SMALL };
112: private final SDragLabel[] dragIcons = new SDragLabel[] {
113: dragIconOne, dragIconTwo, dragIconThree, dragIconFour,
114: dragIconFive, dragIconSix, dragIconSeven, dragIconEight,
115: dragIconNine };
116: private final SDropLabel[] dropIcons = new SDropLabel[] {
117: dropIconOne, dropIconTwo, dropIconThree, dropIconFour,
118: dropIconFive, dropIconSix, dropIconSeven, dropIconEight,
119: dropIconNine };
120:
121: private int[] shuffleTable = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
122:
123: private int piecesRight;
124: private final SLabel statusLabel = new SLabel();
125:
126: private ComponentControls controls;
127:
128: protected SComponent createControls() {
129: controls = new Controls();
130: return controls;
131: }
132:
133: protected SComponent createExample() {
134: final SPanel container = new SPanel(new SBoxLayout(
135: SBoxLayout.HORIZONTAL));
136: final SPanel puzzleContainer = new SPanel(new SGridLayout(2, 1,
137: 0, 20));
138:
139: // initialize the drag components
140: for (int i = 0; i < dragIcons.length; i++) {
141: dragIcons[i].setDragEnabled(true);
142: dragIcons[i].setPreferredSize(new SDimension(30, 30));
143: }
144:
145: // init the shuffle transformation table
146: Random random = new Random();
147: for (int i = 0; i < shuffleTable.length; i++) {
148: int swapWith = random.nextInt(shuffleTable.length);
149: int oldVal = shuffleTable[i];
150: shuffleTable[i] = shuffleTable[swapWith];
151: shuffleTable[swapWith] = oldVal;
152: }
153:
154: // initialize the drop components
155: for (int i = 0; i < dropIcons.length; i++) {
156: final int position = i;
157: final SDropLabel dropIcon = dropIcons[i];
158: dropIcon.setPreferredSize(new SDimension(100, 100));
159: dropIcon
160: .addComponentDropListener(new SComponentDropListener() {
161:
162: // the drag and drop magic
163: public boolean handleDrop(SComponent dragSource) {
164: if (dragIcons[shuffleTable[position]]
165: .equals(dragSource)) {
166: dragIcons[shuffleTable[position]]
167: .setIcon(null);
168: dropIcon.setIcon(beeIcons[position]);
169: piecesRight++;
170: if (piecesRight == 9) {
171: statusLabel
172: .setText("Look, it's got wingS ;) !");
173: statusLabel.setAttribute(
174: CSSProperty.FONT_WEIGHT,
175: "600");
176: statusLabel.setAttribute(
177: CSSProperty.COLOR, "red");
178: } else {
179: statusLabel
180: .setText("You have "
181: + piecesRight
182: + " pieces right!\nCan you guess what it is?");
183: }
184: return true;
185: }
186: statusLabel
187: .setText("That piece doesn't belong there!\nWhat are you thinking?");
188: return false;
189: }
190:
191: });
192: }
193: resetPuzzle();
194:
195: // build the puzzle
196: SGridLayout gridLayout = new SGridLayout(3, 3);
197: gridLayout.setBorder(1);
198: final SPanel puzzle = new SPanel(gridLayout);
199: for (int i = 0; i < dropIcons.length; i++) {
200: puzzle.add(dropIcons[i]);
201: }
202:
203: // build the pieces area
204: gridLayout = new SGridLayout(9);
205: gridLayout.setBorder(1);
206: final SPanel pieces = new SPanel(gridLayout);
207: for (int i = 0; i < dragIcons.length; i++) {
208: pieces.add(dragIcons[i]);
209: }
210:
211: puzzleContainer.add(puzzle);
212: puzzleContainer.add(pieces);
213:
214: statusLabel.setPreferredSize(new SDimension("400px", null));
215:
216: container.add(puzzleContainer);
217: container.add(statusLabel);
218:
219: return container;
220: }
221:
222: protected void resetPuzzle() {
223: piecesRight = 0;
224: statusLabel.setText("Try to solve the puzzle.");
225: statusLabel.setAttribute(CSSProperty.FONT_WEIGHT, "normal");
226: statusLabel.setAttribute(CSSProperty.COLOR, "black");
227:
228: // initialize the drag components
229: for (int i = 0; i < dragIcons.length; i++) {
230: SDragLabel dragIcon = dragIcons[shuffleTable[i]];
231: dragIcon.setIcon(beeIconsSmall[i]);
232: }
233: // initialize the drop components
234: for (int i = 0; i < dropIcons.length; i++) {
235: dropIcons[i].setIcon(null);
236: }
237: }
238:
239: /**
240: * This class extends the SLabel class with Drag functionality.
241: * @author ole
242: *
243: */
244: private static class SDragLabel extends SLabel implements
245: DragSource {
246:
247: private boolean dragEnabled;
248:
249: public boolean isDragEnabled() {
250: return dragEnabled;
251: }
252:
253: public void setDragEnabled(boolean dragEnabled) {
254: if (this .dragEnabled != dragEnabled) {
255: this .dragEnabled = dragEnabled;
256:
257: if (dragEnabled) {
258: getSession().getDragAndDropManager()
259: .registerDragSource(this );
260: } else {
261: getSession().getDragAndDropManager()
262: .deregisterDragSource(this );
263: }
264: reload();
265: }
266: }
267: }
268:
269: /**
270: * This class extends the SLabel class with Drop functionality.
271: * @author ole
272: *
273: */
274: private static class SDropLabel extends SLabel implements
275: DropTarget {
276: private ArrayList componentDropListeners = new ArrayList();
277:
278: /* (non-Javadoc)
279: * @see org.wings.dnd.DropTarget#addComponentDropListener(org.wings.event.SComponentDropListener)
280: */
281: public void addComponentDropListener(
282: SComponentDropListener listener) {
283: componentDropListeners.add(listener);
284: getSession().getDragAndDropManager().registerDropTarget(
285: this );
286: }
287:
288: /* (non-Javadoc)
289: * @see org.wings.dnd.DropTarget#getComponentDropListeners()
290: */
291: public List getComponentDropListeners() {
292: return componentDropListeners;
293: }
294: }
295:
296: class Controls extends ComponentControls {
297: public Controls() {
298: globalControls.setVisible(false);
299:
300: final SButton resetButton = new SButton("Reset");
301: resetButton
302: .addActionListener(new java.awt.event.ActionListener() {
303: public void actionPerformed(ActionEvent e) {
304: resetPuzzle();
305: }
306: });
307: resetButton.setHorizontalAlignment(SConstants.LEFT_ALIGN);
308:
309: addControl(resetButton);
310: }
311: }
312: }
|