diff options
author | Thomas Schindl | 2011-01-28 21:01:57 +0000 |
---|---|---|
committer | Thomas Schindl | 2011-01-28 21:01:57 +0000 |
commit | 74b86ea845557f9e1fc5d9994188af3d69f75cb4 (patch) | |
tree | 7045d60f8b70cc3732b7d8c3ff6fe9c2c4cd42ac | |
parent | c2063c4efa9cba284ab8cb02c9588932e2cdb779 (diff) | |
download | org.eclipse.e4.tools-74b86ea845557f9e1fc5d9994188af3d69f75cb4.tar.gz org.eclipse.e4.tools-74b86ea845557f9e1fc5d9994188af3d69f75cb4.tar.xz org.eclipse.e4.tools-74b86ea845557f9e1fc5d9994188af3d69f75cb4.zip |
Bug 316371 - [ModelTooling] ModelEditor should be multipart editors which allows to edit xml or via UI
19 files changed, 689 insertions, 2 deletions
diff --git a/bundles/org.eclipse.e4.tools.emf.ui/META-INF/MANIFEST.MF b/bundles/org.eclipse.e4.tools.emf.ui/META-INF/MANIFEST.MF index 252f2ad4..8af951f2 100644 --- a/bundles/org.eclipse.e4.tools.emf.ui/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.e4.tools.emf.ui/META-INF/MANIFEST.MF @@ -28,7 +28,8 @@ Require-Bundle: org.eclipse.core.databinding;bundle-version="1.3.0", org.eclipse.e4.ui.di;bundle-version="0.9.0", javax.annotation;bundle-version="1.0.0", org.eclipse.e4.tools.services;bundle-version="1.0.0", - org.eclipse.e4.core.di.extensions;bundle-version="0.9.0" + org.eclipse.e4.core.di.extensions;bundle-version="0.9.0", + org.eclipse.jface.text;bundle-version="3.7.0" Bundle-ActivationPolicy: lazy Import-Package: javax.inject;version="1.0.0", org.eclipse.core.runtime.jobs, diff --git a/bundles/org.eclipse.e4.tools.emf.ui/OSGI-INF/images.properties b/bundles/org.eclipse.e4.tools.emf.ui/OSGI-INF/images.properties index f38970e7..769230be 100644 --- a/bundles/org.eclipse.e4.tools.emf.ui/OSGI-INF/images.properties +++ b/bundles/org.eclipse.e4.tools.emf.ui/OSGI-INF/images.properties @@ -81,6 +81,8 @@ IMG_org.eclipse.e4.tools.emf.ui.obj16.headermenu=/icons/full/obj16/headermenu.pn IMG_org.eclipse.e4.tools.emf.ui.obj16.table_add=/icons/full/obj16/table_add.png IMG_org.eclipse.e4.tools.emf.ui.obj16.table_delete=/icons/full/obj16/table_delete.png IMG_org.eclipse.e4.tools.emf.ui.obj16.zoom=/icons/full/obj16/zoom.png +IMG_org.eclipse.e4.tools.emf.ui.obj16.application_form=/icons/full/obj16/application_form.png +IMG_org.eclipse.e4.tools.emf.ui.obj16.chart_organisation=/icons/full/obj16/chart_organisation.png IMG_org.eclipse.e4.tools.emf.ui.wizban.fieldrefact_wiz=/icons/full/wizban/fieldrefact_wiz.png IMG_org.eclipse.e4.tools.emf.ui.wizban.import_wiz=/icons/full/wizban/import_wiz.png diff --git a/bundles/org.eclipse.e4.tools.emf.ui/icons/full/obj16/application_form.png b/bundles/org.eclipse.e4.tools.emf.ui/icons/full/obj16/application_form.png Binary files differnew file mode 100644 index 00000000..807b862c --- /dev/null +++ b/bundles/org.eclipse.e4.tools.emf.ui/icons/full/obj16/application_form.png diff --git a/bundles/org.eclipse.e4.tools.emf.ui/icons/full/obj16/chart_organisation.png b/bundles/org.eclipse.e4.tools.emf.ui/icons/full/obj16/chart_organisation.png Binary files differnew file mode 100644 index 00000000..c32d25c1 --- /dev/null +++ b/bundles/org.eclipse.e4.tools.emf.ui/icons/full/obj16/chart_organisation.png diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/Messages.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/Messages.java index 0253df7a..63be298c 100644 --- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/Messages.java +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/Messages.java @@ -580,4 +580,6 @@ public class Messages { public String AreaIconDialogEditor_DialogMessage; public String ModelEditor_Delete; + public String ModelEditor_Form; + public String ModelEditor_XMI; }
\ No newline at end of file diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/Messages.properties b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/Messages.properties index 163b7d2c..59e04fdb 100644 --- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/Messages.properties +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/Messages.properties @@ -578,4 +578,6 @@ AreaIconDialogEditor_ShellTitle=Area Icon Search AreaIconDialogEditor_DialogTitle=Area Icon Search AreaIconDialogEditor_DialogMessage=Search for GIF, PNG and JPG icons in the current project -ModelEditor_Delete=Remove
\ No newline at end of file +ModelEditor_Delete=Remove +ModelEditor_Form=Form +ModelEditor_XMI=XMI
\ No newline at end of file diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/ResourceProvider.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/ResourceProvider.java index e1ca66fd..aec4dbbb 100644 --- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/ResourceProvider.java +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/ResourceProvider.java @@ -88,6 +88,8 @@ public class ResourceProvider extends AbstractBundleResourceProvider { public static final String IMG_Obj16_table_add = "IMG_org.eclipse.e4.tools.emf.ui.obj16.table_add"; //$NON-NLS-1$ public static final String IMG_Obj16_table_delete = "IMG_org.eclipse.e4.tools.emf.ui.obj16.table_delete"; //$NON-NLS-1$ public static final String IMG_Obj16_zoom = "IMG_org.eclipse.e4.tools.emf.ui.obj16.zoom"; //$NON-NLS-1$ + public static final String IMG_Obj16_application_form = "IMG_org.eclipse.e4.tools.emf.ui.obj16.application_form"; //$NON-NLS-1$ + public static final String IMG_Obj16_chart_organisation = "IMG_org.eclipse.e4.tools.emf.ui.obj16.chart_organisation"; //$NON-NLS-1$ public static final String IMG_Wizban16_fieldrefact_wiz = "IMG_org.eclipse.e4.tools.emf.ui.wizban.fieldrefact_wiz"; //$NON-NLS-1$ public static final String IMG_Wizban16_import_wiz = "IMG_org.eclipse.e4.tools.emf.ui.wizban.import_wiz"; //$NON-NLS-1$ diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/ModelEditor.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/ModelEditor.java index 03348715..c11030d0 100644 --- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/ModelEditor.java +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/ModelEditor.java @@ -114,6 +114,10 @@ import org.eclipse.e4.tools.emf.ui.internal.common.component.virtual.VWindowCont import org.eclipse.e4.tools.emf.ui.internal.common.component.virtual.VWindowEditor; import org.eclipse.e4.tools.emf.ui.internal.common.component.virtual.VWindowSharedElementsEditor; import org.eclipse.e4.tools.emf.ui.internal.common.component.virtual.VWindowTrimEditor; +import org.eclipse.e4.tools.emf.ui.internal.common.xml.ColorManager; +import org.eclipse.e4.tools.emf.ui.internal.common.xml.EMFDocument; +import org.eclipse.e4.tools.emf.ui.internal.common.xml.XMLConfiguration; +import org.eclipse.e4.tools.emf.ui.internal.common.xml.XMLPartitionScanner; import org.eclipse.e4.tools.services.IClipboardService; import org.eclipse.e4.tools.services.IClipboardService.Handler; import org.eclipse.e4.tools.services.IResourcePool; @@ -154,6 +158,11 @@ import org.eclipse.jface.action.MenuManager; import org.eclipse.jface.databinding.viewers.ObservableListTreeContentProvider; import org.eclipse.jface.databinding.viewers.TreeStructureAdvisor; import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IDocumentPartitioner; +import org.eclipse.jface.text.rules.FastPartitioner; +import org.eclipse.jface.text.source.SourceViewer; +import org.eclipse.jface.text.source.VerticalRuler; import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.SelectionChangedEvent; @@ -162,6 +171,8 @@ import org.eclipse.jface.viewers.TreeViewer; import org.eclipse.jface.viewers.Viewer; import org.eclipse.jface.viewers.ViewerDropAdapter; import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.CTabFolder; +import org.eclipse.swt.custom.CTabItem; import org.eclipse.swt.custom.SashForm; import org.eclipse.swt.custom.ScrolledComposite; import org.eclipse.swt.dnd.Clipboard; @@ -172,11 +183,14 @@ import org.eclipse.swt.dnd.Transfer; import org.eclipse.swt.dnd.TransferData; import org.eclipse.swt.events.ControlAdapter; import org.eclipse.swt.events.ControlEvent; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.TreeItem; @@ -204,6 +218,8 @@ public class ModelEditor { public static final String VIRTUAL_MENUELEMENTS = ModelEditor.class.getName() + ".VIRTUAL_MENUELEMENTS"; //$NON-NLS-1$ public static final String VIRTUAL_ROOT_CONTEXTS = ModelEditor.class.getName() + ".VIRTUAL_ROOT_CONTEXTS"; //$NON-NLS-1$ + private static final int VERTICAL_RULER_WIDTH = 12; + private Map<EClass, AbstractComponentEditor> editorMap = new HashMap<EClass, AbstractComponentEditor>(); private Map<String, AbstractComponentEditor> virtualEditors = new HashMap<String, AbstractComponentEditor>(); private List<FeaturePath> labelFeaturePaths = new ArrayList<FeaturePath>(); @@ -242,6 +258,8 @@ public class ModelEditor { private final IResourcePool resourcePool; + private EMFDocument emfDocumentProvider; + public ModelEditor(Composite composite, IEclipseContext context, IModelResource modelProvider, IProject project, final IResourcePool resourcePool) { this.resourcePool = resourcePool; this.modelProvider = modelProvider; @@ -272,6 +290,47 @@ public class ModelEditor { fragment = modelProvider.getRoot().get(0) instanceof MModelFragments; + final CTabFolder folder = new CTabFolder(composite, SWT.BOTTOM); + CTabItem item = new CTabItem(folder, SWT.NONE); + item.setText(messages.ModelEditor_Form); + item.setControl(createFormTab(folder)); + item.setImage(resourcePool.getImageUnchecked(ResourceProvider.IMG_Obj16_application_form)); + + emfDocumentProvider = new EMFDocument(modelProvider); + + item = new CTabItem(folder, SWT.NONE); + item.setText(messages.ModelEditor_XMI); + item.setControl(createXMITab(folder)); + item.setImage(resourcePool.getImageUnchecked(ResourceProvider.IMG_Obj16_chart_organisation)); + folder.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + if (folder.getSelectionIndex() == 1) { + emfDocumentProvider.updateFromEMF(); + } + } + }); + + folder.setSelection(0); + } + + private Control createXMITab(Composite composite) { + VerticalRuler verticalRuler = new VerticalRuler(VERTICAL_RULER_WIDTH); + ColorManager colorManager = new ColorManager(); + int styles = SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION; + SourceViewer viewer = new SourceViewer(composite, verticalRuler, styles); + viewer.configure(new XMLConfiguration(colorManager)); + viewer.setEditable(false); + IDocument document = emfDocumentProvider.getDocument(); + IDocumentPartitioner partitioner = new FastPartitioner(new XMLPartitionScanner(), new String[] { XMLPartitionScanner.XML_TAG, XMLPartitionScanner.XML_COMMENT }); + partitioner.connect(document); + document.setDocumentPartitioner(partitioner); + viewer.setDocument(document); + + return viewer.getControl(); + } + + private Composite createFormTab(Composite composite) { SashForm form = new SashForm(composite, SWT.HORIZONTAL); form.setBackground(form.getDisplay().getSystemColor(SWT.COLOR_WHITE)); @@ -429,6 +488,8 @@ public class ModelEditor { }); viewer.getControl().setMenu(mgr.createContextMenu(viewer.getControl())); viewer.setSelection(new StructuredSelection(modelProvider.getRoot())); + + return form; } public IExtensionLookup getExtensionLookup() { diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/ColorManager.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/ColorManager.java new file mode 100644 index 00000000..4a3a225d --- /dev/null +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/ColorManager.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2010 BestSolution.at and others. + * 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: + * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation + ******************************************************************************/ +package org.eclipse.e4.tools.emf.ui.internal.common.xml; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.RGB; +import org.eclipse.swt.widgets.Display; + +public class ColorManager { + + protected Map<RGB, Color> fColorTable = new HashMap<RGB, Color>(10); + + public void dispose() { + Iterator<Color> e = fColorTable.values().iterator(); + while (e.hasNext()) + ((Color) e.next()).dispose(); + } + + public Color getColor(RGB rgb) { + Color color = (Color) fColorTable.get(rgb); + if (color == null) { + color = new Color(Display.getCurrent(), rgb); + fColorTable.put(rgb, color); + } + return color; + } +} diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/EMFDocument.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/EMFDocument.java new file mode 100644 index 00000000..47325508 --- /dev/null +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/EMFDocument.java @@ -0,0 +1,41 @@ +package org.eclipse.e4.tools.emf.ui.internal.common.xml; + +import java.io.IOException; +import java.io.StringWriter; +import org.eclipse.e4.tools.emf.ui.common.IModelResource; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl; +import org.eclipse.jface.text.Document; + +public class EMFDocument { + private IModelResource modelResource; + private Document document; + + public EMFDocument(IModelResource modelResource) { + this.modelResource = modelResource; + this.document = new Document(); + updateFromEMF(); + } + + public void updateFromEMF() { + this.document.set(toXMI((EObject) modelResource.getRoot().get(0))); + } + + public Document getDocument() { + return document; + } + + private String toXMI(EObject root) { + XMIResourceImpl resource = new XMIResourceImpl(); + resource.getContents().add(EcoreUtil.copy(root)); + StringWriter writer = new StringWriter(); + try { + resource.save(writer, null); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return writer.toString(); + } +} diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/IXMLColorConstants.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/IXMLColorConstants.java new file mode 100644 index 00000000..0c7a2a58 --- /dev/null +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/IXMLColorConstants.java @@ -0,0 +1,21 @@ +/******************************************************************************* + * Copyright (c) 2010 BestSolution.at and others. + * 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: + * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation + ******************************************************************************/ +package org.eclipse.e4.tools.emf.ui.internal.common.xml; + +import org.eclipse.swt.graphics.RGB; + +public interface IXMLColorConstants { + RGB XML_COMMENT = new RGB(128, 0, 0); + RGB PROC_INSTR = new RGB(128, 128, 128); + RGB STRING = new RGB(0, 128, 0); + RGB DEFAULT = new RGB(0, 0, 0); + RGB TAG = new RGB(0, 0, 128); +} diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/NonRuleBasedDamagerRepairer.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/NonRuleBasedDamagerRepairer.java new file mode 100644 index 00000000..627ce6af --- /dev/null +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/NonRuleBasedDamagerRepairer.java @@ -0,0 +1,148 @@ +/******************************************************************************* + * Copyright (c) 2010 BestSolution.at and others. + * 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: + * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation + ******************************************************************************/ +package org.eclipse.e4.tools.emf.ui.internal.common.xml; + +import org.eclipse.core.runtime.Assert; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.DocumentEvent; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.ITypedRegion; +import org.eclipse.jface.text.Region; +import org.eclipse.jface.text.TextAttribute; +import org.eclipse.jface.text.TextPresentation; +import org.eclipse.jface.text.presentation.IPresentationDamager; +import org.eclipse.jface.text.presentation.IPresentationRepairer; +import org.eclipse.swt.custom.StyleRange; + +public class NonRuleBasedDamagerRepairer + implements IPresentationDamager, IPresentationRepairer { + + /** The document this object works on */ + protected IDocument fDocument; + /** The default text attribute if non is returned as data by the current token */ + protected TextAttribute fDefaultTextAttribute; + + /** + * Constructor for NonRuleBasedDamagerRepairer. + */ + public NonRuleBasedDamagerRepairer(TextAttribute defaultTextAttribute) { + Assert.isNotNull(defaultTextAttribute); + + fDefaultTextAttribute = defaultTextAttribute; + } + + /** + * @see IPresentationRepairer#setDocument(IDocument) + */ + public void setDocument(IDocument document) { + fDocument = document; + } + + /** + * Returns the end offset of the line that contains the specified offset or + * if the offset is inside a line delimiter, the end offset of the next line. + * + * @param offset the offset whose line end offset must be computed + * @return the line end offset for the given offset + * @exception BadLocationException if offset is invalid in the current document + */ + protected int endOfLineOf(int offset) throws BadLocationException { + + IRegion info = fDocument.getLineInformationOfOffset(offset); + if (offset <= info.getOffset() + info.getLength()) + return info.getOffset() + info.getLength(); + + int line = fDocument.getLineOfOffset(offset); + try { + info = fDocument.getLineInformation(line + 1); + return info.getOffset() + info.getLength(); + } catch (BadLocationException x) { + return fDocument.getLength(); + } + } + + /** + * @see IPresentationDamager#getDamageRegion(ITypedRegion, DocumentEvent, boolean) + */ + public IRegion getDamageRegion( + ITypedRegion partition, + DocumentEvent event, + boolean documentPartitioningChanged) { + if (!documentPartitioningChanged) { + try { + + IRegion info = + fDocument.getLineInformationOfOffset(event.getOffset()); + int start = Math.max(partition.getOffset(), info.getOffset()); + + int end = + event.getOffset() + + (event.getText() == null + ? event.getLength() + : event.getText().length()); + + if (info.getOffset() <= end + && end <= info.getOffset() + info.getLength()) { + // optimize the case of the same line + end = info.getOffset() + info.getLength(); + } else + end = endOfLineOf(end); + + end = + Math.min( + partition.getOffset() + partition.getLength(), + end); + return new Region(start, end - start); + + } catch (BadLocationException x) { + } + } + + return partition; + } + + /** + * @see IPresentationRepairer#createPresentation(TextPresentation, ITypedRegion) + */ + public void createPresentation( + TextPresentation presentation, + ITypedRegion region) { + addRange( + presentation, + region.getOffset(), + region.getLength(), + fDefaultTextAttribute); + } + + /** + * Adds style information to the given text presentation. + * + * @param presentation the text presentation to be extended + * @param offset the offset of the range to be styled + * @param length the length of the range to be styled + * @param attr the attribute describing the style of the range to be styled + */ + protected void addRange( + TextPresentation presentation, + int offset, + int length, + TextAttribute attr) { + if (attr != null) + presentation.addStyleRange( + new StyleRange( + offset, + length, + attr.getForeground(), + attr.getBackground(), + attr.getStyle())); + } +}
\ No newline at end of file diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/TagRule.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/TagRule.java new file mode 100644 index 00000000..85719e91 --- /dev/null +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/TagRule.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * Copyright (c) 2010 BestSolution.at and others. + * 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: + * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation + ******************************************************************************/ +package org.eclipse.e4.tools.emf.ui.internal.common.xml; + +import org.eclipse.jface.text.rules.ICharacterScanner; +import org.eclipse.jface.text.rules.IToken; +import org.eclipse.jface.text.rules.MultiLineRule; + +public class TagRule extends MultiLineRule { + + public TagRule(IToken token) { + super("<", ">", token); //$NON-NLS-1$//$NON-NLS-2$ + } + + protected boolean sequenceDetected(ICharacterScanner scanner, char[] sequence, boolean eofAllowed) { + int c = scanner.read(); + if (sequence[0] == '<') { + if (c == '?') { + // processing instruction - abort + scanner.unread(); + return false; + } + if (c == '!') { + scanner.unread(); + // comment - abort + return false; + } + } else if (sequence[0] == '>') { + scanner.unread(); + } + return super.sequenceDetected(scanner, sequence, eofAllowed); + } +} diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/XMLConfiguration.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/XMLConfiguration.java new file mode 100644 index 00000000..42080119 --- /dev/null +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/XMLConfiguration.java @@ -0,0 +1,77 @@ +/******************************************************************************* + * Copyright (c) 2010 BestSolution.at and others. + * 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: + * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation + ******************************************************************************/ +package org.eclipse.e4.tools.emf.ui.internal.common.xml; + +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.ITextDoubleClickStrategy; +import org.eclipse.jface.text.TextAttribute; +import org.eclipse.jface.text.presentation.IPresentationReconciler; +import org.eclipse.jface.text.presentation.PresentationReconciler; +import org.eclipse.jface.text.rules.DefaultDamagerRepairer; +import org.eclipse.jface.text.rules.Token; +import org.eclipse.jface.text.source.ISourceViewer; +import org.eclipse.jface.text.source.SourceViewerConfiguration; + +public class XMLConfiguration extends SourceViewerConfiguration { + private XMLDoubleClickStrategy doubleClickStrategy; + private XMLTagScanner tagScanner; + private XMLScanner scanner; + private ColorManager colorManager; + + public XMLConfiguration(ColorManager colorManager) { + this.colorManager = colorManager; + } + + public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) { + return new String[] { IDocument.DEFAULT_CONTENT_TYPE, XMLPartitionScanner.XML_COMMENT, XMLPartitionScanner.XML_TAG }; + } + + public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) { + if (doubleClickStrategy == null) + doubleClickStrategy = new XMLDoubleClickStrategy(); + return doubleClickStrategy; + } + + protected XMLScanner getXMLScanner() { + if (scanner == null) { + scanner = new XMLScanner(colorManager); + scanner.setDefaultReturnToken(new Token(new TextAttribute(colorManager.getColor(IXMLColorConstants.DEFAULT)))); + } + return scanner; + } + + protected XMLTagScanner getXMLTagScanner() { + if (tagScanner == null) { + tagScanner = new XMLTagScanner(colorManager); + tagScanner.setDefaultReturnToken(new Token(new TextAttribute(colorManager.getColor(IXMLColorConstants.TAG)))); + } + return tagScanner; + } + + public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) { + PresentationReconciler reconciler = new PresentationReconciler(); + + DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getXMLTagScanner()); + reconciler.setDamager(dr, XMLPartitionScanner.XML_TAG); + reconciler.setRepairer(dr, XMLPartitionScanner.XML_TAG); + + dr = new DefaultDamagerRepairer(getXMLScanner()); + reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE); + reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE); + + NonRuleBasedDamagerRepairer ndr = new NonRuleBasedDamagerRepairer(new TextAttribute(colorManager.getColor(IXMLColorConstants.XML_COMMENT))); + reconciler.setDamager(ndr, XMLPartitionScanner.XML_COMMENT); + reconciler.setRepairer(ndr, XMLPartitionScanner.XML_COMMENT); + + return reconciler; + } + +}
\ No newline at end of file diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/XMLDoubleClickStrategy.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/XMLDoubleClickStrategy.java new file mode 100644 index 00000000..492d51bb --- /dev/null +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/XMLDoubleClickStrategy.java @@ -0,0 +1,125 @@ +/******************************************************************************* + * Copyright (c) 2010 BestSolution.at and others. + * 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: + * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation + ******************************************************************************/ +package org.eclipse.e4.tools.emf.ui.internal.common.xml; + +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.ITextDoubleClickStrategy; +import org.eclipse.jface.text.ITextViewer; + +public class XMLDoubleClickStrategy implements ITextDoubleClickStrategy { + protected ITextViewer fText; + + public void doubleClicked(ITextViewer part) { + int pos = part.getSelectedRange().x; + + if (pos < 0) + return; + + fText = part; + + if (!selectComment(pos)) { + selectWord(pos); + } + } + protected boolean selectComment(int caretPos) { + IDocument doc = fText.getDocument(); + int startPos, endPos; + + try { + int pos = caretPos; + char c = ' '; + + while (pos >= 0) { + c = doc.getChar(pos); + if (c == '\\') { + pos -= 2; + continue; + } + if (c == Character.LINE_SEPARATOR || c == '\"') + break; + --pos; + } + + if (c != '\"') + return false; + + startPos = pos; + + pos = caretPos; + int length = doc.getLength(); + c = ' '; + + while (pos < length) { + c = doc.getChar(pos); + if (c == Character.LINE_SEPARATOR || c == '\"') + break; + ++pos; + } + if (c != '\"') + return false; + + endPos = pos; + + int offset = startPos + 1; + int len = endPos - offset; + fText.setSelectedRange(offset, len); + return true; + } catch (BadLocationException x) { + } + + return false; + } + protected boolean selectWord(int caretPos) { + + IDocument doc = fText.getDocument(); + int startPos, endPos; + + try { + + int pos = caretPos; + char c; + + while (pos >= 0) { + c = doc.getChar(pos); + if (!Character.isJavaIdentifierPart(c)) + break; + --pos; + } + + startPos = pos; + + pos = caretPos; + int length = doc.getLength(); + + while (pos < length) { + c = doc.getChar(pos); + if (!Character.isJavaIdentifierPart(c)) + break; + ++pos; + } + + endPos = pos; + selectRange(startPos, endPos); + return true; + + } catch (BadLocationException x) { + } + + return false; + } + + private void selectRange(int startPos, int stopPos) { + int offset = startPos + 1; + int length = stopPos - offset; + fText.setSelectedRange(offset, length); + } +}
\ No newline at end of file diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/XMLPartitionScanner.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/XMLPartitionScanner.java new file mode 100644 index 00000000..04ea48a9 --- /dev/null +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/XMLPartitionScanner.java @@ -0,0 +1,35 @@ +/******************************************************************************* + * Copyright (c) 2010 BestSolution.at and others. + * 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: + * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation + ******************************************************************************/ +package org.eclipse.e4.tools.emf.ui.internal.common.xml; + +import org.eclipse.jface.text.rules.IPredicateRule; +import org.eclipse.jface.text.rules.IToken; +import org.eclipse.jface.text.rules.MultiLineRule; +import org.eclipse.jface.text.rules.RuleBasedPartitionScanner; +import org.eclipse.jface.text.rules.Token; + +public class XMLPartitionScanner extends RuleBasedPartitionScanner { + public final static String XML_COMMENT = "__xml_comment"; //$NON-NLS-1$ + public final static String XML_TAG = "__xml_tag"; //$NON-NLS-1$ + + public XMLPartitionScanner() { + + IToken xmlComment = new Token(XML_COMMENT); + IToken tag = new Token(XML_TAG); + + IPredicateRule[] rules = new IPredicateRule[2]; + + rules[0] = new MultiLineRule("<!--", "-->", xmlComment); //$NON-NLS-1$//$NON-NLS-2$ + rules[1] = new TagRule(tag); + + setPredicateRules(rules); + } +} diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/XMLScanner.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/XMLScanner.java new file mode 100644 index 00000000..7eaafe06 --- /dev/null +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/XMLScanner.java @@ -0,0 +1,34 @@ +/******************************************************************************* + * Copyright (c) 2010 BestSolution.at and others. + * 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: + * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation + ******************************************************************************/ +package org.eclipse.e4.tools.emf.ui.internal.common.xml; + +import org.eclipse.jface.text.TextAttribute; +import org.eclipse.jface.text.rules.IRule; +import org.eclipse.jface.text.rules.IToken; +import org.eclipse.jface.text.rules.RuleBasedScanner; +import org.eclipse.jface.text.rules.SingleLineRule; +import org.eclipse.jface.text.rules.Token; +import org.eclipse.jface.text.rules.WhitespaceRule; + +public class XMLScanner extends RuleBasedScanner { + + public XMLScanner(ColorManager manager) { + IToken procInstr = new Token(new TextAttribute(manager.getColor(IXMLColorConstants.PROC_INSTR))); + + IRule[] rules = new IRule[2]; + // Add rule for processing instructions + rules[0] = new SingleLineRule("<?", "?>", procInstr); //$NON-NLS-1$//$NON-NLS-2$ + // Add generic whitespace rule. + rules[1] = new WhitespaceRule(new XMLWhitespaceDetector()); + + setRules(rules); + } +} diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/XMLTagScanner.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/XMLTagScanner.java new file mode 100644 index 00000000..47ccf71b --- /dev/null +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/XMLTagScanner.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * Copyright (c) 2010 BestSolution.at and others. + * 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: + * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation + ******************************************************************************/ +package org.eclipse.e4.tools.emf.ui.internal.common.xml; + +import org.eclipse.jface.text.TextAttribute; +import org.eclipse.jface.text.rules.IRule; +import org.eclipse.jface.text.rules.IToken; +import org.eclipse.jface.text.rules.RuleBasedScanner; +import org.eclipse.jface.text.rules.SingleLineRule; +import org.eclipse.jface.text.rules.Token; +import org.eclipse.jface.text.rules.WhitespaceRule; + +public class XMLTagScanner extends RuleBasedScanner { + + public XMLTagScanner(ColorManager manager) { + IToken string = new Token(new TextAttribute(manager.getColor(IXMLColorConstants.STRING))); + + IRule[] rules = new IRule[3]; + + // Add rule for double quotes + rules[0] = new SingleLineRule("\"", "\"", string, '\\'); //$NON-NLS-1$//$NON-NLS-2$ + // Add a rule for single quotes + rules[1] = new SingleLineRule("'", "'", string, '\\'); //$NON-NLS-1$ //$NON-NLS-2$ + // Add generic whitespace rule. + rules[2] = new WhitespaceRule(new XMLWhitespaceDetector()); + + setRules(rules); + } +} diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/XMLWhitespaceDetector.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/XMLWhitespaceDetector.java new file mode 100644 index 00000000..3ded701a --- /dev/null +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/XMLWhitespaceDetector.java @@ -0,0 +1,20 @@ +/******************************************************************************* + * Copyright (c) 2010 BestSolution.at and others. + * 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: + * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation + ******************************************************************************/ +package org.eclipse.e4.tools.emf.ui.internal.common.xml; + +import org.eclipse.jface.text.rules.IWhitespaceDetector; + +public class XMLWhitespaceDetector implements IWhitespaceDetector { + + public boolean isWhitespace(char c) { + return (c == ' ' || c == '\t' || c == '\n' || c == '\r'); + } +} |