KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > codegen > presentation > CodeGenUIPlugin


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: CodeGenUIPlugin.java,v 1.3 2005/06/08 06:23:43 nickb Exp $
16  */

17 package org.eclipse.emf.codegen.presentation;
18
19
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.text.MessageFormat JavaDoc;
23
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IStatus;
26 import org.eclipse.core.runtime.Platform;
27 import org.eclipse.core.runtime.Status;
28 import org.eclipse.jface.dialogs.ErrorDialog;
29 import org.eclipse.jface.resource.ImageDescriptor;
30 import org.eclipse.ui.plugin.AbstractUIPlugin;
31
32 import org.eclipse.emf.codegen.jet.JETException;
33
34
35 /**
36  * This is the central singleton for the emf.codegen plugin.
37  */

38 public class CodeGenUIPlugin extends AbstractUIPlugin
39 {
40   /**
41    * Get the singleton instance.
42    */

43   public static CodeGenUIPlugin getPlugin()
44   {
45     return plugin;
46   }
47
48   /**
49    * This keeps track of the one instance of this class.
50    */

51   private static CodeGenUIPlugin plugin;
52
53   /**
54    * Create the instance.
55    */

56   public CodeGenUIPlugin()
57   {
58     super();
59
60     // Remember the static instance.
61
//
62
plugin = this;
63   }
64
65   /**
66    * This fetches a resource string from the plugin.properites file.
67    */

68   public String JavaDoc getString(String JavaDoc key)
69   {
70     return Platform.getResourceBundle(plugin.getBundle()).getString(key);
71   }
72
73   public ImageDescriptor getImage(String JavaDoc key)
74   {
75     try
76     {
77       return ImageDescriptor.createFromURL(new URL JavaDoc(getBundle().getEntry("/") + "icons/" + key + ".gif"));
78     }
79     catch (MalformedURLException JavaDoc exception)
80     {
81       write(exception);
82       return null;
83     }
84   }
85
86   /**
87    * This fetches a resource string from the plugin.properites file and performs message substitution.
88    */

89   public String JavaDoc getString(String JavaDoc key, Object JavaDoc[] objects)
90   {
91     return MessageFormat.format(getString(key), objects);
92   }
93
94   public static void write(Exception JavaDoc exception)
95   {
96     IStatus status;
97     String JavaDoc message;
98     if (exception instanceof JETException)
99     {
100       status = ((JETException)exception).getStatus();
101       message = getPlugin().getString("_UI_JETCompileProblem_message");
102     }
103     else if (exception instanceof CoreException)
104     {
105       status = ((CoreException)exception).getStatus();
106       message = exception.getLocalizedMessage();
107     }
108     else
109     {
110       status =
111         new Status
112           (IStatus.ERROR,
113            getPlugin().getBundle().getSymbolicName(),
114            0,
115            exception.getLocalizedMessage(),
116            exception);
117
118       message = exception.getLocalizedMessage();
119     }
120
121     ErrorDialog.openError
122       (getPlugin().getWorkbench().getActiveWorkbenchWindow().getShell(),
123        getPlugin().getString("_UI_JETProblem_title"),
124        message,
125        status);
126   }
127 }
128
Popular Tags