|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2012 MEDEVIT and FHV and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* Marco Descher <marco@descher.at> - initial API and implementation |
| 10 |
******************************************************************************/ |
| 11 |
package org.eclipse.e4.tools.emf.ui.internal; |
| 12 |
|
| 13 |
import java.io.File; |
| 14 |
import java.io.IOException; |
| 15 |
import java.net.URISyntaxException; |
| 16 |
import org.eclipse.core.runtime.URIUtil; |
| 17 |
import org.eclipse.jface.dialogs.DialogSettings; |
| 18 |
import org.eclipse.jface.dialogs.IDialogSettings; |
| 19 |
import org.eclipse.osgi.service.datalocation.Location; |
| 20 |
import org.osgi.framework.BundleActivator; |
| 21 |
import org.osgi.framework.BundleContext; |
| 22 |
import org.osgi.framework.Filter; |
| 23 |
import org.osgi.framework.InvalidSyntaxException; |
| 24 |
import org.osgi.util.tracker.ServiceTracker; |
| 25 |
|
| 26 |
public class ToolsEmfUiPlugin implements BundleActivator { |
| 27 |
// The plug-in ID |
| 28 |
public static final String PLUGIN_ID = "org.eclipse.e4.tools.emf.ui"; //$NON-NLS-1$ |
| 29 |
|
| 30 |
private static ToolsEmfUiPlugin plugin; |
| 31 |
private BundleContext context; |
| 32 |
|
| 33 |
private ServiceTracker locationTracker; |
| 34 |
|
| 35 |
public void start(BundleContext context) throws Exception { |
| 36 |
plugin = this; |
| 37 |
this.context = context; |
| 38 |
} |
| 39 |
|
| 40 |
public void stop(BundleContext context) throws Exception { |
| 41 |
saveDialogSettings(); |
| 42 |
plugin = null; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Returns the shared instance |
| 47 |
* |
| 48 |
* @return the shared instance |
| 49 |
*/ |
| 50 |
public static ToolsEmfUiPlugin getDefault() { |
| 51 |
return plugin; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @return the instance Location service |
| 56 |
*/ |
| 57 |
private Location getInstanceLocation() { |
| 58 |
if (locationTracker == null) { |
| 59 |
Filter filter = null; |
| 60 |
try { |
| 61 |
filter = context.createFilter(Location.INSTANCE_FILTER); |
| 62 |
} catch (InvalidSyntaxException e) { |
| 63 |
// ignore this. It should never happen as we have tested the |
| 64 |
// above format. |
| 65 |
} |
| 66 |
locationTracker = new ServiceTracker(context, filter, null); |
| 67 |
locationTracker.open(); |
| 68 |
} |
| 69 |
return (Location) locationTracker.getService(); |
| 70 |
} |
| 71 |
|
| 72 |
private void loadDialogSettings() { |
| 73 |
dialogSettings = new DialogSettings(PLUGIN_ID); |
| 74 |
|
| 75 |
File dialogSettingsFile = getDialogSettingsFile(); |
| 76 |
|
| 77 |
if (dialogSettingsFile.exists()) { |
| 78 |
try { |
| 79 |
dialogSettings.load(dialogSettingsFile.getAbsolutePath()); |
| 80 |
} catch (IOException e) { |
| 81 |
// load failed spec'ed to ignore problems |
| 82 |
} |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
private void saveDialogSettings() { |
| 89 |
if (dialogSettings == null) { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
File dialogSettingsFile = getDialogSettingsFile(); |
| 94 |
|
| 95 |
try { |
| 96 |
dialogSettings.save(dialogSettingsFile.getAbsolutePath()); |
| 97 |
} catch (IOException e) { |
| 98 |
e.printStackTrace(); |
| 99 |
// spec'ed to ignore problems |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
private File getDialogSettingsFile() { |
| 104 |
File baseLocation; |
| 105 |
try { |
| 106 |
baseLocation = new File(URIUtil.toURI(getInstanceLocation().getURL())); |
| 107 |
} catch (URISyntaxException e) { |
| 108 |
throw new RuntimeException(e); |
| 109 |
} |
| 110 |
baseLocation = new File(baseLocation, ".metadata"); //$NON-NLS-1$ |
| 111 |
baseLocation = new File(baseLocation, ".plugins"); //$NON-NLS-1$ |
| 112 |
baseLocation = new File(baseLocation, PLUGIN_ID); |
| 113 |
File dialogSettingsFile = new File(baseLocation, FN_DIALOG_SETTINGS); |
| 114 |
if (!dialogSettingsFile.exists()) |
| 115 |
dialogSettingsFile.getParentFile().mkdirs(); |
| 116 |
return dialogSettingsFile; |
| 117 |
} |
| 118 |
|
| 119 |
// ////////////////////////////////////////////////////////////////////// |
| 120 |
// The following code was copied from AbstractUIPlugin class. |
| 121 |
/** |
| 122 |
* The name of the dialog settings file (value |
| 123 |
* <code>"dialog_settings.xml"</code>). |
| 124 |
*/ |
| 125 |
private static final String FN_DIALOG_SETTINGS = "dialog_settings.xml"; //$NON-NLS-1$ |
| 126 |
/** |
| 127 |
* Storage for dialog and wizard data; <code>null</code> if not yet |
| 128 |
* initialized. |
| 129 |
*/ |
| 130 |
private IDialogSettings dialogSettings = null; |
| 131 |
|
| 132 |
/** |
| 133 |
* @see AbstractUiPlugin#getDialogSettings() |
| 134 |
* @return the dialog settings |
| 135 |
*/ |
| 136 |
public IDialogSettings getDialogSettings() { |
| 137 |
if (dialogSettings == null) { |
| 138 |
loadDialogSettings(); |
| 139 |
} |
| 140 |
|
| 141 |
return dialogSettings; |
| 142 |
} |
| 143 |
|
| 144 |
} |