1 11 12 package org.eclipse.ui.internal.intro.impl.model; 13 14 import java.io.*; 15 import java.net.*; 16 import java.util.*; 17 18 import org.eclipse.jface.action.*; 19 import org.eclipse.swt.custom.*; 20 import org.eclipse.swt.widgets.*; 21 import org.eclipse.ui.*; 22 import org.eclipse.ui.help.*; 23 import org.eclipse.ui.internal.intro.impl.*; 24 import org.eclipse.ui.internal.intro.impl.model.loader.*; 25 import org.eclipse.ui.internal.intro.impl.parts.*; 26 import org.eclipse.ui.internal.intro.impl.util.*; 27 import org.eclipse.ui.intro.*; 28 import org.eclipse.ui.intro.config.*; 29 30 35 public class IntroURL implements IIntroURL { 36 37 38 41 public static final String INTRO_PROTOCOL = "http"; public static final String INTRO_HOST_ID = "org.eclipse.ui.intro"; 44 47 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 RUN_ACTION = "runAction"; public static final String SHOW_PAGE = "showPage"; public static final String SHOW_MESSAGE = "showMessage"; public static final String NAVIGATE = "navigate"; 58 61 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"; 71 72 public static final String VALUE_BACKWARD = "backward"; public static final String VALUE_FORWARD = "forward"; public static final String VALUE_HOME = "home"; 76 private String action = null; 77 private Properties parameters = null; 78 79 85 IntroURL(String action, Properties parameters) { 86 this.action = action; 87 this.parameters = parameters; 88 } 89 90 94 public boolean execute() { 95 final boolean[] result = new boolean[1]; 96 Display display = Display.getCurrent(); 97 BusyIndicator.showWhile(display, new Runnable () { 98 99 public void run() { 100 result[0] = doExecute(); 101 } 102 }); 103 return result[0]; 104 } 105 106 private boolean doExecute() { 107 108 if (action.equals(CLOSE)) 110 return closeIntro(); 111 112 else if (action.equals(SET_STANDBY_MODE)) 113 return setStandbyState(getParameter(KEY_STANDBY)); 116 117 else if (action.equals(SHOW_STANDBY)) 118 return handleStandbyState(getParameter(KEY_PART_ID), 119 getParameter(KEY_INPUT)); 120 121 else if (action.equals(SHOW_HELP)) 122 return showHelp(); 124 125 else if (action.equals(SHOW_HELP_TOPIC)) 126 return showHelpTopic(getParameter(KEY_ID)); 128 129 else if (action.equals(OPEN_BROWSER)) 130 return openBrowser(getParameter(KEY_URL), 132 getParameter(KEY_PLUGIN_ID)); 133 134 else if (action.equals(RUN_ACTION)) 135 return runAction(getParameter(KEY_PLUGIN_ID), 138 getParameter(KEY_CLASS), parameters, 139 getParameter(KEY_STANDBY)); 140 141 else if (action.equals(SHOW_PAGE)) 142 return showPage(getParameter(KEY_ID), getParameter(KEY_STANDBY)); 144 145 else if (action.equals(SHOW_MESSAGE)) 146 return showMessage(getParameter(KEY_MESSAGE)); 147 148 else if (action.equals(NAVIGATE)) 149 return navigate(getParameter(KEY_DIRECTION)); 150 151 else 152 return handleCustomAction(); 153 } 154 155 156 private boolean closeIntro() { 157 return IntroPlugin.closeIntro(); 159 } 160 161 168 private boolean handleStandbyState(String partId, String input) { 169 CustomizableIntroPart introPart = (CustomizableIntroPart) IntroPlugin 171 .getIntro(); 172 if (introPart == null) 173 introPart = (CustomizableIntroPart) IntroPlugin.showIntro(true); 174 introPart.getControl().setData(IIntroConstants.SHOW_STANDBY_PART, 176 "true"); IntroPlugin.setIntroStandby(true); 178 StandbyPart standbyPart = (StandbyPart) introPart 179 .getAdapter(StandbyPart.class); 180 181 boolean success = standbyPart.showContentPart(partId, input); 182 if (success) 183 return true; 184 185 standbyPart.setTopControl(IIntroConstants.EMPTY_STANDBY_CONTENT_PART); 188 return false; 189 } 190 191 196 private boolean setStandbyState(String state) { 197 if (state == null) 198 return false; 199 boolean standby = state.equals("true") ? true : false; IIntroPart introPart = IntroPlugin.showIntro(standby); 201 if (introPart == null) 202 return false; 203 else 204 return true; 205 } 206 207 208 211 private boolean runAction(String pluginId, String className, 212 Properties parameters, String standbyState) { 213 214 Object actionObject = ModelLoaderUtil.createClassInstance(pluginId, 215 className); 216 try { 217 if (actionObject instanceof IIntroAction) { 218 IIntroAction introAction = (IIntroAction) actionObject; 219 IIntroSite site = IntroPlugin.getDefault().getIntroModelRoot() 220 .getPresentation().getIntroPart().getIntroSite(); 221 introAction.run(site, parameters); 222 } else if (actionObject instanceof IAction) { 223 IAction action = (IAction) actionObject; 224 action.run(); 225 226 } else if (actionObject instanceof IActionDelegate) { 227 final IActionDelegate delegate = (IActionDelegate) actionObject; 228 if (delegate instanceof IWorkbenchWindowActionDelegate) 229 ((IWorkbenchWindowActionDelegate) delegate).init(PlatformUI 230 .getWorkbench().getActiveWorkbenchWindow()); 231 Action proxy = new Action(this.action) { 232 233 public void run() { 234 delegate.run(this); 235 } 236 }; 237 proxy.run(); 238 } else 239 return false; 241 if (standbyState == null) 243 return true; 244 else 245 return setStandbyState(standbyState); 246 } catch (Exception e) { 247 Log.error("Could not run action: " + className, e); return false; 249 } 250 } 251 252 255 private boolean showHelpTopic(String href) { 256 WorkbenchHelp.displayHelpResource(href); 258 return true; 259 } 260 261 264 private boolean showHelp() { 265 WorkbenchHelp.displayHelp(); 266 return true; 267 } 268 269 272 private boolean openBrowser(String url, String pluginId) { 273 url = IntroModelRoot.resolveURL(url, pluginId); 277 return Util.openBrowser(url); 278 } 279 280 private boolean showMessage(String message) { 281 if (message == null) 282 return false; 283 else { 284 try { 285 message = URLDecoder.decode(message, "UTF-8"); DialogUtil.displayInfoMessage(null, message); 287 return true; 288 } catch (UnsupportedEncodingException e) { 289 DialogUtil.displayInfoMessage(null, "IntroURL.failedToDecode", new Object [] { message }); 291 return false; 292 } 293 } 294 } 295 296 302 private boolean showPage(String pageId, String standbyState) { 303 CustomizableIntroPart currentIntroPart = (CustomizableIntroPart) IntroPlugin 309 .getIntro(); 310 currentIntroPart.getControl().setRedraw(false); 311 312 IntroModelRoot modelRoot = IntroPlugin.getDefault().getIntroModelRoot(); 313 boolean success = modelRoot.setCurrentPageId(pageId); 314 if (!success) 315 success = includePageToShow(modelRoot, pageId); 316 317 currentIntroPart.getControl().setRedraw(true); 319 320 if (success) { 321 modelRoot.getPresentation().updateHistory(pageId); 323 if (standbyState == null) 325 return true; 326 else 327 return setStandbyState(standbyState); 328 } else 329 return false; 331 } 332 333 339 private boolean includePageToShow(IntroModelRoot model, String pageId) { 340 AbstractIntroPage page = findPageToShow(pageId); 341 if (page == null) { 342 Log.error("Failed to clone Intro page.", null); return false; 344 } 345 page.getChildren(); 351 String currentPresentationKind = model.getPresentation() 353 .getImplementationKind(); 354 IntroPartPresentation targetPresentation = ((IntroModelRoot) page 357 .getParent()).getPresentation(); 358 String targetSharedStyle = targetPresentation 359 .getSharedStyle(currentPresentationKind); 360 AbstractIntroPage clonedPage = null; 362 try { 363 clonedPage = (AbstractIntroPage) page.clone(); 364 } catch (CloneNotSupportedException ex) { 365 Log.error("Failed to clone Intro model node.", ex); return false; 368 } 369 clonedPage.setParent(model); 371 if (targetSharedStyle != null) 374 clonedPage.insertStyle(targetSharedStyle, 0); 376 model.children.add(clonedPage); 377 return model.setCurrentPageId(clonedPage.getId()); 378 } 379 380 381 387 private AbstractIntroPage findPageToShow(String pageId) { 388 Hashtable models = ExtensionPointManager.getInst().getIntroModels(); 390 Enumeration values = models.elements(); 391 while (values.hasMoreElements()) { 392 IntroModelRoot model = (IntroModelRoot) values.nextElement(); 393 AbstractIntroPage page = (AbstractIntroPage) model.findChild( 394 pageId, AbstractIntroElement.ABSTRACT_PAGE); 395 if (page != null) 396 return page; 397 } 398 return null; 400 } 401 402 407 private boolean navigate(String direction) { 408 CustomizableIntroPart introPart = (CustomizableIntroPart) IntroPlugin 410 .getIntro(); 411 if (introPart == null) 412 return false; 414 415 IntroPartPresentation presentation = (IntroPartPresentation) introPart 416 .getAdapter(IntroPartPresentation.class); 417 418 if (direction.equalsIgnoreCase(VALUE_BACKWARD)) 419 return presentation.navigateBackward(); 420 else if (direction.equalsIgnoreCase(VALUE_FORWARD)) 421 return presentation.navigateForward(); 422 else if (direction.equalsIgnoreCase(VALUE_HOME)) 423 return presentation.navigateHome(); 424 return false; 425 } 426 427 428 431 public String getAction() { 432 return action; 433 } 434 435 442 public String getParameter(String parameterId) { 443 return parameters.getProperty(parameterId); 444 } 445 446 private boolean handleCustomAction() { 447 IntroURLAction command = ExtensionPointManager.getInst() 448 .getSharedConfigExtensionsManager().getCommand(action); 449 if (command == null) { 450 DialogUtil.displayInfoMessage(null, "IntroURL.badCommand", new Object [] { action }); 452 return false; 453 } 454 455 StringBuffer url = new StringBuffer (); 457 url.append("http://org.eclipse.ui.intro/"); url.append(command.getReplaceValue().trim()); 459 if (command.getReplaceValue().indexOf("?") == -1) url.append("?"); else 463 url.append("&"); url.append(retrieveInitialQuery()); 466 IIntroURL introURL = IntroURLFactory.createIntroURL(url.toString()); 467 if (introURL != null) 468 return introURL.execute(); 469 else 470 return false; 471 } 472 473 474 479 private String retrieveInitialQuery() { 480 StringBuffer query = new StringBuffer (); 481 Enumeration keys = parameters.keys(); 482 while (keys.hasMoreElements()) { 483 String key = (String ) keys.nextElement(); 484 query.append(key); 485 query.append("="); query.append(parameters.get(key)); 487 if (keys.hasMoreElements()) 488 query.append("&"); } 490 return query.toString(); 491 } 492 493 } 494 495 | Popular Tags |