Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian de Alwis2012-04-30 11:25:46 -0400
committerBrian de Alwis2012-04-30 11:25:46 -0400
commitfcb0517fb62a8d570aaca0352bb973988b681031 (patch)
treed21519d5bcf814617af10db5ed814d95619ca18b
parent7f0a539ddcaca209a036c47467cfe7a923ec48f2 (diff)
downloadorg.eclipse.e4.tools-fcb0517fb62a8d570aaca0352bb973988b681031.tar.gz
org.eclipse.e4.tools-fcb0517fb62a8d570aaca0352bb973988b681031.tar.xz
org.eclipse.e4.tools-fcb0517fb62a8d570aaca0352bb973988b681031.zip
Commit CSS Scratchpad (from Brian's EclipseCon talk): open using M1+M2+M3+F4 or Ctrl-Alt-Shift-F4v20120430-1525I20120430-2100I20120430-1600
-rw-r--r--bundles/org.eclipse.e4.tools.css.spy/src/org/eclipse/e4/tools/css/spy/CSSScratchPadWindow.java132
-rw-r--r--bundles/org.eclipse.e4.tools.css.spy/src/org/eclipse/e4/tools/css/spy/OpenScratchpadHandler.java17
-rw-r--r--bundles/org.eclipse.e4.tools.css.spy/src/org/eclipse/e4/tools/css/spy/SpyInstaller.java48
3 files changed, 182 insertions, 15 deletions
diff --git a/bundles/org.eclipse.e4.tools.css.spy/src/org/eclipse/e4/tools/css/spy/CSSScratchPadWindow.java b/bundles/org.eclipse.e4.tools.css.spy/src/org/eclipse/e4/tools/css/spy/CSSScratchPadWindow.java
new file mode 100644
index 00000000..810a9660
--- /dev/null
+++ b/bundles/org.eclipse.e4.tools.css.spy/src/org/eclipse/e4/tools/css/spy/CSSScratchPadWindow.java
@@ -0,0 +1,132 @@
+package org.eclipse.e4.tools.css.spy;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.e4.ui.css.core.dom.ExtendedDocumentCSS;
+import org.eclipse.e4.ui.css.core.engine.CSSEngine;
+import org.eclipse.e4.ui.css.swt.dom.WidgetElement;
+import org.eclipse.e4.ui.css.swt.internal.theme.ThemeEngine;
+import org.eclipse.e4.ui.css.swt.theme.IThemeEngine;
+import org.eclipse.jface.dialogs.Dialog;
+import org.eclipse.jface.dialogs.IDialogConstants;
+import org.eclipse.jface.layout.GridDataFactory;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.SashForm;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.Text;
+import org.w3c.css.sac.CSSParseException;
+import org.w3c.dom.stylesheets.StyleSheet;
+import org.w3c.dom.stylesheets.StyleSheetList;
+
+public class CSSScratchPadWindow extends Dialog {
+
+ private IThemeEngine themeEngine;
+
+ public CSSScratchPadWindow(Shell parentShell, IThemeEngine themeEngine) {
+ super(parentShell);
+ this.themeEngine = themeEngine;
+ setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE/* | SWT.PRIMARY_MODAL */);
+ // setShellStyle(SWT.DIALOG_TRIM | SWT.MAX | SWT.RESIZE
+ // | getDefaultOrientation());
+ }
+
+ protected void configureShell(Shell newShell) {
+ super.configureShell(newShell);
+ newShell.setText("CSS Scratchpad");
+ }
+
+ private static final int APPLY_ID = IDialogConstants.OK_ID + 100;
+
+ private Text cssText;
+ private Text exceptions;
+
+ @Override
+ protected Control createDialogArea(Composite parent) {
+ Composite outer = (Composite) super.createDialogArea(parent);
+
+ SashForm sashForm = new SashForm(outer, SWT.VERTICAL);
+
+ cssText = new Text(sashForm, SWT.BORDER | SWT.MULTI | SWT.WRAP
+ | SWT.V_SCROLL);
+
+ exceptions = new Text(sashForm, SWT.BORDER | SWT.MULTI | SWT.READ_ONLY);
+
+ GridDataFactory.fillDefaults().grab(true, true).applyTo(sashForm);
+ sashForm.setWeights(new int[] { 80, 20 });
+
+ return outer;
+ }
+
+ @Override
+ protected void createButtonsForButtonBar(Composite parent) {
+ createButton(parent, APPLY_ID, "Apply", true);
+ createButton(parent, IDialogConstants.OK_ID, "Close", false);
+ // createButton(parent, IDialogConstants.CANCEL_ID,
+ // IDialogConstants.CANCEL_LABEL, false);
+ }
+
+ /**
+ * Return the initial size of the dialog.
+ */
+ @Override
+ protected Point getInitialSize() {
+ return new Point(600, 500);
+ }
+
+ @Override
+ protected void buttonPressed(int buttonId) {
+ switch (buttonId) {
+ case APPLY_ID:
+ applyCSS();
+ break;
+ default:
+ super.buttonPressed(buttonId);
+ }
+ }
+
+ private void applyCSS() {
+ long start = System.nanoTime();
+ exceptions.setText("");
+ CSSEngine engine = WidgetElement.getEngine(getShell().getDisplay());
+ if (engine == null) {
+ exceptions.setText("No CSS Engine available");
+ }
+ if (themeEngine != null) {
+ // FIXME: expose this new protocol
+ ((ThemeEngine) themeEngine).resetCurrentTheme();
+ }
+ ExtendedDocumentCSS doc = (ExtendedDocumentCSS) engine
+ .getDocumentCSS();
+ List<StyleSheet> sheets = new ArrayList<StyleSheet>();
+ StyleSheetList list = doc.getStyleSheets();
+ for (int i = 0; i < list.getLength(); i++) {
+ sheets.add(list.item(i));
+ }
+
+ try {
+ Reader reader = new StringReader(cssText.getText());
+ sheets.add(0, engine.parseStyleSheet(reader));
+ doc.removeAllStyleSheets();
+ for (StyleSheet sheet : sheets) {
+ doc.addStyleSheet(sheet);
+ }
+ engine.reapply();
+
+ long nanoDiff = System.nanoTime() - start;
+ exceptions.setText("Total time: " + (nanoDiff / 1000000) + "ms");
+ } catch (CSSParseException e) {
+ exceptions.setText("Error: line " + e.getLineNumber() + " col "
+ + e.getColumnNumber() + ": " + e.getLocalizedMessage());
+ } catch (IOException e) {
+ exceptions.setText("Error: " + e.getLocalizedMessage());
+ }
+ }
+
+}
diff --git a/bundles/org.eclipse.e4.tools.css.spy/src/org/eclipse/e4/tools/css/spy/OpenScratchpadHandler.java b/bundles/org.eclipse.e4.tools.css.spy/src/org/eclipse/e4/tools/css/spy/OpenScratchpadHandler.java
new file mode 100644
index 00000000..b887ec07
--- /dev/null
+++ b/bundles/org.eclipse.e4.tools.css.spy/src/org/eclipse/e4/tools/css/spy/OpenScratchpadHandler.java
@@ -0,0 +1,17 @@
+package org.eclipse.e4.tools.css.spy;
+
+import javax.inject.Named;
+
+import org.eclipse.e4.core.di.annotations.Execute;
+import org.eclipse.e4.ui.css.swt.theme.IThemeEngine;
+import org.eclipse.e4.ui.services.IServiceConstants;
+import org.eclipse.swt.widgets.Shell;
+
+public class OpenScratchpadHandler {
+ @Execute
+ public void open(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell,
+ IThemeEngine themeEngine) {
+ CSSScratchPadWindow scratchpad = new CSSScratchPadWindow(shell, themeEngine);
+ scratchpad.open();
+ }
+}
diff --git a/bundles/org.eclipse.e4.tools.css.spy/src/org/eclipse/e4/tools/css/spy/SpyInstaller.java b/bundles/org.eclipse.e4.tools.css.spy/src/org/eclipse/e4/tools/css/spy/SpyInstaller.java
index c2bb4196..510ad1dc 100644
--- a/bundles/org.eclipse.e4.tools.css.spy/src/org/eclipse/e4/tools/css/spy/SpyInstaller.java
+++ b/bundles/org.eclipse.e4.tools.css.spy/src/org/eclipse/e4/tools/css/spy/SpyInstaller.java
@@ -26,48 +26,64 @@ public class SpyInstaller {
private static final String SPY_HANDLER_URI = "bundleclass://org.eclipse.e4.tools.css.spy/"
+ OpenSpyHandler.class.getName();
+ public static final String OPEN_SCRATCHPAD_COMMAND_ID = "org.eclipse.e4.css.OpenSctachpad";
+ private static final String SCRATCHPAD_HANDLER_ID = OpenScratchpadHandler.class
+ .getName();
+ private static final String SCRATCHPAD_HANDLER_URI = "bundleclass://org.eclipse.e4.tools.css.spy/"
+ + OpenScratchpadHandler.class.getName();
+
@Inject
protected MApplication app;
@Execute
public void execute() {
- MCommand cmd = installSpyCommand();
- installSpyHandler(cmd);
- installSpyBinding("org.eclipse.ui.contexts.dialogAndWindow", cmd, "M2+M3+F4");
+ MCommand openSpyCommand = installCommand("Open CSS Spy",
+ OPEN_SPY_COMMAND_ID);
+ installHandler(openSpyCommand, SPY_HANDLER_ID, SPY_HANDLER_URI);
+ installBinding("org.eclipse.ui.contexts.dialogAndWindow",
+ openSpyCommand, "M2+M3+F4");
+
+ MCommand openScratchpadCommand = installCommand("Open CSS Scratchpad",
+ OPEN_SCRATCHPAD_COMMAND_ID);
+ installHandler(openScratchpadCommand, SCRATCHPAD_HANDLER_ID, SCRATCHPAD_HANDLER_URI);
+ installBinding("org.eclipse.ui.contexts.dialogAndWindow",
+ openScratchpadCommand, "M1+M2+M3+F4");
}
- private MCommand installSpyCommand() {
+ private MCommand installCommand(String label, String commandId) {
for(MCommand cmd : app.getCommands()) {
- if(OPEN_SPY_COMMAND_ID.equals(cmd.getElementId())) {
- System.err.println("CSS Spy command already setup");
+ if (commandId.equals(cmd.getElementId())) {
+ System.err.println(cmd.getElementId()
+ + ": command already installed");
return cmd;
}
}
MCommand cmd = MCommandsFactory.INSTANCE.createCommand();
- cmd.setCommandName("Open CSS Spy");
- cmd.setElementId(OPEN_SPY_COMMAND_ID);
+ cmd.setCommandName(label);
+ cmd.setElementId(commandId);
app.getCommands().add(cmd);
return cmd;
}
- private MHandler installSpyHandler(MCommand cmd) {
+ private MHandler installHandler(MCommand cmd, String handlerId,
+ String handlerURI) {
for(MHandler hdlr : app.getHandlers()) {
- if(SPY_HANDLER_ID.equals(hdlr.getElementId())) {
- System.err.println("CSS Spy handler already setup");
+ if (handlerId.equals(hdlr.getElementId())) {
+ System.err.println(handlerId + ": handler already installed");
return hdlr;
}
}
MHandler hdlr = MCommandsFactory.INSTANCE.createHandler();
- hdlr.setElementId(SPY_HANDLER_ID);
- hdlr.setContributionURI(SPY_HANDLER_URI);
+ hdlr.setElementId(handlerId);
+ hdlr.setContributionURI(handlerURI);
hdlr.setCommand(cmd);
app.getHandlers().add(hdlr);
return hdlr;
}
- private void installSpyBinding(String bindingContextId, MCommand cmd,
+ private void installBinding(String bindingContextId, MCommand cmd,
String keySeq) {
// there is a one-to-one mapping between binding contexts and
// binding tables, though binding tables may not necessarily
@@ -76,7 +92,9 @@ public class SpyInstaller {
for(MBindingTable table : app.getBindingTables()) {
for(MKeyBinding binding : table.getBindings()) {
if(binding.getCommand() == cmd) {
- System.err.println("Spy binding already installed");
+ System.err.println(cmd.getElementId()
+ + ": binding already installed as "
+ + binding.getKeySequence());
return;
}
}

Back to the top