KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > universal > PageData


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.intro.universal;
12
13 import java.io.PrintWriter JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.Path;
19 import org.w3c.dom.Element JavaDoc;
20 import org.w3c.dom.Node JavaDoc;
21 import org.w3c.dom.NodeList JavaDoc;
22
23 public class PageData {
24     public static final String JavaDoc P_TOP_LEFT = "page-content/top-left"; //$NON-NLS-1$
25
public static final String JavaDoc P_TOP_RIGHT = "page-content/top-right"; //$NON-NLS-1$
26
public static final String JavaDoc P_BOTTOM_LEFT = "page-content/bottom-left"; //$NON-NLS-1$
27
public static final String JavaDoc P_BOTTOM_RIGHT = "page-content/bottom-right"; //$NON-NLS-1$
28

29     private String JavaDoc id;
30     private ArrayList JavaDoc groups = new ArrayList JavaDoc();
31     private GroupData hidden=null;
32     
33     public PageData(String JavaDoc id) {
34         this.id = id;
35     }
36     
37     public PageData(Element JavaDoc page) {
38         this.id = page.getAttribute("id"); //$NON-NLS-1$
39
NodeList JavaDoc children = page.getChildNodes();
40         for (int i=0; i<children.getLength(); i++) {
41             Node JavaDoc child = children.item(i);
42             if (child.getNodeType()==Node.ELEMENT_NODE) {
43                 Element JavaDoc element = (Element JavaDoc)child;
44                 if (element.getNodeName().equals("group")) { //$NON-NLS-1$
45
addGroup(element, false);
46                 }
47                 else if (element.getNodeName().equals(IUniversalIntroConstants.HIDDEN)) {
48                     addGroup(element, true);
49                 }
50             }
51         }
52     }
53     
54     public void add(GroupData gd) {
55         if (gd.isHidden())
56             hidden = gd;
57         else
58             groups.add(gd);
59     }
60
61     public void addAnchors(List JavaDoc result, String JavaDoc groupId) {
62         GroupData group = findGroup(groupId);
63         if (group==null) return;
64         group.addAnchors(result);
65     }
66
67     public String JavaDoc resolvePath(String JavaDoc extensionId) {
68         if (isHidden(extensionId))
69             return null;
70         for (int i=0; i<groups.size(); i++) {
71             GroupData gdata = (GroupData)groups.get(i);
72             if (gdata.contains(extensionId)) {
73                 IPath resolvedPath = new Path(id);
74                 resolvedPath = resolvedPath.append(gdata.getPath());
75                 resolvedPath = resolvedPath.append(extensionId);
76                 return resolvedPath.toString();
77             }
78         }
79         return null;
80     }
81
82     public String JavaDoc resolveDefaultPath() {
83         for (int i=0; i<groups.size(); i++) {
84             GroupData gdata = (GroupData)groups.get(i);
85             if (gdata.isDefault()) {
86                 IPath resolvedPath = new Path(id).append(gdata.getPath());
87                 resolvedPath = resolvedPath.append(IUniversalIntroConstants.DEFAULT_ANCHOR);
88                 return resolvedPath.toString();
89             }
90         }
91         return null;
92     }
93
94     public boolean isHidden(String JavaDoc extensionId) {
95         return hidden!=null && hidden.contains(extensionId);
96     }
97     
98     public GroupData findGroup(String JavaDoc groupId) {
99         if (groupId.equals(IUniversalIntroConstants.HIDDEN))
100             return hidden;
101         for (int i=0; i<groups.size(); i++) {
102             GroupData gdata = (GroupData)groups.get(i);
103             IPath path = new Path(gdata.getPath());
104             if (path.lastSegment().equals(groupId))
105                 return gdata;
106         }
107         return null;
108     }
109     
110     private void addGroup(Element JavaDoc element, boolean hide) {
111         GroupData gd = new GroupData(element);
112         if (hide) hidden = gd;
113         else
114             groups.add(gd);
115     }
116     
117     public void addImplicitExtension(String JavaDoc extensionId, String JavaDoc name) {
118         ExtensionData ed = findExtension(extensionId, true);
119         if (ed!=null) {
120             // see if name needs to be supplied
121
if (ed.getName()==null || ed.getName().length()==0)
122                 ed.setName(name);
123             return;
124         }
125         GroupData gd = findDefaultGroup();
126         if (gd==null && groups.size()==0) {
127             // add bottoms as the default group
128
gd = new GroupData(P_BOTTOM_LEFT, true);
129             groups.add(gd);
130             groups.add(new GroupData(P_BOTTOM_RIGHT, true));
131         }
132         gd.addImplicitExtension(extensionId, name);
133     }
134
135     private GroupData findDefaultGroup() {
136         GroupData defaultGroup = null;
137         for (int i=0; i<groups.size(); i++) {
138             GroupData gd = (GroupData)groups.get(i);
139             if (gd.isDefault()) {
140                 if (defaultGroup==null)
141                     defaultGroup = gd;
142                 else
143                     if (defaultGroup.getExtensionCount()>gd.getExtensionCount())
144                         defaultGroup = gd;
145             }
146         }
147         return defaultGroup;
148     }
149     
150     public String JavaDoc getId() {
151         return id;
152     }
153     
154     public ExtensionData findExtension(String JavaDoc extensionId, boolean checkHidden) {
155         for (int i=0; i<groups.size(); i++) {
156             GroupData gdata = (GroupData)groups.get(i);
157             ExtensionData ed = find(gdata, extensionId);
158             if (ed!=null)
159                 return ed;
160         }
161         // check the hidden
162
if (checkHidden && hidden!=null)
163             return find (hidden, extensionId);
164         return null;
165     }
166         
167     private ExtensionData find(GroupData gd, String JavaDoc extensionId) {
168         BaseData bd = gd.find(extensionId);
169         if (bd!=null && bd instanceof ExtensionData)
170             return (ExtensionData)bd;
171         return null;
172     }
173
174     public String JavaDoc resolveExtension(String JavaDoc extensionId) {
175         // check the explicit groups
176
for (int i=0; i<groups.size(); i++) {
177             GroupData gdata = (GroupData)groups.get(i);
178             if (gdata.contains(extensionId)) {
179                 return id+"/"+gdata.getPath()+"/"+extensionId; //$NON-NLS-1$//$NON-NLS-2$
180
}
181         }
182         // check the hidden
183
if (hidden!=null && hidden.contains(extensionId))
184             return null;
185         // create the default: pick the last group
186
if (groups.size()==0) return null;
187         GroupData last = (GroupData)groups.get(groups.size()-1);
188         return id + "/" + last.getPath() + "/" + IUniversalIntroConstants.DEFAULT_ANCHOR; //$NON-NLS-1$//$NON-NLS-2$
189
}
190     
191     public void write(PrintWriter JavaDoc writer, String JavaDoc indent) {
192         writer.print(indent);
193         String JavaDoc indent2 = indent+" "; //$NON-NLS-1$
194
writer.println("<page id=\""+id+"\">"); //$NON-NLS-1$//$NON-NLS-2$
195
for (int i=0; i<groups.size(); i++) {
196             GroupData gd = (GroupData)groups.get(i);
197             gd.write(writer, indent2);
198         }
199         if (hidden!=null)
200             hidden.write(writer, indent2);
201         writer.print(indent);
202         writer.println("</page>"); //$NON-NLS-1$
203
}
204 }
Popular Tags