1 11 package org.eclipse.jface.preference; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.jface.resource.ImageDescriptor; 17 import org.eclipse.core.runtime.Assert; 18 import org.eclipse.swt.graphics.Image; 19 20 24 public class PreferenceNode implements IPreferenceNode { 25 28 private IPreferencePage page; 29 30 34 private List subNodes; 35 36 40 private String classname; 41 42 45 private String id; 46 47 51 private String label; 52 53 56 private ImageDescriptor imageDescriptor; 57 58 61 private Image image; 62 63 70 public PreferenceNode(String id) { 71 Assert.isNotNull(id); 72 this.id = id; 73 } 74 75 93 public PreferenceNode(String id, String label, ImageDescriptor image, 94 String className) { 95 this(id); 96 this.imageDescriptor = image; 97 Assert.isNotNull(label); 98 this.label = label; 99 this.classname = className; 100 } 101 102 112 public PreferenceNode(String id, IPreferencePage preferencePage) { 113 this(id); 114 Assert.isNotNull(preferencePage); 115 page = preferencePage; 116 } 117 118 121 public void add(IPreferenceNode node) { 122 if (subNodes == null) { 123 subNodes = new ArrayList (); 124 } 125 subNodes.add(node); 126 } 127 128 134 private Object createObject(String className) { 135 Assert.isNotNull(className); 136 try { 137 Class cl = Class.forName(className); 138 if (cl != null) { 139 return cl.newInstance(); 140 } 141 } catch (ClassNotFoundException e) { 142 return null; 143 } catch (InstantiationException e) { 144 return null; 145 } catch (IllegalAccessException e) { 146 return null; 147 } catch (NoSuchMethodError e) { 148 return null; 149 } 150 return null; 151 } 152 153 156 public void createPage() { 157 page = (IPreferencePage) createObject(classname); 158 if (getLabelImage() != null) { 159 page.setImageDescriptor(imageDescriptor); 160 } 161 page.setTitle(label); 162 } 163 164 167 public void disposeResources() { 168 if (image != null) { 169 image.dispose(); 170 image = null; 171 } 172 if (page != null) { 173 page.dispose(); 174 page = null; 175 } 176 } 177 178 181 public IPreferenceNode findSubNode(String id) { 182 Assert.isNotNull(id); 183 Assert.isTrue(id.length() > 0); 184 if (subNodes == null) { 185 return null; 186 } 187 int size = subNodes.size(); 188 for (int i = 0; i < size; i++) { 189 IPreferenceNode node = (IPreferenceNode) subNodes.get(i); 190 if (id.equals(node.getId())) { 191 return node; 192 } 193 } 194 return null; 195 } 196 197 200 public String getId() { 201 return this.id; 202 } 203 204 209 protected ImageDescriptor getImageDescriptor() { 210 return imageDescriptor; 211 } 212 213 216 public Image getLabelImage() { 217 if (image == null && imageDescriptor != null) { 218 image = imageDescriptor.createImage(); 219 } 220 return image; 221 } 222 223 226 public String getLabelText() { 227 if (page != null) { 228 return page.getTitle(); 229 } 230 return label; 231 } 232 233 236 public IPreferencePage getPage() { 237 return page; 238 } 239 240 243 public IPreferenceNode[] getSubNodes() { 244 if (subNodes == null) { 245 return new IPreferenceNode[0]; 246 } 247 return (IPreferenceNode[]) subNodes 248 .toArray(new IPreferenceNode[subNodes.size()]); 249 } 250 251 254 public IPreferenceNode remove(String id) { 255 IPreferenceNode node = findSubNode(id); 256 if (node != null) { 257 remove(node); 258 } 259 return node; 260 } 261 262 265 public boolean remove(IPreferenceNode node) { 266 if (subNodes == null) { 267 return false; 268 } 269 return subNodes.remove(node); 270 } 271 272 277 public void setPage(IPreferencePage newPage) { 278 page = newPage; 279 } 280 } 281 | Popular Tags |