KickJava   Java API By Example, From Geeks To Geeks.

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


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.ui.intro.config.IntroElement;
18 import org.w3c.dom.Element JavaDoc;
19 import org.w3c.dom.Node JavaDoc;
20 import org.w3c.dom.NodeList JavaDoc;
21
22
23 public class GroupData {
24     boolean fDefault=false;
25     private String JavaDoc path;
26     private ArrayList JavaDoc children = new ArrayList JavaDoc();
27     
28     public GroupData(String JavaDoc path, boolean defaultGroup) {
29         fDefault = defaultGroup;
30         this.path = path;
31     }
32
33     public GroupData(Element element) {
34         if (element.getNodeName().equals("hidden")) //$NON-NLS-1$
35
path = IUniversalIntroConstants.HIDDEN;
36         else
37             path = element.getAttribute("path"); //$NON-NLS-1$
38
NodeList JavaDoc children = element.getChildNodes();
39         for (int i = 0; i < children.getLength(); i++) {
40             Node JavaDoc child = children.item(i);
41             if (child.getNodeType() == Node.ELEMENT_NODE) {
42                 if (child.getNodeName().equals("extension")) { //$NON-NLS-1$
43
loadExtension((Element) child);
44                 }
45                 else if (child.getNodeName().equals("separator")) {//$NON-NLS-1$"
46
loadSeparator((Element)child);
47                 }
48             }
49         }
50         String JavaDoc df = element.getAttribute("default"); //$NON-NLS-1$
51
if (df!=null && df.equalsIgnoreCase("true")) //$NON-NLS-1$
52
fDefault = true;
53     }
54     
55     public boolean isHidden() {
56         return (path.equals(IUniversalIntroConstants.HIDDEN));
57     }
58     
59     public boolean isDefault() {
60         return fDefault;
61     }
62
63     public void addAnchors(List JavaDoc result) {
64         for (int i = 0; i < children.size(); i++) {
65             BaseData edata = (BaseData) children.get(i);
66             String JavaDoc id = edata.getId();
67             IntroElement element = null;
68             String JavaDoc tagName="anchor"; //$NON-NLS-1$
69
if (edata instanceof SeparatorData)
70                 tagName = "hr"; //$NON-NLS-1$
71
element = new IntroElement(tagName);
72             element.setAttribute("id", id); //$NON-NLS-1$
73
result.add(element);
74         }
75     }
76     
77     public void add(BaseData ed) {
78         children.add(ed);
79         ed.setParent(this);
80     }
81     public void add(int index, BaseData ed) {
82         children.add(index, ed);
83         ed.setParent(this);
84     }
85     public void remove(BaseData ed) {
86         children.remove(ed);
87         ed.setParent(null);
88     }
89     
90     public void addImplicitExtension(String JavaDoc id, String JavaDoc name) {
91         ExtensionData ed = new ExtensionData(id, name, IUniversalIntroConstants.LOW, true);
92         add(ed);
93     }
94     
95     private void loadSeparator(Element element) {
96         String JavaDoc id = element.getAttribute("id"); //$NON-NLS-1$
97
SeparatorData sd = new SeparatorData(id);
98         add(sd);
99     }
100
101     private void loadExtension(Element element) {
102         String JavaDoc id = element.getAttribute("id"); //$NON-NLS-1$
103
String JavaDoc name = element.getAttribute("name"); //$NON-NLS-1$
104
String JavaDoc importance = element.getAttribute("importance"); //$NON-NLS-1$
105
ExtensionData ed = new ExtensionData(id, name, importance, false);
106         add(ed);
107     }
108     
109     public BaseData[] getChildren() {
110         return (BaseData[])children.toArray(new BaseData[children.size()]);
111     }
112
113     public int getExtensionCount() {
114         int count=0;
115         for (int i=0; i<children.size(); i++) {
116             BaseData data = (BaseData)children.get(i);
117             if (data instanceof ExtensionData)
118                 count++;
119         }
120         return count;
121     }
122 /*
123     public ExtensionData[] getExtensions() {
124         ArrayList result = new ArrayList();
125         for (int i=0; i<children.size(); i++) {
126             BaseData data = (BaseData)children.get(i);
127             if (data instanceof ExtensionData)
128                 result.add(data);
129         }
130         return (ExtensionData[]) result.toArray(new ExtensionData[result.size()]);
131     }
132     */

133
134     public String JavaDoc getPath() {
135         return path;
136     }
137
138     public boolean contains(String JavaDoc id) {
139         return find(id)!=null;
140     }
141     
142     BaseData find(String JavaDoc extensionId) {
143         for (int i = 0; i < children.size(); i++) {
144             BaseData data = (BaseData) children.get(i);
145             if (data.getId().equals(extensionId))
146                 return data;
147         }
148         return null;
149     }
150
151     public int getIndexOf(BaseData ed) {
152         return children.indexOf(ed);
153     }
154     
155     public int getIndexOf(String JavaDoc baseId) {
156         for (int i = 0; i < children.size(); i++) {
157             BaseData bd = (BaseData) children.get(i);
158             if (bd.getId().equals(baseId))
159                 return i;
160         }
161         return -1;
162     }
163     
164     public boolean canMoveUp(BaseData ed) {
165         int index = children.indexOf(ed);
166         return (index>0);
167     }
168     
169     public boolean canMoveDown(BaseData ed) {
170         int index = children.indexOf(ed);
171         return (index!= -1 && index < children.size()-1);
172     }
173     
174     public void moveUp(BaseData ed) {
175         int index = children.indexOf(ed);
176         BaseData swapped = (BaseData)children.get(index-1);
177         children.set(index, swapped);
178         children.set(index-1, ed);
179     }
180
181     public void moveDown(BaseData ed) {
182         int index = children.indexOf(ed);
183         BaseData swapped = (BaseData)children.get(index+1);
184         children.set(index, swapped);
185         children.set(index+1, ed);
186     }
187     
188     public void addSeparator(BaseData after) {
189         SeparatorData sd = new SeparatorData();
190         sd.id = ""+sd.hashCode(); //$NON-NLS-1$
191
if (after!=null) {
192             int index = children.indexOf(after);
193             if (index!= -1) {
194                 children.add(index+1, sd);
195                 return;
196             }
197         }
198         children.add(sd);
199     }
200     
201     public void write(PrintWriter JavaDoc writer, String JavaDoc indent) {
202         writer.print(indent);
203         if (isHidden())
204             writer.print("<hidden>"); //$NON-NLS-1$
205
else {
206             writer.print("<group path=\""+path+"\""); //$NON-NLS-1$ //$NON-NLS-2$
207
if (fDefault)
208                 writer.println(" default=\"true\">"); //$NON-NLS-1$
209
else
210                 writer.println(">"); //$NON-NLS-1$
211
}
212         for (int i=0; i<children.size(); i++) {
213             BaseData ed = (BaseData)children.get(i);
214             ed.write(writer, indent+" "); //$NON-NLS-1$
215
}
216         writer.print(indent);
217         if (isHidden())
218             writer.println("</hidden>"); //$NON-NLS-1$
219
else
220             writer.println("</group>"); //$NON-NLS-1$
221
}
222 }
Popular Tags