1 11 package org.eclipse.ui.internal; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.Map ; 17 18 import org.eclipse.core.runtime.IExtension; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.MultiStatus; 21 import org.eclipse.core.runtime.SafeRunner; 22 import org.eclipse.core.runtime.Status; 23 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler; 24 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker; 25 import org.eclipse.jface.util.SafeRunnable; 26 import org.eclipse.osgi.util.NLS; 27 import org.eclipse.ui.IMemento; 28 import org.eclipse.ui.IViewPart; 29 import org.eclipse.ui.IViewReference; 30 import org.eclipse.ui.IWorkbenchPart3; 31 import org.eclipse.ui.PartInitException; 32 import org.eclipse.ui.PlatformUI; 33 import org.eclipse.ui.views.IViewDescriptor; 34 import org.eclipse.ui.views.IViewRegistry; 35 36 41 class ViewFactory implements IExtensionChangeHandler { 42 43 private ReferenceCounter counter; 44 45 private HashMap mementoTable = new HashMap (); 46 47 WorkbenchPage page; 48 49 IViewRegistry viewReg; 50 51 54 static final String ID_SEP = ":"; 56 64 static String getKey(String id, String secondaryId) { 65 return secondaryId == null ? id : id + ID_SEP + secondaryId; 66 } 67 68 74 static String getKey(IViewReference viewRef) { 75 return getKey(viewRef.getId(), viewRef.getSecondaryId()); 76 } 77 78 83 static String extractPrimaryId(String compoundId) { 84 int i = compoundId.lastIndexOf(ID_SEP); 85 if (i == -1) { 86 return compoundId; 87 } 88 return compoundId.substring(0, i); 89 } 90 91 96 static String extractSecondaryId(String compoundId) { 97 int i = compoundId.lastIndexOf(ID_SEP); 98 if (i == -1) { 99 return null; 100 } 101 return compoundId.substring(i + 1); 102 } 103 104 114 static boolean hasWildcard(String viewId) { 115 return viewId.indexOf(PartPlaceholder.WILD_CARD) >= 0; 116 } 117 118 121 public ViewFactory(WorkbenchPage page, IViewRegistry reg) { 122 super(); 123 this.page = page; 124 this.viewReg = reg; 125 counter = new ReferenceCounter(); 126 page.getExtensionTracker().registerHandler(this, null); 127 } 128 129 138 public IViewReference createView(final String id) throws PartInitException { 139 return createView(id, null); 140 } 141 142 151 public IViewReference createView(String id, String secondaryId) 152 throws PartInitException { 153 IViewDescriptor desc = viewReg.find(id); 154 if (desc == null) { 156 throw new PartInitException(NLS.bind(WorkbenchMessages.ViewFactory_couldNotCreate, id )); 157 } 158 if (secondaryId != null) { 160 if (!desc.getAllowMultiple()) { 161 throw new PartInitException(NLS.bind(WorkbenchMessages.ViewFactory_noMultiple, id)); 162 } 163 } 164 String key = getKey(id, secondaryId); 165 IViewReference ref = (IViewReference) counter.get(key); 166 if (ref == null) { 167 IMemento memento = (IMemento) mementoTable.get(key); 168 ref = new ViewReference(this, id, secondaryId, memento); 169 mementoTable.remove(key); 170 counter.put(key, ref); 171 getWorkbenchPage().partAdded((ViewReference)ref); 172 } else { 173 counter.addRef(key); 174 } 175 return ref; 176 } 177 178 183 public IViewReference[] getViewReferences() { 184 List values = counter.values(); 185 186 return (IViewReference[]) values.toArray(new IViewReference[values.size()]); 187 } 188 189 192 public IViewReference getView(String id) { 193 return getView(id, null); 194 } 195 196 199 public IViewReference getView(String id, String secondaryId) { 200 String key = getKey(id, secondaryId); 201 return (IViewReference) counter.get(key); 202 } 203 204 208 public IViewRegistry getViewRegistry() { 209 return viewReg; 210 } 211 212 215 public IViewReference[] getViews() { 216 List list = counter.values(); 217 IViewReference[] array = new IViewReference[list.size()]; 218 list.toArray(array); 219 return array; 220 } 221 222 226 public WorkbenchPage getWorkbenchPage() { 227 return page; 228 } 229 230 235 public int getReferenceCount(IViewReference viewRef) { 236 String key = getKey(viewRef); 237 IViewReference ref = (IViewReference) counter.get(key); 238 return ref==null ? 0 : counter.getRef(key); 239 } 240 241 247 public void releaseView(IViewReference viewRef) { 248 String key = getKey(viewRef); 249 IViewReference ref = (IViewReference) counter.get(key); 250 if (ref == null) { 251 return; 252 } 253 int count = counter.removeRef(key); 254 if (count <= 0) { 255 getWorkbenchPage().partRemoved((ViewReference)ref); 256 } 257 } 258 259 265 public IStatus restoreState(IMemento memento) { 266 IMemento mem[] = memento.getChildren(IWorkbenchConstants.TAG_VIEW); 267 for (int i = 0; i < mem.length; i++) { 268 restoreViewState(mem[i]); 270 } 271 return new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 0, "", null); } 273 274 280 public IStatus saveState(IMemento memento) { 281 final MultiStatus result = new MultiStatus(PlatformUI.PLUGIN_ID, 282 IStatus.OK, WorkbenchMessages.ViewFactory_problemsSavingViews, null); 283 284 final IViewReference refs[] = getViews(); 285 for (int i = 0; i < refs.length; i++) { 286 saveViewState(memento, refs[i], result); 288 } 289 return result; 290 } 291 292 public IMemento saveViewState(IMemento memento, IViewReference ref, 294 MultiStatus res) { 295 final MultiStatus result = res; 296 final IMemento viewMemento = memento 297 .createChild(IWorkbenchConstants.TAG_VIEW); 298 viewMemento.putString(IWorkbenchConstants.TAG_ID, ViewFactory 299 .getKey(ref)); 300 if (ref instanceof ViewReference) { 301 viewMemento.putString(IWorkbenchConstants.TAG_PART_NAME, 302 ((ViewReference) ref).getPartName()); 303 } 304 final IViewReference viewRef = ref; 305 final IViewPart view = (IViewPart) ref.getPart(false); 306 if (view != null) { 307 SafeRunner.run(new SafeRunnable() { 308 public void run() { 309 if (view instanceof IWorkbenchPart3) { 310 Map properties = ((IWorkbenchPart3) view) 311 .getPartProperties(); 312 if (!properties.isEmpty()) { 313 IMemento propBag = viewMemento 314 .createChild(IWorkbenchConstants.TAG_PROPERTIES); 315 Iterator i = properties.entrySet().iterator(); 316 while (i.hasNext()) { 317 Map.Entry entry = (Map.Entry ) i.next(); 318 IMemento p = propBag.createChild( 319 IWorkbenchConstants.TAG_PROPERTY, 320 (String ) entry.getKey()); 321 p.putTextData((String ) entry.getValue()); 322 } 323 } 324 } 325 view.saveState(viewMemento 326 .createChild(IWorkbenchConstants.TAG_VIEW_STATE)); 327 } 328 329 public void handleException(Throwable e) { 330 result 331 .add(new Status( 332 IStatus.ERROR, 333 PlatformUI.PLUGIN_ID, 334 0, 335 NLS.bind(WorkbenchMessages.ViewFactory_couldNotSave, viewRef.getTitle() ), 336 e)); 337 } 338 }); 339 } else { 340 IMemento mem = null; 341 IMemento props = null; 342 343 if (ref instanceof ViewReference) { 347 mem = ((ViewReference) ref).getMemento(); 348 if (mem!=null) { 349 props = mem.getChild(IWorkbenchConstants.TAG_PROPERTIES); 350 } 351 if (mem!=null) { 352 mem = mem.getChild(IWorkbenchConstants.TAG_VIEW_STATE); 353 } 354 } 355 if (props != null) { 356 viewMemento.createChild(IWorkbenchConstants.TAG_PROPERTIES) 357 .putMemento(props); 358 } 359 if (mem != null) { 360 IMemento child = viewMemento 361 .createChild(IWorkbenchConstants.TAG_VIEW_STATE); 362 child.putMemento(mem); 363 } 364 } 365 return viewMemento; 366 } 367 368 public void restoreViewState(IMemento memento) { 370 String compoundId = memento.getString(IWorkbenchConstants.TAG_ID); 371 mementoTable.put(compoundId, memento); 372 } 373 374 IMemento getViewState(String key) { 375 IMemento memento = (IMemento) mementoTable.get(key); 376 377 if (memento == null) { 378 return null; 379 } 380 381 return memento.getChild(IWorkbenchConstants.TAG_VIEW_STATE); 382 } 383 384 387 public void removeExtension(IExtension source, Object [] objects) { 388 for (int i = 0; i < objects.length; i++) { 389 if (objects[i] instanceof IViewPart) { 390 IViewPart part = (IViewPart) objects[i]; 391 page.hideView(part); 407 } 408 409 } 410 } 411 412 415 public void addExtension(IExtensionTracker tracker,IExtension extension) { 416 } 418 419 } 420 421 | Popular Tags |