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-2007 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.visualweb.gravy;
043:
044: import java.awt.Component;
045: import java.awt.Point;
046: import java.awt.Rectangle;
047: import javax.swing.JPopupMenu;
048: import javax.swing.JScrollPane;
049: import javax.swing.JTable;
050: import javax.swing.JTree;
051: import javax.swing.tree.TreePath;
052: import org.netbeans.jemmy.ComponentChooser;
053: import org.netbeans.jemmy.ComponentSearcher;
054: import org.netbeans.jemmy.Timeout;
055: import org.netbeans.jemmy.drivers.DriverManager;
056: import org.netbeans.jemmy.drivers.MouseDriver;
057: import org.netbeans.jemmy.drivers.SupportiveDriver;
058: import org.netbeans.jemmy.operators.ComponentOperator;
059: import org.netbeans.jemmy.operators.ContainerOperator;
060: import org.netbeans.jemmy.operators.JPopupMenuOperator;
061: import org.netbeans.jemmy.operators.JScrollPaneOperator;
062: import org.netbeans.jemmy.operators.JTableOperator;
063: import org.netbeans.jemmy.operators.JTreeOperator;
064: import org.netbeans.jemmy.util.EmptyVisualizer;
065:
066: /**
067: * This class is used for dealing with IDE's TreeTable component,
068: * used instead of JTree in some dialogs: Options, SetupWizard, ...
069: */
070: public class TreeTableOperator extends JTableOperator {
071: private JTreeOperator _tree;
072:
073: /**
074: * Creates new instance.
075: * @param view JTable representing requested TreeTable
076: */
077: public TreeTableOperator(JTable view) {
078: super (view);
079: }
080:
081: /**
082: * Creates new instance for the first TreeTable in container.
083: * @param contOper container where to find TreeTable
084: */
085: public TreeTableOperator(ContainerOperator contOper) {
086: this (contOper, 0);
087: }
088:
089: /**
090: * Creates new instance for the first TreeTable in container.
091: * @param contOper container where to find TreeTable
092: * @param index int index
093: */
094: public TreeTableOperator(ContainerOperator contOper, int index) {
095: this ((JTable) waitComponent(contOper, new TreeTableFinder(
096: ComponentSearcher.getTrueChooser("Any TreeTable")),
097: index));
098: copyEnvironment(contOper);
099: }
100:
101: /**
102: * Returns operator for a tree which is showed as a part of the table.
103: * @return JTreeOperator instance
104: */
105: public JTreeOperator tree() {
106: if (_tree == null) {
107: // cell renderer component for first column is JTree
108: Object value = getValueAt(0, 0);
109: JTree jTree = (JTree) getCellRenderer(0, 0)
110: .getTableCellRendererComponent(
111: (JTable) this .getSource(), value, false,
112: false, 0, 0);
113: // Need to set EmptyVisualizer because found JTree doesn't have any parent Container
114: // and calling makeComponentVisible() throws NPE
115: // _tree = new JTreeOperator(jTree);
116: _tree = new RenderedTreeOperator(this , jTree);
117: _tree.setVisualizer(new EmptyVisualizer());
118: }
119: // Everytime make parent container visible because tree has EmptyVisualizer
120: // and it is need for example for popup menu operations on JTree
121: makeComponentVisible();
122: return _tree;
123: }
124:
125: /** Registers RenderedMouseDriver to be used by RenderedTreeOperator. */
126: static {
127: DriverManager.setDriver(DriverManager.MOUSE_DRIVER_ID,
128: new RenderedMouseDriver(), RenderedTreeOperator.class);
129: }
130:
131: /**
132: * This inner class is a finder, which helps to search a component TreeTable.
133: */
134: static class TreeTableFinder implements ComponentChooser {
135: ComponentChooser subFinder;
136:
137: public TreeTableFinder(ComponentChooser sf) {
138: subFinder = sf;
139: }
140:
141: public boolean checkComponent(Component comp) {
142: Class cls = comp.getClass();
143: do {
144: if (cls.getName().equals(
145: "org.openide.explorer.view.TreeTable")) {
146: return (subFinder.checkComponent(comp));
147: }
148: } while ((cls = cls.getSuperclass()) != null);
149: return (false);
150: }
151:
152: public String getDescription() {
153: return (subFinder.getDescription());
154: }
155: }
156:
157: /**
158: * MouseDriver used to process events not on JTree but on TreeTable
159: * component which is used to handle events in real.
160: */
161: public static class RenderedMouseDriver extends SupportiveDriver
162: implements MouseDriver {
163:
164: /**
165: * Creates new instance of RenderedMouseDriver.
166: */
167: public RenderedMouseDriver() {
168: super (new Class[] { RenderedTreeOperator.class });
169: }
170:
171: /**
172: * Presses mouse on operator given by {@link TreeTableOperator.RenderedTreeOperator#getRealOperator getRealOperator()} method.
173: * @param oper operator which delegates events on an real operator
174: * @param x x position of mouse operation
175: * @param y y position of mouse operation
176: * @param mouseButton mouse button identification
177: * @param modifiers modifiers pressed during mouse click
178: */
179: public void pressMouse(ComponentOperator oper, int x, int y,
180: int mouseButton, int modifiers) {
181: checkSupported(oper);
182: ComponentOperator realOper = ((RenderedTreeOperator) oper)
183: .getRealOperator();
184: DriverManager.getMouseDriver(realOper).pressMouse(realOper,
185: x, y, mouseButton, modifiers);
186: }
187:
188: /**
189: * Releases mouse on operator given by {@link TreeTableOperator.RenderedTreeOperator#getRealOperator getRealOperator()} method.
190: * @param oper operator which delegates events on an real operator
191: * @param x x position of mouse operation
192: * @param y y position of mouse operation
193: * @param mouseButton mouse button identification
194: * @param modifiers modifiers pressed during mouse click
195: */
196: public void releaseMouse(ComponentOperator oper, int x, int y,
197: int mouseButton, int modifiers) {
198: checkSupported(oper);
199: ComponentOperator realOper = ((RenderedTreeOperator) oper)
200: .getRealOperator();
201: DriverManager.getMouseDriver(realOper).releaseMouse(
202: realOper, x, y, mouseButton, modifiers);
203: }
204:
205: /**
206: * Clicks mouse on operator given by {@link TreeTableOperator.RenderedTreeOperator#getRealOperator getRealOperator()} method.
207: * @param oper operator which delegates events on an real operator
208: * @param x x position of mouse operation
209: * @param y y position of mouse operation
210: * @param clickCount how many times to be clicked
211: * @param mouseButton mouse button identification
212: * @param modifiers modifiers pressed during mouse click
213: * @param mouseClick timeout of mouse click
214: */
215: public void clickMouse(ComponentOperator oper, int x, int y,
216: int clickCount, int mouseButton, int modifiers,
217: Timeout mouseClick) {
218: checkSupported(oper);
219: ComponentOperator realOper = ((RenderedTreeOperator) oper)
220: .getRealOperator();
221: DriverManager.getMouseDriver(realOper).clickMouse(realOper,
222: x, y, clickCount, mouseButton, modifiers,
223: mouseClick);
224: }
225:
226: /**
227: * Moves mouse on operator given by {@link TreeTableOperator.RenderedTreeOperator#getRealOperator getRealOperator()} method.
228: * @param oper operator which delegates events on an real operator
229: * @param x x position of mouse operation
230: * @param y y position of mouse operation
231: */
232: public void moveMouse(ComponentOperator oper, int x, int y) {
233: checkSupported(oper);
234: ComponentOperator realOper = ((RenderedTreeOperator) oper)
235: .getRealOperator();
236: DriverManager.getMouseDriver(realOper).moveMouse(realOper,
237: x, y);
238: }
239:
240: /**
241: * Drags mouse on operator given by {@link TreeTableOperator.RenderedTreeOperator#getRealOperator getRealOperator()} method.
242: * @param oper operator which delegates events on an real operator
243: * @param x x position of mouse operation
244: * @param y y position of mouse operation
245: * @param mouseButton mouse button identification
246: * @param modifiers modifiers pressed during mouse click
247: */
248: public void dragMouse(ComponentOperator oper, int x, int y,
249: int mouseButton, int modifiers) {
250: checkSupported(oper);
251: ComponentOperator realOper = ((RenderedTreeOperator) oper)
252: .getRealOperator();
253: DriverManager.getMouseDriver(realOper).dragMouse(realOper,
254: x, y, mouseButton, modifiers);
255: }
256:
257: /**
258: * Provides drag and drop operation on operator given by {@link TreeTableOperator.RenderedTreeOperator#getRealOperator getRealOperator()} method.
259: * @param oper operator which delegates events on an real operator
260: * @param start_x x position where to drag
261: * @param start_y y position where to drag
262: * @param end_x x position where to drop
263: * @param end_y y position where to drop
264: * @param mouseButton mouse button identification
265: * @param modifiers modifiers pressed during mouse click
266: * @param before timeout before drag
267: * @param after timeout after drop
268: */
269: public void dragNDrop(ComponentOperator oper, int start_x,
270: int start_y, int end_x, int end_y, int mouseButton,
271: int modifiers, Timeout before, Timeout after) {
272: checkSupported(oper);
273: ComponentOperator realOper = ((RenderedTreeOperator) oper)
274: .getRealOperator();
275: DriverManager.getMouseDriver(realOper).dragNDrop(realOper,
276: start_x, start_y, end_x, end_y, mouseButton,
277: modifiers, before, after);
278: }
279:
280: /**
281: * Enters mouse on operator given by {@link TreeTableOperator.RenderedTreeOperator#getRealOperator getRealOperator()} method.
282: * @param oper operator which delegates events on an real operator
283: */
284: public void enterMouse(ComponentOperator oper) {
285: checkSupported(oper);
286: ComponentOperator realOper = ((RenderedTreeOperator) oper)
287: .getRealOperator();
288: DriverManager.getMouseDriver(realOper).enterMouse(realOper);
289: }
290:
291: /**
292: * Exits mouse on operator given by {@link TreeTableOperator.RenderedTreeOperator#getRealOperator getRealOperator()} method.
293: * @param oper operator which delegates events on an real operator
294: */
295: public void exitMouse(ComponentOperator oper) {
296: checkSupported(oper);
297: ComponentOperator realOper = ((RenderedTreeOperator) oper)
298: .getRealOperator();
299: DriverManager.getMouseDriver(realOper).exitMouse(realOper);
300: }
301: }
302:
303: /**
304: * Operator used to process events not on JTree but on TreeTable
305: * component which is used to handle events in real.
306: */
307: public static class RenderedTreeOperator extends JTreeOperator {
308: TreeTableOperator oper;
309:
310: /**
311: * Creates new instance of RenderedTreeOperator.
312: * @param oper TreeTable operator to handle events
313: * @param tree JTree representing nodes
314: */
315: public RenderedTreeOperator(TreeTableOperator oper, JTree tree) {
316: super (tree);
317: this .oper = oper;
318: }
319:
320: /**
321: * Returns TreeTableOperator instance which is used to handle events.
322: * @return TreeTableOperator instance
323: */
324: public ComponentOperator getRealOperator() {
325: return (oper);
326: }
327:
328: /**
329: * Calls popup menu on specified tree paths.
330: * @param paths an array of TreePath instances
331: * @param mouseButton mouse button identification
332: * @return JPopupMenu instance opened by this method
333: */
334: public JPopupMenu callPopupOnPaths(TreePath[] paths,
335: int mouseButton) {
336: oper.makeComponentVisible();
337: for (int i = 0; i < paths.length; i++) {
338: if (paths[i].getParentPath() != null) {
339: expandPath(paths[i].getParentPath());
340: }
341: }
342: selectPaths(paths);
343: scrollToPath(paths[paths.length - 1]);
344: Point point = getPointToClick(paths[paths.length - 1]);
345: return (JPopupMenuOperator
346: .callPopup(oper.getSource(), (int) point.getX(),
347: (int) point.getY(), mouseButton));
348: }
349:
350: /**
351: * Scrolls to a path if the tree is on a JScrollPane component.
352: * @param path TreePath where to scroll
353: */
354: public void scrollToPath(TreePath path) {
355: makeComponentVisible();
356: //try to find JScrollPane under.
357: JScrollPane scroll = (JScrollPane) getContainer(new JScrollPaneOperator.JScrollPaneFinder(
358: ComponentSearcher.getTrueChooser("JScrollPane")));
359: if (scroll == null) {
360: return;
361: }
362: JScrollPaneOperator scroller = new JScrollPaneOperator(
363: scroll);
364: scroller.copyEnvironment(this );
365: scroller.setVisualizer(new EmptyVisualizer());
366: Rectangle rect = getPathBounds(path);
367: if (rect != null) {
368: scroller.scrollToComponentRectangle(getRealOperator()
369: .getSource(), (int) rect.getX(), (int) rect
370: .getY(), (int) rect.getWidth(), (int) rect
371: .getHeight());
372: } else {
373: throw (new NoSuchPathException(path));
374: }
375: }
376: }
377:
378: /**
379: * Performs verification by accessing all sub-components
380: */
381: public void verify() {
382: tree();
383: }
384: }
|