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.test.umllib;
043:
044: import java.awt.event.KeyEvent;
045: import java.io.PrintStream;
046: import java.util.Iterator;
047: import java.util.LinkedList;
048: import javax.swing.JComboBox;
049: import org.netbeans.jemmy.EventTool;
050: import org.netbeans.jemmy.operators.JButtonOperator;
051: import org.netbeans.jemmy.operators.JCheckBoxOperator;
052: import org.netbeans.jemmy.operators.JComboBoxOperator;
053: import org.netbeans.jemmy.operators.JDialogOperator;
054: import org.netbeans.jemmy.operators.JLabelOperator;
055: import org.netbeans.jemmy.operators.JListOperator;
056: import org.netbeans.jemmy.operators.JRadioButtonOperator;
057: import org.netbeans.jemmy.operators.JTableOperator;
058: import org.netbeans.test.umllib.actions.FindInModelAction;
059: import org.netbeans.test.umllib.util.LabelsAndTitles;
060:
061: public class FindDialogOperator extends JDialogOperator {
062: public final static String BTN_FIND = "Find";
063: public final static String BTN_CLOSE = "Close";
064:
065: public final static String OPTION_MATCH_CASE = "Match case";
066: public final static String OPTION_MATCH_WHOLE_WORD = "Match whole word only";
067: public final static String OPTION_XPATH = "This is an XPath expression";
068: public final static String OPTION_ALIAS = "Also search alias";
069:
070: public final static String SEARCH_IN_ELEMENTS = "Elements";
071: public final static String SEARCH_IN_DESCRIPTIONS = "Descriptions";
072:
073: public final static String COLUMN_NAME = "Name";
074: public final static String COLUMN_ALIAS = "Alias";
075:
076: public final static String CBOX_FIND_WHAT = "Find What:";
077:
078: private PrintStream prn = null;
079:
080: private EventTool eventTool = new EventTool();
081:
082: public FindDialogOperator() {
083: this (LabelsAndTitles.FIND_IN_MODEL_DIALOG_TITLE, null);
084: }
085:
086: /**
087: *
088: * @param log
089: */
090: public FindDialogOperator(PrintStream log) {
091: this (LabelsAndTitles.FIND_IN_MODEL_DIALOG_TITLE, log);
092: }
093:
094: /**
095: *
096: * @param title
097: * @param log
098: */
099: protected FindDialogOperator(String title, PrintStream log) {
100: super (title);
101: setLog(log);
102: }
103:
104: /**
105: *
106: * @return
107: */
108: public static FindDialogOperator invoke() {
109: new FindInModelAction().perform();
110: return new FindDialogOperator();
111: }
112:
113: /**
114: *
115: * @param log
116: * @return
117: */
118: public static FindDialogOperator invoke(PrintStream log) {
119: new FindInModelAction().perform();
120: return new FindDialogOperator(log);
121: }
122:
123: protected void closeFindConfirmationDialog() {
124: new Thread(new Runnable() {
125: public void run() {
126: JDialogOperator dlgFind = new JDialogOperator(
127: "Find Confirmation", 0);
128: new JButtonOperator(dlgFind, "Yes").pushNoBlock();
129: }
130: }).start();
131: }
132:
133: /**
134: *
135: * @param projectName
136: * @param searchString
137: * @param flagMatchCase
138: * @param flagMatchWholeWord
139: * @param flagAlias
140: */
141: public void doFindElements(String projectName, String searchString,
142: boolean flagMatchCase, boolean flagMatchWholeWord,
143: boolean flagAlias) {
144: setSearchString(searchString);
145: setOptionSearchInElements();
146: setOptionMatchCase(flagMatchCase);
147: setOptionMatchWholeWord(flagMatchWholeWord);
148: setOptionAlias(flagAlias);
149: setProject(projectName);
150:
151: if (!flagMatchCase) {
152: closeFindConfirmationDialog();
153: }
154: clickFind();
155: new EventTool().waitNoEvent(1500);
156: }
157:
158: /**
159: *
160: * @param projectName
161: * @param searchString
162: * @param flagMatchCase
163: * @param flagMatchWholeWord
164: */
165: public void doFindDescriptions(String projectName,
166: String searchString, boolean flagMatchCase,
167: boolean flagMatchWholeWord) {
168: setSearchString(searchString);
169: setOptionSearchInDescriptions();
170: setOptionMatchCase(flagMatchCase);
171: setOptionMatchWholeWord(flagMatchWholeWord);
172: setProject(projectName);
173:
174: if (!flagMatchCase) {
175: closeFindConfirmationDialog();
176: }
177:
178: clickFind();
179: new EventTool().waitNoEvent(1500);
180: }
181:
182: /**
183: *
184: * @param projectName
185: * @param searchString
186: */
187: public void doFindXPath(String projectName, String searchString) {
188: setSearchString(searchString);
189: setOptionXPath(true);
190: setProject(projectName);
191: clickFind();
192: }
193:
194: private void setSearchString(String searchString) {
195: JLabelOperator dnLbl = new JLabelOperator(this , CBOX_FIND_WHAT);
196: JComboBoxOperator cbox = new JComboBoxOperator(
197: (JComboBox) (dnLbl.getLabelFor()));
198: cbox.clearText();
199: cbox.typeText(searchString);
200: cbox.pushKey(KeyEvent.VK_ENTER);
201: }
202:
203: /**
204: *
205: * @param projectName
206: */
207: public void setProject(String projectName) {
208: JListOperator projects = new JListOperator(this );
209: int index = projects.findItemIndex(projectName);
210: if (index >= 0) {
211: projects.clickOnItem(index, 1);
212: }
213: }
214:
215: public void setOptionSearchInElements() {
216: JRadioButtonOperator rbtn = new JRadioButtonOperator(this ,
217: SEARCH_IN_ELEMENTS);
218: if (!rbtn.isSelected()) {
219: rbtn.clickMouse(1);
220: }
221: }
222:
223: public void setOptionSearchInDescriptions() {
224: JRadioButtonOperator rbtn = new JRadioButtonOperator(this ,
225: SEARCH_IN_DESCRIPTIONS);
226: if (!rbtn.isSelected()) {
227: rbtn.clickMouse(1);
228: }
229: }
230:
231: /**
232: *
233: * @param flag
234: */
235: public void setOptionMatchCase(boolean flag) {
236: JCheckBoxOperator chbox = new JCheckBoxOperator(this ,
237: OPTION_MATCH_CASE);
238: if (chbox.isSelected() != flag) {
239: chbox.clickMouse(1);
240: }
241: }
242:
243: /**
244: *
245: * @param flag
246: */
247: public void setOptionMatchWholeWord(boolean flag) {
248: JCheckBoxOperator chbox = new JCheckBoxOperator(this ,
249: OPTION_MATCH_WHOLE_WORD);
250: if (chbox.isSelected() != flag) {
251: chbox.clickMouse(1);
252: }
253: }
254:
255: /**
256: *
257: * @param flag
258: */
259: public void setOptionAlias(boolean flag) {
260: if (new JRadioButtonOperator(this , SEARCH_IN_ELEMENTS)
261: .isSelected()) {
262: JCheckBoxOperator chbox = new JCheckBoxOperator(this ,
263: OPTION_ALIAS);
264: if (chbox.isSelected() != flag) {
265: chbox.clickMouse(1);
266: }
267: }
268: }
269:
270: /**
271: *
272: * @param flag
273: */
274: public void setOptionXPath(boolean flag) {
275: JCheckBoxOperator chbox = new JCheckBoxOperator(this ,
276: OPTION_XPATH);
277: if (chbox.isSelected() != flag) {
278: chbox.clickMouse(1);
279: }
280: }
281:
282: public void clickFind() {
283: new JButtonOperator(this , BTN_FIND).clickMouse(1);
284: }
285:
286: public void clickClose() {
287: new JButtonOperator(this , BTN_CLOSE).clickMouse(1);
288: }
289:
290: /**
291: *
292: * @return
293: */
294: public JTableOperator getSearchResults() {
295: return new JTableOperator(this , 0);
296: }
297:
298: /**
299: *
300: * @return
301: */
302: public LinkedList getSearchResultsList() {
303: log("===== getSearchResultsList =====");
304: LinkedList list = new LinkedList();
305:
306: JTableOperator findResultsTable = getSearchResults();
307: int colName = findResultsTable.findColumn(COLUMN_NAME);
308: int colAlias = findResultsTable.findColumn(COLUMN_ALIAS);
309: log("ColNameIndex = " + colName + " , colAliasIndex = "
310: + colAlias);
311: if ((colName == -1) || (colAlias == -1)) {
312: return null;
313: }
314:
315: for (int i = 0; i < findResultsTable.getRowCount(); i++) {
316: String tn = findResultsTable.getValueAt(i, colName)
317: .toString();
318: String ta = findResultsTable.getValueAt(i, colAlias)
319: .toString();
320: list.add(new Object[] { new String(tn), new String(ta), "",
321: SearchTarget.ANY });
322: log("item: (" + tn + "," + ta + ")");
323: }
324:
325: return list;
326: }
327:
328: //------------------------------------------------------------------------------
329:
330: /**
331: *
332: * @param targetCriteria
333: * @param itemsForSearch
334: * @param searchString
335: * @param flagMatchCase
336: * @param flagMatchWholeWord
337: * @return
338: */
339: protected LinkedList searchItems(
340: SearchTargetCriteria targetCriteria,
341: LinkedList itemsForSearch, String searchString,
342: boolean flagMatchCase, boolean flagMatchWholeWord) {
343: LinkedList result = new LinkedList();
344: String ss = searchString;
345: Iterator iter = itemsForSearch.iterator();
346: for (int i = 0; i < itemsForSearch.size(); i++) {
347: Object[] item = (Object[]) iter.next();
348: String ns = (String) item[0];
349: String as = (String) item[1];
350: String ds = (String) item[2];
351:
352: if (!flagMatchCase) {
353: ns = ns.toLowerCase();
354: as = as.toLowerCase();
355: ds = ds.toLowerCase();
356: ss = ss.toLowerCase();
357: }
358: switch (targetCriteria) {
359: case NAME:
360: if (flagMatchWholeWord) {
361: if (ns.equals(ss)) {
362: result.add(item);
363: }
364: } else {
365: if (ns.indexOf(ss) != -1) {
366: result.add(item);
367: }
368: }
369: break;
370: case ALIAS:
371: if (flagMatchWholeWord) {
372: if (as.equals(ss)) {
373: result.add(item);
374: }
375: } else {
376: if (as.indexOf(ss) != -1) {
377: result.add(item);
378: }
379: }
380: break;
381: case NAME_AND_ALIAS:
382: if (flagMatchWholeWord) {
383: if (ns.equals(ss) || as.equals(ss)) {
384: result.add(item);
385: }
386: } else {
387: if ((ns.indexOf(ss) != -1)
388: || (as.indexOf(ss) != -1)) {
389: result.add(item);
390: }
391: }
392: break;
393: case DESCRIPTION:
394: if (flagMatchWholeWord) {
395: if (ds.equals(ss)) {
396: result.add(item);
397: }
398: } else {
399: if (ds.indexOf(ss) != -1) {
400: result.add(item);
401: }
402: }
403: break;
404: default:
405: }
406:
407: }
408: return result;
409: }
410:
411: /**
412: *
413: * @param target
414: * @param targetCriteria
415: * @param itemsForSearch
416: * @param searchString
417: * @return
418: */
419: protected LinkedList searchXPathItems(SearchTarget target,
420: SearchTargetCriteria targetCriteria,
421: LinkedList itemsForSearch, String searchString) {
422: LinkedList result = new LinkedList();
423: Iterator iter = itemsForSearch.iterator();
424: for (int i = 0; i < itemsForSearch.size(); i++) {
425: Object[] item = (Object[]) iter.next();
426: String ns = (String) item[0];
427: String as = (String) item[1];
428: SearchTarget tgt = (SearchTarget) item[3];
429:
430: switch (targetCriteria) {
431: case NAME:
432: if ((tgt == target) && (ns.equals(searchString))) {
433: result.add(item);
434: }
435: break;
436: case ALIAS:
437: if ((tgt == target) && (as.equals(searchString))) {
438: result.add(item);
439: }
440: break;
441: default:
442: }
443: }
444:
445: return result;
446: }
447:
448: /**
449: *
450: * @param target
451: * @param targetCriteria
452: * @param itemsForSearch
453: * @param searchString
454: * @return
455: */
456: public boolean checkXPathSearchResult(SearchTarget target,
457: SearchTargetCriteria targetCriteria,
458: LinkedList itemsForSearch, String searchString) {
459: LinkedList ourResults = searchXPathItems(target,
460: targetCriteria, itemsForSearch, searchString);
461: return compareResults(ourResults);
462: }
463:
464: /**
465: *
466: * @param targetCriteria
467: * @param itemsForSearch
468: * @param searchString
469: * @param flagMatchCase
470: * @param flagMatchWholeWord
471: * @return
472: */
473: public boolean checkSearchResult(
474: SearchTargetCriteria targetCriteria,
475: LinkedList itemsForSearch, String searchString,
476: boolean flagMatchCase, boolean flagMatchWholeWord) {
477: LinkedList ourResults = searchItems(targetCriteria,
478: itemsForSearch, searchString, flagMatchCase,
479: flagMatchWholeWord);
480: return compareResults(ourResults);
481: }
482:
483: /**
484: *
485: * @param ourResults
486: * @return
487: */
488: protected boolean compareResults(LinkedList ourResults) {
489: log("===== Compare results =====");
490: JTableOperator findResultsTable = getSearchResults();
491: int colName = findResultsTable.findColumn(COLUMN_NAME);
492: int colAlias = findResultsTable.findColumn(COLUMN_ALIAS);
493: log("ColNameIndex = " + colName + " , colAliasIndex = "
494: + colAlias);
495: if ((colName == -1) || (colAlias == -1)) {
496: return false;
497: }
498: log("ourResultsSize = " + ourResults.size()
499: + " , tableResultsSize = "
500: + findResultsTable.getRowCount());
501: if (ourResults.size() != findResultsTable.getRowCount()) {
502: log("Items in ourResults:");
503: for (int j = 0; j < ourResults.size(); j++) {
504: Object[] item = (Object[]) ourResults.get(j);
505: log("item: name = " + (String) item[0] + " , alias = "
506: + (String) item[1]);
507: }
508: log("Items in table:");
509: for (int j = 0; j < findResultsTable.getRowCount(); j++) {
510: String tn1 = findResultsTable.getValueAt(j, colName)
511: .toString();
512: String ta1 = findResultsTable.getValueAt(j, colAlias)
513: .toString();
514: log("item: name = " + tn1 + " , alias = " + ta1);
515: }
516: return false;
517: }
518: for (int i = 0; i < findResultsTable.getRowCount(); i++) {
519: String tn = findResultsTable.getValueAt(i, colName)
520: .toString();
521: String ta = findResultsTable.getValueAt(i, colAlias)
522: .toString();
523: boolean isRemoved = false;
524: Iterator iter = ourResults.iterator();
525: while (iter.hasNext()) {
526: Object[] item = (Object[]) iter.next();
527: if ((tn.equals((String) item[0]))
528: && (ta.equals((String) item[1]))) {
529: log("Remove item: name = " + (String) item[0]
530: + " , alias = " + (String) item[1]);
531: iter.remove();
532: isRemoved = true;
533: break;
534: }
535: }
536: if (!isRemoved) {
537: log("Last item not removed from ourResults");
538: log("Items in ourResults:");
539: for (int j = 0; j < ourResults.size(); j++) {
540: Object[] item = (Object[]) ourResults.get(j);
541: log("item: name = " + (String) item[0]
542: + " , alias = " + (String) item[1]);
543: }
544: log("Items in table:");
545: for (int j = 0; j < findResultsTable.getRowCount(); j++) {
546: String tn1 = findResultsTable
547: .getValueAt(j, colName).toString();
548: String ta1 = findResultsTable.getValueAt(j,
549: colAlias).toString();
550: log("item: name = " + tn1 + " , alias = " + ta1);
551: }
552: return false;
553: }
554: }
555: log("ourResultsSize = " + ourResults.size());
556: if (ourResults.size() != 0) {
557: return false;
558: }
559: return true;
560: }
561:
562: /**
563: *
564: * @param log
565: */
566: public void setLog(PrintStream log) {
567: prn = log;
568: }
569:
570: /**
571: *
572: * @param message
573: */
574: protected void log(String message) {
575: if (prn != null) {
576: prn.println(message);
577: }
578: }
579:
580: public enum SearchTargetCriteria {
581: NAME, ALIAS, NAME_AND_ALIAS, DESCRIPTION
582: }
583:
584: public enum SearchTarget {
585: ANY, PROJECT, DIAGRAM, CLASS, INTERFACE, PACKAGE, ATTRIBUTE, OPERATION, PARAMETER, DATATYPE, GENERALIZATION, IMPLEMENTATION
586: }
587: }
|