From fdf19296653f39a8a6a2ddbf97acd0ccb6556cba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Mail=C3=A4nder?= Date: Thu, 30 Sep 2021 17:30:51 +0200 Subject: [PATCH] Add support for labeled enum settings. --- .../META-INF/MANIFEST.MF | 3 +- .../peak/detector/core/FilterMode.java | 27 +++++++++++--- .../peak/detector/model/Threshold.java | 16 +++++++-- .../serialization/JSONSerialization.java | 15 ++++---- .../chemclipse/support/text/ILabel.java | 17 +++++++++ .../extension/xxd/ui/methods/WidgetItem.java | 36 +++++++++++-------- 6 files changed, 85 insertions(+), 29 deletions(-) create mode 100644 chemclipse/plugins/org.eclipse.chemclipse.support/src/org/eclipse/chemclipse/support/text/ILabel.java diff --git a/chemclipse/plugins/org.eclipse.chemclipse.chromatogram.peak.detector/META-INF/MANIFEST.MF b/chemclipse/plugins/org.eclipse.chemclipse.chromatogram.peak.detector/META-INF/MANIFEST.MF index 300916de02..6394eec908 100644 --- a/chemclipse/plugins/org.eclipse.chemclipse.chromatogram.peak.detector/META-INF/MANIFEST.MF +++ b/chemclipse/plugins/org.eclipse.chemclipse.chromatogram.peak.detector/META-INF/MANIFEST.MF @@ -18,4 +18,5 @@ Require-Bundle: org.eclipse.chemclipse.numeric;bundle-version="0.8.0", org.eclipse.chemclipse.model;bundle-version="0.8.0", org.eclipse.core.runtime;bundle-version="3.9.100", org.eclipse.chemclipse.processing;bundle-version="0.8.0", - org.eclipse.chemclipse.chromatogram.xxd.filter.supplier.savitzkygolay + org.eclipse.chemclipse.chromatogram.xxd.filter.supplier.savitzkygolay, + org.eclipse.chemclipse.support diff --git a/chemclipse/plugins/org.eclipse.chemclipse.chromatogram.peak.detector/src/org/eclipse/chemclipse/chromatogram/peak/detector/core/FilterMode.java b/chemclipse/plugins/org.eclipse.chemclipse.chromatogram.peak.detector/src/org/eclipse/chemclipse/chromatogram/peak/detector/core/FilterMode.java index 702f50de8e..ec39c2dcab 100644 --- a/chemclipse/plugins/org.eclipse.chemclipse.chromatogram.peak.detector/src/org/eclipse/chemclipse/chromatogram/peak/detector/core/FilterMode.java +++ b/chemclipse/plugins/org.eclipse.chemclipse.chromatogram.peak.detector/src/org/eclipse/chemclipse/chromatogram/peak/detector/core/FilterMode.java @@ -9,20 +9,37 @@ * Contributors: * Alexander Kerner - initial API and implementation * Philip Wenig - add elements for combo support + * Matthias Mailänder - add labels *******************************************************************************/ package org.eclipse.chemclipse.chromatogram.peak.detector.core; -public enum FilterMode { - INCLUDE, // - EXCLUDE; +import org.eclipse.chemclipse.support.text.ILabel; + +public enum FilterMode implements ILabel { + + INCLUDE("inclusive"), // + EXCLUDE("exclusive"); + + private String label; + + private FilterMode(String label) { + + this.label = label; + } + + @Override + public String label() { + + return label; + } public static String[][] getElements() { String[][] elements = new String[values().length][2]; int counter = 0; for(FilterMode filterMode : values()) { - elements[counter][0] = filterMode.name(); // Label - elements[counter][1] = filterMode.name(); // Value + elements[counter][0] = filterMode.label(); + elements[counter][1] = filterMode.name(); counter++; } return elements; diff --git a/chemclipse/plugins/org.eclipse.chemclipse.chromatogram.peak.detector/src/org/eclipse/chemclipse/chromatogram/peak/detector/model/Threshold.java b/chemclipse/plugins/org.eclipse.chemclipse.chromatogram.peak.detector/src/org/eclipse/chemclipse/chromatogram/peak/detector/model/Threshold.java index 6094e1eafc..006d4184d5 100644 --- a/chemclipse/plugins/org.eclipse.chemclipse.chromatogram.peak.detector/src/org/eclipse/chemclipse/chromatogram/peak/detector/model/Threshold.java +++ b/chemclipse/plugins/org.eclipse.chemclipse.chromatogram.peak.detector/src/org/eclipse/chemclipse/chromatogram/peak/detector/model/Threshold.java @@ -8,10 +8,14 @@ * * Contributors: * Dr. Philip Wenig - initial API and implementation + * Matthias Mailänder - add label *******************************************************************************/ package org.eclipse.chemclipse.chromatogram.peak.detector.model; -public enum Threshold { +import org.eclipse.chemclipse.support.text.ILabel; + +public enum Threshold implements ILabel { + OFF(1), // LOW(2), // MEDIUM(3), // @@ -29,13 +33,19 @@ public int getThreshold() { return threshold; } + @Override + public String label() { + + return name().toLowerCase(); + } + public static String[][] getElements() { String[][] elements = new String[values().length][2]; int counter = 0; for(Threshold threshold : values()) { - elements[counter][0] = threshold.name(); // Label - elements[counter][1] = threshold.name(); // Value + elements[counter][0] = threshold.label(); + elements[counter][1] = threshold.name(); counter++; } return elements; diff --git a/chemclipse/plugins/org.eclipse.chemclipse.support/src/org/eclipse/chemclipse/support/settings/serialization/JSONSerialization.java b/chemclipse/plugins/org.eclipse.chemclipse.support/src/org/eclipse/chemclipse/support/settings/serialization/JSONSerialization.java index 9e11f50f75..9b14b3b62d 100644 --- a/chemclipse/plugins/org.eclipse.chemclipse.support/src/org/eclipse/chemclipse/support/settings/serialization/JSONSerialization.java +++ b/chemclipse/plugins/org.eclipse.chemclipse.support/src/org/eclipse/chemclipse/support/settings/serialization/JSONSerialization.java @@ -97,12 +97,15 @@ private ObjectMapper createMapper() { /* * Add additional mapper dynamically. */ - for(Object object : Activator.getDefault().getSerializationServices()) { - if(object instanceof ISerializationService) { - ISerializationService serializationService = (ISerializationService)object; - Class clazz = serializationService.getSupportedClass(); - simpleModule.addSerializer(clazz, serializationService.getSerializer()); - simpleModule.addDeserializer(clazz, serializationService.getDeserializer()); + Object[] serializationServices = Activator.getDefault().getSerializationServices(); + if(serializationServices != null) { + for(Object object : Activator.getDefault().getSerializationServices()) { + if(object instanceof ISerializationService) { + ISerializationService serializationService = (ISerializationService)object; + Class clazz = serializationService.getSupportedClass(); + simpleModule.addSerializer(clazz, serializationService.getSerializer()); + simpleModule.addDeserializer(clazz, serializationService.getDeserializer()); + } } } // diff --git a/chemclipse/plugins/org.eclipse.chemclipse.support/src/org/eclipse/chemclipse/support/text/ILabel.java b/chemclipse/plugins/org.eclipse.chemclipse.support/src/org/eclipse/chemclipse/support/text/ILabel.java new file mode 100644 index 0000000000..b6b40becfd --- /dev/null +++ b/chemclipse/plugins/org.eclipse.chemclipse.support/src/org/eclipse/chemclipse/support/text/ILabel.java @@ -0,0 +1,17 @@ +/******************************************************************************* + * Copyright (c) 2021 Lablicate GmbH. + * + * 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: + * Matthias Mailänder - initial API and implementation + *******************************************************************************/ +package org.eclipse.chemclipse.support.text; + +public interface ILabel { + + String label(); +} diff --git a/chemclipse/plugins/org.eclipse.chemclipse.ux.extension.xxd.ui/src/org/eclipse/chemclipse/ux/extension/xxd/ui/methods/WidgetItem.java b/chemclipse/plugins/org.eclipse.chemclipse.ux.extension.xxd.ui/src/org/eclipse/chemclipse/ux/extension/xxd/ui/methods/WidgetItem.java index fc1c2d8002..c3a857add5 100644 --- a/chemclipse/plugins/org.eclipse.chemclipse.ux.extension.xxd.ui/src/org/eclipse/chemclipse/ux/extension/xxd/ui/methods/WidgetItem.java +++ b/chemclipse/plugins/org.eclipse.chemclipse.ux.extension.xxd.ui/src/org/eclipse/chemclipse/ux/extension/xxd/ui/methods/WidgetItem.java @@ -9,18 +9,18 @@ * Contributors: * Dr. Philip Wenig - initial API and implementation * Christoph Läubrich - support file selection, refactor for new settings model, use validators, support for longs + * Matthias Mailänder - add labeled combo boxes *******************************************************************************/ package org.eclipse.chemclipse.ux.extension.xxd.ui.methods; import java.io.File; -import java.util.ArrayList; -import java.util.List; import org.eclipse.chemclipse.support.settings.ComboSettingsProperty.ComboSupplier; import org.eclipse.chemclipse.support.settings.FileSettingProperty; import org.eclipse.chemclipse.support.settings.FileSettingProperty.DialogType; import org.eclipse.chemclipse.support.settings.parser.InputValue; import org.eclipse.chemclipse.support.settings.validation.InputValidator; +import org.eclipse.chemclipse.support.text.ILabel; import org.eclipse.chemclipse.support.ui.provider.AbstractLabelProvider; import org.eclipse.chemclipse.support.ui.provider.AdapterLabelProvider; import org.eclipse.chemclipse.ux.extension.xxd.ui.Activator; @@ -171,9 +171,8 @@ public Object getValue() { if(comboSupplier != null) { return getValueAsString(comboSupplier); } - Combo combo = (Combo)control; if(rawType.isEnum()) { - return combo.getText().trim(); + return currentSelection.toString(); } } else { /* @@ -229,12 +228,9 @@ private Control createControl(Composite parent) { } else if(rawType == boolean.class || rawType == Boolean.class) { return createCheckboxWidget(parent); } else if(rawType.isEnum()) { - List input = new ArrayList<>(); Enum[] enums = (Enum[])rawType.getEnumConstants(); - for(int i = 0; i < enums.length; i++) { - input.add(enums[i].toString()); - } - return createEnumComboViewerWidget(parent, input); + ComboViewer viewer = createLabeledEnumComboViewerWidget(parent, enums); + return viewer.getControl(); } else if(rawType == File.class) { return createFileWidget(parent); } else { @@ -407,29 +403,41 @@ private Control createCheckboxWidget(Composite parent) { return button; } - private Control createEnumComboViewerWidget(Composite parent, List input) { + private ComboViewer createLabeledEnumComboViewerWidget(Composite parent, Enum[] input) { ComboViewer comboViewer = new ComboViewer(parent, SWT.READ_ONLY); - // - Combo combo = comboViewer.getCombo(); comboViewer.setContentProvider(ArrayContentProvider.getInstance()); comboViewer.setLabelProvider(new AbstractLabelProvider() { @Override public String getText(Object element) { + if(element instanceof ILabel) { + return ((ILabel)element).label(); + } return element.toString(); } }); + Combo combo = comboViewer.getCombo(); combo.setToolTipText(inputValue.getDescription()); GridData gridData = new GridData(GridData.FILL_HORIZONTAL); gridData.widthHint = 150; combo.setLayoutData(gridData); // comboViewer.setInput(input); - combo.setText(getValueAsString()); + Enum initialSelection = Enum.valueOf(input[0].getDeclaringClass(), getValueAsString()); + comboViewer.setSelection(new StructuredSelection(initialSelection)); + // + comboViewer.addSelectionChangedListener(new ISelectionChangedListener() { + + @Override + public void selectionChanged(SelectionChangedEvent event) { + + currentSelection = comboViewer.getStructuredSelection().getFirstElement(); + } + }); // - return combo; + return comboViewer; } private boolean getValueAsBoolean() {