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.*;
045: import org.netbeans.modules.visualweb.gravy.model.components.*;
046: import org.netbeans.modules.visualweb.gravy.model.project.components.*;
047: import org.netbeans.modules.visualweb.gravy.RaveWindowOperator;
048: import org.netbeans.modules.visualweb.gravy.PageTopComponentOperator;
049: import org.netbeans.modules.visualweb.gravy.TestUtils;
050: import org.netbeans.modules.visualweb.gravy.designer.DesignerPaneOperator;
051: import org.netbeans.modules.visualweb.gravy.toolbox.PaletteContainerOperator;
052: import org.netbeans.modules.visualweb.gravy.properties.SheetTableOperator;
053:
054: import org.netbeans.jemmy.TimeoutExpiredException;
055: import org.netbeans.jemmy.JemmyException;
056:
057: import javax.swing.tree.TreePath;
058: import java.awt.Point;
059: import java.io.File;
060: import java.util.ArrayList;
061: import java.util.List;
062:
063: /**
064: * Web pages included in Project.
065: */
066:
067: public class WebPage extends JSPFile implements LinkableSource,
068: LinkableTarget {
069:
070: private final static String propID = "id";
071:
072: PageTopComponentOperator page = null;
073:
074: private JavaFile jf;
075:
076: private String sep = File.separator;
077:
078: /**
079: * Creates a new instance of web page.
080: * @param path Path to web page in project.
081: * @param name Name of web page.
082: */
083: WebPage(TreePath path, String name, ProjectEntry parent) {
084: super (path, name, parent);
085: SourceFolder sf = ((WebPageFolder) parent).getSourceFolder();
086: JavaFile jf = sf.addJavaFile(name);
087: jf.webpage = this ;
088: this .jf = jf;
089: webpage = this ;
090: }
091:
092: /**
093: * Get Java file for this web page.
094: * @return Java file for this web page.
095: */
096: public JavaFile getJavaFile() {
097: return jf;
098: }
099:
100: /**
101: * Return added component.
102: * @param webcomponent Component in palette to add.
103: * @return Component which was added.
104: */
105: public Component add(WebComponent webcomponent) {
106: return add(webcomponent, new Point(0, 0));
107: }
108:
109: /**
110: * Return added component.
111: * @param webcomponent Component in palette to add.
112: * @param point Point to place component.
113: * @return Component which was added.
114: */
115: public Component add(WebComponent webcomponent, Point point) {
116: return add(webcomponent, point, null);
117: }
118:
119: /**
120: * Return added component.
121: * @param webcomponent Component in palette to add.
122: * @param point Point to place component.
123: * @param name Name of component.
124: * @return Component which was added.
125: */
126: public Component add(WebComponent webcomponent, Point point,
127: String name) {
128: open();
129: DesignerPaneOperator designer = page.getDesigner();
130: TestUtils.wait(1000);
131: PaletteContainerOperator palette = null;
132: try {
133: palette = new PaletteContainerOperator(webcomponent
134: .getComponentSet().getID());
135: } catch (Exception e) {
136: throw new JemmyException("Palette category with name "
137: + webcomponent.getComponentSet().getID()
138: + " can't be found!", e);
139: }
140: TestUtils.wait(2000);
141: try {
142: palette.addComponent(webcomponent.getID(), designer, point);
143: } catch (Exception e) {
144: throw new JemmyException("Component "
145: + webcomponent.getID() + " can't be added!", e);
146: }
147: TestUtils.wait(2000);
148: try {
149: SheetTableOperator sheet = new SheetTableOperator();
150: if (name == null)
151: name = sheet.getValue(propID);
152: else
153: sheet.setTextValue(propID, name);
154: } catch (Exception e) {
155: throw new JemmyException("Name for component "
156: + webcomponent.getID() + " can't be set!", e);
157: }
158: Component newComponent;
159: if (webcomponent.isVisual()) {
160: if (webcomponent.isEventHadling())
161: newComponent = new VisualEventHandlingComponent(
162: webcomponent, name);
163: else
164: newComponent = new VisualComponent(webcomponent, name);
165: ((VisualComponent) newComponent).setCoordinates(point);
166: } else
167: newComponent = new NonVisualComponent(webcomponent, name);
168: newComponent.Page = this ;
169: childList.add(newComponent);
170: return newComponent;
171: }
172:
173: /**
174: * Return all component on web page.
175: * @return Array of Component on web page.
176: */
177: public Component[] getComponents() {
178: return ((Component[]) childList.toArray(new Component[childList
179: .size()]));
180: }
181:
182: /**
183: * Return component on web page with specified index.
184: * @param index Index of necessary component.
185: * @return Component on web page with specified index.
186: */
187: public Component getComponent(int index) {
188: return ((Component) childList.get(index));
189: }
190:
191: /**
192: * Return component on web page with specified type and index.
193: * @param index Index of necessary component.
194: * @param type Type of necessary component.
195: * @return Component on web page with specified type and index.
196: */
197: public Component getComponent(Component type, int index) {
198: List tmpComponents = new ArrayList();
199: for (int i = 0; i < childList.size(); i++) {
200: if (((Component) childList.get(i)).getType().equals(
201: type.getType()))
202: tmpComponents.add(childList.get(i));
203: }
204: return ((Component) tmpComponents.get(index));
205: }
206:
207: /**
208: * Return component on web page with specified ID.
209: * @param ID ID of necessary component.
210: * @return Component on web page with specified ID.
211: */
212: public Component getComponent(String ID) {
213: return null;
214: }
215:
216: /**
217: * Get name of linkable source web page.
218: * @return Name of linkable source web page.
219: */
220: public String getLinkableSourceName() {
221: String folder_path = getTreePath().toString();
222: if (folder_path.indexOf("|", folder_path.indexOf("|") + 1) == -1)
223: return getName() + ".jsp";
224: folder_path = folder_path
225: .substring(
226: folder_path.indexOf("|", folder_path
227: .indexOf("|") + 1) + 1,
228: folder_path.length() - 1).replace('|', '/');
229: return folder_path + "/" + getName() + ".jsp";
230: }
231:
232: /**
233: * Get name of linkable target web page.
234: * @return Name of linkable target web page.
235: */
236: public String getLinkableTargetName() {
237: String folder_path = getTreePath().toString();
238: if (folder_path.indexOf("|", folder_path.indexOf("|") + 1) == -1)
239: return getName() + ".jsp";
240: folder_path = folder_path
241: .substring(
242: folder_path.indexOf("|", folder_path
243: .indexOf("|") + 1) + 1,
244: folder_path.length() - 1).replace('|', '/');
245: return folder_path + "/" + getName() + ".jsp";
246: }
247:
248: /**
249: * Get project, which this web page belongs to.
250: * @return Project Project, which this web page belongs to.
251: */
252: private Project getProject() {
253: ProjectEntry tmp = this ;
254: while (!((tmp = tmp.getParent()) instanceof RootEntry))
255: ;
256: return ((RootEntry) tmp).getProject();
257: }
258:
259: /**
260: * Open web page file.
261: */
262: public void open() {
263: long tmpTimeout = 0;
264: String tmp_path = getTreePath().toString();
265: int start_compare = tmp_path.indexOf(RootEntry.webPagesName)
266: + RootEntry.webPagesName.length() + 1;
267: String compare_tooltip;
268: if (start_compare != tmp_path.length())
269: compare_tooltip = ((tmp_path.substring(start_compare,
270: tmp_path.length() - 1))
271: + "|" + this .name + ".jsp").replace('|',
272: File.separatorChar);
273: else
274: compare_tooltip = this .name + ".jsp";
275: String project_path = getProject().getLocation()
276: + getProject().getName() + sep + "web" + sep;
277: compare_tooltip = project_path + compare_tooltip;
278: try {
279: tmpTimeout = RaveWindowOperator.getDefaultRave()
280: .getTimeouts().getTimeout(
281: "ComponentOperator.WaitComponentTimeout");
282: RaveWindowOperator.getDefaultRave().getTimeouts()
283: .setTimeout(
284: "ComponentOperator.WaitComponentTimeout",
285: 5000);
286: if (page == null) {
287: int i = 0;
288: while (!((String) (page = new PageTopComponentOperator(
289: getName(), i++)).getDump().get("Tooltip text"))
290: .equals(compare_tooltip))
291: ;
292: } else {
293: try {
294: page.makeComponentVisible();
295: } catch (Exception e) {
296: throw new TimeoutExpiredException("");
297: }
298: }
299: } catch (TimeoutExpiredException e) {
300: int i = 0;
301: super .open();
302: while (!((String) (page = new PageTopComponentOperator(
303: getName(), i++)).getDump().get("Tooltip text"))
304: .equals(compare_tooltip))
305: ;
306: } finally {
307: RaveWindowOperator.getDefaultRave().getTimeouts()
308: .setTimeout(
309: "ComponentOperator.WaitComponentTimeout",
310: tmpTimeout);
311: }
312: page.getDesigner();
313: }
314:
315: /**
316: * Close web page file.
317: */
318: public void close() {
319: open();
320: TestUtils.wait(500);
321: page.close();
322: }
323: }
|