diff options
author | Brian de Alwis | 2014-02-13 15:42:10 +0000 |
---|---|---|
committer | Brian de Alwis | 2014-02-13 15:42:10 +0000 |
commit | 337ae1bf3996c51da598fb1fae72b7313d210c5b (patch) | |
tree | 7dde00d7d677474a020764f7b1a2960e58795096 | |
parent | 92fd5c2cf87d66ec3ed102280716880f25af0a3c (diff) | |
download | org.eclipse.e4.tools-337ae1bf3996c51da598fb1fae72b7313d210c5b.tar.gz org.eclipse.e4.tools-337ae1bf3996c51da598fb1fae72b7313d210c5b.tar.xz org.eclipse.e4.tools-337ae1bf3996c51da598fb1fae72b7313d210c5b.zip |
[CSS Spy] Add "Show CSS fragment" button to generate CSS snippet
for the currently selected element based on its current property
values.
2 files changed, 166 insertions, 2 deletions
diff --git a/bundles/org.eclipse.e4.tools.css.spy/src/org/eclipse/e4/tools/css/spy/CssSpyDialog.java b/bundles/org.eclipse.e4.tools.css.spy/src/org/eclipse/e4/tools/css/spy/CssSpyDialog.java index cb03dd6e..982596ad 100644 --- a/bundles/org.eclipse.e4.tools.css.spy/src/org/eclipse/e4/tools/css/spy/CssSpyDialog.java +++ b/bundles/org.eclipse.e4.tools.css.spy/src/org/eclipse/e4/tools/css/spy/CssSpyDialog.java @@ -14,6 +14,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Iterator; import java.util.LinkedList; import java.util.List; @@ -41,6 +42,7 @@ import org.eclipse.jface.viewers.EditingSupport; import org.eclipse.jface.viewers.FocusCellOwnerDrawHighlighter; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelectionChangedListener; +import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.SelectionChangedEvent; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.jface.viewers.TableViewer; @@ -87,6 +89,7 @@ import org.w3c.css.sac.CSSParseException; import org.w3c.css.sac.SelectorList; import org.w3c.dom.NodeList; import org.w3c.dom.css.CSSStyleDeclaration; +import org.w3c.dom.css.CSSValue; public class CssSpyDialog extends Dialog { /** @return the CSS element corresponding to the argument, or null if none */ @@ -135,6 +138,7 @@ public class CssSpyDialog extends Dialog { private List<Region> highlightRegions = new LinkedList<Region>(); private Text cssSearchBox; private Button showUnsetProperties; + private Button showCssFragment; protected ViewerFilter unsetPropertyFilter = new ViewerFilter() { @@ -648,6 +652,10 @@ public class CssSpyDialog extends Dialog { // / THE CSS PROPERTIES TABLE (again) showUnsetProperties = new Button(container, SWT.CHECK); showUnsetProperties.setText("Show unset properties"); + showCssFragment = new Button(container, SWT.PUSH); + showCssFragment.setText("Show CSS fragment"); + showCssFragment + .setToolTipText("Generates CSS rule block for the selected widget"); // and for balance new Label(container, SWT.NONE); @@ -685,6 +693,8 @@ public class CssSpyDialog extends Dialog { .addSelectionChangedListener(new ISelectionChangedListener() { public void selectionChanged(SelectionChangedEvent event) { updateForWidgetSelection(event.getSelection()); + showCssFragment.setEnabled(!event.getSelection() + .isEmpty()); } }); if (isLive()) { @@ -739,12 +749,133 @@ public class CssSpyDialog extends Dialog { } }); + showCssFragment.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + showCssFragment(); + } + + @Override + public void widgetDefaultSelected(SelectionEvent e) { + widgetSelected(e); + } + }); + update(); sashForm.setWeights(new int[] { 50, 50 }); widgetTreeViewer.getControl().setFocus(); return outer; } + protected void showCssFragment() { + if (!(widgetTreeViewer.getSelection() instanceof IStructuredSelection) + || widgetTreeViewer.getSelection().isEmpty()) { + return; + } + + StringBuilder sb = new StringBuilder(); + for (Object o : ((IStructuredSelection) widgetTreeViewer.getSelection()) + .toArray()) { + if (o instanceof Widget) { + if (sb.length() > 0) { + sb.append('\n'); + } + addCssFragment((Widget) o, sb); + } + } + TextPopupDialog tpd = new TextPopupDialog(widgetTreeViewer.getControl() + .getShell(), "CSS", sb.toString(), true, + "Escape to dismiss"); + tpd.open(); + } + + private void addCssFragment(Widget w, StringBuilder sb) { + CSSStylableElement element = getCSSElement(w); + if (element == null) { + return; + } + + sb.append(element.getLocalName()); + if (element.getCSSId() != null) { + sb.append("#").append(element.getCSSId()); + } + sb.append(" {"); + + CSSEngine engine = getCSSEngine(element); + // we first check the viewCSS and then the property values + CSSStyleDeclaration decl = engine.getViewCSS().getComputedStyle( + element, null); + + List<String> propertyNames = new ArrayList<String>( + engine.getCSSProperties(element)); + Collections.sort(propertyNames); + + int count = 0; + + // First list the generated properties + for (Iterator<String> iter = propertyNames.iterator(); iter.hasNext();) { + String propertyName = iter.next(); + String genValue = trim(engine.retrieveCSSProperty(element, + propertyName, "")); + String declValue = null; + + if (genValue == null) { + continue; + } + + if (decl != null) { + CSSValue cssValue = decl.getPropertyCSSValue(propertyName); + if (cssValue != null) { + declValue = trim(cssValue.getCssText()); + } + } + if (count == 0) { + sb.append("\n /* actual values */"); + } + sb.append("\n ").append(propertyName).append(": ") + .append(genValue).append(";"); + if (declValue != null) { + sb.append("\t/* declared in CSS: ").append(declValue) + .append(" */"); + } + count++; + iter.remove(); // remove so we don't re-report below + } + + // then list any declared properties; generated properties already + // removed + if (decl != null) { + int declCount = 0; + for (String propertyName : propertyNames) { + String declValue = null; + CSSValue cssValue = decl.getPropertyCSSValue(propertyName); + if (cssValue != null) { + declValue = trim(cssValue.getCssText()); + } + if (declValue == null) { + continue; + } + if (declCount == 0) { + sb.append("\n\n /* declared in CSS rules */"); + } + sb.append("\n ").append(propertyName).append(": ") + .append(declValue).append(";"); + count++; + declCount++; + } + } + sb.append(count > 0 ? "\n}" : "}"); + } + + /** Trim the string; return null if empty */ + private String trim(String s) { + if (s == null) { + return null; + } + s = s.trim(); + return s.length() > 0 ? s : null; + } + protected void performCSSSearch(IProgressMonitor progress) { List<Widget> widgets = new ArrayList<Widget>(); performCSSSearch(progress, cssSearchBox.getText(), widgets); @@ -829,8 +960,6 @@ public class CssSpyDialog extends Dialog { protected void createButtonsForButtonBar(Composite parent) { createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true); - // createButton(parent, IDialogConstants.CANCEL_ID, - // IDialogConstants.CANCEL_LABEL, false); } /** diff --git a/bundles/org.eclipse.e4.tools.css.spy/src/org/eclipse/e4/tools/css/spy/TextPopupDialog.java b/bundles/org.eclipse.e4.tools.css.spy/src/org/eclipse/e4/tools/css/spy/TextPopupDialog.java new file mode 100644 index 00000000..7f693e7f --- /dev/null +++ b/bundles/org.eclipse.e4.tools.css.spy/src/org/eclipse/e4/tools/css/spy/TextPopupDialog.java @@ -0,0 +1,35 @@ +package org.eclipse.e4.tools.css.spy; + +import org.eclipse.jface.dialogs.PopupDialog; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Text; + +public class TextPopupDialog extends PopupDialog { + + private String content; + private boolean contentEditable; + + public TextPopupDialog(Shell shell, String title, String content, boolean editable, + String infoText) { + super(shell, SWT.RESIZE | SWT.ON_TOP, true, true, true, false, false, title, infoText); + this.content = content; + this.contentEditable = editable; + } + + @Override + protected Control createDialogArea(Composite parent) { + Composite container = (Composite) super.createDialogArea(parent); + int styleBits = SWT.MULTI | SWT.WRAP | SWT.H_SCROLL | SWT.V_SCROLL; + if(!contentEditable) { + styleBits |= SWT.READ_ONLY; + } + Text textWidget = new Text(container, styleBits); + textWidget.setText(content); + return container; + } + + +} |