diff options
5 files changed, 364 insertions, 241 deletions
diff --git a/bundles/org.eclipse.e4.tools.orion.css.editor/META-INF/MANIFEST.MF b/bundles/org.eclipse.e4.tools.orion.css.editor/META-INF/MANIFEST.MF index 43224a68..9c2fe56a 100644 --- a/bundles/org.eclipse.e4.tools.orion.css.editor/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.e4.tools.orion.css.editor/META-INF/MANIFEST.MF @@ -6,6 +6,9 @@ Bundle-Version: 1.0.0.qualifier Bundle-Activator: org.eclipse.e4.tools.orion.css.editor.Activator Require-Bundle: org.eclipse.ui, org.eclipse.core.runtime, - org.eclipse.e4.ui.css.swt.theme;bundle-version="0.9.100" + org.eclipse.e4.ui.css.swt.theme;bundle-version="0.9.100", + org.eclipse.e4.ui.model.workbench;bundle-version="1.0.0", + org.eclipse.e4.core.contexts;bundle-version="1.3.0", + org.eclipse.e4.ui.workbench Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Bundle-ActivationPolicy: lazy diff --git a/bundles/org.eclipse.e4.tools.orion.css.editor/plugin.xml b/bundles/org.eclipse.e4.tools.orion.css.editor/plugin.xml index 11e8deab..3b952f71 100644 --- a/bundles/org.eclipse.e4.tools.orion.css.editor/plugin.xml +++ b/bundles/org.eclipse.e4.tools.orion.css.editor/plugin.xml @@ -3,40 +3,13 @@ <plugin> <extension - point="org.eclipse.ui.views"> - <category - name="Sample CSS Category" - id="org.eclipse.e4.tools.orion.css.editor"> - </category> - <view - name="Sample CSS View" - icon="icons/sample.gif" - category="org.eclipse.e4.tools.orion.css.editor" - class="org.eclipse.e4.tools.orion.css.editor.views.SampleCSSView" - id="org.eclipse.e4.tools.orion.css.editor.views.SampleCSSView"> - </view> - </extension> - <extension - point="org.eclipse.ui.commands"> - <command - categoryId="org.eclipse.ui.category.file" - defaultHandler="org.eclipse.e4.tools.orion.css.editor.views.GetContentHandler" - id="org.eclipse.e4.tools.orion.css.editor.get.content" - name="Get Editor Content"> - </command> - </extension> - <extension - point="org.eclipse.ui.menus"> - <menuContribution - allPopups="false" - locationURI="toolbar:org.eclipse.e4.tools.orion.css.editor.views.SampleCSSView"> - <command - commandId="org.eclipse.e4.tools.orion.css.editor.get.content" - icon="icons/sample.gif" - label="Get Editor content" - style="push"> - </command> - </menuContribution> + point="org.eclipse.ui.internalTweaklets"> + <tweaklet + definition="org.eclipse.ui.internal.tweaklets.PreferencePageEnhancer" + id="org.eclipse.e4.tools.orion.css.editor.tweaklet" + implementation="org.eclipse.e4.tools.orion.css.editor.CSSEditorPreferences" + name="Orion CSS Editor"> + </tweaklet> </extension> </plugin> diff --git a/bundles/org.eclipse.e4.tools.orion.css.editor/src/org/eclipse/e4/tools/orion/css/editor/CSSEditorPreferences.java b/bundles/org.eclipse.e4.tools.orion.css.editor/src/org/eclipse/e4/tools/orion/css/editor/CSSEditorPreferences.java new file mode 100644 index 00000000..40cd283e --- /dev/null +++ b/bundles/org.eclipse.e4.tools.orion.css.editor/src/org/eclipse/e4/tools/orion/css/editor/CSSEditorPreferences.java @@ -0,0 +1,353 @@ +package org.eclipse.e4.tools.orion.css.editor; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.eclipse.core.runtime.FileLocator; +import org.eclipse.core.runtime.IConfigurationElement; +import org.eclipse.core.runtime.IExtension; +import org.eclipse.core.runtime.IExtensionPoint; +import org.eclipse.core.runtime.IExtensionRegistry; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.RegistryFactory; +import org.eclipse.core.runtime.Status; +import org.eclipse.e4.ui.css.swt.internal.theme.ThemeEngine; +import org.eclipse.e4.ui.css.swt.theme.ITheme; +import org.eclipse.e4.ui.css.swt.theme.IThemeEngine; +import org.eclipse.e4.ui.model.application.ui.basic.MWindow; +import org.eclipse.e4.ui.workbench.modeling.EModelService; +import org.eclipse.e4.ui.workbench.modeling.EPartService; +import org.eclipse.osgi.service.datalocation.Location; +import org.eclipse.swt.SWT; +import org.eclipse.swt.browser.Browser; +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.internal.tweaklets.PreferencePageEnhancer; +import org.osgi.framework.Bundle; + +public class CSSEditorPreferences extends PreferencePageEnhancer { + + ITheme selection; + IThemeEngine engine; + boolean resetCurrentTheme; + private Browser browser; + private String editorHtml; + private String editorContent; + private Path filePath; + + @SuppressWarnings("restriction") + @Override + public void createContents(Composite parent) { + resetCurrentTheme = false; + IWorkbenchWindow wbw = PlatformUI.getWorkbench() + .getActiveWorkbenchWindow(); + MWindow hostWin = (MWindow) wbw.getService(MWindow.class); + EPartService partService = hostWin.getContext().get(EPartService.class); + EModelService modelService = hostWin.getContext().get( + EModelService.class); + + engine = hostWin.getContext().get(IThemeEngine.class); + + try { + initContent(); + + loadEditorHtml(); + + // Render it + Composite composite3 = new Composite(parent, SWT.BORDER); + composite3.setLayout(new FillLayout()); + GridData data = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 2); + data.widthHint = 500; + data.heightHint = 500; + composite3.setLayoutData(data); + + browser = new Browser(composite3, SWT.NONE); + browser.setText(editorHtml, true); + } catch (IOException e) { + Activator + .getDefault() + .getLog() + .log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, + "Failed to load CSS", e)); + return; + } + } + + private void loadEditorHtml() throws IOException { + final Bundle bundle = Activator.getDefault().getBundle(); + final InputStream contentAssistInput = bundle.getEntry( + "/web/swtContentAssist.js").openStream(); + String contentAssistTemplate = loadFile(contentAssistInput, 1024); + String keywords = loadKeywords(); + String contentAssist = String.format(contentAssistTemplate, keywords); + final InputStream cssTemplateInput = bundle.getEntry("/web/css.html") + .openStream(); + String cssTemplate = loadFile(cssTemplateInput, 1024); + final String editorCssUrl = FileLocator.toFileURL( + bundle.getEntry("/web/built-editor.css")).toExternalForm(); + final String editorJsUrl = FileLocator.toFileURL( + bundle.getEntry("/web/built-editor.js")).toExternalForm(); + + editorHtml = String.format(cssTemplate, editorCssUrl, editorJsUrl, + contentAssist, editorContent); + } + + private void initContent() throws MalformedURLException, IOException { + final ITheme theme = selection; + List<String> sheets = ((ThemeEngine) engine).getStylesheets(theme); + List<String> mod = ((ThemeEngine) engine) + .getModifiedStylesheets(selection); + if (mod.size() > 0) { + sheets = mod; + } + if (sheets.size() > 0) { + String path = sheets.get(0); + URL fileURL = FileLocator.toFileURL(new URL(path)); + final InputStream in = fileURL.openStream(); + filePath = new Path(fileURL.getPath()); + editorContent = loadFile(in, 1024); + } else { + editorContent = "/*\n * This is an Orion editor sample.\n */\nfunction() {\n var a = 'hi there!';\n window.console.log(a);\n}"; + } + } + + private String loadKeywords() { + StringBuilder buf = new StringBuilder(); + + IExtensionRegistry registry = RegistryFactory.getRegistry(); + IExtensionPoint extPoint = registry + .getExtensionPoint("org.eclipse.e4.ui.css.core.propertyHandler"); + ArrayList<IConfigurationElement> matchingElements = new ArrayList<IConfigurationElement>(); + ArrayList<IConfigurationElement> controlAdapters = new ArrayList<IConfigurationElement>(); + for (IExtension e : extPoint.getExtensions()) { + IConfigurationElement[] elements = e.getConfigurationElements(); + for (int i = 0; i < elements.length; i++) { + IConfigurationElement element = elements[i]; + controlAdapters.add(element); + IConfigurationElement[] child = element + .getChildren("property-name"); + for (int j = 0; j < child.length; j++) { + matchingElements.add(child[j]); + } + } + } + Iterator<IConfigurationElement> iter = matchingElements.iterator(); + boolean once = true; + while (iter.hasNext()) { + IConfigurationElement type = iter.next(); + String name = type.getAttribute("name"); + if (!once) { + buf.append(','); + buf.append('\n'); + } + buf.append('"'); + buf.append(name); + buf.append('"'); + once = false; + } + buf.append('\n'); + + return buf.toString(); + } + + public String loadFile(final InputStream in, final int bufferSize) + throws IOException { + final char[] buffer = new char[bufferSize]; + final StringBuilder out = new StringBuilder(); + final Reader reader = new InputStreamReader(in, "UTF-8"); + try { + int size = reader.read(buffer, 0, buffer.length); + while (size > 0) { + out.append(buffer, 0, size); + size = reader.read(buffer, 0, buffer.length); + } + } finally { + reader.close(); + } + return out.toString(); + } + + @Override + public void setSelection(Object sel) { + if (sel instanceof ITheme) { + ITheme newTheme = (ITheme) sel; + ITheme oldSelection = selection; + selection = newTheme; + if (oldSelection != null + && !newTheme.getId().equals(oldSelection.getId())) { + try { + initContent(); + loadEditorHtml(); + browser.setText(editorHtml, true); + } catch (IOException e) { + Activator + .getDefault() + .getLog() + .log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, + "Failed to load CSS", e)); + return; + } + } + } + + } + + private boolean isDirty() { + final Object rc = browser.evaluate("return window.editor.isDirty();"); + if (rc instanceof Boolean) { + return (Boolean) rc; + } + return false; + } + + @Override + public void performOK() { + + if (isDirty()) { + // check for .e4css folder + Location configLocation = Platform.getConfigurationLocation(); + String e4CSSPath = null; + try { + URL locationURL = new URL(configLocation.getDataArea( + ThemeEngine.THEME_PLUGIN_ID).toString()); + File locationFile = new File(locationURL.getFile()); + e4CSSPath = locationFile.getPath(); + if (!locationFile.exists()) { + locationFile.mkdirs(); + } + } catch (IOException e1) { + Activator + .getDefault() + .getLog() + .log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, + "Failed to create config CSS", e1)); + return; + } + IPath path = new Path(e4CSSPath + + System.getProperty("file.separator") + + filePath.lastSegment()); + final Object rc = browser + .evaluate("return window.editor.getText();"); + if (!(rc instanceof String)) { + Activator + .getDefault() + .getLog() + .log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, + "Failed to retrieve editor content " + rc)); + return; + } + byte[] bytes = ((String) rc).getBytes(); + FileOutputStream outputStream = null; + try { + outputStream = new FileOutputStream(path.toOSString()); + outputStream.write(bytes, 0, bytes.length); + } catch (FileNotFoundException e) { + Activator + .getDefault() + .getLog() + .log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, + "Failed to write CSS", e)); + } catch (IOException e) { + Activator + .getDefault() + .getLog() + .log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, + "Failed to write CSS", e)); + } finally { + if (outputStream != null) + try { + outputStream.close(); + } catch (IOException e) { + } + } + if (engine instanceof ThemeEngine) { + ArrayList<String> styleSheets = new ArrayList<String>(); + try { + URL styleSheetURL = path.toFile().toURI().toURL(); + styleSheets.add(styleSheetURL.toString()); + ((ThemeEngine) engine) + .themeModified(selection, styleSheets); + } catch (MalformedURLException e) { + Activator + .getDefault() + .getLog() + .log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, + "Failed to update CSS Theme", e)); + } catch (IOException e) { + Activator + .getDefault() + .getLog() + .log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, + "Failed to update CSS Theme", e)); + } + } + } + + if (resetCurrentTheme) { + ((ThemeEngine) engine).resetCurrentTheme(); + resetCurrentTheme = false; + } + } + + @Override + public void performCancel() { + // Just let the text disappear + } + + @Override + public void performDefaults() { + List<String> mod = ((ThemeEngine) engine) + .getModifiedStylesheets(selection); + if (mod.size() > 0) { + + // For now just get the first element + String path = mod.get(0); + try { + + URL styleSheetURL = FileLocator.toFileURL(new URL(path)); + File file = new File(styleSheetURL.getFile()); + if (file.exists()) + file.delete(); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (engine instanceof ThemeEngine) { + ((ThemeEngine) engine).resetModifiedStylesheets(selection); + } + + try { + initContent(); + loadEditorHtml(); + browser.setText(editorHtml, true); + } catch (IOException e) { + Activator + .getDefault() + .getLog() + .log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, + "Failed to load CSS", e)); + return; + } + + resetCurrentTheme = true; + } + +} diff --git a/bundles/org.eclipse.e4.tools.orion.css.editor/src/org/eclipse/e4/tools/orion/css/editor/views/GetContentHandler.java b/bundles/org.eclipse.e4.tools.orion.css.editor/src/org/eclipse/e4/tools/orion/css/editor/views/GetContentHandler.java deleted file mode 100644 index 7bf51f70..00000000 --- a/bundles/org.eclipse.e4.tools.orion.css.editor/src/org/eclipse/e4/tools/orion/css/editor/views/GetContentHandler.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.eclipse.e4.tools.orion.css.editor.views; - -import org.eclipse.core.commands.AbstractHandler; -import org.eclipse.core.commands.ExecutionEvent; -import org.eclipse.core.commands.ExecutionException; -import org.eclipse.swt.browser.Browser; -import org.eclipse.ui.IWorkbenchPart; -import org.eclipse.ui.handlers.HandlerUtil; - -public class GetContentHandler extends AbstractHandler { - - @Override - public Object execute(ExecutionEvent event) throws ExecutionException { - final IWorkbenchPart part = HandlerUtil.getActivePartChecked(event); - if (part instanceof SampleCSSView) { - SampleCSSView view = (SampleCSSView) part; - final Browser browser = view.getBrowser(); - final Object rc = browser.evaluate("return window.editor.getText();"); - System.out.println(rc); - } - return null; - } - -} diff --git a/bundles/org.eclipse.e4.tools.orion.css.editor/src/org/eclipse/e4/tools/orion/css/editor/views/SampleCSSView.java b/bundles/org.eclipse.e4.tools.orion.css.editor/src/org/eclipse/e4/tools/orion/css/editor/views/SampleCSSView.java deleted file mode 100644 index d4ec3c6d..00000000 --- a/bundles/org.eclipse.e4.tools.orion.css.editor/src/org/eclipse/e4/tools/orion/css/editor/views/SampleCSSView.java +++ /dev/null @@ -1,182 +0,0 @@ -package org.eclipse.e4.tools.orion.css.editor.views; - -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -import org.eclipse.core.runtime.FileLocator; -import org.eclipse.core.runtime.IConfigurationElement; -import org.eclipse.core.runtime.IExtension; -import org.eclipse.core.runtime.IExtensionPoint; -import org.eclipse.core.runtime.IExtensionRegistry; -import org.eclipse.core.runtime.RegistryFactory; -import org.eclipse.e4.tools.orion.css.editor.Activator; -import org.eclipse.e4.ui.css.swt.internal.theme.ThemeEngine; -import org.eclipse.e4.ui.css.swt.theme.ITheme; -import org.eclipse.e4.ui.css.swt.theme.IThemeEngine; -import org.eclipse.jface.action.IToolBarManager; -import org.eclipse.swt.SWT; -import org.eclipse.swt.browser.Browser; -import org.eclipse.swt.widgets.Composite; -import org.eclipse.ui.IViewSite; -import org.eclipse.ui.PartInitException; -import org.eclipse.ui.part.ViewPart; -import org.osgi.framework.Bundle; - -/** - * This sample class demonstrates how to plug-in a new workbench view. The view - * shows data obtained from the model. The sample creates a dummy model on the - * fly, but a real implementation would connect to the model available either in - * this or another plug-in (e.g. the workspace). The view is connected to the - * model using a content provider. - * <p> - * The view uses a label provider to define how model objects should be - * presented in the view. Each view can present the same model objects using - * different labels and icons, if needed. Alternatively, a single label provider - * can be shared between views in order to ensure that objects of the same type - * are presented in the same way everywhere. - * <p> - */ - -public class SampleCSSView extends ViewPart { - - /** - * The ID of the view as specified by the extension. - */ - public static final String ID = "z.ex.css.editor.views.SampleCSSView"; - private Browser browser; - private String editorHtml; - private String editorContent; - - /** - * This is a callback that will allow us to create the viewer and initialize - * it. - * - * @throws PartInitException - */ - public void createPartControl(Composite parent) { - browser = new Browser(parent, SWT.NONE); - browser.setText(editorHtml, true); - } - - @Override - public void init(IViewSite site) throws PartInitException { - super.init(site); - try { - initContent(); - final Bundle bundle = Activator.getDefault().getBundle(); - final InputStream contentAssistInput = bundle.getEntry( - "/web/swtContentAssist.js").openStream(); - String contentAssistTemplate = loadFile(contentAssistInput, 1024); - String keywords = loadKeywords(); - String contentAssist = String.format(contentAssistTemplate, - keywords); - final InputStream cssTemplateInput = bundle.getEntry( - "/web/css.html").openStream(); - String cssTemplate = loadFile(cssTemplateInput, 1024); - final String editorCssUrl = FileLocator.toFileURL( - bundle.getEntry("/web/built-editor.css")).toExternalForm(); - final String editorJsUrl = FileLocator.toFileURL( - bundle.getEntry("/web/built-editor.js")).toExternalForm(); - - editorHtml = String.format(cssTemplate, editorCssUrl, editorJsUrl, - contentAssist, editorContent); - System.out.println(editorHtml); - } catch (IOException e) { - throw new PartInitException("Failed to load CSS editor", e); - } - } - - private String loadKeywords() { - StringBuilder buf = new StringBuilder(); - - IExtensionRegistry registry = RegistryFactory.getRegistry(); - IExtensionPoint extPoint = registry - .getExtensionPoint("org.eclipse.e4.ui.css.core.propertyHandler"); - ArrayList<IConfigurationElement> matchingElements = new ArrayList<IConfigurationElement>(); - ArrayList<IConfigurationElement> controlAdapters = new ArrayList<IConfigurationElement>(); - for (IExtension e : extPoint.getExtensions()) { - IConfigurationElement[] elements = e.getConfigurationElements(); - for (int i = 0; i < elements.length; i++) { - IConfigurationElement element = elements[i]; - controlAdapters.add(element); - IConfigurationElement[] child = element - .getChildren("property-name"); - for (int j = 0; j < child.length; j++) { - matchingElements.add(child[j]); - } - } - } - Iterator<IConfigurationElement> iter = matchingElements.iterator(); - boolean once = true; - while (iter.hasNext()) { - IConfigurationElement type = iter.next(); - String name = type.getAttribute("name"); - if (!once) { - buf.append(','); - buf.append('\n'); - } - buf.append('"'); - buf.append(name); - buf.append('"'); - once = false; - } - buf.append('\n'); - - return buf.toString(); - } - - private void initContent() throws MalformedURLException, IOException { - IThemeEngine engine = (IThemeEngine) getSite().getService( - IThemeEngine.class); - final ITheme theme = engine.getActiveTheme(); - final List<String> sheets = ((ThemeEngine) engine) - .getStylesheets(theme); - if (sheets.size() > 0) { - String path = sheets.get(0); - final InputStream in = FileLocator - .toFileURL(new URL(sheets.get(0))).openStream(); - editorContent = loadFile(in, 1024); - } else { - editorContent = "/*\n * This is an Orion editor sample.\n */\nfunction() {\n var a = 'hi there!';\n window.console.log(a);\n}"; - } - } - - public String loadFile(final InputStream in, final int bufferSize) - throws IOException { - final char[] buffer = new char[bufferSize]; - final StringBuilder out = new StringBuilder(); - final Reader reader = new InputStreamReader(in, "UTF-8"); - try { - int size = reader.read(buffer, 0, buffer.length); - while (size > 0) { - out.append(buffer, 0, size); - size = reader.read(buffer, 0, buffer.length); - } - } finally { - reader.close(); - } - return out.toString(); - } - - /** - * Passing the focus request to the viewer's control. - */ - public void setFocus() { - browser.setFocus(); - } - - /** - * @return the browser - */ - public Browser getBrowser() { - return browser; - } - -}
\ No newline at end of file |