1 11 package org.eclipse.ui.plugin; 12 13 import java.io.BufferedReader ; 14 import java.io.File ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.io.InputStreamReader ; 18 import java.net.MalformedURLException ; 19 import java.net.URL ; 20 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IPath; 23 import org.eclipse.core.runtime.IPluginDescriptor; 24 import org.eclipse.core.runtime.Platform; 25 import org.eclipse.core.runtime.Plugin; 26 import org.eclipse.core.runtime.preferences.InstanceScope; 27 import org.eclipse.jface.dialogs.DialogSettings; 28 import org.eclipse.jface.dialogs.IDialogSettings; 29 import org.eclipse.jface.preference.IPreferenceStore; 30 import org.eclipse.jface.resource.ImageDescriptor; 31 import org.eclipse.jface.resource.ImageRegistry; 32 import org.eclipse.swt.SWT; 33 import org.eclipse.swt.SWTError; 34 import org.eclipse.swt.widgets.Display; 35 import org.eclipse.ui.IWorkbench; 36 import org.eclipse.ui.PlatformUI; 37 import org.eclipse.ui.internal.WWinPluginAction; 38 import org.eclipse.ui.internal.util.BundleUtility; 39 import org.eclipse.ui.preferences.ScopedPreferenceStore; 40 import org.osgi.framework.Bundle; 41 import org.osgi.framework.BundleContext; 42 import org.osgi.framework.BundleEvent; 43 import org.osgi.framework.BundleListener; 44 45 116 public abstract class AbstractUIPlugin extends Plugin { 117 118 122 private static final String FN_DIALOG_SETTINGS = "dialog_settings.xml"; 124 128 private DialogSettings dialogSettings = null; 129 130 133 private ScopedPreferenceStore preferenceStore; 134 135 139 private ImageRegistry imageRegistry = null; 140 141 146 private BundleListener bundleListener; 147 148 169 public AbstractUIPlugin(IPluginDescriptor descriptor) { 170 super(descriptor); 171 } 172 173 185 public AbstractUIPlugin() { 186 super(); 187 } 188 189 200 protected ImageRegistry createImageRegistry() { 201 202 if(Display.getCurrent() != null) { 204 return new ImageRegistry(Display.getCurrent()); 205 } 206 207 if(PlatformUI.isWorkbenchRunning()) { 208 return new ImageRegistry(PlatformUI.getWorkbench().getDisplay()); 209 } 210 211 throw new SWTError(SWT.ERROR_THREAD_INVALID_ACCESS); 214 } 215 216 230 public IDialogSettings getDialogSettings() { 231 if (dialogSettings == null) { 232 loadDialogSettings(); 233 } 234 return dialogSettings; 235 } 236 237 255 public ImageRegistry getImageRegistry() { 256 if (imageRegistry == null) { 257 imageRegistry = createImageRegistry(); 258 initializeImageRegistry(imageRegistry); 259 } 260 return imageRegistry; 261 } 262 263 281 public IPreferenceStore getPreferenceStore() { 282 if (preferenceStore == null) { 284 preferenceStore = new ScopedPreferenceStore(new InstanceScope(),getBundle().getSymbolicName()); 285 286 } 287 return preferenceStore; 288 } 289 290 298 public IWorkbench getWorkbench() { 299 return PlatformUI.getWorkbench(); 300 } 301 302 325 protected void initializeDefaultPreferences(IPreferenceStore store) { 326 } 328 329 350 protected void initializeDefaultPluginPreferences() { 351 354 loadPreferenceStore(); 356 initializeDefaultPreferences(getPreferenceStore()); 359 } 360 361 382 protected void initializeImageRegistry(ImageRegistry reg) { 383 } 385 386 398 protected void loadDialogSettings() { 399 dialogSettings = new DialogSettings("Workbench"); 401 IPath dataLocation = getStateLocationOrNull(); 404 if (dataLocation != null) { 405 String readWritePath = dataLocation.append(FN_DIALOG_SETTINGS) 407 .toOSString(); 408 File settingsFile = new File (readWritePath); 409 if (settingsFile.exists()) { 410 try { 411 dialogSettings.load(readWritePath); 412 } catch (IOException e) { 413 dialogSettings = new DialogSettings("Workbench"); } 416 417 return; 418 } 419 } 420 421 URL dsURL = BundleUtility.find(getBundle(), FN_DIALOG_SETTINGS); 423 if (dsURL == null) { 424 return; 425 } 426 427 InputStream is = null; 428 try { 429 is = dsURL.openStream(); 430 BufferedReader reader = new BufferedReader ( 431 new InputStreamReader (is, "utf-8")); dialogSettings.load(reader); 433 } catch (IOException e) { 434 dialogSettings = new DialogSettings("Workbench"); } finally { 437 try { 438 if (is != null) { 439 is.close(); 440 } 441 } catch (IOException e) { 442 } 444 } 445 } 446 447 463 protected void loadPreferenceStore() { 464 } 466 467 475 protected void refreshPluginActions() { 476 if (!PlatformUI.isWorkbenchRunning()) { 478 return; 479 } 480 481 Display.getDefault().asyncExec(new Runnable () { 485 public void run() { 486 WWinPluginAction.refreshActionList(); 487 } 488 }); 489 } 490 491 495 protected void saveDialogSettings() { 496 if (dialogSettings == null) { 497 return; 498 } 499 500 try { 501 IPath path = getStateLocationOrNull(); 502 if(path == null) { 503 return; 504 } 505 String readWritePath = path 506 .append(FN_DIALOG_SETTINGS).toOSString(); 507 dialogSettings.save(readWritePath); 508 } catch (IOException e) { 509 } catch (IllegalStateException e) { 511 } 513 } 514 515 525 protected void savePreferenceStore() { 526 savePluginPreferences(); 527 } 528 529 547 public void startup() throws CoreException { 548 super.startup(); 551 } 552 553 568 public void shutdown() throws CoreException { 569 super.shutdown(); 574 } 575 576 584 public void start(BundleContext context) throws Exception { 585 super.start(context); 586 final BundleContext fc = context; 587 bundleListener = new BundleListener() { 595 public void bundleChanged(BundleEvent event) { 596 if (event.getBundle() == getBundle()) { 597 if (event.getType() == BundleEvent.STARTED) { 598 if (getBundle().getState() == Bundle.ACTIVE) { 602 refreshPluginActions(); 603 } 604 fc.removeBundleListener(this); 605 } 606 } 607 } 608 }; 609 context.addBundleListener(bundleListener); 610 } 612 613 624 public void stop(BundleContext context) throws Exception { 625 try { 626 if (bundleListener != null) { 627 context.removeBundleListener(bundleListener); 628 } 629 saveDialogSettings(); 630 savePreferenceStore(); 631 preferenceStore = null; 632 imageRegistry = null; 633 } finally { 634 super.stop(context); 635 } 636 } 637 638 659 public static ImageDescriptor imageDescriptorFromPlugin(String pluginId, 660 String imageFilePath) { 661 if (pluginId == null || imageFilePath == null) { 662 throw new IllegalArgumentException (); 663 } 664 665 Bundle bundle = Platform.getBundle(pluginId); 667 if (!BundleUtility.isReady(bundle)) { 668 return null; 669 } 670 671 URL fullPathString = BundleUtility.find(bundle, imageFilePath); 673 if (fullPathString == null) { 674 try { 675 fullPathString = new URL (imageFilePath); 676 } catch (MalformedURLException e) { 677 return null; 678 } 679 } 680 681 if (fullPathString == null) { 682 return null; 683 } 684 return ImageDescriptor.createFromURL(fullPathString); 685 } 686 687 700 private IPath getStateLocationOrNull() { 701 try { 702 return getStateLocation(); 703 } catch (IllegalStateException e) { 704 return null; 707 } 708 } 709 710 } 711 | Popular Tags |