1 11 12 package org.eclipse.ui.internal.intro.impl.model.url; 13 14 import java.util.Enumeration ; 15 import java.util.Hashtable ; 16 import java.util.Properties ; 17 18 import org.eclipse.core.commands.ParameterizedCommand; 19 import org.eclipse.core.commands.common.CommandException; 20 import org.eclipse.jface.action.Action; 21 import org.eclipse.jface.action.IAction; 22 import org.eclipse.jface.util.Geometry; 23 import org.eclipse.swt.browser.Browser; 24 import org.eclipse.swt.custom.BusyIndicator; 25 import org.eclipse.swt.graphics.Rectangle; 26 import org.eclipse.swt.widgets.Display; 27 import org.eclipse.ui.IActionDelegate; 28 import org.eclipse.ui.IWorkbench; 29 import org.eclipse.ui.IWorkbenchWindow; 30 import org.eclipse.ui.IWorkbenchWindowActionDelegate; 31 import org.eclipse.ui.PlatformUI; 32 import org.eclipse.ui.commands.ICommandService; 33 import org.eclipse.ui.handlers.IHandlerService; 34 import org.eclipse.ui.internal.RectangleAnimation; 35 import org.eclipse.ui.internal.intro.impl.IIntroConstants; 36 import org.eclipse.ui.internal.intro.impl.IntroPlugin; 37 import org.eclipse.ui.internal.intro.impl.Messages; 38 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroElement; 39 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroPage; 40 import org.eclipse.ui.internal.intro.impl.model.IntroLaunchBarElement; 41 import org.eclipse.ui.internal.intro.impl.model.IntroModelRoot; 42 import org.eclipse.ui.internal.intro.impl.model.IntroPartPresentation; 43 import org.eclipse.ui.internal.intro.impl.model.IntroTheme; 44 import org.eclipse.ui.internal.intro.impl.model.IntroURLAction; 45 import org.eclipse.ui.internal.intro.impl.model.loader.ExtensionPointManager; 46 import org.eclipse.ui.internal.intro.impl.model.loader.ModelLoaderUtil; 47 import org.eclipse.ui.internal.intro.impl.model.util.ModelUtil; 48 import org.eclipse.ui.internal.intro.impl.parts.StandbyPart; 49 import org.eclipse.ui.internal.intro.impl.presentations.BrowserIntroPartImplementation; 50 import org.eclipse.ui.internal.intro.impl.presentations.IntroLaunchBar; 51 import org.eclipse.ui.internal.intro.impl.util.DialogUtil; 52 import org.eclipse.ui.internal.intro.impl.util.Log; 53 import org.eclipse.ui.internal.intro.impl.util.StringUtil; 54 import org.eclipse.ui.internal.intro.impl.util.Util; 55 import org.eclipse.ui.intro.IIntroPart; 56 import org.eclipse.ui.intro.IIntroSite; 57 import org.eclipse.ui.intro.config.CustomizableIntroPart; 58 import org.eclipse.ui.intro.config.IIntroAction; 59 import org.eclipse.ui.intro.config.IIntroURL; 60 import org.eclipse.ui.intro.config.IntroURLFactory; 61 62 67 public class IntroURL implements IIntroURL { 68 69 70 73 public static final String INTRO_PROTOCOL = "http"; public static final String INTRO_HOST_ID = "org.eclipse.ui.intro"; 76 79 public static final String SET_STANDBY_MODE = "setStandbyMode"; public static final String SHOW_STANDBY = "showStandby"; public static final String CLOSE = "close"; public static final String SHOW_HELP_TOPIC = "showHelpTopic"; public static final String SHOW_HELP = "showHelp"; public static final String OPEN_BROWSER = "openBrowser"; public static final String OPEN_URL = "openURL"; public static final String RUN_ACTION = "runAction"; public static final String SHOW_PAGE = "showPage"; public static final String SHOW_MESSAGE = "showMessage"; public static final String NAVIGATE = "navigate"; public static final String SWITCH_TO_LAUNCH_BAR = "switchToLaunchBar"; public static final String EXECUTE = "execute"; 93 96 public static final String KEY_ID = "id"; public static final String KEY_PLUGIN_ID = "pluginId"; public static final String KEY_CLASS = "class"; public static final String KEY_STANDBY = "standby"; public static final String KEY_PART_ID = "partId"; public static final String KEY_INPUT = "input"; public static final String KEY_MESSAGE = "message"; public static final String KEY_URL = "url"; public static final String KEY_DIRECTION = "direction"; public static final String KEY_EMBED = "embed"; public static final String KEY_EMBED_TARGET = "embedTarget"; public static final String KEY_DECODE = "decode"; public static final String KEY_COMAND = "command"; 110 111 public static final String VALUE_BACKWARD = "backward"; public static final String VALUE_FORWARD = "forward"; public static final String VALUE_HOME = "home"; public static final String VALUE_TRUE = "true"; public static final String VALUE_FALSE = "false"; 117 118 119 private String action = null; 120 private Properties parameters = null; 121 122 128 IntroURL(String action, Properties parameters) { 129 this.action = action; 130 this.parameters = parameters; 131 } 132 133 137 public boolean execute() { 138 final boolean[] result = new boolean[1]; 139 Display display = Display.getCurrent(); 140 141 BusyIndicator.showWhile(display, new Runnable () { 142 143 public void run() { 144 result[0] = doExecute(); 145 } 146 }); 147 return result[0]; 148 } 149 150 protected boolean doExecute() { 151 if (Log.logInfo) { 152 String msg = StringUtil.concat("Running Intro URL action ", action, " with parameters: ", parameters.toString()).toString(); Log.info(msg); 155 } 156 157 if (action.equals(CLOSE)) 159 return closeIntro(); 160 161 else if (action.equals(SET_STANDBY_MODE)) 162 return setStandbyState(getParameter(KEY_STANDBY)); 165 166 else if (action.equals(SHOW_STANDBY)) 167 return handleStandbyState(getParameter(KEY_PART_ID), 168 getParameter(KEY_INPUT)); 169 170 else if (action.equals(SHOW_HELP)) 171 return showHelp(); 173 174 else if (action.equals(SHOW_HELP_TOPIC)) 175 return showHelpTopic(getParameter(KEY_ID), getParameter(KEY_EMBED), 179 getParameter(KEY_EMBED_TARGET)); 180 181 else if (action.equals(OPEN_BROWSER)) 182 return openBrowser(getParameter(KEY_URL), 184 getParameter(KEY_PLUGIN_ID)); 185 186 if (action.equals(OPEN_URL)) 187 return openURL(getParameter(KEY_URL), getParameter(KEY_PLUGIN_ID)); 189 190 else if (action.equals(RUN_ACTION)) 191 return runAction(getParameter(KEY_PLUGIN_ID), 194 getParameter(KEY_CLASS), parameters, getParameter(KEY_STANDBY)); 195 196 else if (action.equals(EXECUTE)) 197 return executeCommand(getParameter(KEY_COMAND), getParameter(KEY_STANDBY)); 199 200 else if (action.equals(SHOW_PAGE)) 201 return showPage(getParameter(KEY_ID), getParameter(KEY_STANDBY)); 203 204 else if (action.equals(SHOW_MESSAGE)) 205 return showMessage(getParameter(KEY_MESSAGE)); 206 207 else if (action.equals(NAVIGATE)) 208 return navigate(getParameter(KEY_DIRECTION)); 209 210 else if (action.equals(SWITCH_TO_LAUNCH_BAR)) 211 return switchToLaunchBar(); 212 213 else 214 return handleCustomAction(); 215 } 216 217 218 private boolean closeIntro() { 219 return IntroPlugin.closeIntro(); 221 } 222 223 230 private boolean handleStandbyState(String partId, String input) { 231 CustomizableIntroPart introPart = (CustomizableIntroPart) IntroPlugin 233 .getIntro(); 234 if (introPart == null) 235 introPart = (CustomizableIntroPart) IntroPlugin.showIntro(true); 236 introPart.getControl().setData(IIntroConstants.SHOW_STANDBY_PART, 238 VALUE_TRUE); 239 IntroPlugin.setIntroStandby(true); 240 StandbyPart standbyPart = (StandbyPart) introPart 241 .getAdapter(StandbyPart.class); 242 243 boolean success = standbyPart.showContentPart(partId, input); 244 if (success) 245 return true; 246 247 return false; 250 } 251 252 258 private boolean setStandbyState(String state) { 259 if (state == null) 260 return false; 261 boolean standby = state.equals(VALUE_TRUE) ? true : false; 262 IIntroPart introPart = IntroPlugin.showIntro(standby); 263 if (introPart == null) 264 return false; 265 return true; 266 } 267 268 269 272 private boolean runAction(String pluginId, String className, 273 Properties parameters, String standbyState) { 274 275 Object actionObject = ModelLoaderUtil.createClassInstance(pluginId, 276 className); 277 try { 278 if (actionObject instanceof IIntroAction) { 279 IIntroAction introAction = (IIntroAction) actionObject; 280 IIntroSite site = IntroPlugin.getDefault().getIntroModelRoot() 281 .getPresentation().getIntroPart().getIntroSite(); 282 introAction.run(site, parameters); 283 } else if (actionObject instanceof IAction) { 284 IAction action = (IAction) actionObject; 285 action.run(); 286 287 } else if (actionObject instanceof IActionDelegate) { 288 final IActionDelegate delegate = (IActionDelegate) actionObject; 289 if (delegate instanceof IWorkbenchWindowActionDelegate) 290 ((IWorkbenchWindowActionDelegate) delegate).init(PlatformUI 291 .getWorkbench().getActiveWorkbenchWindow()); 292 Action proxy = new Action(this.action) { 293 294 public void run() { 295 delegate.run(this); 296 } 297 }; 298 proxy.run(); 299 } else 300 return false; 302 if (standbyState == null) 304 return true; 305 return setStandbyState(standbyState); 306 } catch (Exception e) { 307 Log.error("Could not run action: " + className, e); return false; 309 } 310 } 311 312 317 private boolean executeCommand(String command, String standbyState) { 318 ICommandService commandService = getCommandService(); 319 IHandlerService handlerService = getHandlerService(); 320 if (commandService == null || handlerService == null) { 321 Log.error("Could not get ICommandService or IHandlerService while trying to execute: " + command, null); return false; 323 } 324 325 try { 326 ParameterizedCommand pCommand = commandService.deserialize(command); 327 pCommand.executeWithChecks(null, handlerService.getCurrentState()); 328 329 if (standbyState == null) 331 return true; 332 return setStandbyState(standbyState); 333 } catch (CommandException ex) { 334 Log.error("Could not execute command: " + command, ex); return false; 336 } 337 } 338 339 private ICommandService getCommandService() { 340 IWorkbench wb = PlatformUI.getWorkbench(); 341 if (wb != null) { 342 Object serviceObject = wb.getAdapter(ICommandService.class); 343 if (serviceObject != null) { 344 ICommandService service = (ICommandService)serviceObject; 345 return service; 346 } 347 } 348 return null; 349 } 350 351 private IHandlerService getHandlerService() { 352 IWorkbench wb = PlatformUI.getWorkbench(); 353 if (wb != null) { 354 Object serviceObject = wb.getAdapter(IHandlerService.class); 355 if (serviceObject != null) { 356 IHandlerService service = (IHandlerService)serviceObject; 357 return service; 358 } 359 } 360 return null; 361 } 362 363 371 private boolean showHelpTopic(String href, String embed, String embedTarget) { 372 return new ShowHelpURLHandler(this).showHelpTopic(href, embed, 373 embedTarget); 374 } 375 376 377 378 381 private boolean showHelp() { 382 PlatformUI.getWorkbench().getHelpSystem().displayHelp(); 383 return true; 384 } 385 386 389 private boolean openBrowser(String url, String pluginId) { 390 url = ModelUtil.resolveURL(url, pluginId); 394 return Util.openBrowser(url); 395 } 396 397 398 404 405 private boolean openURL(String url, String pluginId) { 406 IntroModelRoot model = IntroPlugin.getDefault().getIntroModelRoot(); 407 String presentationStyle = model.getPresentation() 408 .getImplementationKind(); 409 410 if (presentationStyle.equals(IntroPartPresentation.BROWSER_IMPL_KIND)) { 411 url = ModelUtil.resolveURL(url, pluginId); 413 BrowserIntroPartImplementation impl = (BrowserIntroPartImplementation) IntroPlugin 414 .getDefault().getIntroModelRoot().getPresentation() 415 .getIntroPartImplementation(); 416 Browser browser = impl.getBrowser(); 417 return browser.setUrl(url); 418 } 419 { 420 return openBrowser(url, pluginId); 422 } 423 } 424 425 426 private boolean showMessage(String message) { 427 if (message == null) 428 return false; 429 DialogUtil.displayInfoMessage(null, message); 430 return true; 431 } 432 433 438 boolean showPage(String pageId, String standbyState) { 439 CustomizableIntroPart currentIntroPart = (CustomizableIntroPart) IntroPlugin 445 .getIntro(); 446 currentIntroPart.getControl().setRedraw(false); 447 448 IntroModelRoot modelRoot = IntroPlugin.getDefault().getIntroModelRoot(); 449 boolean success = modelRoot.setCurrentPageId(pageId); 450 if (!success) 451 success = includePageToShow(modelRoot, pageId); 452 453 currentIntroPart.getControl().setRedraw(true); 455 456 if (success) { 457 modelRoot.getPresentation().updateHistory( 459 modelRoot.getCurrentPage()); 460 if (standbyState == null) 462 return true; 463 return setStandbyState(standbyState); 464 } 465 return false; 467 } 468 469 475 private boolean includePageToShow(IntroModelRoot model, String pageId) { 476 AbstractIntroPage page = findPageToShow(pageId); 477 if (page == null) { 478 Log.warning("Could not find an Intro page with id=" + pageId); return false; 480 } 481 page.getChildren(); 487 String currentPresentationKind = model.getPresentation() 489 .getImplementationKind(); 490 IntroPartPresentation targetPresentation = ((IntroModelRoot) page 493 .getParent()).getPresentation(); 494 String targetSharedStyle = targetPresentation 495 .getSharedStyle(currentPresentationKind); 496 AbstractIntroPage clonedPage = null; 498 try { 499 clonedPage = (AbstractIntroPage) page.clone(); 500 } catch (CloneNotSupportedException ex) { 501 Log.error("Failed to clone Intro model node.", ex); return false; 504 } 505 clonedPage.setParent(model); 507 if (targetSharedStyle != null) 510 clonedPage.insertStyle(targetSharedStyle, 0); 512 model.addChild(clonedPage); 513 return model.setCurrentPageId(clonedPage.getId()); 514 } 515 516 517 523 private AbstractIntroPage findPageToShow(String pageId) { 524 Hashtable models = ExtensionPointManager.getInst().getIntroModels(); 526 Enumeration values = models.elements(); 527 while (values.hasMoreElements()) { 528 IntroModelRoot model = (IntroModelRoot) values.nextElement(); 529 AbstractIntroPage page = (AbstractIntroPage) model.findChild( 530 pageId, AbstractIntroElement.ABSTRACT_PAGE); 531 if (page != null) 532 return page; 533 } 534 return null; 536 } 537 538 543 private boolean navigate(String direction) { 544 CustomizableIntroPart introPart = (CustomizableIntroPart) IntroPlugin 546 .getIntro(); 547 if (introPart == null) 548 return false; 550 551 IntroPartPresentation presentation = (IntroPartPresentation) introPart 552 .getAdapter(IntroPartPresentation.class); 553 554 if (direction.equalsIgnoreCase(VALUE_BACKWARD)) 555 return presentation.navigateBackward(); 556 else if (direction.equalsIgnoreCase(VALUE_FORWARD)) 557 return presentation.navigateForward(); 558 else if (direction.equalsIgnoreCase(VALUE_HOME)) 559 return presentation.navigateHome(); 560 return false; 561 } 562 563 564 567 public String getAction() { 568 return action; 569 } 570 571 579 public String getParameter(String parameterId) { 580 String value = parameters.getProperty(parameterId); 583 String decode = parameters.getProperty(KEY_DECODE); 584 585 if (value != null) 586 try { 587 if (decode!=null && decode.equalsIgnoreCase(VALUE_TRUE)) 588 return StringUtil.decode(value, "UTF-8"); return value; 593 } catch (Exception e) { 594 Log.error("Failed to decode URL: " + parameterId, e); } 597 return value; 598 } 599 600 private boolean handleCustomAction() { 601 IntroURLAction command = ExtensionPointManager.getInst() 602 .getSharedConfigExtensionsManager().getCommand(action); 603 if (command == null) { 604 String message = Messages.IntroURL_badCommand; 605 DialogUtil.displayInfoMessage(null, message, 606 new Object [] { action }); 607 return false; 608 } 609 610 StringBuffer url = new StringBuffer (); 612 url.append("http://org.eclipse.ui.intro/"); url.append(command.getReplaceValue().trim()); 614 if (command.getReplaceValue().indexOf("?") == -1) url.append("?"); else 618 url.append("&"); url.append(retrieveInitialQuery()); 621 IIntroURL introURL = IntroURLFactory.createIntroURL(url.toString()); 622 if (introURL != null) 623 return introURL.execute(); 624 return false; 625 } 626 627 628 633 private String retrieveInitialQuery() { 634 StringBuffer query = new StringBuffer (); 635 Enumeration keys = parameters.keys(); 636 while (keys.hasMoreElements()) { 637 String key = (String ) keys.nextElement(); 638 query.append(key); 639 query.append("="); query.append(parameters.get(key)); 641 if (keys.hasMoreElements()) 642 query.append("&"); } 644 return query.toString(); 645 } 646 647 648 private boolean switchToLaunchBar() { 649 IIntroPart intro = PlatformUI.getWorkbench().getIntroManager() 650 .getIntro(); 651 if (intro == null) 652 return false; 653 654 CustomizableIntroPart cpart = (CustomizableIntroPart) intro; 655 IntroModelRoot modelRoot = IntroPlugin.getDefault().getIntroModelRoot(); 656 String pageId = modelRoot.getCurrentPageId(); 657 IntroTheme theme = modelRoot.getTheme(); 658 Rectangle bounds = cpart.getControl().getBounds(); 659 Rectangle startBounds = Geometry.toDisplay(cpart.getControl() 660 .getParent(), bounds); 661 closeIntro(); 662 663 IWorkbenchWindow window = PlatformUI.getWorkbench() 664 .getActiveWorkbenchWindow(); 665 IntroLaunchBarElement launchBarElement = modelRoot.getPresentation() 666 .getLaunchBarElement(); 667 if (launchBarElement==null) 668 return true; 669 IntroLaunchBar launchBar = new IntroLaunchBar(launchBarElement 670 .getOrientation(), pageId, launchBarElement, theme); 671 launchBar.createInActiveWindow(); 672 Rectangle endBounds = Geometry.toDisplay(launchBar.getControl() 673 .getParent(), launchBar.getControl().getBounds()); 674 675 RectangleAnimation animation = new RectangleAnimation( 676 window.getShell(), startBounds, endBounds); 677 animation.schedule(); 678 return true; 679 } 680 } 681 | Popular Tags |