1 11 12 package org.eclipse.ui.intro.config; 13 14 import org.eclipse.core.runtime.IAdapterFactory; 15 import org.eclipse.core.runtime.IRegistryChangeEvent; 16 import org.eclipse.core.runtime.IRegistryChangeListener; 17 import org.eclipse.core.runtime.PerformanceStats; 18 import org.eclipse.core.runtime.Platform; 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.custom.StackLayout; 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Control; 23 import org.eclipse.swt.widgets.Display; 24 import org.eclipse.ui.IMemento; 25 import org.eclipse.ui.PartInitException; 26 import org.eclipse.ui.internal.intro.impl.IIntroConstants; 27 import org.eclipse.ui.internal.intro.impl.IntroPlugin; 28 import org.eclipse.ui.internal.intro.impl.Messages; 29 import org.eclipse.ui.internal.intro.impl.model.IntroModelRoot; 30 import org.eclipse.ui.internal.intro.impl.model.IntroPartPresentation; 31 import org.eclipse.ui.internal.intro.impl.model.loader.ContentProviderManager; 32 import org.eclipse.ui.internal.intro.impl.model.loader.ExtensionPointManager; 33 import org.eclipse.ui.internal.intro.impl.model.loader.ModelLoaderUtil; 34 import org.eclipse.ui.internal.intro.impl.parts.StandbyPart; 35 import org.eclipse.ui.internal.intro.impl.presentations.BrowserIntroPartImplementation; 36 import org.eclipse.ui.internal.intro.impl.util.DialogUtil; 37 import org.eclipse.ui.internal.intro.impl.util.Log; 38 import org.eclipse.ui.intro.IIntroSite; 39 import org.eclipse.ui.part.IntroPart; 40 41 70 71 79 public final class CustomizableIntroPart extends IntroPart implements 80 IIntroConstants, IRegistryChangeListener { 81 82 private IntroPartPresentation presentation; 83 private StandbyPart standbyPart; 84 private Composite container; 85 private IMemento memento; 86 IntroModelRoot model; 87 private boolean restoreStandby; 90 91 92 IAdapterFactory factory = new IAdapterFactory() { 94 95 public Class [] getAdapterList() { 96 return new Class [] { StandbyPart.class, IntroPartPresentation.class }; 97 } 98 99 public Object getAdapter(Object adaptableObject, Class adapterType) { 100 if (!(adaptableObject instanceof CustomizableIntroPart)) 101 return null; 102 103 if (adapterType.equals(StandbyPart.class)) { 104 return getStandbyPart(); 105 } else if (adapterType.equals(IntroPartPresentation.class)) { 106 return getPresentation(); 107 } else 108 return null; 109 } 110 }; 111 112 public CustomizableIntroPart() { 113 Platform.getAdapterManager().registerAdapters(factory, 115 CustomizableIntroPart.class); 116 119 if (Log.logPerformance) { 122 if (PerformanceStats.ENABLED) 123 PerformanceStats.getStats( 124 IIntroConstants.PERF_VIEW_CREATION_TIME, 125 IIntroConstants.INTRO).startRun(); 126 else 127 IntroPlugin.getDefault().setUICreationStartTime( 131 System.currentTimeMillis()); 132 } 133 } 134 135 141 public void init(IIntroSite site, IMemento memento) 142 throws PartInitException { 143 super.init(site, memento); 144 IntroPlugin.getDefault().closeLaunchBar(); 145 String introId = getConfigurationElement().getAttribute("id"); ExtensionPointManager extensionPointManager = IntroPlugin.getDefault() 149 .getExtensionPointManager(); 150 extensionPointManager.setIntroId(introId); 151 model = extensionPointManager.getCurrentModel(); 152 153 if (model != null && model.hasValidConfig()) { 154 presentation = model.getPresentation(); 157 if (presentation != null) 158 presentation.init(this, getMemento(memento, 159 MEMENTO_PRESENTATION_TAG)); 160 161 163 this.memento = memento; 165 restoreStandby = needToRestoreStandby(memento); 166 167 Platform.getExtensionRegistry().addRegistryChangeListener(this, 169 IIntroConstants.PLUGIN_ID); 170 } 171 172 if (model == null || !model.hasValidConfig()) 173 DialogUtil.displayErrorMessage(site.getShell(), 174 Messages.CustomizableIntroPart_configNotFound, 175 new Object [] { ModelLoaderUtil.getLogString( 176 getConfigurationElement(), null) }, null); 177 178 } 179 180 185 public void createPartControl(Composite parent) { 186 container = new Composite(parent, SWT.NULL); 187 StackLayout layout = new StackLayout(); 188 layout.marginHeight = 0; 189 layout.marginWidth = 0; 190 container.setLayout(layout); 191 192 if (model != null && model.hasValidConfig()) { 193 presentation.createPartControl(container); 194 } 196 197 if (Log.logPerformance) { 198 PerformanceStats stats = PerformanceStats.getStats( 199 IIntroConstants.PERF_UI_ZOOM, IIntroConstants.INTRO); 200 stats.startRun(); 201 } 202 203 } 204 205 206 207 208 216 private boolean needToRestoreStandby(IMemento memento) { 217 IMemento standbyMemento = getMemento(memento, MEMENTO_STANDBY_PART_TAG); 220 if (standbyMemento == null) 221 return false; 222 String restore = standbyMemento.getString(MEMENTO_RESTORE_ATT); 223 if (restore == null) 224 return false; 225 String cachedStandbyPart = standbyMemento 226 .getString(MEMENTO_STANDBY_CONTENT_PART_ID_ATT); 227 if (cachedStandbyPart != null 228 && cachedStandbyPart.equals(EMPTY_STANDBY_CONTENT_PART)) 229 return false; 230 231 return cachedStandbyPart != null ? true : false; 232 } 233 234 240 public void standbyStateChanged(boolean standby) { 241 242 if (model == null || !model.hasValidConfig()) 244 return; 245 246 if (!standby) 247 restoreStandby = false; 249 250 boolean isStandbyPartNeeded = isStandbyPartNeeded(); 251 isStandbyPartNeeded = isStandbyPartNeeded | restoreStandby; 252 253 if (standbyPart == null && standby && isStandbyPartNeeded) 254 createStandbyPart(); 257 258 handleSetFocus(isStandbyPartNeeded); 259 setTopControl(isStandbyPartNeeded ? getStandbyControl() 260 : getPresentationControl()); 261 presentation.standbyStateChanged(standby, isStandbyPartNeeded); 265 266 } 267 268 277 private boolean isStandbyPartNeeded() { 278 return container.getData(SHOW_STANDBY_PART) == null ? false : true; 279 } 280 281 285 private void createStandbyPart() { 286 standbyPart = new StandbyPart(model); 287 standbyPart.init(this, getMemento(memento, MEMENTO_STANDBY_PART_TAG)); 288 standbyPart.createPartControl((Composite) getControl()); 289 restoreStandby = false; 290 container.setData(SHOW_STANDBY_PART, "true"); } 292 293 private void handleSetFocus(boolean standby) { 294 if (standby) { 295 if (standbyPart != null) 298 standbyPart.setFocus(); 299 } else 300 presentation.setFocus(); 301 } 302 303 308 public void setFocus() { 309 handleSetFocus(IntroPlugin.isIntroStandby()); 310 } 311 312 private void setTopControl(Control c) { 313 StackLayout layout = (StackLayout) container.getLayout(); 315 layout.topControl = c; 316 container.layout(); 317 } 318 319 private Control getPresentationControl() { 320 return container.getChildren()[0]; 321 } 322 323 private Control getStandbyControl() { 324 if (standbyPart != null) 328 return container.getChildren()[1]; 329 return null; 330 } 331 332 IntroPartPresentation getPresentation() { 333 return presentation; 334 } 335 336 341 public void dispose() { 342 super.dispose(); 343 if (presentation != null) 345 presentation.dispose(); 346 if (standbyPart != null) 347 standbyPart.dispose(); 348 IntroPlugin.getDefault().getExtensionPointManager().clear(); 350 ContentProviderManager.getInst().clear(); 351 Platform.getAdapterManager().unregisterAdapters(factory, 353 CustomizableIntroPart.class); 354 if (model != null && model.hasValidConfig()) 355 Platform.getExtensionRegistry().removeRegistryChangeListener(this); 356 357 } 358 359 362 StandbyPart getStandbyPart() { 363 return standbyPart; 364 } 365 366 373 public Control getControl() { 374 return container; 375 } 376 377 public void saveState(IMemento memento) { 378 383 boolean restorePresentation = false; 386 StackLayout layout = (StackLayout) container.getLayout(); 387 if (getPresentationControl().equals(layout.topControl)) 388 restorePresentation = true; 389 390 IMemento presentationMemento = memento 391 .createChild(MEMENTO_PRESENTATION_TAG); 392 IMemento standbyPartMemento = memento 393 .createChild(MEMENTO_STANDBY_PART_TAG); 394 if (restorePresentation) 395 presentationMemento.putString(MEMENTO_RESTORE_ATT, "true"); else 397 standbyPartMemento.putString(MEMENTO_RESTORE_ATT, "true"); if (presentation != null) 399 presentation.saveState(presentationMemento); 400 if (standbyPart != null) 401 standbyPart.saveState(standbyPartMemento); 402 } 403 404 409 410 private IMemento getMemento(IMemento memento, String key) { 411 if (memento == null) 412 return null; 413 return memento.getChild(key); 414 } 415 416 421 public void registryChanged(final IRegistryChangeEvent event) { 422 Display.getDefault().syncExec(new Runnable () { 428 429 public void run() { 430 String currentPageId = model.getCurrentPageId(); 431 ExtensionPointManager.getInst().clear(); 433 ContentProviderManager.getInst().clear(); 434 model = ExtensionPointManager.getInst().getCurrentModel(); 436 model.setPresentation(getPresentation()); 438 model.setCurrentPageId(currentPageId, false); 440 if (getPresentation() != null) 441 getPresentation().registryChanged(event); 442 443 } 444 }); 445 446 } 447 448 451 public boolean internal_isFinishedLoading() { 452 BrowserIntroPartImplementation impl = (BrowserIntroPartImplementation)presentation.getIntroPartImplementation(); 453 return impl.isFinishedLoading(); 454 } 455 } 456 | Popular Tags |