From 645f490825bd2bc33a5ebe465915b671f1d9fca4 Mon Sep 17 00:00:00 2001 From: Alexander Fedorov Date: Sat, 15 Feb 2020 14:31:46 +0300 Subject: [PATCH 1/2] Bug 548112 - [Passage] create license plan from issue license wizard Change ZeroOneMany to accept Supplier Add Appearance infra Add SelectFromDialog infra Remove unused code Bump API version Signed-off-by: Alexander Fedorov --- .../META-INF/MANIFEST.MF | 2 +- .../passage/loc/internal/api/ZeroOneMany.java | 7 +- .../META-INF/MANIFEST.MF | 9 +- .../workbench/i18n/WorkbenchMessages.java | 6 + .../i18n/WorkbenchMessages.properties | 6 + .../passage/loc/jface/dialogs/Appearance.java | 103 ++++++++++++++++++ .../passage/loc/workbench/LocWokbench.java | 9 -- .../loc/workbench/SelectFromDialog.java | 87 +++++++++++++++ .../loc/api/tests/ZeroOneManyTest.java | 8 +- 9 files changed, 216 insertions(+), 21 deletions(-) create mode 100644 bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/jface/dialogs/Appearance.java create mode 100644 bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/workbench/SelectFromDialog.java diff --git a/bundles/org.eclipse.passage.loc.api/META-INF/MANIFEST.MF b/bundles/org.eclipse.passage.loc.api/META-INF/MANIFEST.MF index c8f489ea0..b1b8cfa53 100644 --- a/bundles/org.eclipse.passage.loc.api/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.passage.loc.api/META-INF/MANIFEST.MF @@ -8,7 +8,7 @@ Bundle-Vendor: %Bundle-Vendor Bundle-Copyright: %Bundle-Copyright Bundle-RequiredExecutionEnvironment: JavaSE-1.8 Export-Package: org.eclipse.passage.loc.api, - org.eclipse.passage.loc.internal.api;x-internal:=true + org.eclipse.passage.loc.internal.api;x-friends:="org.eclipse.passage.loc.workbench" Require-Bundle: org.eclipse.osgi.services;bundle-version="0.0.0", org.eclipse.equinox.common;bundle-version="0.0.0";visibility:=reexport, org.eclipse.e4.core.services;bundle-version="0.0.0", diff --git a/bundles/org.eclipse.passage.loc.api/src/org/eclipse/passage/loc/internal/api/ZeroOneMany.java b/bundles/org.eclipse.passage.loc.api/src/org/eclipse/passage/loc/internal/api/ZeroOneMany.java index 367e6f80e..2e8910c8f 100644 --- a/bundles/org.eclipse.passage.loc.api/src/org/eclipse/passage/loc/internal/api/ZeroOneMany.java +++ b/bundles/org.eclipse.passage.loc.api/src/org/eclipse/passage/loc/internal/api/ZeroOneMany.java @@ -24,13 +24,14 @@ */ public final class ZeroOneMany { - private final Iterable input; + private final Supplier> supplier; - public ZeroOneMany(Iterable input) { - this.input = input; + public ZeroOneMany(Supplier> input) { + this.supplier = input; } public Optional choose(Supplier create, Function, Optional> select) { + Iterable input = supplier.get(); Iterator iterator = input.iterator(); if (!iterator.hasNext()) { return Optional.ofNullable(create.get()); diff --git a/bundles/org.eclipse.passage.loc.workbench/META-INF/MANIFEST.MF b/bundles/org.eclipse.passage.loc.workbench/META-INF/MANIFEST.MF index c77a17585..182357aa5 100644 --- a/bundles/org.eclipse.passage.loc.workbench/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.passage.loc.workbench/META-INF/MANIFEST.MF @@ -2,13 +2,12 @@ Manifest-Version: 1.0 Automatic-Module-Name: org.eclipse.passage.loc.workbench Bundle-ManifestVersion: 2 Bundle-SymbolicName: org.eclipse.passage.loc.workbench;singleton:=true -Bundle-Version: 0.5.100.qualifier +Bundle-Version: 0.6.0.qualifier Bundle-Name: %Bundle-Name Bundle-Vendor: %Bundle-Vendor Bundle-Copyright: %Bundle-Copyright Bundle-RequiredExecutionEnvironment: JavaSE-1.8 -Require-Bundle: org.slf4j.api;bundle-version="0.0.0", - org.eclipse.osgi.services;bundle-version="0.0.0", +Require-Bundle: org.eclipse.osgi.services;bundle-version="0.0.0", org.eclipse.core.runtime;bundle-version="0.0.0", org.eclipse.jface;bundle-version="0.0.0", org.eclipse.emf.ecore.xmi;bundle-version="0.0.0", @@ -23,10 +22,12 @@ Require-Bundle: org.slf4j.api;bundle-version="0.0.0", org.eclipse.e4.ui.model.workbench;bundle-version="0.0.0", org.eclipse.e4.ui.services;bundle-version="0.0.0", org.eclipse.e4.ui.di;bundle-version="0.0.0", + org.eclipse.passage.lic.emf;bundle-version="0.0.0", org.eclipse.passage.lic.equinox;bundle-version="0.0.0";visibility:=reexport, org.eclipse.passage.lic.jface;bundle-version="0.0.0";visibility:=reexport, org.eclipse.passage.lic.products.model;bundle-version="0.0.0";visibility:=reexport, - org.eclipse.passage.lic.emf;bundle-version="0.0.0" + org.eclipse.passage.loc.api;bundle-version="0.5.200", + org.slf4j.api;bundle-version="0.0.0" Import-Package: javax.annotation;version="1.0.0";resolution:=optional, javax.inject;version="1.0.0" Export-Package: org.eclipse.passage.loc.internal.workbench;x-internal:=true, diff --git a/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/internal/workbench/i18n/WorkbenchMessages.java b/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/internal/workbench/i18n/WorkbenchMessages.java index 41b0fb8ef..4d9a298d7 100644 --- a/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/internal/workbench/i18n/WorkbenchMessages.java +++ b/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/internal/workbench/i18n/WorkbenchMessages.java @@ -16,6 +16,9 @@ public class WorkbenchMessages extends NLS { private static final String BUNDLE_NAME = "org.eclipse.passage.loc.internal.workbench.i18n.WorkbenchMessages"; //$NON-NLS-1$ + public static String Appearance_e_null_image; + public static String Appearance_e_null_labels; + public static String Appearance_e_null_title; public static String CreateFileWizard_q_exists_message; public static String CreateFileWizard_q_exists_title; public static String CreateFileWizardPage_button_browse; @@ -36,6 +39,9 @@ public class WorkbenchMessages extends NLS { public static String LocWokbench_e_saving; public static String RedoHandler_label_base; public static String RedoHandler_label_pattern; + public static String SelectFromDialog_e_null_appearance; + public static String SelectFromDialog_e_null_initial; + public static String SelectFromDialog_e_null_shell; public static String UndoHandler_label_base; public static String UndoHandler_label_handler; static { diff --git a/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/internal/workbench/i18n/WorkbenchMessages.properties b/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/internal/workbench/i18n/WorkbenchMessages.properties index 71a413037..709877b34 100644 --- a/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/internal/workbench/i18n/WorkbenchMessages.properties +++ b/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/internal/workbench/i18n/WorkbenchMessages.properties @@ -11,6 +11,9 @@ # ArSysOp - initial API and implementation ############################################################################### +Appearance_e_null_title=Title must not be null +Appearance_e_null_image=Image must not be null +Appearance_e_null_labels=LabelProvider must not be null CreateFileWizard_q_exists_message=The file "%s" already exists. Do you want to replace the existing file? CreateFileWizard_q_exists_title=Question CreateFileWizardPage_button_browse=Browse ... @@ -31,5 +34,8 @@ LocWokbench_e_nothing_to_select=Nothing to select from LocWokbench_e_saving=Error saving resource RedoHandler_label_base=Redo RedoHandler_label_pattern={0} {1} +SelectFromDialog_e_null_appearance=Appearance must not be null +SelectFromDialog_e_null_initial=Initial object must not be null +SelectFromDialog_e_null_shell=Shell must not be null UndoHandler_label_base=Undo UndoHandler_label_handler={0} {1} diff --git a/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/jface/dialogs/Appearance.java b/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/jface/dialogs/Appearance.java new file mode 100644 index 000000000..5486f0f96 --- /dev/null +++ b/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/jface/dialogs/Appearance.java @@ -0,0 +1,103 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0/. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.loc.jface.dialogs; + +import java.util.Objects; + +import org.eclipse.jface.viewers.LabelProvider; +import org.eclipse.passage.lic.jface.resource.LicensingImages; +import org.eclipse.passage.loc.internal.workbench.i18n.WorkbenchMessages; +import org.eclipse.swt.graphics.Image; + +/** + * Encapsulates dialog appearance to reduce the number of arguments in methods + * + * @since 0.6 + * + */ +public class Appearance { + + private final String title; + private final Image image; + private final LabelProvider labelProvider; + + /** + * Creates the dialog appearance descriptor with the given non-null + * title, default image and default label provider will be used + * + * @param title title for dialog, should not be null + * @param image image for dialog, should not be null + * + */ + public Appearance(String title) { + this(title, LicensingImages.getImageRegistry().get(LicensingImages.IMG_DEFAULT)); + } + + /** + * Creates the dialog appearance descriptor with the given non-null + * title and image, default label provider will be used + * + * @param title title for dialog, should not be null + * @param image image for dialog, should not be null + * + */ + public Appearance(String title, Image image) { + this(title, image, new LabelProvider()); + } + + /** + * Creates the dialog appearance descriptor with the given non-null + * title, image, and label provider + * + * @param title title for dialog, should not be null + * @param image image for dialog, should not be null + * @param labels label provider for dialog, should not be null + * + */ + public Appearance(String title, Image image, LabelProvider labels) { + Objects.requireNonNull(title, WorkbenchMessages.Appearance_e_null_title); + Objects.requireNonNull(title, WorkbenchMessages.Appearance_e_null_image); + Objects.requireNonNull(title, WorkbenchMessages.Appearance_e_null_labels); + this.title = title; + this.image = image; + this.labelProvider = labels; + } + + /** + * The title to use for dialog's shell + * + * @return non-null title + */ + public String title() { + return title; + } + + /** + * The image to use for dialog's shell + * + * @return non-null image + */ + public Image image() { + return image; + } + + /** + * The {@link LabelProvider} to use for dialog + * + * @return non-null label provider + */ + public LabelProvider labelProvider() { + return labelProvider; + } + +} diff --git a/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/workbench/LocWokbench.java b/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/workbench/LocWokbench.java index a5e83e56f..8783e0bb0 100644 --- a/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/workbench/LocWokbench.java +++ b/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/workbench/LocWokbench.java @@ -134,15 +134,6 @@ public static void switchPerspective(IEclipseContext eclipseContext, String pers } } - public static C selectClassifier(IEclipseContext context, String classifier, String title, Iterable input, - C initial, Class clazz) { - Object selected = selectClassifier(context, classifier, title, input, initial); - if (clazz.isInstance(selected)) { - return clazz.cast(selected); - } - return null; - } - public static Object selectClassifier(IEclipseContext context, String classifier, String title, Iterable input, C initial) { Shell shell = context.get(Shell.class); diff --git a/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/workbench/SelectFromDialog.java b/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/workbench/SelectFromDialog.java new file mode 100644 index 000000000..78884f91a --- /dev/null +++ b/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/workbench/SelectFromDialog.java @@ -0,0 +1,87 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0/. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.loc.workbench; + +import java.util.Collections; +import java.util.Objects; +import java.util.Optional; +import java.util.function.Function; + +import org.eclipse.emf.ecore.EClass; +import org.eclipse.jface.dialogs.Dialog; +import org.eclipse.jface.viewers.LabelProvider; +import org.eclipse.passage.loc.internal.workbench.i18n.WorkbenchMessages; +import org.eclipse.passage.loc.jface.dialogs.Appearance; +import org.eclipse.passage.loc.jface.dialogs.FilteredSelectionDialog; +import org.eclipse.passage.loc.jface.dialogs.LabelSearchFilter; +import org.eclipse.swt.widgets.Shell; + +/** + * Selects the classifier from the given input with + * {@link FilteredSelectionDialog} + * + * @param classifier to be selected see {@link EClass#getName()} + */ +public class SelectFromDialog implements Function, Optional> { + + private final FilteredSelectionDialog dialog; + + /** + * + * @param shell the {@link Shell} to use for + * {@link FilteredSelectionDialog}, must not be + * null + * @param appearance the title, image and {@link LabelProvider} to use for + * {@link FilteredSelectionDialog}, must not be + * null + */ + public SelectFromDialog(Shell shell, Appearance appearance) { + Objects.requireNonNull(shell, WorkbenchMessages.SelectFromDialog_e_null_shell); + Objects.requireNonNull(appearance, WorkbenchMessages.SelectFromDialog_e_null_appearance); + this.dialog = new FilteredSelectionDialog(shell, false, new LabelSearchFilter()); + dialog.setTitle(appearance.title()); + dialog.setImage(appearance.image()); + dialog.setLabelProvider(appearance.labelProvider()); + } + + /** + * + * @param shell the {@link Shell} to use for + * {@link FilteredSelectionDialog}, must not be + * null + * @param appearance the title, image and {@link LabelProvider} to use for + * {@link FilteredSelectionDialog}, must not be + * null + * @param initial the object to be a default choice for + * {@link FilteredSelectionDialog}, must not be + * null + */ + public SelectFromDialog(Shell shell, Appearance appearance, C initial) { + this(shell, appearance); + Objects.requireNonNull(initial, WorkbenchMessages.SelectFromDialog_e_null_initial); + dialog.setInitialSelection(Collections.singletonList(initial)); + } + + /** + * Returns the selected object or {@link Optional#empty()} + */ + @Override + public Optional apply(Iterable input) { + dialog.setInput(input); + if (dialog.open() == Dialog.OK) { + return dialog.getFirstResult(); + } + return Optional.empty(); + } + +} diff --git a/tests/org.eclipse.passage.loc.api.tests/src/org/eclipse/passage/loc/api/tests/ZeroOneManyTest.java b/tests/org.eclipse.passage.loc.api.tests/src/org/eclipse/passage/loc/api/tests/ZeroOneManyTest.java index 37b7b24a3..4a96f8de7 100644 --- a/tests/org.eclipse.passage.loc.api.tests/src/org/eclipse/passage/loc/api/tests/ZeroOneManyTest.java +++ b/tests/org.eclipse.passage.loc.api.tests/src/org/eclipse/passage/loc/api/tests/ZeroOneManyTest.java @@ -29,7 +29,7 @@ public class ZeroOneManyTest { @Test public void testZero() throws Exception { - ZeroOneMany zom = new ZeroOneMany<>(Collections.emptyList()); + ZeroOneMany zom = new ZeroOneMany<>(() -> Collections.emptyList()); Optional zero = zom.choose(() -> null, null); assertEquals(false, zero.isPresent()); } @@ -37,7 +37,7 @@ public void testZero() throws Exception { @Test public void testCreated() throws Exception { Object single = new Object(); - ZeroOneMany zom = new ZeroOneMany<>(Collections.emptyList()); + ZeroOneMany zom = new ZeroOneMany<>(() -> Collections.emptyList()); Optional one = zom.choose(() -> single, null); assertEquals(single, one.get()); } @@ -45,7 +45,7 @@ public void testCreated() throws Exception { @Test public void testOne() throws Exception { Object single = new Object(); - ZeroOneMany zom = new ZeroOneMany<>(Collections.singleton(single)); + ZeroOneMany zom = new ZeroOneMany<>(() -> Collections.singleton(single)); Optional one = zom.choose(null, null); assertEquals(single, one.get()); } @@ -54,7 +54,7 @@ public void testOne() throws Exception { public void testMany() throws Exception { Object first = new Object(); Object second = new Object(); - ZeroOneMany zom = new ZeroOneMany<>(Arrays.asList(first, second)); + ZeroOneMany zom = new ZeroOneMany<>(() -> Arrays.asList(first, second)); Optional one = zom.choose(null, source -> select(source, 1)); assertEquals(second, one.get()); } From 82fe05ba5d5a98a6446a95f080270b8271f6acb1 Mon Sep 17 00:00:00 2001 From: Alexander Fedorov Date: Sat, 15 Feb 2020 15:13:54 +0300 Subject: [PATCH 2/2] Bug 548112 - [Passage] create license plan from issue license wizard Change ZeroOneMany to accept Supplier Add Appearance infra Add SelectFromDialog infra Remove unused code Bump API version Signed-off-by: Alexander Fedorov --- .../src/org/eclipse/passage/loc/jface/dialogs/Appearance.java | 2 +- .../org/eclipse/passage/loc/workbench/SelectFromDialog.java | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/jface/dialogs/Appearance.java b/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/jface/dialogs/Appearance.java index 5486f0f96..d48d8673a 100644 --- a/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/jface/dialogs/Appearance.java +++ b/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/jface/dialogs/Appearance.java @@ -25,7 +25,7 @@ * @since 0.6 * */ -public class Appearance { +public final class Appearance { private final String title; private final Image image; diff --git a/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/workbench/SelectFromDialog.java b/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/workbench/SelectFromDialog.java index 78884f91a..04a7774ce 100644 --- a/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/workbench/SelectFromDialog.java +++ b/bundles/org.eclipse.passage.loc.workbench/src/org/eclipse/passage/loc/workbench/SelectFromDialog.java @@ -29,10 +29,12 @@ /** * Selects the classifier from the given input with * {@link FilteredSelectionDialog} + * + * @since 0.6 * * @param classifier to be selected see {@link EClass#getName()} */ -public class SelectFromDialog implements Function, Optional> { +public final class SelectFromDialog implements Function, Optional> { private final FilteredSelectionDialog dialog;