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 java.util.ListIterator;
049: import javax.swing.JComboBox;
050: import org.netbeans.jemmy.EventTool;
051: import org.netbeans.jemmy.operators.JButtonOperator;
052: import org.netbeans.jemmy.operators.JCheckBoxOperator;
053: import org.netbeans.jemmy.operators.JComboBoxOperator;
054: import org.netbeans.jemmy.operators.JLabelOperator;
055: import org.netbeans.jemmy.operators.JRadioButtonOperator;
056: import org.netbeans.test.umllib.FindDialogOperator.SearchTarget;
057: import org.netbeans.test.umllib.actions.ReplaceInModelAction;
058: import org.netbeans.test.umllib.util.LabelsAndTitles;
059:
060: public class ReplaceDialogOperator extends FindDialogOperator {
061:
062: public final static String BTN_REPLACE = "Replace";
063: public final static String BTN_REPLACE_ALL = "Replace All";
064:
065: public final static String SEARCH_IN_ALIAS = "Alias";
066: public final static String CBOX_REPLACE_WITH = "Replace With:";
067:
068: public ReplaceDialogOperator() {
069: super (LabelsAndTitles.REPLACE_IN_MODEL_DIALOG_TITLE, null);
070: }
071:
072: /**
073: *
074: * @param log
075: */
076: public ReplaceDialogOperator(PrintStream log) {
077: super (LabelsAndTitles.REPLACE_IN_MODEL_DIALOG_TITLE, log);
078: }
079:
080: /**
081: *
082: * @return
083: */
084: public static ReplaceDialogOperator invoke() {
085: new ReplaceInModelAction().perform();
086: return new ReplaceDialogOperator();
087: }
088:
089: /**
090: *
091: * @param log
092: * @return
093: */
094: public static ReplaceDialogOperator invoke(PrintStream log) {
095: new ReplaceInModelAction().perform();
096: return new ReplaceDialogOperator(log);
097: }
098:
099: /**
100: *
101: * @param replaceString
102: */
103: public void setReplaceString(String replaceString) {
104: JLabelOperator dnLbl = new JLabelOperator(this ,
105: CBOX_REPLACE_WITH);
106: JComboBoxOperator cbox = new JComboBoxOperator(
107: (JComboBox) (dnLbl.getLabelFor()));
108: cbox.clearText();
109: cbox.typeText(replaceString);
110: cbox.pushKey(KeyEvent.VK_ENTER);
111:
112: // pressed enter activates find. this opens 'find' warning again if mach case was deselected.
113: // we should close it.
114: JCheckBoxOperator chbox = new JCheckBoxOperator(this ,
115: OPTION_MATCH_CASE);
116: if (!chbox.isSelected()) {
117: closeFindConfirmationDialog();
118: new EventTool().waitNoEvent(1500);
119: }
120:
121: }
122:
123: /**
124: *
125: * @param flag
126: */
127: public void setOptionAlias(boolean flag) {
128: setOptionXPath(false);
129: if (new JRadioButtonOperator(this , SEARCH_IN_ELEMENTS)
130: .isSelected()) {
131: JRadioButtonOperator rbtn = new JRadioButtonOperator(this ,
132: SEARCH_IN_ALIAS);
133: if (rbtn.isSelected() != flag) {
134: rbtn.clickMouse(1);
135: }
136: }
137: }
138:
139: public void clickReplace() {
140: JButtonOperator btn = new JButtonOperator(this , BTN_REPLACE);
141: if (btn.isEnabled()) {
142: btn.clickMouse(1);
143: }
144: }
145:
146: public void clickReplaceAll() {
147: JButtonOperator btn = new JButtonOperator(this , BTN_REPLACE_ALL);
148: if (btn.isEnabled()) {
149: btn.clickMouse(1);
150: }
151: }
152:
153: private void replaceAllItems(SearchTargetCriteria targetCriteria,
154: LinkedList searchResults, String searchString,
155: String replaceString, boolean flagMatchCase) {
156: ListIterator iter = searchResults.listIterator();
157: while (iter.hasNext()) {
158: Object[] item = (Object[]) iter.next();
159: switch (targetCriteria) {
160: case NAME:
161: if (((String) item[0]).equals((String) item[1])) {
162: item[1] = replaceString((String) item[1],
163: searchString, replaceString, flagMatchCase);
164: }
165: item[0] = replaceString((String) item[0], searchString,
166: replaceString, flagMatchCase);
167: break;
168: case ALIAS:
169: item[1] = replaceString((String) item[1], searchString,
170: replaceString, flagMatchCase);
171: break;
172: default:
173: }
174: iter.set(item);
175: }
176: }
177:
178: private String replaceString(String sourceString,
179: String searchString, String replaceString,
180: boolean flagMatchCase) {
181: if (flagMatchCase) {
182: return sourceString.replaceAll(searchString, replaceString);
183: } else {
184: String result = sourceString;
185: String rs = sourceString.toLowerCase();
186: String ss = searchString.toLowerCase();
187: int pos = rs.indexOf(ss, 0);
188: while (pos != -1) {
189: result = result.substring(0, pos) + replaceString
190: + result.substring(pos + ss.length());
191: rs = result.toLowerCase();
192: pos = rs.indexOf(ss, pos + replaceString.length());
193: }
194: return result;
195: }
196: }
197:
198: /**
199: *
200: * @param targetCriteria
201: * @param itemsForSearch
202: * @param searchString
203: * @param replaceString
204: * @param flagMatchCase
205: * @param flagMatchWholeWord
206: * @return
207: */
208: public boolean checkReplace(SearchTargetCriteria targetCriteria,
209: LinkedList itemsForSearch, String searchString,
210: String replaceString, boolean flagMatchCase,
211: boolean flagMatchWholeWord) {
212: // LinkedList ourResults = searchAndRemoveItems(targetCriteria, itemsForSearch, searchString, flagMatchCase, flagMatchWholeWord);
213: LinkedList ourResults = getSearchResultsList();
214: replaceAllItems(targetCriteria, ourResults, searchString,
215: replaceString, flagMatchCase);
216: // itemsForSearch.addAll(ourResults);
217: getSearchResults().selectAll();
218: clickReplace();
219: new EventTool().waitNoEvent(1500);
220: boolean res = compareResults(ourResults);
221: return res;
222: }
223:
224: /**
225: *
226: * @param targetCriteria
227: * @param itemsForSearch
228: * @param searchString
229: * @param replaceString
230: * @param flagMatchCase
231: * @param flagMatchWholeWord
232: * @return
233: */
234: public boolean checkReplaceAll(SearchTargetCriteria targetCriteria,
235: LinkedList itemsForSearch, String searchString,
236: String replaceString, boolean flagMatchCase,
237: boolean flagMatchWholeWord) {
238: // LinkedList ourResults = searchItems(targetCriteria, itemsForSearch, searchString, flagMatchCase, flagMatchWholeWord);
239: LinkedList ourResults = getSearchResultsList();
240: replaceAllItems(targetCriteria, ourResults, searchString,
241: replaceString, flagMatchCase);
242: // itemsForSearch.addAll(ourResults);
243: clickReplaceAll();
244: new EventTool().waitNoEvent(1500);
245: boolean res = compareResults(ourResults);
246: return res;
247: }
248:
249: /**
250: *
251: * @param targetCriteria
252: * @param itemsForSearch
253: * @param searchString
254: * @param flagMatchCase
255: * @param flagMatchWholeWord
256: * @return
257: */
258: protected LinkedList searchAndRemoveItems(
259: SearchTargetCriteria targetCriteria,
260: LinkedList itemsForSearch, String searchString,
261: boolean flagMatchCase, boolean flagMatchWholeWord) {
262: LinkedList result = new LinkedList();
263: String ss = searchString;
264: Iterator iter = itemsForSearch.iterator();
265: for (int i = 0; i < itemsForSearch.size(); i++) {
266: Object[] item = (Object[]) iter.next();
267: String ns = (String) item[0];
268: String as = (String) item[1];
269: String ds = (String) item[2];
270:
271: if (!flagMatchCase) {
272: ns = ns.toLowerCase();
273: as = as.toLowerCase();
274: ds = ds.toLowerCase();
275: ss = ss.toLowerCase();
276: }
277: // prn.println("ns=" + ns + " , as=" + as + " , ds=" + ds + " , ss=" + ss);
278: switch (targetCriteria) {
279: case NAME:
280: if (flagMatchWholeWord) {
281: if (ns.equals(ss)) {
282: result.add(cloneObjectArray(item));
283: iter.remove();
284: }
285: } else {
286: if (ns.indexOf(ss) != -1) {
287: result.add(cloneObjectArray(item));
288: iter.remove();
289: }
290: }
291: break;
292: case ALIAS:
293: if (flagMatchWholeWord) {
294: // prn.println("case ALIAS 1");
295: if (as.equals(ss)) {
296: result.add(cloneObjectArray(item));
297: iter.remove();
298: }
299: } else {
300: // prn.println("case ALIAS 2");
301: if (as.indexOf(ss) != -1) {
302: result.add(cloneObjectArray(item));
303: iter.remove();
304: }
305: }
306: break;
307: case NAME_AND_ALIAS:
308: if (flagMatchWholeWord) {
309: // prn.println("case NAME and ALIAS 1");
310: if (ns.equals(ss) || as.equals(ss)) {
311: result.add(cloneObjectArray(item));
312: iter.remove();
313: }
314: } else {
315: // prn.println("case NAME and ALIAS 2");
316: if ((ns.indexOf(ss) != -1)
317: || (as.indexOf(ss) != -1)) {
318: result.add(cloneObjectArray(item));
319: iter.remove();
320: }
321: }
322: break;
323: case DESCRIPTION:
324: if (flagMatchWholeWord) {
325: if (ds.equals(ss)) {
326: result.add(cloneObjectArray(item));
327: iter.remove();
328: }
329: } else {
330: if (ds.indexOf(ss) != -1) {
331: result.add(cloneObjectArray(item));
332: iter.remove();
333: }
334: }
335: break;
336: default:
337: }
338:
339: }
340: return result;
341: }
342:
343: private Object[] cloneObjectArray(Object[] array) {
344: return new Object[] { new String((String) array[0]),
345: new String((String) array[1]),
346: new String((String) array[2]), (SearchTarget) array[3] };
347: }
348:
349: }
|