diff options
author | Steven Spungin | 2014-06-12 01:48:25 +0000 |
---|---|---|
committer | Steven Spungin | 2014-06-12 02:16:41 +0000 |
commit | 66a6b62c203854a8ab53cc3bcbfaea29a10a0840 (patch) | |
tree | bec5ab9e1d33efb6e34a83a25739b92c73b90166 | |
parent | 40f1fb02fbd578e6d50c14630a569984e6bd481e (diff) | |
download | org.eclipse.e4.tools-66a6b62c203854a8ab53cc3bcbfaea29a10a0840.tar.gz org.eclipse.e4.tools-66a6b62c203854a8ab53cc3bcbfaea29a10a0840.tar.xz org.eclipse.e4.tools-66a6b62c203854a8ab53cc3bcbfaea29a10a0840.zip |
Bug 391089 - Add possibility to search and replace in Application Model
editor
Bug 437236 - [model editor] xmi tab does not update when changes are
made to tree tab
Other changes:
Refactored XMI tab to class, and used injection instead of passing in a
gazillion parameters.
Change-Id: Ia35bb03f07e29906316e3f9ed5a2761e3b456860
Signed-off-by: Steven Spungin <steven@spungin.tv>
3 files changed, 228 insertions, 78 deletions
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 121cbac8..46db1ba6 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 @@ -10,7 +10,7 @@ * Wim Jongman <wim.jongman@remainsoftware.com> - Maintenance * Marco Descher <marco@descher.at> - Bug395982, 426653, 422465 * Lars Vogel <Lars.Vogel@gmail.com> - Ongoing maintenance - * Steven Spungin <steven@spungin.tv> - Bug 396902, 431755, 431735, 424730, 424730 + * Steven Spungin <steven@spungin.tv> - Bug 396902, 431755, 431735, 424730, 424730, 391089, 437236 ******************************************************************************/ package org.eclipse.e4.tools.emf.ui.internal.common; @@ -108,6 +108,7 @@ import org.eclipse.e4.tools.emf.ui.internal.common.component.WizardDialogEditor; import org.eclipse.e4.tools.emf.ui.internal.common.component.tabs.EmfUtil; import org.eclipse.e4.tools.emf.ui.internal.common.component.tabs.IGotoObject; import org.eclipse.e4.tools.emf.ui.internal.common.component.tabs.ListTab; +import org.eclipse.e4.tools.emf.ui.internal.common.component.tabs.XmiTab; import org.eclipse.e4.tools.emf.ui.internal.common.component.virtual.VApplicationAddons; import org.eclipse.e4.tools.emf.ui.internal.common.component.virtual.VApplicationCategoriesEditor; import org.eclipse.e4.tools.emf.ui.internal.common.component.virtual.VApplicationWindowEditor; @@ -134,10 +135,7 @@ import org.eclipse.e4.tools.emf.ui.internal.common.component.virtual.VWindowWind import org.eclipse.e4.tools.emf.ui.internal.common.properties.ExportIdsHandler; import org.eclipse.e4.tools.emf.ui.internal.common.properties.ExternalizeStringHandler; import org.eclipse.e4.tools.emf.ui.internal.common.properties.ProjectOSGiTranslationProvider; -import org.eclipse.e4.tools.emf.ui.internal.common.xml.AnnotationAccess; import org.eclipse.e4.tools.emf.ui.internal.common.xml.EMFDocumentResourceMediator; -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; @@ -170,7 +168,6 @@ import org.eclipse.emf.ecore.EClass; import org.eclipse.emf.ecore.EClassifier; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EStructuralFeature; -import org.eclipse.emf.ecore.resource.Resource.Diagnostic; import org.eclipse.emf.ecore.util.EcoreUtil; import org.eclipse.emf.edit.command.AddCommand; import org.eclipse.emf.edit.command.CommandParameter; @@ -189,17 +186,6 @@ import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.jface.resource.JFaceResources; import org.eclipse.jface.resource.StringConverter; -import org.eclipse.jface.text.BadLocationException; -import org.eclipse.jface.text.IDocument; -import org.eclipse.jface.text.IDocumentPartitioner; -import org.eclipse.jface.text.IRegion; -import org.eclipse.jface.text.Position; -import org.eclipse.jface.text.TextSelection; -import org.eclipse.jface.text.rules.FastPartitioner; -import org.eclipse.jface.text.source.Annotation; -import org.eclipse.jface.text.source.AnnotationModel; -import org.eclipse.jface.text.source.SourceViewer; -import org.eclipse.jface.text.source.VerticalRuler; import org.eclipse.jface.viewers.AbstractTreeViewer; import org.eclipse.jface.viewers.DoubleClickEvent; import org.eclipse.jface.viewers.IDoubleClickListener; @@ -272,8 +258,6 @@ public class ModelEditor implements IGotoObject { public static final String VIRTUAL_PERSPECTIVE_CONTROLS = ModelEditor.class.getName() + "VIRTUAL_PERSPECTIVE_CONTROLS"; //$NON-NLS-1$ public static final String VIRTUAL_SNIPPETS = ModelEditor.class.getName() + "VIRTUAL_SNIPPETS"; //$NON-NLS-1$ - private static final int VERTICAL_RULER_WIDTH = 20; - public static final int TAB_FORM = 0; public static final int TAB_XMI = 1; public static final int TAB_LIST = 2; @@ -339,8 +323,6 @@ public class ModelEditor implements IGotoObject { private CTabFolder editorTabFolder; - private SourceViewer sourceViewer; - private boolean mod1Down = false; private boolean saving; @@ -353,6 +335,8 @@ public class ModelEditor implements IGotoObject { private CTabItem tabItemTree; + private XmiTab xmiTab; + public ModelEditor(Composite composite, IEclipseContext context, IModelResource modelProvider, IProject project, final IResourcePool resourcePool) { this.resourcePool = resourcePool; this.modelProvider = modelProvider; @@ -437,12 +421,13 @@ public class ModelEditor implements IGotoObject { tabItemXmi = new CTabItem(editorTabFolder, SWT.NONE); tabItemXmi.setText(messages.ModelEditor_XMI); - tabItemXmi.setControl(createXMITab(editorTabFolder)); + xmiTab = createXMITab(editorTabFolder); + tabItemXmi.setControl(xmiTab); tabItemXmi.setImage(resourcePool.getImageUnchecked(ResourceProvider.IMG_Obj16_chart_organisation)); editorTabFolder.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { - if (editorTabFolder.getSelectionIndex() == 1) { + if (editorTabFolder.getSelectionIndex() == getTabIndex(tabItemXmi)) { emfDocumentProvider.updateFromEMF(); } } @@ -508,50 +493,14 @@ public class ModelEditor implements IGotoObject { return null; } - private Control createXMITab(Composite composite) { - - final AnnotationModel model = new AnnotationModel(); - VerticalRuler verticalRuler = new VerticalRuler(VERTICAL_RULER_WIDTH, new AnnotationAccess(resourcePool)); - int styles = SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION; - sourceViewer = new SourceViewer(composite, verticalRuler, styles); - sourceViewer.configure(new XMLConfiguration(resourcePool)); - sourceViewer.setEditable(project != null); - sourceViewer.getTextWidget().setFont(JFaceResources.getTextFont()); - - final IDocument document = emfDocumentProvider.getDocument(); - IDocumentPartitioner partitioner = new FastPartitioner(new XMLPartitionScanner(), new String[] { XMLPartitionScanner.XML_TAG, XMLPartitionScanner.XML_COMMENT }); - partitioner.connect(document); - document.setDocumentPartitioner(partitioner); - sourceViewer.setDocument(document); - verticalRuler.setModel(model); - - emfDocumentProvider.setValidationChangedCallback(new Runnable() { - - @Override - public void run() { - model.removeAllAnnotations(); - - for (Diagnostic d : emfDocumentProvider.getErrorList()) { - Annotation a = new Annotation("e4xmi.error", false, d.getMessage()); //$NON-NLS-1$ - int l; - try { - l = document.getLineOffset(d.getLine() - 1); - model.addAnnotation(a, new Position(l)); - } catch (BadLocationException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - } - }); - - String property = System.getProperty(ORG_ECLIPSE_E4_TOOLS_MODELEDITOR_FILTEREDTREE_ENABLED_XMITAB_DISABLED); - if (property != null || preferences.getBoolean("tab-form-search-show", false)) { //$NON-NLS-1$ - sourceViewer.setEditable(false); - sourceViewer.getTextWidget().setEnabled(false); - } - - return sourceViewer.getControl(); + private XmiTab createXMITab(Composite composite) { + IEclipseContext childContext = context.createChild(); + childContext.set(Composite.class, composite); + childContext.set(EMFDocumentResourceMediator.class, emfDocumentProvider); + childContext.set(IEclipsePreferences.class, preferences); + childContext.set(IResourcePool.class, resourcePool); + XmiTab ret = ContextInjectionFactory.make(XmiTab.class, childContext); + return ret; } private Composite createFormTab(Composite composite) { @@ -1481,6 +1430,7 @@ public class ModelEditor implements IGotoObject { if (project == null) { context.get(Display.class).removeFilter(SWT.MouseUp, keyListener); } + ContextInjectionFactory.uninject(xmiTab, xmiTab.getContext()); } public IModelResource getModelProvider() { @@ -1498,7 +1448,7 @@ public class ModelEditor implements IGotoObject { currentEditor.handlePaste(); } } else { - sourceViewer.getTextWidget().paste(); + xmiTab.paste(); } } @@ -1570,7 +1520,7 @@ public class ModelEditor implements IGotoObject { currentEditor.handleCopy(); } } else { - sourceViewer.getTextWidget().copy(); + xmiTab.copy(); } } @@ -1592,7 +1542,7 @@ public class ModelEditor implements IGotoObject { currentEditor.handleCut(); } } else { - sourceViewer.getTextWidget().cut(); + xmiTab.cut(); } } @@ -1855,13 +1805,7 @@ public class ModelEditor implements IGotoObject { emfDocumentProvider.updateFromEMF(); try { - // select the entire start tag - IRegion region = emfDocumentProvider.findStartTag(object); - if (region != null) { - sourceViewer.setSelection(new TextSelection(region.getOffset(), region.getLength()), true); - } else { - sourceViewer.setSelection(new TextSelection(0, 0), true); - } + xmiTab.gotoEObject(object); } catch (Exception e) { e.printStackTrace(); } diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/tabs/XmiTab.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/tabs/XmiTab.java new file mode 100644 index 00000000..6498d2c0 --- /dev/null +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/tabs/XmiTab.java @@ -0,0 +1,185 @@ +/******************************************************************************* + * Copyright (c) 2014 TwelveTone LLC and others. + * Copyright (c) 2010-2014 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: + * Steven Spungin <steven@spungin.tv> - initial API and implementation, Bug 391089 + *******************************************************************************/ + +package org.eclipse.e4.tools.emf.ui.internal.common.component.tabs; + +import javax.annotation.PostConstruct; +import javax.inject.Inject; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.preferences.IEclipsePreferences; +import org.eclipse.e4.core.contexts.IEclipseContext; +import org.eclipse.e4.tools.emf.ui.internal.common.xml.AnnotationAccess; +import org.eclipse.e4.tools.emf.ui.internal.common.xml.EMFDocumentResourceMediator; +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.IResourcePool; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.resource.Resource.Diagnostic; +import org.eclipse.jface.resource.JFaceResources; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IDocumentPartitioner; +import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.Position; +import org.eclipse.jface.text.TextSelection; +import org.eclipse.jface.text.rules.FastPartitioner; +import org.eclipse.jface.text.source.Annotation; +import org.eclipse.jface.text.source.AnnotationModel; +import org.eclipse.jface.text.source.SourceViewer; +import org.eclipse.jface.text.source.VerticalRuler; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.KeyAdapter; +import org.eclipse.swt.events.KeyEvent; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Text; + +public class XmiTab extends Composite { + + private static final String ORG_ECLIPSE_E4_TOOLS_MODELEDITOR_FILTEREDTREE_ENABLED_XMITAB_DISABLED = "org.eclipse.e4.tools.modeleditor.filteredtree.enabled.xmitab.disabled";//$NON-NLS-1$ + private static final int VERTICAL_RULER_WIDTH = 20; + + @Inject + private IEclipseContext context; + @Inject + private IProject project; + @Inject + private EMFDocumentResourceMediator emfDocumentProvider; + @Inject + private IResourcePool resourcePool; + @Inject + private IEclipsePreferences preferences; + + private Text text; + protected int offsetStart; + private SourceViewer sourceViewer; + + @Inject + public XmiTab(Composite parent) { + super(parent, SWT.NONE); + setLayout(new GridLayout(1, false)); + } + + @PostConstruct + protected void postConstruct() { + + text = new Text(this, SWT.SINGLE | SWT.LEAD | SWT.BORDER); + text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); + text.setMessage("type text to search (press <ENTER> to search for next instance)"); + text.addKeyListener(new KeyAdapter() { + @Override + public void keyPressed(KeyEvent e) { + if (e.keyCode != SWT.CR) { + offsetStart = 0; + } + offsetStart = searchAndHighlight(text.getText(), offsetStart); + } + }); + + final AnnotationModel model = new AnnotationModel(); + VerticalRuler verticalRuler = new VerticalRuler(VERTICAL_RULER_WIDTH, new AnnotationAccess(resourcePool)); + int styles = SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION; + sourceViewer = new SourceViewer(this, verticalRuler, styles); + sourceViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); + + sourceViewer.configure(new XMLConfiguration(resourcePool)); + sourceViewer.setEditable(project != null); + sourceViewer.getTextWidget().setFont(JFaceResources.getTextFont()); + + final IDocument document = emfDocumentProvider.getDocument(); + IDocumentPartitioner partitioner = new FastPartitioner(new XMLPartitionScanner(), new String[] { XMLPartitionScanner.XML_TAG, XMLPartitionScanner.XML_COMMENT }); + partitioner.connect(document); + document.setDocumentPartitioner(partitioner); + sourceViewer.setDocument(document); + verticalRuler.setModel(model); + + emfDocumentProvider.setValidationChangedCallback(new Runnable() { + + @Override + public void run() { + model.removeAllAnnotations(); + + for (Diagnostic d : emfDocumentProvider.getErrorList()) { + Annotation a = new Annotation("e4xmi.error", false, d.getMessage()); //$NON-NLS-1$ + int l; + try { + l = document.getLineOffset(d.getLine() - 1); + model.addAnnotation(a, new Position(l)); + } catch (BadLocationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + }); + + String property = System.getProperty(ORG_ECLIPSE_E4_TOOLS_MODELEDITOR_FILTEREDTREE_ENABLED_XMITAB_DISABLED); + if (property != null || preferences.getBoolean("tab-form-search-show", false)) { //$NON-NLS-1$ + sourceViewer.setEditable(false); + sourceViewer.getTextWidget().setEnabled(false); + } + } + + /** + * + * @param text + * @param startOffset + * @return The endOFfset, or -1 if not found + */ + protected int searchAndHighlight(String text, int startOffset) { + try { + // select the entire start tag + IRegion region; + region = emfDocumentProvider.findText(text, startOffset); + if (region == null && startOffset > 0) { + region = emfDocumentProvider.findText(text, 0); + } + if (region != null) { + sourceViewer.setSelection(new TextSelection(region.getOffset(), region.getLength()), true); + return region.getOffset() + region.getLength(); + } else { + sourceViewer.setSelection(new TextSelection(0, 0), true); + return -1; + } + } catch (Exception e) { + e.printStackTrace(); + return -1; + } + } + + public IEclipseContext getContext() { + return context; + } + + public void gotoEObject(EObject object) { + // select the entire start tag + IRegion region = emfDocumentProvider.findStartTag(object); + if (region != null) { + sourceViewer.setSelection(new TextSelection(region.getOffset(), region.getLength()), true); + } else { + sourceViewer.setSelection(new TextSelection(0, 0), true); + } + } + + public void paste() { + sourceViewer.getTextWidget().paste(); + } + + public void copy() { + sourceViewer.getTextWidget().copy(); + } + + public void cut() { + sourceViewer.getTextWidget().cut(); + } +} diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/EMFDocumentResourceMediator.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/EMFDocumentResourceMediator.java index 9a4c036a..50e129ae 100644 --- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/EMFDocumentResourceMediator.java +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/xml/EMFDocumentResourceMediator.java @@ -1,6 +1,6 @@ /******************************************************************************* * Contributors: - * Steven Spungin <steven@spungin.tv> - Bug 431735 + * Steven Spungin <steven@spungin.tv> - Bug 431735, Bug 391089 *******************************************************************************/ package org.eclipse.e4.tools.emf.ui.internal.common.xml; @@ -12,6 +12,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.eclipse.e4.tools.emf.ui.common.IModelResource; +import org.eclipse.e4.tools.emf.ui.internal.common.component.tabs.empty.E; import org.eclipse.e4.ui.internal.workbench.E4XMIResource; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.resource.Resource.Diagnostic; @@ -106,7 +107,7 @@ public class EMFDocumentResourceMediator { * @return The region for the start tag of the EObject, or null if not found. */ public IRegion findStartTag(EObject object) { - if (object == null){ + if (object == null) { return null; } E4XMIResource root = (E4XMIResource) ((EObject) modelResource.getRoot().get(0)).eResource(); @@ -123,4 +124,24 @@ public class EMFDocumentResourceMediator { } } + /** + * @param object + * @return The region for the start of the text, or null if not found or the + * text is empty. + */ + public IRegion findText(String text, int startOffset) { + if (E.isEmpty(text)) { + return null; + } + + FindReplaceDocumentAdapter find = new FindReplaceDocumentAdapter(document); + IRegion region; + try { + region = find.find(startOffset, text, true, true, false, false); //$NON-NLS-1$ //$NON-NLS-2$ + return region; + } catch (BadLocationException e) { + return null; + } + } + } |