package net.sourceforge.phpeclipse.wiki.editor;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia;
import net.sourceforge.phpeclipse.wiki.internal.ConfigurationManager;
import net.sourceforge.phpeclipse.wiki.internal.IConfigurationWorkingCopy;
import net.sourceforge.phpeclipse.wiki.sql.WikipediaDB;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IPluginDescriptor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.text.templates.ContextTypeRegistry;
import org.eclipse.jface.text.templates.persistence.TemplateStore;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.editors.text.templates.ContributionContextTypeRegistry;
import org.eclipse.ui.editors.text.templates.ContributionTemplateStore;
import org.eclipse.ui.plugin.AbstractUIPlugin;
public class WikiEditorPlugin extends AbstractUIPlugin {
private static WikiEditorPlugin fgPlugin;
public final static String AUTOMATICALLY_CREATED = "";
public final static String WP_EXTENSION = "wp";
public final static String XML_START_1 = "\n"
+ "\n";
public final static String XML_END ="\n";
public static final String PREFIX_LOAD = "Load ";
public static final String PREFIX_STORE = "Store ";
public static final String HTTP_QUERY = "HTTP Query";
// public static final String WIKIPEDIA_GET_TEXT = "Load WikipediaEN";
//
// public static final String WIKIPEDIA_SET_TEXT = "Store WikipediaEN";
public static final String WIKIPEDIA_SQL = "Wikipedia SQL access";
public static final String BLOG_A_WIKI = "Blog as Wiki Text";
public static final String BLOG_A_HTML = "Blog as HTML Text";
public static final String PLOG4U_DE_LOAD = PREFIX_LOAD + "Plog4UDE";
public static final String PLOG4U_DE_STORE = PREFIX_STORE + "Plog4UDE";
public static final String PLOG4U_ORG_LOAD = PREFIX_LOAD + "Plog4UORG";
public static final String PLOG4U_ORG_STORE = PREFIX_STORE + "Plog4UORG";
public static final String[] PREDEFINED_TYPES = {
HTTP_QUERY,
WIKIPEDIA_SQL,
PLOG4U_DE_LOAD,
PLOG4U_DE_STORE,
PLOG4U_ORG_LOAD,
PLOG4U_ORG_STORE,
BLOG_A_WIKI,
BLOG_A_HTML };
public static final String[] PREDEFINED_WIKIS = {
"WikibooksDE",
"WikibooksEN",
"WikinewsDE",
"WikinewsEN",
"WikipediaAR",
"WikipediaCA",
"WikipediaDE",
"WikipediaEN",
"WikipediaEO",
"WikipediaES",
"WikipediaET",
"WikipediaFI",
"WikipediaFR",
"WikipediaHE",
"WikipediaIA",
"WikipediaIT",
"WikipediaJA",
"WikipediaNL",
"WikipediaPL",
"WikipediaPT",
"WikipediaRO",
"WikipediaSL",
"WikipediaSV",
"WikipediaZH" };
public static final ArrayList CONFIGURATION_TYPES = new ArrayList();
//image paths
public static final String ICON_PATH = "icons/full/"; //$NON-NLS-1$
public final static String PLUGIN_ID = "net.sourceforge.phpeclipse.wiki";
public final static String HTML_OUTPUT_PATH = "__static_wiki_folder";
public final static String WIKI_TEXTS_BASE_PATH = "__wiki_texts_base_path";
public final static String LOCAL_TEMPLATE_FILE_NAME = "__local_template_file_name";
public final static String EXPORT_TEMPLATE_FILE_NAME = "__export_template_file_name";
public final static String LOCAL_CSS_URL = "__local_css_url";
public final static String EXPORT_CSS_URL = "__export_css_url";
public final static String PREF_STRING_CONFIGURATIONS = "__configurations3";
public final static String CONFIG_MEMENTO = "" + ""
+ ""
+ ""
+ ""
+ "" + ""
+ ""
+ ""
+ "" + ""
+ "" + ""
+ ""
+ "";
public static WikipediaDB fWikiDB = null;
private static ConfigurationManager manager;
//
// public static final String IMG_MONITOR_ON = "monitorOn";
//
// public static final String IMG_MONITOR_OFF = "monitorOff";
/**
* Creates an image and places it in the image registry.
*
* @param id
* the identifier for the image
* @param baseURL
* the base URL for the image
*/
protected static void createImageDescriptor(WikiEditorPlugin plugin, String id, URL baseURL) {
// Delegate to the plugin instance to avoid concurrent class loading problems
plugin.privateCreateImageDescriptor(id, baseURL);
}
public static WikiEditorPlugin getDefault() {
return fgPlugin;
}
/**
* Returns the image descriptor for the given image ID. Returns null if there is no such image.
*
* @param id
* the identifier for the image to retrieve
* @return the image associated with the given ID
*/
public static ImageDescriptor getImageDescriptor(String id) {
// Delegate to the plugin instance to avoid concurrent class loading problems
return getDefault().privateGetImageDescriptor(id);
}
/**
* Convenience method to get an image descriptor for an extension
*
* @param extension
* the extension declaring the image
* @param subdirectoryAndFilename
* the path to the image
* @return the image
*/
public static ImageDescriptor getImageDescriptorFromExtension(IExtension extension, String subdirectoryAndFilename) {
IPluginDescriptor pluginDescriptor = extension.getDeclaringPluginDescriptor();
URL path = pluginDescriptor.getInstallURL();
URL fullPathString = null;
try {
fullPathString = new URL(path, subdirectoryAndFilename);
return ImageDescriptor.createFromURL(fullPathString);
} catch (MalformedURLException e) {
}
return null;
}
public static String getResourceString(String key) {
ResourceBundle bundle = WikiEditorPlugin.getDefault().getResourceBundle();
try {
return (bundle != null) ? bundle.getString(key) : key;
} catch (MissingResourceException e) {
return key;
}
}
/**
* Returns the standard display to be used. The method first checks, if the thread calling this method has an associated display.
* If so, this display is returned. Otherwise the method returns the default display.
*/
public static Display getStandardDisplay() {
Display display = Display.getCurrent();
if (display == null) {
display = Display.getDefault();
}
return display;
}
private ContributionContextTypeRegistry fContextTypeRegistry;
private ResourceBundle fResourceBundle;
private ContributionTemplateStore fTemplateStore;
private Hashtable imageDescriptors = new Hashtable(20);
public WikiEditorPlugin(IPluginDescriptor descriptor) {
super(descriptor);
initializeImages();
fgPlugin = this;
for (int i = 0; i < PREDEFINED_TYPES.length; i++) {
CONFIGURATION_TYPES.add(PREDEFINED_TYPES[i]);
}
for (int i = 0; i < PREDEFINED_WIKIS.length; i++) {
CONFIGURATION_TYPES.add(PREFIX_LOAD + PREDEFINED_WIKIS[i]);
}
for (int i = 0; i < PREDEFINED_WIKIS.length; i++) {
CONFIGURATION_TYPES.add(PREFIX_STORE + PREDEFINED_WIKIS[i]);
}
manager = ConfigurationManager.getInstance();
try {
fResourceBundle = ResourceBundle.getBundle("net.sourceforge.phpeclipse.wiki.editor.WikiEditorMessages");
} catch (MissingResourceException x) {
fResourceBundle = null;
}
}
/**
* Creates an image and places it in the image registry.
*/
protected void createImageDescriptor(String id, URL baseURL) {
URL url = null;
try {
url = new URL(baseURL, ICON_PATH + id);
} catch (MalformedURLException e) {
}
ImageDescriptor desc = ImageDescriptor.createFromURL(url);
imageDescriptors.put(id, desc);
}
public IWorkbenchPage getActivePage() {
IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
if (window != null)
return window.getActivePage();
return null;
}
public ContextTypeRegistry getContextTypeRegistry() {
if (fContextTypeRegistry == null) {
fContextTypeRegistry = new ContributionContextTypeRegistry();
fContextTypeRegistry.addContextType("net.sourceforge.phpeclipse.wiki.editor.templates");
}
return fContextTypeRegistry;
}
public Image getImage(String key) {
Image image = getImageRegistry().get(key);
if (image == null) {
ImageDescriptor d = getImageDescriptor(key);
image = d.createImage();
getImageRegistry().put(key, image);
}
return image;
}
public ResourceBundle getResourceBundle() {
return fResourceBundle;
}
public TemplateStore getTemplateStore() {
if (fTemplateStore == null) {
fTemplateStore = new ContributionTemplateStore(getContextTypeRegistry(), getDefault().getPreferenceStore(), "templates");
try {
fTemplateStore.load();
} catch (IOException e) {
e.printStackTrace();
}
}
return fTemplateStore;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.plugin.AbstractUIPlugin#initializeDefaultPreferences(org.eclipse.jface.preference.IPreferenceStore)
*/
protected void initializeDefaultPreferences(IPreferenceStore store) {
store.setDefault(PREF_STRING_CONFIGURATIONS, CONFIG_MEMENTO);
}
/*
* Initializes the table of images used in this plugin. The plugin is provided because this method is called before the plugin
* staic variable has been set. See the comment on the getPlugin() method for a description of why this is required.
*/
private void initializeImages() {
URL baseURL = getDescriptor().getInstallURL();
// special
createImageDescriptor("glyphs/glyph1.gif", baseURL); //$NON-NLS-1$
createImageDescriptor("glyphs/glyph2.gif", baseURL); //$NON-NLS-1$
createImageDescriptor("glyphs/glyph3.gif", baseURL); //$NON-NLS-1$
createImageDescriptor("glyphs/glyph4.gif", baseURL); //$NON-NLS-1$
createImageDescriptor("glyphs/glyph5.gif", baseURL); //$NON-NLS-1$
createImageDescriptor("glyphs/glyph6.gif", baseURL); //$NON-NLS-1$
createImageDescriptor("glyphs/glyph7.gif", baseURL); //$NON-NLS-1$
createImageDescriptor("glyphs/glyph8.gif", baseURL); //$NON-NLS-1$
}
public void log(String message) {
getDefault().getLog().log(new Status(IStatus.OK, PLUGIN_ID, IStatus.OK, message, null));
}
public void log(String message, Exception e) {
getDefault().getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, "Caught exception", e));
}
public void logAndReport(String title, String message, Exception e) {
log(message, e);
reportError(title, message);
}
private void privateCreateImageDescriptor(String id, URL baseURL) {
URL url = null;
try {
url = new URL(baseURL, ICON_PATH + id);
} catch (MalformedURLException e) {
}
ImageDescriptor desc = ImageDescriptor.createFromURL(url);
imageDescriptors.put(id, desc);
}
private ImageDescriptor privateGetImageDescriptor(String id) {
if (!imageDescriptors.containsKey(id)) {
URL baseURL = WikiEditorPlugin.getDefault().getDescriptor().getInstallURL();
createImageDescriptor(getDefault(), id, baseURL);
}
return (ImageDescriptor) imageDescriptors.get(id);
}
public void reportError(String title, String message) {
try {
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
MessageDialog.openError(shell, title, message);
} catch (RuntimeException e) {
log(e.getLocalizedMessage(), e);
}
}
public void startup() throws CoreException {
super.startup();
}
/**
* Returns the translated String found with the given key.
*
* @return java.lang.String
* @param key
* java.lang.String
*/
public static String getResource(String key) {
try {
return Platform.getResourceString(getDefault().getBundle(), key);
} catch (Exception e) {
return key;
}
}
/**
* Returns the translated String found with the given key, and formatted with the given object.
*
* @param key
* java.lang.String
* @param obj
* java.lang.Object[]
* @return java.lang.String
*/
public static String getResource(String key, Object[] obj) {
try {
return MessageFormat.format(getResource(key), obj);
} catch (Exception e) {
return key;
}
}
/**
* Returns the translated String found with the given key, and formatted with the given object.
*
* @param key
* java.lang.String
* @param s
* java.lang.String
* @return java.lang.String
*/
public static String getResource(String key, String s) {
try {
return MessageFormat.format(getResource(key), new String[] { s });
} catch (Exception e) {
return key;
}
}
/**
* Return a list of all the existing configurations.
*
* @return java.util.List
*/
public static List getConfigurations() {
return manager.getConfigurations();
}
/**
* Create a new monitor.
*
* @return working copy
*/
public static IConfigurationWorkingCopy createConfiguration() {
return manager.createConfiguration();
}
public static ArrayList getTypes() {
return CONFIGURATION_TYPES;
}
public static void addType(String type) {
CONFIGURATION_TYPES.add(type);
}
/*
* (non-Javadoc)
*
* @see org.eclipse.core.runtime.Plugin#shutdown()
*/
public void shutdown() throws CoreException {
if (fWikiDB != null) {
try {
fWikiDB.shutdown();
} catch (SQLException e) {
}
}
super.shutdown();
}
public static void log(int severity, String message) {
Status status = new Status(severity, PLUGIN_ID, IStatus.OK, message, null);
log(status);
}
public static void log(IStatus status) {
getDefault().getLog().log(status);
}
public static void log(Throwable e) {
log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "PHPeclipsePlugin.internalErrorOccurred", e)); //$NON-NLS-1$
}
/**
* Get a IWikpedia singleton instance through reflection (i.e. Method#invoke() )
*
* @param wikiLocale
* @return
* @throws NoSuchMethodException
* @throws IllegalAccessException
* @throws ClassNotFoundException
* @throws InvocationTargetException
*/
public static IWikipedia getWikiInstance(String wikiLocale) throws NoSuchMethodException, IllegalAccessException,
ClassNotFoundException, InvocationTargetException {
String className = "net.sourceforge.phpeclipse.wiki.actions.mediawiki.config." + wikiLocale;
Class cls = Class.forName(className);
Method method = cls.getMethod("getInstance", new Class[0]);
return (IWikipedia) method.invoke(null, new Object[0]);
}
/**
* Returns the workspace instance.
*/
public static IWorkspace getWorkspace() {
return ResourcesPlugin.getWorkspace();
}
// test code
// public static void main(String[] args) {
// try {
// getWikiInstance("WikipediaEN");
// } catch (NoSuchMethodException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (IllegalAccessException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (ClassNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (InvocationTargetException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
}