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.model.project;
043:
044: import org.netbeans.modules.visualweb.gravy.model.navigation.LinkManager;
045: import org.netbeans.modules.visualweb.gravy.Bundle;
046: import org.netbeans.modules.visualweb.gravy.Util;
047: import org.netbeans.modules.visualweb.gravy.TestUtils;
048: import org.netbeans.modules.visualweb.gravy.ProjectNavigatorOperator;
049:
050: import org.netbeans.jemmy.JemmyException;
051: import org.netbeans.jemmy.operators.JLabelOperator;
052:
053: /**
054: * Common class for all project types, describing all general actions and methods.
055: */
056:
057: public abstract class Project {
058:
059: private final static String bundle = "org.netbeans.modules.visualweb.gravy.model.project.Bundle";
060: private final static String popupClose = Bundle.getStringTrimmed(
061: Bundle.getStringTrimmed(bundle, "NBProjectsBundle"), Bundle
062: .getStringTrimmed(bundle, "CloseProjectPopupItem"));
063: private final static String popupRun = Bundle.getStringTrimmed(
064: Bundle.getStringTrimmed(bundle, "WebProjectsBundle"),
065: Bundle.getStringTrimmed(bundle, "RunProjectPopupItem"));
066:
067: private final static String STR_NO_VIEW = "<No View Available>";
068: private final static String STR_MAIN = " [Main]";
069:
070: /**
071: * Descriptor of project.
072: */
073: private ProjectDescriptor prjDescriptor;
074:
075: /**
076: * Root entry of the project.
077: */
078: protected RootEntry root;
079:
080: /**
081: * Link manager of project.
082: */
083: private LinkManager linkMng;
084:
085: /**
086: * Creates project with specified descriptor.
087: * @param prjDescriptor Descriptor of project.
088: */
089: public Project(ProjectDescriptor prjDescriptor) {
090: setDescriptor(prjDescriptor);
091: root = new RootEntry(this );
092: }
093:
094: /**
095: * Get root of project tree.
096: */
097: public RootEntry getRoot() {
098: return root;
099: }
100:
101: /**
102: * Get link manager of project tree.
103: */
104: public LinkManager getLinkManager() {
105: if (linkMng == null)
106: return linkMng = new LinkManager(this );
107: else
108: return linkMng;
109: }
110:
111: /**
112: * Rename project.
113: * @param newProjectName New project name.
114: */
115: public void rename(String newProjectName) {
116: String name = prjDescriptor.getProperty(prjDescriptor.NAME_KEY);
117: if (!name.equals(newProjectName))
118: setName(newProjectName);
119: }
120:
121: /**
122: * Close project.
123: */
124: public void close() {
125: try {
126: new ProjectNavigatorOperator().pressPopupItemOnNode(
127: getName(), popupClose);
128: } catch (Exception e) {
129: throw new JemmyException("Project can't be closed!", e);
130: }
131: }
132:
133: /**
134: * Run project.
135: */
136: public void run() {
137: try {
138: new ProjectNavigatorOperator().pressPopupItemOnNode(
139: getName(), popupRun);
140: TestUtils.wait(2000);
141: String[] runFinished = { getName() + STR_MAIN, STR_NO_VIEW,
142: "" };
143: waitLabelString(1, runFinished);
144: } catch (Exception e) {
145: throw new JemmyException("Project can't be run!", e);
146: }
147: TestUtils.wait(3000);
148: }
149:
150: /**
151: * Check label with specified index for correspondence to specified string.
152: * Method wait when specified string appears in label.
153: */
154: private void waitLabelString(int index, String compareString) {
155: String[] compareStrings = { compareString };
156: waitLabelString(index, compareStrings);
157: }
158:
159: /**
160: * Check label with specified index for correspondence to one of a specified string.
161: * Method wait when one of a specified string appears in label.
162: */
163: private void waitLabelString(int index, String[] compareStrings) {
164: JLabelOperator jlo = new JLabelOperator(Util.getMainWindow(),
165: index);
166: boolean isStringAppeared = false;
167: while (true) {
168: jlo = new JLabelOperator(Util.getMainWindow(), index);
169: TestUtils.wait(1000);
170: String labelText = jlo.getText();
171: for (int i = 0; i < compareStrings.length; i++) {
172: if (labelText == null
173: || !labelText.equals(compareStrings[i]))
174: continue;
175: else {
176: isStringAppeared = true;
177: break;
178: }
179: }
180: if (isStringAppeared)
181: break;
182: }
183: }
184:
185: /**
186: * Check label with specified index for correspondence to specified string.
187: * Method wait when specified string appears in label and then wait until it disappears.
188: */
189: private void waitLabelStringDisappear(int index,
190: String compareString) {
191: JLabelOperator jlo = new JLabelOperator(Util.getMainWindow(),
192: index);
193: while (jlo.getText() == null
194: || !jlo.getText().equals(compareString)) {
195: jlo = new JLabelOperator(Util.getMainWindow(), index);
196: TestUtils.wait(1000);
197: }
198: while (jlo.getText() != null
199: && jlo.getText().equals(compareString)) {
200: jlo = new JLabelOperator(Util.getMainWindow(), index);
201: TestUtils.wait(1000);
202: }
203: }
204:
205: /**
206: * Save All project entries.
207: */
208: public void saveAll() {
209: }
210:
211: /**
212: * Get descriptor of project.
213: * @return descriptor of project.
214: */
215: public ProjectDescriptor getDescriptor() {
216: return prjDescriptor;
217: }
218:
219: /**
220: * Set descriptor of project.
221: * @param prjDescriptor New descriptor of project.
222: */
223: private void setDescriptor(ProjectDescriptor prjDescriptor) {
224: this .prjDescriptor = prjDescriptor;
225: }
226:
227: /**
228: * Get project name.
229: * @return project name.
230: */
231: public String getName() {
232: return prjDescriptor.getProperty(prjDescriptor.NAME_KEY);
233: }
234:
235: /**
236: * Set project name.
237: * @param name New project name.
238: */
239: private void setName(String name) {
240: String location = prjDescriptor
241: .getProperty(prjDescriptor.LOCATION_KEY);
242: prjDescriptor = new ProjectDescriptor(name, location);
243: }
244:
245: /**
246: * Get project location.
247: * @return Absolute project location.
248: */
249: public String getLocation() {
250: return prjDescriptor.getProperty(prjDescriptor.LOCATION_KEY);
251: }
252:
253: /**
254: * Set project location.
255: * @param location Absolute project location.
256: */
257: private void setLocation(String location) {
258: String name = prjDescriptor.getProperty(prjDescriptor.NAME_KEY);
259: prjDescriptor = new ProjectDescriptor(name, location);
260: }
261:
262: /**
263: * Makes copy of the current project.
264: * @param newName Project name.
265: * @return Copy of the current project.
266: */
267: public abstract Project saveAs(String newName);
268:
269: /**
270: * Makes copy of the current project.
271: * @param newName Project name.
272: * @param newLocation absolute project location.
273: * @return Copy of the current project.
274: */
275: public abstract Project saveAs(String newName, String newLocation);
276: }
|