1 11 package org.eclipse.ui.internal.intro.universal; 12 13 import java.io.PrintWriter ; 14 import java.util.ArrayList ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.IPath; 18 import org.eclipse.core.runtime.Path; 19 import org.w3c.dom.Element ; 20 import org.w3c.dom.Node ; 21 import org.w3c.dom.NodeList ; 22 23 public class PageData { 24 public static final String P_TOP_LEFT = "page-content/top-left"; public static final String P_TOP_RIGHT = "page-content/top-right"; public static final String P_BOTTOM_LEFT = "page-content/bottom-left"; public static final String P_BOTTOM_RIGHT = "page-content/bottom-right"; 29 private String id; 30 private ArrayList groups = new ArrayList (); 31 private GroupData hidden=null; 32 33 public PageData(String id) { 34 this.id = id; 35 } 36 37 public PageData(Element page) { 38 this.id = page.getAttribute("id"); NodeList children = page.getChildNodes(); 40 for (int i=0; i<children.getLength(); i++) { 41 Node child = children.item(i); 42 if (child.getNodeType()==Node.ELEMENT_NODE) { 43 Element element = (Element )child; 44 if (element.getNodeName().equals("group")) { 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 result, String groupId) { 62 GroupData group = findGroup(groupId); 63 if (group==null) return; 64 group.addAnchors(result); 65 } 66 67 public String resolvePath(String 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 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 extensionId) { 95 return hidden!=null && hidden.contains(extensionId); 96 } 97 98 public GroupData findGroup(String 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 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 extensionId, String name) { 118 ExtensionData ed = findExtension(extensionId, true); 119 if (ed!=null) { 120 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 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 getId() { 151 return id; 152 } 153 154 public ExtensionData findExtension(String 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 if (checkHidden && hidden!=null) 163 return find (hidden, extensionId); 164 return null; 165 } 166 167 private ExtensionData find(GroupData gd, String 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 resolveExtension(String extensionId) { 175 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; } 181 } 182 if (hidden!=null && hidden.contains(extensionId)) 184 return null; 185 if (groups.size()==0) return null; 187 GroupData last = (GroupData)groups.get(groups.size()-1); 188 return id + "/" + last.getPath() + "/" + IUniversalIntroConstants.DEFAULT_ANCHOR; } 190 191 public void write(PrintWriter writer, String indent) { 192 writer.print(indent); 193 String indent2 = indent+" "; writer.println("<page id=\""+id+"\">"); 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>"); } 204 } | Popular Tags |