1 11 package org.eclipse.ui.internal.intro.impl.parts; 12 13 import java.util.Enumeration ; 14 import java.util.Hashtable ; 15 16 import org.eclipse.osgi.util.NLS; 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.custom.StackLayout; 19 import org.eclipse.swt.graphics.Point; 20 import org.eclipse.swt.graphics.Rectangle; 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Control; 23 import org.eclipse.swt.widgets.Layout; 24 import org.eclipse.ui.IMemento; 25 import org.eclipse.ui.forms.events.HyperlinkAdapter; 26 import org.eclipse.ui.forms.events.HyperlinkEvent; 27 import org.eclipse.ui.forms.widgets.FormToolkit; 28 import org.eclipse.ui.forms.widgets.ImageHyperlink; 29 import org.eclipse.ui.internal.intro.impl.IIntroConstants; 30 import org.eclipse.ui.internal.intro.impl.IntroPlugin; 31 import org.eclipse.ui.internal.intro.impl.Messages; 32 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroPage; 33 import org.eclipse.ui.internal.intro.impl.model.IntroModelRoot; 34 import org.eclipse.ui.internal.intro.impl.model.IntroStandbyContentPart; 35 import org.eclipse.ui.internal.intro.impl.model.loader.ExtensionPointManager; 36 import org.eclipse.ui.internal.intro.impl.model.loader.ModelLoaderUtil; 37 import org.eclipse.ui.internal.intro.impl.util.ImageUtil; 38 import org.eclipse.ui.internal.intro.impl.util.Log; 39 import org.eclipse.ui.internal.intro.impl.util.StringUtil; 40 import org.eclipse.ui.intro.IIntroPart; 41 import org.eclipse.ui.intro.config.CustomizableIntroPart; 42 import org.eclipse.ui.intro.config.IStandbyContentPart; 43 44 59 public class StandbyPart implements IIntroConstants { 60 61 private FormToolkit toolkit; 62 private IntroModelRoot model; 63 protected ImageHyperlink returnLink; 64 protected Control separator; 65 private Composite container; 66 protected Composite content; 67 private IIntroPart introPart; 68 private EmptyStandbyContentPart emptyPart; 69 private IMemento memento; 70 71 private Hashtable cachedContentParts = new Hashtable (); 73 74 private ControlKey cachedControlKey; 75 76 class StandbyLayout extends Layout { 77 78 private int VGAP = 9; 79 private int VMARGIN = 5; 80 private int HMARGIN = 5; 81 private int SEPARATOR_HEIGHT = 1; 82 83 89 protected Point computeSize(Composite composite, int wHint, int hHint, 90 boolean flushCache) { 91 Point lsize = returnLink.computeSize(SWT.DEFAULT, SWT.DEFAULT, 92 flushCache); 93 Point csize = content.computeSize(SWT.DEFAULT, SWT.DEFAULT, 94 flushCache); 95 int width = Math.max(lsize.x + 2 * HMARGIN, csize.x); 96 int height = VMARGIN + lsize.y + VGAP + SEPARATOR_HEIGHT + csize.y; 97 return new Point(width, height); 98 } 99 100 106 protected void layout(Composite composite, boolean flushCache) { 107 Rectangle carea = composite.getClientArea(); 108 int lwidth = carea.width - HMARGIN * 2; 109 Point lsize = returnLink.computeSize(lwidth, SWT.DEFAULT, 110 flushCache); 111 int x = HMARGIN; 112 int y = VMARGIN; 113 returnLink.setBounds(x, y, lsize.x, lsize.y); 114 x = 0; 115 y += lsize.y + VGAP; 116 separator.setBounds(x, y, carea.width, SEPARATOR_HEIGHT); 117 y += SEPARATOR_HEIGHT; 118 content.setBounds(x, y, carea.width, carea.height - VMARGIN 119 - lsize.y - VGAP - SEPARATOR_HEIGHT); 120 } 121 } 122 123 126 public StandbyPart(IntroModelRoot model) { 127 this.model = model; 128 } 129 130 131 public void init(IIntroPart introPart, IMemento memento) { 132 this.introPart = introPart; 133 this.memento = memento; 134 } 135 136 141 private IMemento getMemento(IMemento memento, String key) { 142 if (memento == null) 143 return null; 144 return memento.getChild(key); 145 } 146 147 public void createPartControl(Composite parent) { 148 toolkit = new FormToolkit(parent.getDisplay()); 149 container = toolkit.createComposite(parent); 152 container.setLayout(new StandbyLayout()); 153 154 ImageUtil.registerImage(ImageUtil.BACK, "full/elcl16/home_nav.gif"); returnLink = toolkit.createImageHyperlink(container, SWT.WRAP 157 | SWT.CENTER); 158 returnLink.setImage(ImageUtil.getImage(ImageUtil.BACK)); 159 returnLink.addHyperlinkListener(new HyperlinkAdapter() { 160 161 public void linkActivated(HyperlinkEvent e) { 162 doReturn(); 163 } 164 }); 165 166 separator = toolkit.createCompositeSeparator(container); 168 content = toolkit.createComposite(container); 170 StackLayout slayout = new StackLayout(); 171 slayout.marginWidth = slayout.marginHeight = 0; 172 content.setLayout(slayout); 173 174 boolean success = false; 175 if (memento != null) { 176 success = restoreState(memento); 177 if (!success) 178 addAndShowEmptyPart(Messages.StandbyPart_canNotRestore); 180 } 181 182 updateReturnLinkLabel(); 183 } 184 185 189 private void addAndShowEmptyPart(String message) { 190 if (emptyPart == null) 191 emptyPart = new EmptyStandbyContentPart(); 192 addStandbyContentPart(EMPTY_STANDBY_CONTENT_PART, emptyPart); 193 emptyPart.setMessage(message); 194 setTopControl(EMPTY_STANDBY_CONTENT_PART); 195 } 196 197 203 private boolean restoreState(IMemento memento) { 204 String contentPartId = memento 205 .getString(MEMENTO_STANDBY_CONTENT_PART_ID_ATT); 206 if (contentPartId == null) 207 return false; 208 return showContentPart(contentPartId, null); 211 } 212 213 214 221 public boolean showContentPart(String partId, String input) { 222 IntroStandbyContentPart standbyPartContent = ExtensionPointManager 224 .getInst().getSharedConfigExtensionsManager() 225 .getStandbyPart(partId); 226 227 if (standbyPartContent != null) { 228 String standbyContentClassName = standbyPartContent.getClassName(); 229 String pluginId = standbyPartContent.getPluginId(); 230 231 Object standbyContentObject = ModelLoaderUtil.createClassInstance( 232 pluginId, standbyContentClassName); 233 if (standbyContentObject instanceof IStandbyContentPart) { 234 IStandbyContentPart contentPart = (IStandbyContentPart) standbyContentObject; 235 Control c = addStandbyContentPart(partId, contentPart); 236 if (c != null) { 237 try { 238 setTopControl(partId); 239 setInput(input); 240 return true; 241 } catch (Exception e) { 242 Log.error("Failed to set the input: " + input + " on standby part: " + partId, e); } 245 } 246 247 String message = NLS.bind(Messages.StandbyPart_failedToCreate, 250 partId); 251 addAndShowEmptyPart(message); 252 return false; 253 254 } 255 } 256 257 String message = NLS.bind(Messages.StandbyPart_nonDefined, partId); 260 addAndShowEmptyPart(message); 261 return false; 262 } 263 264 274 public Control addStandbyContentPart(String partId, 275 IStandbyContentPart standbyContent) { 276 277 ControlKey controlKey = getCachedContent(partId); 278 if (controlKey == null) { 279 280 try { 281 standbyContent.init(introPart, getMemento(memento, 282 MEMENTO_STANDBY_CONTENT_PART_TAG)); 283 standbyContent.createPartControl(content, toolkit); 284 } catch (Exception e) { 285 Log.error( 287 "Failed to create part for standby part: " + partId, e); return null; 289 } 290 291 Control control = standbyContent.getControl(); 292 controlKey = new ControlKey(control, standbyContent, partId); 293 cachedContentParts.put(partId, controlKey); 294 if (partId.equals(EMPTY_STANDBY_CONTENT_PART)) 295 emptyPart = (EmptyStandbyContentPart) standbyContent; 297 298 if (controlKey.getControl() == null) { 299 String message = StringUtil 302 .concat("Standby Content part: ", partId, " has a null Control defined. This prevents the part from being displayed.") .toString(); 305 Log.error(message, null); 306 return null; 307 } 308 } 309 310 return controlKey.getControl(); 311 } 312 313 314 315 public void setInput(Object input) { 316 IStandbyContentPart standbyContent = cachedControlKey.getContentPart(); 317 standbyContent.setInput(input); 318 updateReturnLinkLabel(); 319 container.layout(); 320 } 321 322 323 public void setTopControl(String key) { 324 cachedControlKey = getCachedContent(key); 325 if (cachedControlKey != null) { 326 setTopControl(cachedControlKey.getControl()); 327 } 328 } 329 330 private void setTopControl(Control c) { 331 StackLayout layout = (StackLayout) content.getLayout(); 332 layout.topControl = c; 333 if (c instanceof Composite) 334 ((Composite) c).layout(); 335 content.layout(); 336 container.layout(); 337 } 338 339 private void updateReturnLinkLabel() { 340 String linkText = Messages.StandbyPart_returnToIntro; 341 returnLink.setText(linkText); 342 AbstractIntroPage page = model.getCurrentPage(); 343 if (page == null) 344 return; 346 347 String toolTip = Messages.StandbyPart_returnTo; 348 if (page.getTitle() != null) 349 toolTip += " " + page.getTitle(); 351 returnLink.setToolTipText(toolTip); 352 } 353 354 protected void doReturn() { 355 ((CustomizableIntroPart) introPart).getControl().setData( 357 IIntroConstants.SHOW_STANDBY_PART, null); 358 IntroPlugin.setIntroStandby(false); 359 } 360 361 365 public void dispose() { 366 Enumeration values = cachedContentParts.elements(); 367 while (values.hasMoreElements()) { 368 ControlKey controlKey = (ControlKey) values.nextElement(); 369 controlKey.getContentPart().dispose(); 370 } 371 toolkit.dispose(); 372 } 373 374 384 public void saveState(IMemento memento) { 385 if (cachedControlKey != null) { 387 String contentPartId = cachedControlKey.getContentPartId(); 388 if (contentPartId == EMPTY_STANDBY_CONTENT_PART) 389 return; 391 memento.putString(MEMENTO_STANDBY_CONTENT_PART_ID_ATT, 392 contentPartId); 393 IMemento standbyContentPartMemento = memento 396 .createChild(MEMENTO_STANDBY_CONTENT_PART_TAG); 397 IStandbyContentPart standbyContentpart = cachedControlKey 399 .getContentPart(); 400 if (standbyContentpart != null) 401 standbyContentpart.saveState(standbyContentPartMemento); 402 } 403 } 404 405 406 412 public void setFocus() { 413 returnLink.setFocus(); 416 if (cachedControlKey != null) 417 cachedControlKey.getContentPart().setFocus(); 418 } 419 420 421 422 429 private ControlKey getCachedContent(String key) { 430 if (cachedContentParts.containsKey(key)) 431 return (ControlKey) cachedContentParts.get(key); 432 return null; 433 } 434 435 439 class ControlKey { 440 441 Control c; 442 IStandbyContentPart part; 443 String contentPartId; 444 445 ControlKey(Control c, IStandbyContentPart part, String contentPartId) { 446 this.c = c; 447 this.part = part; 448 this.contentPartId = contentPartId; 449 } 450 451 454 public Control getControl() { 455 return c; 456 } 457 458 461 public IStandbyContentPart getContentPart() { 462 return part; 463 } 464 465 468 public String getContentPartId() { 469 return contentPartId; 470 } 471 } 472 473 474 } 475 | Popular Tags |