Skip to content

Commit

Permalink
Merge pull request #134 from eclipse/548112/select_from_dialog
Browse files Browse the repository at this point in the history
Bug 548112 - [Passage] create license plan from issue license wizard
  • Loading branch information
eparovyshnaya authored Feb 15, 2020
2 parents 3ad3c40 + 82fe05b commit ddbe042
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 21 deletions.
2 changes: 1 addition & 1 deletion bundles/org.eclipse.passage.loc.api/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
*/
public final class ZeroOneMany<C> {

private final Iterable<C> input;
private final Supplier<Iterable<C>> supplier;

public ZeroOneMany(Iterable<C> input) {
this.input = input;
public ZeroOneMany(Supplier<Iterable<C>> input) {
this.supplier = input;
}

public Optional<C> choose(Supplier<C> create, Function<Iterable<C>, Optional<C>> select) {
Iterable<C> input = supplier.get();
Iterator<C> iterator = input.iterator();
if (!iterator.hasNext()) {
return Optional.ofNullable(create.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...
Expand All @@ -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}
Original file line number Diff line number Diff line change
@@ -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 final class Appearance {

private final String title;
private final Image image;
private final LabelProvider labelProvider;

/**
* Creates the dialog appearance descriptor with the given non-<code>null</code>
* title, default image and default label provider will be used
*
* @param title title for dialog, should not be <code>null</code>
* @param image image for dialog, should not be <code>null</code>
*
*/
public Appearance(String title) {
this(title, LicensingImages.getImageRegistry().get(LicensingImages.IMG_DEFAULT));
}

/**
* Creates the dialog appearance descriptor with the given non-<code>null</code>
* title and image, default label provider will be used
*
* @param title title for dialog, should not be <code>null</code>
* @param image image for dialog, should not be <code>null</code>
*
*/
public Appearance(String title, Image image) {
this(title, image, new LabelProvider());
}

/**
* Creates the dialog appearance descriptor with the given non-<code>null</code>
* title, image, and label provider
*
* @param title title for dialog, should not be <code>null</code>
* @param image image for dialog, should not be <code>null</code>
* @param labels label provider for dialog, should not be <code>null</code>
*
*/
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-<code>null</code> title
*/
public String title() {
return title;
}

/**
* The image to use for dialog's shell
*
* @return non-<code>null</code> image
*/
public Image image() {
return image;
}

/**
* The {@link LabelProvider} to use for dialog
*
* @return non-<code>null</code> label provider
*/
public LabelProvider labelProvider() {
return labelProvider;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,6 @@ public static void switchPerspective(IEclipseContext eclipseContext, String pers
}
}

public static <C> C selectClassifier(IEclipseContext context, String classifier, String title, Iterable<C> input,
C initial, Class<C> clazz) {
Object selected = selectClassifier(context, classifier, title, input, initial);
if (clazz.isInstance(selected)) {
return clazz.cast(selected);
}
return null;
}

public static <C> Object selectClassifier(IEclipseContext context, String classifier, String title,
Iterable<C> input, C initial) {
Shell shell = context.get(Shell.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*******************************************************************************
* 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}
*
* @since 0.6
*
* @param <C> classifier to be selected see {@link EClass#getName()}
*/
public final class SelectFromDialog<C> implements Function<Iterable<C>, Optional<C>> {

private final FilteredSelectionDialog<C> dialog;

/**
*
* @param shell the {@link Shell} to use for
* {@link FilteredSelectionDialog}, must not be
* <code>null</code>
* @param appearance the title, image and {@link LabelProvider} to use for
* {@link FilteredSelectionDialog}, must not be
* <code>null</code>
*/
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<C>(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
* <code>null</code>
* @param appearance the title, image and {@link LabelProvider} to use for
* {@link FilteredSelectionDialog}, must not be
* <code>null</code>
* @param initial the object to be a default choice for
* {@link FilteredSelectionDialog}, must not be
* <code>null</code>
*/
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<C> apply(Iterable<C> input) {
dialog.setInput(input);
if (dialog.open() == Dialog.OK) {
return dialog.getFirstResult();
}
return Optional.empty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@ public class ZeroOneManyTest {

@Test
public void testZero() throws Exception {
ZeroOneMany<Object> zom = new ZeroOneMany<>(Collections.emptyList());
ZeroOneMany<Object> zom = new ZeroOneMany<>(() -> Collections.emptyList());
Optional<Object> zero = zom.choose(() -> null, null);
assertEquals(false, zero.isPresent());
}

@Test
public void testCreated() throws Exception {
Object single = new Object();
ZeroOneMany<Object> zom = new ZeroOneMany<>(Collections.emptyList());
ZeroOneMany<Object> zom = new ZeroOneMany<>(() -> Collections.emptyList());
Optional<Object> one = zom.choose(() -> single, null);
assertEquals(single, one.get());
}

@Test
public void testOne() throws Exception {
Object single = new Object();
ZeroOneMany<Object> zom = new ZeroOneMany<>(Collections.singleton(single));
ZeroOneMany<Object> zom = new ZeroOneMany<>(() -> Collections.singleton(single));
Optional<Object> one = zom.choose(null, null);
assertEquals(single, one.get());
}
Expand All @@ -54,7 +54,7 @@ public void testOne() throws Exception {
public void testMany() throws Exception {
Object first = new Object();
Object second = new Object();
ZeroOneMany<Object> zom = new ZeroOneMany<>(Arrays.asList(first, second));
ZeroOneMany<Object> zom = new ZeroOneMany<>(() -> Arrays.asList(first, second));
Optional<Object> one = zom.choose(null, source -> select(source, 1));
assertEquals(second, one.get());
}
Expand Down

0 comments on commit ddbe042

Please sign in to comment.