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.navigation;
043:
044: import org.netbeans.modules.visualweb.gravy.model.project.*;
045: import org.netbeans.modules.visualweb.gravy.Bundle;
046: import org.netbeans.modules.visualweb.gravy.TestUtils;
047: import org.netbeans.modules.visualweb.gravy.ProjectNavigatorOperator;
048: import org.netbeans.modules.visualweb.gravy.navigation.NavigatorOperator;
049:
050: import org.netbeans.jemmy.operators.Operator;
051: import org.netbeans.jemmy.JemmyException;
052:
053: import java.util.ArrayList;
054: import java.util.Hashtable;
055:
056: /**
057: * Class for link managers.
058: */
059:
060: public class LinkManager {
061:
062: private final static String bundle = "org.netbeans.modules.visualweb.gravy.model.navigation.Bundle";
063: private final static String navigationBundle = Bundle
064: .getStringTrimmed(bundle, "NavigationBundle");
065: private final static String nodeNavigation = Bundle
066: .getStringTrimmed(navigationBundle, Bundle
067: .getStringTrimmed(bundle, "PageNavigation"));
068: private final static String popupOpen = Bundle.getStringTrimmed(
069: bundle, "OpenPopupItem");
070:
071: private Project project;
072: private Hashtable links = new Hashtable();
073:
074: /**
075: * Create new LinkManager for the project.
076: */
077: public LinkManager(Project project) {
078: this .project = project;
079: }
080:
081: /**
082: * Create link with specified parameters.
083: * @param source LinkableSource which link link to.
084: * @param target LinkableTaget which link link from.
085: * @param name Link's name.
086: * @return Created link.
087: */
088: public Link createLink(LinkableSource source,
089: LinkableTarget target, String name) {
090: ProjectNavigatorOperator prjNav = ProjectNavigatorOperator
091: .showProjectNavigator();
092: TestUtils.wait(1000);
093: try {
094: prjNav.pressPopupItemOnNode(project.getName() + "|"
095: + nodeNavigation, popupOpen,
096: new Operator.DefaultStringComparator(true, true));
097: } catch (Exception e) {
098: throw new JemmyException(popupOpen
099: + " item in popup menu of " + nodeNavigation
100: + " node can't be found!", e);
101: }
102: TestUtils.wait(1000);
103: NavigatorOperator no = new NavigatorOperator();
104: TestUtils.wait(1000);
105: try {
106: no.linkUsingXmlSource(source.getLinkableSourceName(),
107: target.getLinkableTargetName(), name);
108: } catch (Exception e) {
109: throw new JemmyException("Link can't be created!", e);
110: }
111: TestUtils.wait(1000);
112: Link newLink = new Link(source, target, name);
113: links.put(name, newLink);
114: return newLink;
115: }
116:
117: /**
118: * Change link's source.
119: * @param link Link for modification.
120: * @param source New LinkableSource which link should link to.
121: */
122: public void changeSource(Link link, LinkableSource source) {
123: link.source = source;
124: }
125:
126: /**
127: * Change link's target.
128: * @param link Link for modification.
129: * @param target New LinkableTarget which link should link from.
130: */
131: public void changeTarget(Link link, LinkableTarget target) {
132: link.target = target;
133: }
134:
135: /**
136: * Change link's name.
137: * @param link Link for modification.
138: * @param name New link's name.
139: */
140: public void changeName(Link link, String name) {
141: link.name = name;
142: }
143:
144: /**
145: * Remove link.
146: * @param link Link which should be removed.
147: */
148: public void deleteLink(Link link) {
149: links.remove(link.getName());
150: }
151:
152: /**
153: * Return all links.
154: */
155: public Link[] getLinks() {
156: return ((Link[]) links.values().toArray(new Link[links.size()]));
157: }
158:
159: /**
160: * Return all links with specified source.
161: * @param source LinkableSource which links link to.
162: * @return Array of links.
163: */
164: public Link[] getFromLinks(LinkableSource source) {
165: ArrayList srcLinks = new ArrayList();
166: Link[] tmpLinks = ((Link[]) links.values().toArray(
167: new Link[links.size()]));
168: for (int i = 0; i < tmpLinks.length; i++) {
169: if (tmpLinks[i].getSource().equals(source))
170: srcLinks.add(tmpLinks[i]);
171: }
172: return ((Link[]) srcLinks.toArray(new Link[srcLinks.size()]));
173: }
174:
175: /**
176: * Return all links with specified target.
177: * @param target LinkableTarget which links link from.
178: * @return Array of links.
179: */
180: public Link[] getToLinks(LinkableTarget target) {
181: ArrayList trgLinks = new ArrayList();
182: Link[] tmpLinks = ((Link[]) links.values().toArray(
183: new Link[links.size()]));
184: for (int i = 0; i < tmpLinks.length; i++) {
185: if (tmpLinks[i].getTarget().equals(target))
186: trgLinks.add(tmpLinks[i]);
187: }
188: return ((Link[]) trgLinks.toArray(new Link[trgLinks.size()]));
189: }
190: }
|