Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 76d7a91d172a27844bb15d19c747bce033373c6d (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*******************************************************************************
 * Copyright (c) 2001, 2006 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.xsd.ui.internal.editor;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.plugin.*;
import org.eclipse.wst.xsd.ui.internal.adt.editor.ProductCustomizationProvider;
import org.eclipse.wst.xsd.ui.internal.common.properties.sections.appinfo.ExtensionsSchemasRegistry;
import org.eclipse.wst.xsd.ui.internal.common.properties.sections.appinfo.custom.NodeCustomizationRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

import java.net.MalformedURLException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.*;

public class XSDEditorPlugin extends AbstractUIPlugin
{
  public static final String PLUGIN_ID = "org.eclipse.wst.xsd.ui"; //$NON-NLS-1$
  public static final String EDITOR_ID = "org.eclipse.wst.xsd.ui.internal.editor.InternalXSDMultiPageEditor";  //$NON-NLS-1$
  public static final String CONST_XSD_DEFAULT_PREFIX_TEXT = "org.eclipse.wst.xmlschema.xsdDefaultPrefixText"; //$NON-NLS-1$
  public static final String CONST_PREFERED_BUILT_IN_TYPES = "org.eclipse.wst.xmlschema.preferedBuiltInTypes";  //$NON-NLS-1$
  public static final String CUSTOM_LIST_SEPARATOR = "\n"; //$NON-NLS-1$
  public static final String EXTENSIONS_SCHEMAS_EXTENSIONID = "org.eclipse.wst.xsd.ui.extensionCategories"; //$NON-NLS-1$
  private static final String DEPRECATED_EXTENSIONS_SCHEMAS_EXTENSIONID = "org.eclipse.wst.xsd.ui.ExtensionsSchemasDescription"; //$NON-NLS-1$  
  public final static String DEFAULT_TARGET_NAMESPACE = "http://www.example.org"; //$NON-NLS-1$
  
  
	//The shared instance.
	private static XSDEditorPlugin plugin;
	//Resource bundle.
	private ResourceBundle resourceBundle;
  private ExtensionsSchemasRegistry registry;  
  private NodeCustomizationRegistry nodeCustomizationRegistry;
  private XSDEditorConfiguration xsdEditorConfiguration = null;
  
  public static final String CONST_USE_SIMPLE_EDIT_MODE = PLUGIN_ID + ".useSimpleEditMode"; //$NON-NLS-1$
  public static final String CONST_SHOW_INHERITED_CONTENT = PLUGIN_ID + ".showInheritedContent"; //$NON-NLS-1$

  public static final String CONST_XSD_LANGUAGE_QUALIFY = "org.eclipse.wst.xmlschema.xsdQualify"; //$NON-NLS-1$
  public static final String CONST_DEFAULT_TARGET_NAMESPACE = "org.eclipse.wst.xmlschema.defaultTargetnamespaceText"; //$NON-NLS-1$
  public static final String CONST_SHOW_EXTERNALS = PLUGIN_ID + ".showExternals"; //$NON-NLS-1$
  
  public static String DEFAULT_PAGE = "org.eclipse.wst.xsd.ui.internal.defaultPage";
  public static String DESIGN_PAGE = "org.eclipse.wst.xsd.ui.internal.designPage";
  public static String SOURCE_PAGE = "org.eclipse.wst.xsd.ui.internal.sourcePage";

	/**
	 * The constructor.
	 */
	public XSDEditorPlugin() {
		super();
		plugin = this;
	}

	/**
	 * This method is called upon plug-in activation
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
	}

	/**
	 * This method is called when the plug-in is stopped
	 */
	public void stop(BundleContext context) throws Exception {
		super.stop(context);
		plugin = null;
		resourceBundle = null;
	}

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

	/**
	 * Returns the string from the plugin's resource bundle,
	 * or 'key' if not found.
	 */
	public static String getResourceString(String key) {
		ResourceBundle bundle = XSDEditorPlugin.getDefault().getResourceBundle();
		try {
			return (bundle != null) ? bundle.getString(key) : key;
		} catch (MissingResourceException e) {
			return key;
		}
	}
  
  public static String getResourceString(String key, Object o)
  {
      return getResourceString(key, new Object[] { o});
  }

  public static String getResourceString(String key, Object[] objects)
  {
      return MessageFormat.format(getResourceString(key), objects);
  }
	
  public static String getResourceString(String key, Object o1, Object o2)
  {
      return getResourceString(key, new Object[] { o1, o2});
  }

	/**
	 * Returns the plugin's resource bundle,
	 */
	public ResourceBundle getResourceBundle() {
		try {
			if (resourceBundle == null)
				// resourceBundle = ResourceBundle.getBundle("org.eclipse.wst.xsd.ui.internal.editor.EditorPluginResources");
        resourceBundle = Platform.getResourceBundle(getBundle());
		} catch (MissingResourceException x) {
			resourceBundle = null;
		}
		return resourceBundle;
	}

	/**
	 * Returns an image descriptor for the image file at the given
	 * plug-in relative path.
	 *
	 * @param path the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return AbstractUIPlugin.imageDescriptorFromPlugin("org.eclipse.wst.xsd.ui", path); //$NON-NLS-1$
	}
	
	public static ImageDescriptor getImageDescriptor(String name, boolean getBaseURL) {
		try {
			URL installURL = getDefault().getBundle().getEntry("/"); //$NON-NLS-1$
			String imageString = getBaseURL ? "icons/" + name : name; //$NON-NLS-1$

			URL imageURL = new URL(installURL, imageString);
			return ImageDescriptor.createFromURL(imageURL);
		} catch (MalformedURLException e) {
			return null;
		}
	}

	public Image getIcon(String name)
	{
	  try {
		ImageRegistry imageRegistry = getImageRegistry();

		if (imageRegistry.get(name) != null) {
			return imageRegistry.get(name);
		}
		else {
			URL installURL = getDefault().getBundle().getEntry("/"); //$NON-NLS-1$
			String imageString = "icons/" + name; //$NON-NLS-1$

			URL imageURL = new URL(installURL, imageString);
			imageRegistry.put(name, ImageDescriptor.createFromURL(imageURL));
			return imageRegistry.get(name);
		}

	  } catch (Exception e) {
		return null;
	  }
	}
	
	public static XSDEditorPlugin getPlugin() {
		return plugin;
	}

	public static String getXSDString(String key) {
		return getResourceString(key);
	}

  /**
   * This gets the string resource and does one substitution.
   */
  public String getString(String key, Object s1) {
    return MessageFormat.format(Platform.getResourceBundle(getBundle()).getString(key), new Object[]{s1});
  }
  
	public static Image getXSDImage(String iconName) {
		return getDefault().getImage(iconName);
	}

	public Image getImage(String iconName) {
		ImageRegistry imageRegistry = getImageRegistry();

		if (imageRegistry.get(iconName) != null) {
			return imageRegistry.get(iconName);
		}
		else {
			imageRegistry.put(iconName, ImageDescriptor.createFromFile(getClass(), iconName));
			return imageRegistry.get(iconName);
		}
	}
	
	public URL getBaseURL() {
		return getDescriptor().getInstallURL();
	}

	public Image getIconImage(String object) {
		try {
			return ExtendedImageRegistry.getInstance().getImage(new URL(getBaseURL() + "icons/" + object + ".gif")); //$NON-NLS-1$ //$NON-NLS-2$
		}
		catch (MalformedURLException exception) {

		}
		return null;
	}

  public boolean getShowInheritedContent()
  {
    return getPreferenceStore().getBoolean(CONST_SHOW_INHERITED_CONTENT);
  }
  
  protected void initializeDefaultPreferences(IPreferenceStore store)
  {
    store.setDefault(CONST_SHOW_INHERITED_CONTENT, false);
    store.setDefault(CONST_XSD_DEFAULT_PREFIX_TEXT, "xsd"); //$NON-NLS-1$
    store.setDefault(CONST_XSD_LANGUAGE_QUALIFY, false);
    store.setDefault(DEFAULT_PAGE, DESIGN_PAGE);
    store.setDefault(CONST_DEFAULT_TARGET_NAMESPACE, DEFAULT_TARGET_NAMESPACE);
    store.setDefault(CONST_SHOW_EXTERNALS, false);
    
    //Even the last item in the list must contain a trailing List separator
    store.setDefault(CONST_PREFERED_BUILT_IN_TYPES,     		
    		"boolean"+ CUSTOM_LIST_SEPARATOR + //$NON-NLS-1$
    		"date" + CUSTOM_LIST_SEPARATOR + //$NON-NLS-1$
    		"dateTime" + CUSTOM_LIST_SEPARATOR + //$NON-NLS-1$
    		"double" + CUSTOM_LIST_SEPARATOR + //$NON-NLS-1$
    		"float" + CUSTOM_LIST_SEPARATOR + //$NON-NLS-1$
    		"hexBinary" + CUSTOM_LIST_SEPARATOR + //$NON-NLS-1$
    		"int" + CUSTOM_LIST_SEPARATOR + //$NON-NLS-1$
    		"string" + CUSTOM_LIST_SEPARATOR + //$NON-NLS-1$
    		"time" + CUSTOM_LIST_SEPARATOR); //$NON-NLS-1$
  }

  public ExtensionsSchemasRegistry getExtensionsSchemasRegistry()
  {
    if (registry == null)
    {
      registry = new ExtensionsSchemasRegistry(EXTENSIONS_SCHEMAS_EXTENSIONID);
      registry.__internalSetDeprecatedExtensionId(DEPRECATED_EXTENSIONS_SCHEMAS_EXTENSIONID);
      registry.setPrefStore(XSDEditorPlugin.getPlugin().getPreferenceStore() );
    }
    return registry;
  }

 
  public XSDEditorConfiguration getXSDEditorConfiguration()
  {
    if (xsdEditorConfiguration == null)
    {
      xsdEditorConfiguration = new XSDEditorConfiguration();
    }
    return xsdEditorConfiguration;
  }

  /**
   * Get the xml schema default namespace prefix
   */
  public String getXMLSchemaPrefix()
  {
    return getPreferenceStore().getString(CONST_XSD_DEFAULT_PREFIX_TEXT);
  }

  public boolean getShowExternals()
  {
    return getPreferenceStore().getBoolean(CONST_SHOW_EXTERNALS);
  }
  
  public void setShowExternals(boolean doShow)
  {
    getPreferenceStore().setValue(CONST_SHOW_EXTERNALS, doShow);
  }

  /**
   * Get the xml schema default target namespace
   */
  public String getXMLSchemaTargetNamespace() {
    String targetNamespace = getPreferenceStore().getString(CONST_DEFAULT_TARGET_NAMESPACE);
    if (!targetNamespace.endsWith("/")) { //$NON-NLS-1$
      targetNamespace = targetNamespace + "/"; //$NON-NLS-1$
    }
    return targetNamespace;
  }

  /**
   * Get the xml schema language qualification
   */
  public boolean isQualifyXMLSchemaLanguage() {
    return getPreferenceStore().getBoolean(CONST_XSD_LANGUAGE_QUALIFY);
  }

  public static Shell getShell() {
    return getPlugin().getWorkbench().getActiveWorkbenchWindow().getShell();
  }
  
  public void setSourcePageAsDefault()
  {
    getPreferenceStore().setValue(DEFAULT_PAGE, SOURCE_PAGE);
  }

  public void setDesignPageAsDefault()
  {
    getPreferenceStore().setValue(DEFAULT_PAGE, DESIGN_PAGE);
  }
  
  /**
   * Method getDefaultPage.
   * 
   * @return String value of the string constant that is the default page
   *         the editor should turn to when first opened. Changes to the
   *         last visible page when the editor was closed
   */
  public String getDefaultPage() {
    return getPreferenceStore().getString(DEFAULT_PAGE);
  }

  public NodeCustomizationRegistry getNodeCustomizationRegistry()
  {
    if (nodeCustomizationRegistry == null)
    {  
      nodeCustomizationRegistry = new NodeCustomizationRegistry("foo");
    }
    return nodeCustomizationRegistry;
  }
  
  private static final String PRODUCT_CUSTOMIZATION_PROVIDER_PLUGIN_ID = "org.eclipse.wst.xsd.ui.productCustomizationProviderPluginId"; //$NON-NLS-1$
  private static final String PRODUCT_CUSTOMIZATION_PROVIDER_CLASS_NAME = "org.eclipse.wst.xsd.ui.productCustomizationProviderClassName"; //$NON-NLS-1$
  
  private static ProductCustomizationProvider productCustomizationProvider;
  private static boolean productCustomizationProviderInitialized = false;
  
  public ProductCustomizationProvider getProductCustomizationProvider()
  {
    if (!productCustomizationProviderInitialized)
    {
      productCustomizationProviderInitialized = true;
      String pluginName = getPreferenceStore().getString(PRODUCT_CUSTOMIZATION_PROVIDER_PLUGIN_ID);
      String className = getPreferenceStore().getString(PRODUCT_CUSTOMIZATION_PROVIDER_CLASS_NAME);
      if (pluginName != null && pluginName.length() > 0 &&
          className != null && className.length() > 0)
      {
        try
        {
          Bundle bundle = Platform.getBundle(pluginName);
          Class clazz = bundle.loadClass(className);
          productCustomizationProvider = (ProductCustomizationProvider)clazz.newInstance();
        }          
        catch (Exception e)
        {          
        }
      }
    }  
    return productCustomizationProvider;
  } 
}

Back to the top