diff options
author | Steven Spungin | 2014-06-24 12:27:05 +0000 |
---|---|---|
committer | Steven Spungin | 2014-07-14 12:52:23 +0000 |
commit | c90d295c1eeca5683657251374f4061ea6ae83a9 (patch) | |
tree | 5fe392b63d0e18edbe2bef639343e00d3ff2eb83 | |
parent | 99f14dd8b35b02cc755a5194760e9fcb5574bd1e (diff) | |
download | org.eclipse.e4.tools-c90d295c1eeca5683657251374f4061ea6ae83a9.tar.gz org.eclipse.e4.tools-c90d295c1eeca5683657251374f4061ea6ae83a9.tar.xz org.eclipse.e4.tools-c90d295c1eeca5683657251374f4061ea6ae83a9.zip |
Bug 437951 - [model editor] add option to update id suffix when changing
label
Change-Id: Ib87fce899d2111e07865b484f2ed1da242f3de81
Signed-off-by: Steven Spungin <steven@spungin.tv>
12 files changed, 215 insertions, 9 deletions
diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/common/component/AbstractComponentEditor.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/common/component/AbstractComponentEditor.java index 882dda6d..baef0d6b 100644 --- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/common/component/AbstractComponentEditor.java +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/common/component/AbstractComponentEditor.java @@ -8,6 +8,7 @@ * Contributors: * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation * Marco Descher <marco@descher.at> - Bug 422465 + * Steven Spungin <steven@spungin.tv> - Bug 437951 ******************************************************************************/ package org.eclipse.e4.tools.emf.ui.common.component; @@ -33,6 +34,8 @@ import org.eclipse.e4.ui.model.application.MApplicationElement; import org.eclipse.e4.ui.model.application.ui.MUILabel; import org.eclipse.emf.databinding.EMFDataBindingContext; import org.eclipse.emf.databinding.FeaturePath; +import org.eclipse.emf.databinding.edit.EMFEditProperties; +import org.eclipse.emf.ecore.EAttribute; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.edit.domain.EditingDomain; import org.eclipse.jface.action.Action; @@ -85,6 +88,8 @@ public abstract class AbstractComponentEditor { private Composite editorControl; + private IdGenerator generator; + public EditingDomain getEditingDomain() { return editingDomain; } @@ -126,7 +131,11 @@ public abstract class AbstractComponentEditor { public abstract String getDescription(Object element); public Composite getEditor(Composite parent, Object object) { - return editorControl = doGetEditor(parent, object); + if (generator != null) { + generator.stopGenerating(); + generator = null; + } + return doGetEditor(parent, object); } protected abstract Composite doGetEditor(Composite parent, Object object); @@ -249,4 +258,27 @@ public abstract class AbstractComponentEditor { } } + + /** + * Generates an ID when the another field changes. Must be called after + * master is set with the objects value. + * + * @param attSource + * The source attribute + * @param attId + * The id attribute to generate + * @param control + * optional control to disable generator after losing focus or + * disposing + */ + protected void enableIdGenerator(EAttribute attSource, EAttribute attId, Control control) { + if (generator != null) { + generator.stopGenerating(); + generator = null; + } + if (getEditor().isAutoCreateElementId()) { + generator = new IdGenerator(); + generator.bind(getMaster(), EMFEditProperties.value(getEditingDomain(), attSource), EMFEditProperties.value(getEditingDomain(), attId), control); + } + } } diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/common/component/IdGenerator.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/common/component/IdGenerator.java new file mode 100644 index 00000000..a1ac8e23 --- /dev/null +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/common/component/IdGenerator.java @@ -0,0 +1,161 @@ +/******************************************************************************* + * Copyright (c) 2014 TwelveTone LLC 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 437951 + *******************************************************************************/ + +package org.eclipse.e4.tools.emf.ui.common.component; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.eclipse.core.databinding.observable.value.IObservableValue; +import org.eclipse.core.databinding.observable.value.IValueChangeListener; +import org.eclipse.core.databinding.observable.value.ValueChangeEvent; +import org.eclipse.e4.tools.emf.ui.internal.common.component.tabs.empty.E; +import org.eclipse.emf.databinding.edit.IEMFEditValueProperty; +import org.eclipse.swt.events.DisposeEvent; +import org.eclipse.swt.events.DisposeListener; +import org.eclipse.swt.events.FocusAdapter; +import org.eclipse.swt.widgets.Control; + +/** + * Auto generates an id based on another field's current value. + * + * @author Steven Spungin + * + */ +public class IdGenerator { + + private IValueChangeListener listener; + private IObservableValue observableValue; + protected boolean ignore; + private IValueChangeListener listener2; + private IObservableValue observableValue2; + static Pattern patternId = Pattern.compile("^(.*\\.)\\d+$"); //$NON-NLS-1$ + + /** + * Bind must be called AFTER the master observable value is set in order to + * properly initialize. + * + * @param master + * @param ebpLabel + * @param evpId + * @param control + * Optional control. + */ + public void bind(final IObservableValue master, final IEMFEditValueProperty ebpLabel, final IEMFEditValueProperty evpId, Control control) { + + // RULES + // Only start generating if the label is initially empty and the id ends + // with a '.' + // followed by an integer + // If the id is manually changed, stop generating + // If the control loses focus, stop generating + + final String origLabel = (String) ebpLabel.getValue(master.getValue()); + if (E.notEmpty(origLabel)) { + stopGenerating(); + return; + } + String origId = (String) evpId.getValue(master.getValue()); + if (origId == null) { + origId = "id.0"; //$NON-NLS-1$ + } + Matcher m = patternId.matcher(origId); + if (!m.matches()) { + stopGenerating(); + return; + } + final String baseId = m.group(1); + + if (control != null) { + control.addFocusListener(new FocusAdapter() { + @Override + public void focusLost(org.eclipse.swt.events.FocusEvent e) { + stopGenerating(); + }; + }); + + control.addDisposeListener(new DisposeListener() { + + @Override + public void widgetDisposed(DisposeEvent e) { + stopGenerating(); + } + }); + } + + observableValue2 = evpId.observe(master.getValue()); + observableValue2.addValueChangeListener(listener2 = new IValueChangeListener() { + + @Override + public void handleValueChange(ValueChangeEvent event) { + if (!ignore) { + stopGenerating(); + } + } + }); + + observableValue = ebpLabel.observe(master.getValue()); + observableValue.addValueChangeListener(listener = new IValueChangeListener() { + + @Override + public void handleValueChange(ValueChangeEvent event) { + String labelValue = (String) ebpLabel.getValue(master.getValue()); + if (labelValue == null) { + labelValue = ""; //$NON-NLS-1$ + } + String camelCase = camelCase(labelValue); + ignore = true; + evpId.setValue(master.getValue(), baseId + camelCase); + ignore = false; + } + }); + + } + + /** + * Strips all illegal id characters, and camel cases each word. + * + * @param value + * @return + */ + protected static String camelCase(String value) { + String[] parts = value.split("\\s+"); //$NON-NLS-1$ + String ret = ""; //$NON-NLS-1$ + boolean first = true; + for (String part : parts) { + part = part.replaceAll("[^0-9a-zA-Z_-]", ""); //$NON-NLS-1$ //$NON-NLS-2$ + if (first) { + first = false; + ret = part.toLowerCase(); + } else { + part = part.substring(0, 1).toUpperCase() + part.substring(1).toLowerCase(); + ret += part; + } + } + return ret; + } + + /** + * Removes all listeners and prevents the id from changing + */ + public void stopGenerating() { + if (observableValue != null) { + observableValue.removeValueChangeListener(listener); + listener = null; + observableValue = null; + } + if (observableValue2 != null) { + observableValue2.removeValueChangeListener(listener2); + listener2 = null; + observableValue2 = null; + } + } + +} diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/BindingContextEditor.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/BindingContextEditor.java index 1be16537..f2652cbc 100644 --- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/BindingContextEditor.java +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/BindingContextEditor.java @@ -8,6 +8,7 @@ * Contributors: * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation * Lars Vogel <Lars.Vogel@gmail.com> - Ongoing maintenance + * Steven Spungin <steven@spungin.tv> - Bug 437951 ******************************************************************************/ package org.eclipse.e4.tools.emf.ui.internal.common.component; @@ -143,6 +144,7 @@ public class BindingContextEditor extends AbstractComponentEditor { } getMaster().setValue(object); + enableIdGenerator(CommandsPackageImpl.Literals.BINDING_CONTEXT__NAME, ApplicationPackageImpl.Literals.APPLICATION_ELEMENT__ELEMENT_ID, null); return composite; } diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/CategoryEditor.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/CategoryEditor.java index 49fa788c..3b669a63 100644 --- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/CategoryEditor.java +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/CategoryEditor.java @@ -8,6 +8,7 @@ * Contributors: * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation * Lars Vogel <Lars.Vogel@gmail.com> - Ongoing maintenance + * Steven Spungin <steven@spungin.tv> - Bug 437951 ******************************************************************************/ package org.eclipse.e4.tools.emf.ui.internal.common.component; @@ -95,6 +96,7 @@ public class CategoryEditor extends AbstractComponentEditor { } getMaster().setValue(object); + enableIdGenerator(CommandsPackageImpl.Literals.CATEGORY__NAME, ApplicationPackageImpl.Literals.APPLICATION_ELEMENT__ELEMENT_ID, null); return composite; } diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/CommandEditor.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/CommandEditor.java index b26546c3..d460a7cc 100644 --- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/CommandEditor.java +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/CommandEditor.java @@ -8,6 +8,7 @@ * Contributors: * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation * Lars Vogel <Lars.Vogel@gmail.com> - Ongoing maintenance + * Steven Spungin <steven@spungin.tv> - Bug 437951 ******************************************************************************/ package org.eclipse.e4.tools.emf.ui.internal.common.component; @@ -133,6 +134,7 @@ public class CommandEditor extends AbstractComponentEditor { } getMaster().setValue(object); + enableIdGenerator(CommandsPackageImpl.Literals.COMMAND__COMMAND_NAME, ApplicationPackageImpl.Literals.APPLICATION_ELEMENT__ELEMENT_ID, null); return composite; } diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/MenuEditor.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/MenuEditor.java index 118c37f9..b354830c 100644 --- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/MenuEditor.java +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/MenuEditor.java @@ -9,7 +9,7 @@ * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation * Marco Descher <marco@descher.at> - Bug 395982 * Lars Vogel <Lars.Vogel@gmail.com> - Ongoing maintenance - * Steven Spungin <steven@spungin.tv> - Bug 424730 + * Steven Spungin <steven@spungin.tv> - Bug 424730, Bug 437951 ******************************************************************************/ package org.eclipse.e4.tools.emf.ui.internal.common.component; @@ -224,6 +224,7 @@ public class MenuEditor extends AbstractComponentEditor { } getMaster().setValue(object); + enableIdGenerator(UiPackageImpl.Literals.UI_LABEL__LABEL, ApplicationPackageImpl.Literals.APPLICATION_ELEMENT__ELEMENT_ID, null); return composite; } diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/MenuItemEditor.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/MenuItemEditor.java index eeadda41..2cadfd34 100644 --- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/MenuItemEditor.java +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/MenuItemEditor.java @@ -8,7 +8,7 @@ * Contributors: * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation * Lars Vogel <Lars.Vogel@gmail.com> - Ongoing maintenance - * Steven Spungin <steven@spungin.tv> - Bug 424730 + * Steven Spungin <steven@spungin.tv> - Bug 424730, Bug 437951 ******************************************************************************/ package org.eclipse.e4.tools.emf.ui.internal.common.component; @@ -150,6 +150,7 @@ public abstract class MenuItemEditor extends AbstractComponentEditor { } getMaster().setValue(object); + enableIdGenerator(UiPackageImpl.Literals.UI_LABEL__LABEL, ApplicationPackageImpl.Literals.APPLICATION_ELEMENT__ELEMENT_ID, null); return composite; } diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/PartDescriptorEditor.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/PartDescriptorEditor.java index aebc5b3c..5411f8e1 100644 --- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/PartDescriptorEditor.java +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/PartDescriptorEditor.java @@ -7,7 +7,7 @@ * * Contributors: * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation - * Steven Spungin <steven@spungin.tv> - Bug 404166, 424730 + * Steven Spungin <steven@spungin.tv> - Bug 404166, 424730, 437951 ******************************************************************************/ package org.eclipse.e4.tools.emf.ui.internal.common.component; @@ -141,6 +141,7 @@ public class PartDescriptorEditor extends AbstractComponentEditor { } getMaster().setValue(object); + enableIdGenerator(UiPackageImpl.Literals.UI_LABEL__LABEL, ApplicationPackageImpl.Literals.APPLICATION_ELEMENT__ELEMENT_ID, null); return composite; } diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/PartEditor.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/PartEditor.java index 15be9c08..e8b3462c 100644 --- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/PartEditor.java +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/PartEditor.java @@ -7,8 +7,8 @@ * * Contributors: * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation - * Steven Spungin <steven@spungin.tv> - Bug 424730 -******************************************************************************/ + * Steven Spungin <steven@spungin.tv> - Bug 424730, Bug 437951 + ******************************************************************************/ package org.eclipse.e4.tools.emf.ui.internal.common.component; import javax.inject.Inject; @@ -157,6 +157,7 @@ public class PartEditor extends AbstractComponentEditor { } getMaster().setValue(object); + enableIdGenerator(UiPackageImpl.Literals.UI_LABEL__LABEL, ApplicationPackageImpl.Literals.APPLICATION_ELEMENT__ELEMENT_ID, null); return composite; } diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/PerspectiveEditor.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/PerspectiveEditor.java index fbff8850..6ca96161 100644 --- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/PerspectiveEditor.java +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/PerspectiveEditor.java @@ -7,7 +7,7 @@ * * Contributors: * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation - * Steven Spungin <steven@spungin.tv> - Bug 424730 + * Steven Spungin <steven@spungin.tv> - Bug 424730, Bug 437951 ******************************************************************************/ package org.eclipse.e4.tools.emf.ui.internal.common.component; @@ -187,6 +187,7 @@ public class PerspectiveEditor extends AbstractComponentEditor { } getMaster().setValue(object); + enableIdGenerator(UiPackageImpl.Literals.UI_LABEL__LABEL, ApplicationPackageImpl.Literals.APPLICATION_ELEMENT__ELEMENT_ID, null); return composite; } diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/ToolItemEditor.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/ToolItemEditor.java index ea2c3678..7e967324 100644 --- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/ToolItemEditor.java +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/ToolItemEditor.java @@ -8,7 +8,7 @@ * Contributors: * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation * Lars Vogel <Lars.Vogel@gmail.com> - Ongoing maintenance - * Steven Spungin <steven@spungin.tv> - Bug 424730 + * Steven Spungin <steven@spungin.tv> - Bug 424730, Bug 437951 ******************************************************************************/ package org.eclipse.e4.tools.emf.ui.internal.common.component; @@ -129,6 +129,7 @@ public abstract class ToolItemEditor extends AbstractComponentEditor { } getMaster().setValue(object); + enableIdGenerator(UiPackageImpl.Literals.UI_LABEL__LABEL, ApplicationPackageImpl.Literals.APPLICATION_ELEMENT__ELEMENT_ID, null); return composite; } diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/WindowEditor.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/WindowEditor.java index 604612b8..b6538652 100644 --- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/WindowEditor.java +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/WindowEditor.java @@ -8,7 +8,7 @@ * Contributors: * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation * Lars Vogel <Lars.Vogel@gmail.com> - Ongoing maintenance - * Steven Spungin <steven@spungin.tv> - Bug 424730 + * Steven Spungin <steven@spungin.tv> - Bug 424730, Bug 437951 ******************************************************************************/ package org.eclipse.e4.tools.emf.ui.internal.common.component; @@ -166,6 +166,7 @@ public class WindowEditor extends AbstractComponentEditor { } getMaster().setValue(object); + enableIdGenerator(UiPackageImpl.Literals.UI_LABEL__LABEL, ApplicationPackageImpl.Literals.APPLICATION_ELEMENT__ELEMENT_ID, null); return composite; } |