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.spi.palette;
043:
044: import java.awt.datatransfer.DataFlavor;
045: import java.awt.dnd.DnDConstants;
046: import java.io.IOException;
047: import junit.framework.*;
048: import org.netbeans.modules.palette.Category;
049: import org.netbeans.modules.palette.Item;
050: import org.netbeans.modules.palette.Model;
051: import org.openide.nodes.AbstractNode;
052: import org.openide.nodes.Children;
053: import org.openide.nodes.Index;
054: import org.openide.util.datatransfer.ExTransferable;
055:
056: /**
057: *
058: * @author S. Aubrecht
059: */
060: public class DragAndDropHandlerTest extends AbstractPaletteTestHid {
061:
062: public DragAndDropHandlerTest(String testName) {
063: super (testName);
064: }
065:
066: public static Test suite() {
067: TestSuite suite = new TestSuite(DragAndDropHandlerTest.class);
068:
069: return suite;
070: }
071:
072: public void testCustomize() throws Exception {
073: PaletteActions actions = new DummyActions();
074: PaletteController pc = PaletteFactory.createPalette(
075: getRootFolderName(), actions);
076: Model model = pc.getModel();
077:
078: Item item = model.getCategories()[0].getItems()[0];
079:
080: DragAndDropHandler handler = DragAndDropHandler.getDefault();
081:
082: ExTransferable t = ExTransferable.create(item.cut());
083: DataFlavor[] flavorsBefore = t.getTransferDataFlavors();
084:
085: handler.customize(t, item.getLookup());
086:
087: DataFlavor[] flavorsAfter = t.getTransferDataFlavors();
088: assertEquals("Default implementation does nothing",
089: flavorsBefore.length, flavorsAfter.length);
090: for (int i = 0; i < flavorsBefore.length; i++) {
091: assertEquals("Default implementation does nothing",
092: flavorsBefore[i], flavorsAfter[i]);
093: }
094: }
095:
096: public void testCanDrop() throws Exception {
097: PaletteActions actions = new DummyActions();
098: PaletteController pc = PaletteFactory.createPalette(
099: getRootFolderName(), actions);
100: Model model = pc.getModel();
101:
102: Category cat = model.getCategories()[0];
103:
104: DragAndDropHandler handler = DragAndDropHandler.getDefault();
105:
106: DataFlavor[] flavors = new DataFlavor[] { PaletteController.ITEM_DATA_FLAVOR };
107: assertTrue(handler.canDrop(cat.getLookup(), flavors,
108: DnDConstants.ACTION_COPY_OR_MOVE));
109:
110: flavors = new DataFlavor[] { new DataFlavor("text/xml") };
111: assertFalse(handler.canDrop(cat.getLookup(), flavors,
112: DnDConstants.ACTION_COPY_OR_MOVE));
113: }
114:
115: public void testDoDropReorderItemWithinCategory() throws Exception {
116: PaletteActions actions = new DummyActions();
117: PaletteController pc = PaletteFactory.createPalette(
118: getRootFolderName(), actions);
119: Model model = pc.getModel();
120:
121: Category[] categories = model.getCategories();
122:
123: Category cat = categories[0];
124: Item[] itemsBeforeMove = cat.getItems();
125:
126: Item source = itemsBeforeMove[0];
127: Item target = itemsBeforeMove[itemsBeforeMove.length - 1];
128:
129: DragAndDropHandler handler = DragAndDropHandler.getDefault();
130:
131: assertTrue(handler.doDrop(cat.getLookup(), source.cut(),
132: DnDConstants.ACTION_COPY_OR_MOVE,
133: itemsBeforeMove.length));
134:
135: pc.refresh();
136:
137: Item[] itemsAfterMove = pc.getModel().getCategories()[0]
138: .getItems();
139:
140: assertEquals(itemsBeforeMove.length, itemsAfterMove.length);
141: assertEquals(source.getName(),
142: itemsAfterMove[itemsAfterMove.length - 1].getName());
143: assertEquals(itemsBeforeMove[1].getName(), itemsAfterMove[0]
144: .getName());
145: assertEquals(target.getName(),
146: itemsAfterMove[itemsAfterMove.length - 1 - 1].getName());
147: }
148:
149: public void testDoDropItemToOtherCategory() throws Exception {
150: PaletteActions actions = new DummyActions();
151: PaletteController pc = PaletteFactory.createPalette(
152: getRootFolderName(), actions);
153: Model model = pc.getModel();
154:
155: Category[] categories = model.getCategories();
156:
157: Category srcCat = categories[0];
158: Item[] srcItemsBefore = srcCat.getItems();
159: Item dropItem = srcItemsBefore[0];
160:
161: Category tgtCat = categories[1];
162: Item[] tgtItemsBefore = tgtCat.getItems();
163: Item target = tgtItemsBefore[5];
164:
165: DragAndDropHandler handler = DragAndDropHandler.getDefault();
166:
167: assertTrue(handler.doDrop(tgtCat.getLookup(), dropItem.cut(),
168: DnDConstants.ACTION_COPY_OR_MOVE, 5));
169:
170: //force all nodes in the palette to update their children
171: pc.refresh();
172: categories = model.getCategories();
173: srcCat = categories[0];
174: tgtCat = categories[1];
175:
176: Item[] srcItemsAfter = srcCat.getItems();
177: Item[] tgtItemsAfter = tgtCat.getItems();
178:
179: assertEquals(srcItemsBefore.length, srcItemsAfter.length + 1);
180: for (int i = 0; i < srcItemsAfter.length; i++) {
181: assertEquals(srcItemsBefore[i + 1].getName(),
182: srcItemsAfter[i].getName());
183: }
184:
185: assertEquals(tgtItemsBefore.length, tgtItemsAfter.length - 1);
186: assertEquals(target.getName(), tgtItemsAfter[5 + 1].getName());
187: assertEquals(dropItem.getName(), tgtItemsAfter[5].getName());
188: }
189:
190: public void testCanReorderCategories() throws Exception {
191: PaletteActions actions = new DummyActions();
192: PaletteController pc = PaletteFactory.createPalette(
193: getRootFolderName(), actions);
194: Model model = pc.getModel();
195:
196: DragAndDropHandler handler = DragAndDropHandler.getDefault();
197:
198: assertTrue(handler.canReorderCategories(model.getRoot()));
199:
200: assertFalse(handler
201: .canReorderCategories(new NoIndexCookieNode()
202: .getLookup()));
203: }
204:
205: public void testMoveCategory() throws Exception {
206: PaletteActions actions = new DummyActions();
207: PaletteController pc = PaletteFactory.createPalette(
208: getRootFolderName(), actions);
209: Model model = pc.getModel();
210:
211: Category[] catBefore = model.getCategories();
212:
213: Category srcCat = catBefore[0];
214: Category tgtCat = catBefore[5];
215:
216: DragAndDropHandler handler = DragAndDropHandler.getDefault();
217:
218: assertTrue(handler.moveCategory(srcCat.getLookup(), 5));
219:
220: pc.refresh();
221:
222: Category[] catAfter = model.getCategories();
223:
224: assertEquals(catBefore.length, catAfter.length);
225: assertEquals(srcCat.getName(), catAfter[4].getName());
226: assertEquals(catBefore[1].getName(), catAfter[0].getName());
227: assertEquals(tgtCat.getName(), catAfter[5].getName());
228: }
229:
230: private static class NoIndexCookieNode extends AbstractNode {
231: public NoIndexCookieNode() {
232: super (Children.LEAF);
233: }
234:
235: public org.openide.nodes.Node.Cookie getCookie(Class type) {
236: if (Index.class.equals(type))
237: return null;
238: return super.getCookie(type);
239: }
240: }
241: }
|