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: //The class is invalid: terminalemulator was replaced by core/output2
043: //so, removing all the meet from it
044: package org.netbeans.modules.visualweb.gravy.debugger;
045:
046: import java.awt.Component;
047: import java.awt.Container;
048: import java.awt.event.InputEvent;
049: import java.awt.event.KeyEvent;
050: import javax.swing.JComponent;
051: import javax.swing.JTabbedPane;
052: import org.netbeans.jellytools.actions.*;
053:
054: import org.netbeans.jemmy.ComponentChooser;
055: import org.netbeans.jemmy.operators.ComponentOperator;
056: import org.netbeans.jemmy.operators.ContainerOperator;
057: import org.netbeans.jemmy.operators.JTabbedPaneOperator;
058: import org.netbeans.jemmy.operators.Operator;
059:
060: /** Operator for Output pane containing one (normal output)
061: * or more (for example JavaDoc output) output terminals
062: * (org.netbeans.lib.terminalemulator.Term component).
063: * <p>
064: * Usage:<br>
065: * <pre>
066: * // find term with given name
067: * TermOperator to = new TermOperator("MyClass - I/O");
068: * // wait for a message appears in output
069: * to.waitText("my message");
070: * // get the text
071: * String wholeOutput = to.getText();
072: * to.clearOutput();
073: * to.waitText("your message");
074: * // close this output either way
075: * to.close(); // or to.discard();
076: * </pre>
077: *
078: */
079: public class TermOperator extends ComponentOperator {
080:
081: // only first 5 Terms and 5 Screens are to be cached
082: ComponentOperator[] _screen = new ComponentOperator[5];
083: JComponent[] _term = new JComponent[5];
084:
085: // Actions used only in TermOperator.
086: private static final Action findNextAction = new Action(null,
087: "Find Next");//,
088: //null,
089: //new Shortcut(KeyEvent.VK_F3));
090:
091: private static final Action selectAllAction = new Action(null,
092: "Select All");//,
093: //null,
094: //new Shortcut(KeyEvent.VK_A, InputEvent.CTRL_MASK));
095:
096: private static final Action clearAction = new Action(null,
097: "Clear Output");
098:
099: private static final Action startRedirectionAction = new Action(
100: null, "Start Redirection of This View to File");
101:
102: private static final Action stopRedirectionAction = new Action(
103: null, "Stop Redirection of This View to File");
104:
105: private static final CopyAction copyAction = new CopyAction();
106: private static final FindAction findAction = new FindAction();
107:
108: /** Create new instance of TermOperator from given component.
109: * @param source TopComponent instance with Term
110: */
111: public TermOperator(JComponent source) {
112: // used in OutputOperator
113: super (source);
114: }
115:
116: /** Waits for output term with given name.
117: * It is activated by defalt.
118: * @param name name of output term to look for
119: *
120: public TermOperator(String name) {
121: this(name, 0);
122: }
123:
124: /** Waits for index-th output term with given name.
125: * It is activated by defalt.
126: * @param name name of output term to look for
127: * @param index index of requested output term with given name
128: */
129:
130: /*public TermOperator(String name, int index) {
131: super(new OutputOperator().waitSubComponent(new InnerTabSubchooser(name), index));
132: makeComponentVisible();
133: }*/
134:
135: /** Waits for output term with given name in specified container.
136: * It is activated by defalt.
137: * @param contOper container where to search
138: * @param name name of output term to look for
139: * @deprecated Use {@link #TermOperator(String)} or {@link #TermOperator(String, int)}
140: * because term can only be placed inside Output top component
141: */
142: public TermOperator(ContainerOperator contOper, String name) {
143: this (contOper, name, 0);
144: }
145:
146: /** Creates TermOperator instance
147: * @param contOper container where to search
148: * @param index int index of requested output term with given name
149: * @deprecated Use {@link #TermOperator(String)} or {@link #TermOperator(String, int)}
150: * because term can only be placed inside Output top component
151: */
152: public TermOperator(ContainerOperator contOper, int index) {
153: this (contOper, null, index);
154: }
155:
156: /** Creates TermOperator instance
157: * @param contOper container where to search
158: * @param name String name of output term to look for
159: * @param index int index of requested output term with given name
160: * @deprecated Use {@link #TermOperator(String)} or {@link #TermOperator(String, int)}
161: * because term can only be placed inside Output top component
162: */
163: public TermOperator(ContainerOperator contOper, String name,
164: int index) {
165: super (contOper.waitSubComponent(new InnerTabSubchooser(name),
166: index));
167: makeComponentVisible();
168: copyEnvironment(contOper);
169: }
170:
171: /** Creates TermOperator instance
172: * @param contOper container where to search
173: * @deprecated Use {@link #TermOperator(String)} or {@link #TermOperator(String, int)}
174: * because term can only be placed inside Output top component
175: */
176: public TermOperator(ContainerOperator contOper) {
177: super (contOper);
178: }
179:
180: /** Activates this term. If this term is in tabbed pane, it is selected. If
181: * it is only term in the Output top component, the Output top component
182: * is activated.
183: */
184: /*public void makeComponentVisible() {
185: if(getParent() instanceof JTabbedPane) {
186: super.makeComponentVisible();
187: // Term is a tab of JTabbedPane
188: new JTabbedPaneOperator((JTabbedPane)getParent()).setSelectedComponent(getSource());
189: } else {
190: // term is sub component of Output top component
191: new OutputOperator().makeComponentVisible();
192: }
193: }*/
194:
195: /** Getter for Term object
196: * @return instance of Term
197: */
198: public JComponent getTermSource() {
199: return getTermSource(0);
200: }
201:
202: /** Getter for Term object
203: * @return instance of Term
204: * @param termIndex index of Term inside (usualy 0, Javadoc Output has two Terms: 0 and 1).
205: */
206: public JComponent getTermSource(int termIndex) {
207: if (termIndex >= _term.length)
208: return (JComponent) ComponentOperator.waitComponent(
209: (Container) getSource(), new TermFinder(),
210: termIndex);
211: if (_term[termIndex] == null) {
212: _term[termIndex] = (JComponent) ComponentOperator
213: .waitComponent((Container) getSource(),
214: new TermFinder(), termIndex);
215: }
216: return _term[termIndex];
217: }
218:
219: /** Finds a row by text in the first Term.
220: * @param rowText String row text
221: * @return row number of specified text; -1 if text not found
222: */
223: public int findRow(String rowText) {
224: return findRow(rowText, 0);
225: }
226:
227: /** Finds a row by text.
228: * @param rowText String row text
229: * @param termIndex int Term index
230: * @return row number of specified text; -1 if text not found
231: */
232: public int findRow(String rowText, int termIndex) {
233: for (int i = 0; i < getLineCount(termIndex); i++) {
234: if (getComparator().equals(getRowText(i, termIndex),
235: rowText)) {
236: return i;
237: }
238: }
239: return -1;
240: }
241:
242: /** Invokes <code>flush</code> and returns text from the first Term.
243: * @return text from the first Term.
244: */
245: public String getText() {
246: return getText(0);
247: }
248:
249: /** Invokes <code>flush</code> and returns text.
250: * @param termIndex int Term index
251: * @return text from termIndex-th Term
252: */
253: public String getText(int termIndex) {
254: flush(termIndex);
255: return getText(0, getLineCount(termIndex) - 1, termIndex);
256: }
257:
258: /** Get text between <code>startRow</code> and <code>endRow</code> from the first Term
259: * @param startRow first row to be included
260: * @param endRow last row to be included
261: * @return text between <code>startRow</code> and <code>endRow</code> from the first Term
262: */
263: public String getText(int startRow, int endRow) {
264: return getText(startRow, endRow);
265: }
266:
267: /** Get text between <code>startRow</code> and <code>endRow</code>
268: * @param startRow first row to be included
269: * @param endRow last row to be included
270: * @param termIndex int Term index
271: * @return text between <code>startRow</code> and <code>endRow</code>
272: */
273: public String getText(int startRow, int endRow, int termIndex) {
274: flush(termIndex);
275: String result = "";
276: for (int i = startRow; i < endRow; i++) {
277: result = result + getRowText(i, termIndex) + "\n";
278: }
279: return (result);
280: }
281:
282: /** Waits for text to be displayed in output term.
283: * @param text text to wait for
284: */
285: public void waitText(String text) {
286: waitText(text, 0);
287: }
288:
289: /** Waits for text to be displayed in termIndex-th term.
290: * @param text text to wait for
291: * @param termIndex Term index
292: */
293: public void waitText(final String text, final int termIndex) {
294: getOutput().printLine(
295: "Wait \"" + text + "\" text in component \n : "
296: + getTermSource(termIndex).toString());
297: getOutput().printGolden("Wait \"" + text + "\" text");
298: waitState(new ComponentChooser() {
299: public boolean checkComponent(Component comp) {
300: return (findRow(text, termIndex) > -1);
301: }
302:
303: public String getDescription() {
304: return ("\"" + text + "\" text");
305: }
306: });
307: }
308:
309: /** Returns count of filled lines of the first Term.
310: * @return count of filled lines of the first Term.
311: */
312: public int getLineCount() {
313: return getLineCount(0);
314: }
315:
316: /** Returns count of filled lines.
317: * @param termIndex int Term index
318: * @return count of filled lines.
319: */
320: public int getLineCount(int termIndex) {
321: flush(termIndex);
322: return (getCursorRow(termIndex) + 1);
323: }
324:
325: /** Returns the topmost component lying on the first Term.
326: * All events should be dispatched to this component.
327: * @return the topmost component lying on the first Term.
328: */
329: public ComponentOperator screen() {
330: return screen(0);
331: }
332:
333: /** Returns the topmost component lying on the term.
334: * All events should be dispatched to this component.
335: * @param termIndex int Term index
336: * @return the topmost component lying on the termIndex-throws Term.
337: */
338: public ComponentOperator screen(int termIndex) {
339: ComponentOperator sc;
340: if (termIndex >= _screen.length || _screen[termIndex] == null) {
341: sc = ComponentOperator.createOperator(ComponentOperator
342: .waitComponent(getTermSource(termIndex),
343: new ScreenFinder()));
344: sc.copyEnvironment(this );
345: if (termIndex < _screen.length)
346: _screen[termIndex] = sc;
347: return sc;
348: }
349: return _screen[termIndex];
350: }
351:
352: ////////////////////////////////////////////////////////
353: //Mapping //
354: /** Returns text from specified row.
355: * Maps <code>Term.getRowText(int)</code> through queue for the first Term.
356: * @param row row number to get text from
357: * @return text from the specified row
358: */
359: public String getRowText(int row) {
360: return getRowText(row, 0);
361: }
362:
363: /** Returns text from specified row of termIndex-th term.
364: * Maps <code>Term.getRowText(int)</code> through queue
365: * @param row row number to get text from
366: * @param termIndex int Term index
367: * @return text from the specified row
368: */
369: public String getRowText(final int row, final int termIndex) {
370: return ((String) runMapping(new MapAction("getRowText") {
371: public Object map() {
372: return (null);
373: //return(((Term)getTermSource(termIndex)).getRowText(row));
374: }
375: }));
376: }
377:
378: /** Returns text within given coordinates.
379: * Maps <code>Term.textWithin(...)</code> through queue for the first Term.
380: * @param beginRow starting row
381: * @param beginCol starting column
382: * @param endRow ending row
383: * @param endCol ending column
384: * @return text within begin and end coordinates
385: */
386: public String textWithin(int beginRow, int beginCol, int endRow,
387: int endCol) {
388: return textWithin(beginRow, beginCol, endRow, endCol, 0);
389: }
390:
391: /** Returns text within given coordinates of termIndex-th term.
392: * Maps <code>Term.textWithin(...)</code> through queue
393: * @param beginRow starting row
394: * @param beginCol starting column
395: * @param endRow ending row
396: * @param endCol ending column
397: * @param termIndex int Term index
398: * @return text within begin and end coordinates
399: */
400: public String textWithin(int beginRow, int beginCol, int endRow,
401: int endCol, final int termIndex) {
402: //final Coord begin = Coord.make(beginRow, beginCol);
403: //final Coord end = Coord.make(endRow, endCol);
404: final Object begin = null;
405: final Object end = null;
406: return ((String) runMapping(new MapAction("textWithin") {
407: public Object map() {
408: return (null);
409: //return(((Term)getTermSource(termIndex)).textWithin(begin, end));
410: }
411: }));
412: }
413:
414: /* commented to avoid confusing.
415: public int getRows() {
416: return(runMapping(new MapIntegerAction("getRows") {
417: public int map() {
418: return(((Term)getTerm()).getRows());
419: }}));}
420: */
421:
422: /** Returns row where the cursor stands.
423: * Maps <code>Term.getCursorRow()</code> through queue for the first Term
424: * @return int cursor row
425: */
426: public int getCursorRow() {
427: return getCursorRow(0);
428: }
429:
430: /** Returns row where the cursor stands.
431: * Maps <code>Term.getCursorRow()</code> through queue
432: * @param termIndex int Term index
433: * @return int cursor row
434: */
435: public int getCursorRow(final int termIndex) {
436: return (runMapping(new MapIntegerAction("getCursorRow") {
437: public int map() {
438: return (0);
439: //return(((Term)getTermSource(termIndex)).getCursorRow());
440: }
441: }));
442: }
443:
444: /** Flushes buffer.
445: * Maps <code>Term.flush()</code> through queue for the first Term */
446: public void flush() {
447: flush(0);
448: }
449:
450: /** Flushes buffer.
451: * Maps <code>Term.flush()</code> through queue
452: * @param termIndex int Term index
453: */
454: public void flush(final int termIndex) {
455: runMapping(new MapVoidAction("flush") {
456: public void map() {
457: //((Term)getTermSource(termIndex)).flush();
458: }
459: });
460: }
461:
462: //End of mapping //
463: ////////////////////////////////////////////////////////
464:
465: static class ScreenFinder implements ComponentChooser {
466: public boolean checkComponent(Component comp) {
467: Class cls = comp.getClass();
468: do {
469: if (cls.getName().equals(
470: "org.netbeans.lib.terminalemulator.Screen")) {
471: return (true);
472: }
473: } while ((cls = cls.getSuperclass()) != null);
474: return (false);
475: }
476:
477: public String getDescription() {
478: return ("Screen component");
479: }
480: }
481:
482: static class TermFinder implements ComponentChooser {
483: public boolean checkComponent(Component comp) {
484: return false;
485: //return comp instanceof Term;
486: }
487:
488: public String getDescription() {
489: return "org.netbeans.lib.terminalemulator.Term";
490: }
491: }
492:
493: /** SubChooser to determine Term TopComponent
494: * Used in findTopComponent method.
495: */
496: private static final class InnerTabSubchooser implements
497: ComponentChooser {
498:
499: /** Name of term to search for. */
500: private String termName;
501:
502: public InnerTabSubchooser() {
503: }
504:
505: public InnerTabSubchooser(String termName) {
506: this .termName = termName;
507: }
508:
509: public boolean checkComponent(Component comp) {
510: if (comp.getClass().getName().endsWith("OutputTabInner")) { // NOI18N
511: return Operator.getDefaultStringComparator().equals(
512: comp.getName(), termName);
513: } else {
514: return false;
515: }
516: }
517:
518: public String getDescription() {
519: return "org.netbeans.core.output.OutputTabInner"; // NOI18N
520: }
521: }
522:
523: /** Performs verification by accessing all sub-components */
524: public void verify() {
525: screen();
526: }
527:
528: /****************************** Actions *****************************/
529:
530: /** Performs copy action. */
531: public void copy() {
532: //copyAction.perform(screen());
533: }
534:
535: /** Performs find action. */
536: public void find() {
537: //findAction.perform(screen());
538: }
539:
540: /** Performs find next action. */
541: public void findNext() {
542: //findNextAction.perform(screen());
543: }
544:
545: /** Performs select all action. */
546: public void selectAll() {
547: //selectAllAction.perform(screen());
548: }
549:
550: /** Performs clear output action. */
551: public void clearOutput() {
552: //clearAction.perform(screen());
553: }
554:
555: /** Performs start redirection action. */
556: public void startRedirection() {
557: //startRedirectionAction.perform(screen());
558: }
559:
560: /** Performs stop redirection action. */
561: public void stopRedirection() {
562: //stopRedirectionAction.perform(screen());
563: }
564:
565: /** Performs discard action. */
566: public void discard() {
567: //new DiscardOutputAction().perform(this);
568: }
569:
570: /** Performs discard all action. */
571: public void discardAll() {
572: //new DiscardAllOutputsAction().perform(this);
573: }
574:
575: /** Closes output teby by API. */
576: public void close() {
577: //new DiscardOutputAction().performAPI(this);
578: }
579: }
|