Skip to main content
summaryrefslogtreecommitdiffstats
blob: ae1975d50078ea5d8198c4f26dd3e7abf07b6c8a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*****************************************************************************
 * Copyright (c) 2016 CEA LIST and others.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Nicolas FAUVERGUE (ALL4TEC) nicolas.fauvergue@all4tec.net - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.infra.internationalization.common;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.papyrus.infra.core.log.LogHelper;
import org.eclipse.papyrus.infra.core.resource.PapyrusProjectScope;
import org.eclipse.papyrus.infra.ui.preferences.PapyrusScopedPreferenceStore;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.ui.preferences.ScopedPreferenceStore;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle.
 */
public class Activator extends AbstractUIPlugin {

	/**
	 * The plug-in ID.
	 */
	public static final String PLUGIN_ID = "org.eclipse.papyrus.infra.internationalization.common"; //$NON-NLS-1$
	
	/**
	 * The internationalization preference node label.
	 */
	public static final String INTERNATIONALIZATION_NODE_LABEL = "internationalization"; //$NON-NLS-1$

	/**
	 * The shared instance.
	 */
	private static Activator plugin;

	/**
	 * The log helper.
	 */
	public static LogHelper log;
	
	/**
	 * Storage for preferences. Use a list of preference store (one preference
	 * store by internationalization of a project).
	 */
	private List<IPreferenceStore> preferencesStore;

	/**
	 * The constructor
	 */
	public Activator() {
		preferencesStore = new ArrayList<IPreferenceStore>(0);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	@Override
	public void start(final BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
		log = new LogHelper(this);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	@Override
	public void stop(final BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}
	
	/**
	 * Get the preference store and create it if necessary.
	 * 
	 * @param project
	 *            The current project
	 * @param papyrusProjectName
	 *            The current papyrus project name.
	 * @return The preference store.
	 */
	public IPreferenceStore getInternationalizationPreferenceStore(final IProject project,
			final String papyrusProjectName) {
		IPreferenceStore result = null;
		final IScopeContext scope = new PapyrusProjectScope(project, papyrusProjectName);
		if (!preferencesStore.isEmpty()) {
			final IEclipsePreferences scopePreferenceNode = scope.getNode(INTERNATIONALIZATION_NODE_LABEL);
			final Iterator<IPreferenceStore> preferenceStoreIterator = preferencesStore.iterator();
			while (preferenceStoreIterator.hasNext() && null == result) {
				final IPreferenceStore preferenceStore = preferenceStoreIterator.next();
				if (preferenceStore instanceof PapyrusScopedPreferenceStore) {
					final IEclipsePreferences[] preferenceNodes = ((PapyrusScopedPreferenceStore) preferenceStore)
							.getPreferenceNodes(false);
					for (int index = 0; index < preferenceNodes.length && null == result; index++) {
						if (preferenceNodes[index].equals(scopePreferenceNode)) {
							result = preferenceStore;
						}
					}
				}
			}
		}

		if (null == result) {
			result = new PapyrusScopedPreferenceStore(scope, INTERNATIONALIZATION_NODE_LABEL);
			preferencesStore.add(result); // $NON-NLS-1$
		}

		return result;
	}

	/**
	 * Get the first preference store if existing, else a scoped preference
	 * store must be created and added to the list of preferences store.
	 * 
	 * @return The preference store.
	 */
	public IPreferenceStore getInternationalizationPreferenceStore() {
		// Create the preference store lazily.
		if (preferencesStore.isEmpty()) {
			preferencesStore.add(new ScopedPreferenceStore(InstanceScope.INSTANCE, getBundle().getSymbolicName()));
		}
		return preferencesStore.get(0);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}
}

Back to the top