/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* 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:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.editors;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import org.eclipse.core.net.proxy.IProxyService;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.FindReplaceDocumentAdapter;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.ui.texteditor.ITextEditor;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.SynchronousBundleListener;
import org.osgi.util.tracker.ServiceTracker;
import de.innovationgate.eclipse.editors.all.ProjectBuilderManager;
import de.innovationgate.eclipse.editors.all.TMLMetadataFileMaintainer;
import de.innovationgate.eclipse.editors.helpers.ContextInformationProvider;
import de.innovationgate.eclipse.editors.helpers.HTTPContextInformationProvider;
import de.innovationgate.eclipse.editors.helpers.HeaderHandler;
import de.innovationgate.eclipse.editors.helpers.ResourceIndexManager;
import de.innovationgate.eclipse.editors.models.Snippet;
import de.innovationgate.eclipse.utils.BeanListPreferencesStore;
import de.innovationgate.eclipse.utils.WorkbenchUtils;
import de.innovationgate.eclipse.utils.wga.LabelFileEncodingHelper;
public class Plugin extends AbstractUIPlugin implements SynchronousBundleListener {
public static final String PLUGIN_ID = "de.innovationgate.eclipse.plugins.WGAEclipseEditors";
private static final String SYSPROP_DEBUG = "de.innovationgate.eclipse.plugins.WGAEclipseEditors.debug";
private static Plugin plugin;
private ContextInformationProvider _contextInformationProvider;
private ServiceTracker _proxyServiceTracker;
private ResourceIndexManager _resourceIndexManager;
private ResourceBundle _resourceBundle;
public static final String IMAGE_TML_FILE_CREATE = "IMAGE_TML_FILE_CREATE";
public static final String IMAGE_WORLD = "IMAGE_WORLD";
public static final String IMAGE_TML_FILE_DIRECT_ACCESS = "IMAGE_TML_FILE_DIRECT_ACCESS";
public static final String IMAGE_TML_PORTLET_CREATE = "IMAGE_TML_PORTLET_CREATE";
public static final String IMAGE_TML_LINK_TO_DOC = "IMAGE_TML_LINK_TO_DOC";
public static final String IMAGE_REFRESH = "IMAGE_REFRESH";
public static final String IMAGE_CONTENT_TYPE_DEFINITION = "IMAGE_CONTENT_TYPE_DEFINITION";
public static final String IMAGE_METAFIELD_DEFINITION = "IMAGE_METAFIELD_DEFINITION";
public static final String IMAGE_LANGUAGE_DEFINITION = "IMAGE_LANGUAGE_DEFINITION";
public static final String IMAGE_AREA_DEFINITION = "IMAGE_AREA_DEFINITION";
public static final String IMAGE_CONTENT_ITEM_DEFINITION = "IMAGE_CONTENT_ITEM_DEFINITION";
public static final String IMAGE_SCHEMADEFINITION_CATEGORY = "IMAGE_SCHEMADEFINITION_CATEGORY";
public static final String IMAGE_SCHEMADEFINITION_ADD = "IMAGE_SCHEMADEFINITION_ADD";
public static final String IMAGE_SCHEMADEFINITION_REMOVE = "IMAGE_SCHEMADEFINITION_REMOVE";
public static String HEADER_TML_MODUL = "HEADER_TML_MODUL";
public static String HEADER_CSS_MODUL = "HEADER_CSS_MODUL";
public static String HEADER_JS_MODUL = "HEADER_JS_MODUL";
public static String HEADER_TMLSCRIPT_MODUL = "HEADER_TMLSCRIPT_MODUL";
public static String HEADER_VBS_MODUL = "HEADER_VBS_MODUL";
public static String HEADER_XML_MODUL = "HEADER_XML_MODUL";
private Map<String, HeaderHandler> _headerFileMap = new HashMap<String, HeaderHandler>();
public BeanListPreferencesStore<Snippet> _codeSnippetStore;
private TMLMetadataFileMaintainer _tmlMetadataFileListener;
private ProjectBuilderManager _projectBuilderManager;
public Plugin() {
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext
* )
*/
public void start(BundleContext context) throws Exception {
super.start(context);
context.addBundleListener(this);
}
private void init() throws IOException {
plugin = this;
_resourceBundle = ResourceBundle.getBundle("de.innovationgate.eclipse.editors.resources");
_proxyServiceTracker = new ServiceTracker(getBundle().getBundleContext(), IProxyService.class.getName(), null);
_proxyServiceTracker.open();
setDebugging(Boolean.parseBoolean(System.getProperty(SYSPROP_DEBUG, "false")));
try {
File resourceManager = new File(getStateLocation().toFile(), "resourceManager");
if (!resourceManager.exists()) {
resourceManager.mkdir();
}
_resourceIndexManager = new ResourceIndexManager(resourceManager);
Job job = new Job("Indexing TML-files") {
@Override
protected IStatus run(IProgressMonitor monitor) {
try {
_resourceIndexManager.init();
return Status.OK_STATUS;
} catch (CoreException e) {
return new Status(Status.ERROR, Plugin.PLUGIN_ID, "Unable to indexworkspacefilerefs", e);
}
}
};
job.setPriority(Job.SHORT);
job.schedule();
} catch (Exception e) {
logError("unable to initialize resourceIndexManager", e);
}
_tmlMetadataFileListener = new TMLMetadataFileMaintainer();
ResourcesPlugin.getWorkspace().addResourceChangeListener(_tmlMetadataFileListener, IResourceChangeEvent.POST_CHANGE|IResourceChangeEvent.PRE_BUILD);
_projectBuilderManager = new ProjectBuilderManager();
ResourcesPlugin.getWorkspace().addResourceChangeListener(_projectBuilderManager, IResourceChangeEvent.POST_CHANGE|IResourceChangeEvent.PRE_BUILD);
_projectBuilderManager.performPluginStartup();
IPath stateLocation = getStateLocation();
_headerFileMap.put(HEADER_TML_MODUL, new HeaderHandler(HEADER_TML_MODUL, stateLocation.append("defaultTmlModulLicense.txt").toFile(), "TML-Modules"));
_headerFileMap.put(HEADER_CSS_MODUL, new HeaderHandler(HEADER_CSS_MODUL, stateLocation.append("defaultCssModulLicense.txt").toFile(), "CSS-Modules"));
_headerFileMap.put(HEADER_JS_MODUL, new HeaderHandler(HEADER_JS_MODUL, stateLocation.append("defaultJsModulLicense.txt").toFile(), "JS-Modules"));
_headerFileMap.put(HEADER_TMLSCRIPT_MODUL, new HeaderHandler(HEADER_TMLSCRIPT_MODUL, stateLocation.append("defaultTmlScriptModulLicense.txt").toFile(), "TMLScripts-Modules"));
_headerFileMap.put(HEADER_VBS_MODUL, new HeaderHandler(HEADER_VBS_MODUL, stateLocation.append("defaultVbsModulLicense.txt").toFile(), "VBS-Modules"));
_headerFileMap.put(HEADER_XML_MODUL, new HeaderHandler(HEADER_XML_MODUL, stateLocation.append("defaultXmlModulLicense.txt").toFile(), "XML-Modules"));
}
public ResourceIndexManager getResourceIndexManager() {
return _resourceIndexManager;
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext
* )
*/
public void stop(BundleContext context) throws Exception {
ResourcesPlugin.getWorkspace().removeResourceChangeListener(_tmlMetadataFileListener);
plugin = null;
super.stop(context);
}
/**
* Returns the shared instance
*
* @return the shared instance
*/
public static Plugin getDefault() {
return plugin;
}
public ContextInformationProvider getContextInformationProvider() {
if (_contextInformationProvider == null) {
_contextInformationProvider = new HTTPContextInformationProvider();
}
return _contextInformationProvider;
}
public void logError(String message, Throwable e) {
if (e != null)
getLog().log(new Status(Status.ERROR, PLUGIN_ID, message, e));
else
getLog().log(new Status(Status.ERROR, PLUGIN_ID, message));
}
public void logError(Throwable e) {
if (e != null)
getLog().log(new Status(Status.ERROR, PLUGIN_ID, e.getMessage(), e));
else
getLog().log(new Status(Status.ERROR, PLUGIN_ID, "Unknown error occured."));
}
public void logInfo(String message, Throwable e) {
if (e != null)
getLog().log(new Status(Status.INFO, PLUGIN_ID, message, e));
else
getLog().log(new Status(Status.INFO, PLUGIN_ID, message));
}
public void logWarning(String message, Throwable e) {
if (e != null)
getLog().log(new Status(Status.WARNING, PLUGIN_ID, message, e));
else
getLog().log(new Status(Status.WARNING, PLUGIN_ID, message));
}
public void logError(String message) {
logError(message, null);
}
public void logInfo(String message) {
logInfo(message, null);
}
public void logWarning(String message) {
logWarning(message, null);
}
public InputStream getResourceAsStream(String relativePath) throws IOException {
return FileLocator.toFileURL(FileLocator.find(Platform.getBundle(PLUGIN_ID), new Path(relativePath), null)).openStream();
}
public IProxyService getProxyService() {
return (IProxyService) _proxyServiceTracker.getService();
}
/**
* retrieves the active file from the active editor
*/
public IFile getActiveFile() {
IEditorPart editor = getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
IEditorInput input=null;
if(editor!=null){
input = editor.getEditorInput();
}
IFile file = null;
if (input!=null && input instanceof IFileEditorInput) {
file = ((IFileEditorInput) input).getFile();
}
return file;
}
/**
* retrieves the active project by the active editor
*/
public IProject getActiveProject() {
IFile file = getActiveFile();
if (file != null) {
return file.getProject();
} else {
return null;
}
}
public ResourceBundle getResourceBundle() {
return _resourceBundle;
}
public BeanListPreferencesStore<Snippet> getCodeSnippetStore() {
if (_codeSnippetStore == null) {
_codeSnippetStore = new BeanListPreferencesStore<Snippet>(getPreferenceStore(), Preferences.CODE_SNIPPETS, Snippet.class.getClassLoader());
}
return _codeSnippetStore;
}
public void openLabelEditor(IFile labelFile, String labelKey) {
try {
IEditorPart editor = WorkbenchUtils.openEditor(Plugin.getDefault().getWorkbench(), labelFile);
if (editor instanceof TextEditor) {
ITextEditor textEditor = (ITextEditor) editor;
IDocument doc = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
try {
// search for line starting with label key
FindReplaceDocumentAdapter finder = new FindReplaceDocumentAdapter(doc);
IRegion keyRegion = finder.find(0, "^" + LabelFileEncodingHelper.saveConvert(labelKey,true).replaceAll("\\\\", "\\\\\\\\") + "\\s*=", true, false, false, true);
if (keyRegion != null) {
textEditor.getSelectionProvider().setSelection(new TextSelection(keyRegion.getOffset() + keyRegion.getLength(),0));
}
} catch (BadLocationException e) {
}
}
} catch (PartInitException e) {
Plugin.getDefault().logError("Unable to open editor for labelfile '" + labelFile.getLocation() + "'.", e);
}
}
public void bundleChanged(BundleEvent event) {
Bundle bundle = event.getBundle();
if (!bundle.equals(getBundle())) {
return;
}
int type = event.getType();
if (type == BundleEvent.STARTED) {
try {
init();
} catch (IOException e) {
logError("Unable to initialize editor plugin.", e);
}
}
}
@Override
protected void initializeImageRegistry(ImageRegistry reg) {
super.initializeImageRegistry(reg);
try {
reg.put(IMAGE_TML_FILE_CREATE, createImage("resources/icons/page_add.png"));
reg.put(IMAGE_WORLD, createImage("resources/icons/world.png"));
reg.put(IMAGE_TML_FILE_DIRECT_ACCESS, createImage("resources/icons/page_wga_document_direct_access.png"));
reg.put(IMAGE_TML_PORTLET_CREATE, createImage("resources/icons/plugin_add.png"));
reg.put(IMAGE_TML_LINK_TO_DOC, createImage("resources/icons/world_link.png"));
reg.put(IMAGE_REFRESH, createImage("resources/icons/arrow_refresh.png"));
reg.put(IMAGE_SCHEMADEFINITION_CATEGORY, createImage("resources/icons/database.png"));
reg.put(IMAGE_CONTENT_TYPE_DEFINITION, createImage("resources/icons/page_c.png"));
reg.put(IMAGE_METAFIELD_DEFINITION, createImage("resources/icons/page_m.png"));
reg.put(IMAGE_CONTENT_ITEM_DEFINITION, createImage("resources/icons/page_it.png"));
reg.put(IMAGE_AREA_DEFINITION, createImage("resources/icons/page_a.png"));
reg.put(IMAGE_LANGUAGE_DEFINITION, createImage("resources/icons/page_l.png"));
reg.put(IMAGE_SCHEMADEFINITION_ADD, createImage("resources/icons/page_white_add.png"));
reg.put(IMAGE_SCHEMADEFINITION_REMOVE, createImage("resources/icons/bin.png"));
} catch (IOException e) {
logError("Unable to initialize image registry of plugin '" + PLUGIN_ID + "'.", e);
}
}
private Image createImage(String resourcePath) throws IOException {
InputStream in = null;
try {
in = getResourceAsStream(resourcePath);
return new Image(Display.getCurrent(),in);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
}
}
public File getResourceAsFile(String resourcePath) throws IOException {
URL url = FileLocator.find(getBundle(), new Path(resourcePath), null);
if (url != null) {
return new File (FileLocator.toFileURL(url).getPath());
} else {
return null;
}
}
public Map<String, HeaderHandler> getHeaderFileMap(){
for (String current : _headerFileMap.keySet()) {
_headerFileMap.get(current).readDefaultHeaderFile();
}
return _headerFileMap;
}
public IEclipsePreferences getProjectPreferences(IProject project){
IScopeContext projectScope = new ProjectScope(project);
return (IEclipsePreferences) projectScope.getNode(PLUGIN_ID);
}
}