Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Sirius » [Properties view] Getting extra input from user
[Properties view] Getting extra input from user [message #1736198] Mon, 27 June 2016 12:53 Go to next message
Hallvard Traetteberg is currently offline Hallvard TraettebergFriend
Messages: 673
Registered: July 2009
Location: Trondheim, Norway
Senior Member
Hi,

I have an action in a reference widget that needs user input. I haven't
found a way of collecting user input from the property sheet itself, so
I ended up coding an external Java Action that gets input using a popup
dialog. Below I include two classes, the Java Action implementation and
the dialog service (coded so it can be used as a service class in a
diagram), so others may use it. I didn't find out how to pass the
selection (among referenced elements) as arguments to the action, so in
the VSM I added a Java Action Parameter named "slots" with
var:selection as the value expression, that is picked up by the code
below.

Enjoy!

Hallvard

import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Display;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.sirius.business.api.action.AbstractExternalJavaAction;
import org.eclipse.sirius.tools.api.ui.IExternalJavaAction;

public class SetSlotTypeJavaAction extends AbstractExternalJavaAction
implements IExternalJavaAction {

public void setSlotType(Slot slot, String type) {
slot.setType(type);
}

public void setSlotType(Collection<? extends EObject> slots, String type) {
for (EObject slot : slots) {
if (slot instanceof Slot) {
setSlotType((Slot) slot, type);
}
}
}

@Override
public boolean canExecute(Collection<? extends EObject> args) {
return true;
}

private DialogInputServices dialogInputServices = new DialogInputServices();

@Override
public void execute(Collection<? extends EObject> args, Map<String,
Object> options) {
Collection<EObject> slots = getOptionalParameter(options, "slots",
Collection.class);
Collection<EObject> allSlots = new ArrayList<>(args);
if (slots != null) {
allSlots.addAll(slots);
}
String type = getOptionalParameter(options, "slotType", String.class);
if (type == null) {
type = dialogInputServices.stringInput("Enter slot type");
}
if (type != null && type.trim().length() == 0) {
type = null;
}
setSlotType(allSlots, type);
}
}

public class DialogInputServices {

public String stringInput(String title, String prompt, String initial,
String def, String pattern) {
InputDialog dialog = new
InputDialog(Display.getCurrent().getActiveShell(), title, prompt,
initial, new RegexValidator(pattern));
return (dialog.open() == Window.OK ? dialog.getValue() : def);
}

public String stringInput(String title, String prompt) {
return stringInput(title, prompt, null, null, null);
}

public String stringInput(String prompt) {
return stringInput("Input String", prompt);
}

private class RegexValidator implements IInputValidator {

private final String pattern;

public RegexValidator(String pattern) {
this.pattern = pattern;
}

@Override
public String isValid(String newText) {
return (pattern == null || newText.matches(pattern) ? null : "Input
does not match " + pattern);
}
}
}

--
Hallvard Trætteberg (hal@xxxxxxxx.no)
Associate Professor, IS group, Dept. of Computer and Information
Science at the
Norwegian Univ. of Science and Technology
Re: [Properties view] Getting extra input from user [message #1736282 is a reply to message #1736198] Tue, 28 June 2016 08:30 Go to previous message
Laurent Fasani is currently offline Laurent FasaniFriend
Messages: 182
Registered: October 2014
Senior Member
Le 27/06/2016 à 14:53, Hallvard Trætteberg a écrit :
> Hi,
Hi
>
> I have an action in a reference widget that needs user input. I haven't
> found a way of collecting user input from the property sheet itself, so
> I ended up coding an external Java Action that gets input using a popup
> dialog. Below I include two classes, the Java Action implementation and
> the dialog service (coded so it can be used as a service class in a
> diagram), so others may use it. I didn't find out how to pass the
> selection (among referenced elements) as arguments to the action, so in
> the VSM I added a Java Action Parameter named "slots" with var:selection
> as the value expression, that is picked up by the code below.
>
> Enjoy!
So what? :) did you succeed in doing what you want or do you still have
a question?
Note that you can define user variable from a selection of model
elements in a dialog box using "Select Model Element Variables" [1]

[1]
http://help.eclipse.org/mars/topic/org.eclipse.sirius.doc/doc/specifier/general/ToolsSpecification.html?cp=69_2_5

Regards
Laurent
>
> Hallvard
>
> import org.eclipse.jface.dialogs.IInputValidator;
> import org.eclipse.jface.dialogs.InputDialog;
> import org.eclipse.jface.window.Window;
> import org.eclipse.swt.widgets.Display;
>
> import java.util.ArrayList;
> import java.util.Collection;
> import java.util.Map;
>
> import org.eclipse.emf.ecore.EObject;
> import org.eclipse.sirius.business.api.action.AbstractExternalJavaAction;
> import org.eclipse.sirius.tools.api.ui.IExternalJavaAction;
>
> public class SetSlotTypeJavaAction extends AbstractExternalJavaAction
> implements IExternalJavaAction {
>
> public void setSlotType(Slot slot, String type) {
> slot.setType(type);
> }
>
> public void setSlotType(Collection<? extends EObject> slots, String
> type) {
> for (EObject slot : slots) {
> if (slot instanceof Slot) {
> setSlotType((Slot) slot, type);
> }
> }
> }
>
> @Override
> public boolean canExecute(Collection<? extends EObject> args) {
> return true;
> }
>
> private DialogInputServices dialogInputServices = new
> DialogInputServices();
>
> @Override
> public void execute(Collection<? extends EObject> args, Map<String,
> Object> options) {
> Collection<EObject> slots = getOptionalParameter(options,
> "slots", Collection.class);
> Collection<EObject> allSlots = new ArrayList<>(args);
> if (slots != null) {
> allSlots.addAll(slots);
> }
> String type = getOptionalParameter(options, "slotType",
> String.class);
> if (type == null) {
> type = dialogInputServices.stringInput("Enter slot type");
> }
> if (type != null && type.trim().length() == 0) {
> type = null;
> }
> setSlotType(allSlots, type);
> }
> }
>
> public class DialogInputServices {
>
> public String stringInput(String title, String prompt, String
> initial, String def, String pattern) {
> InputDialog dialog = new
> InputDialog(Display.getCurrent().getActiveShell(), title, prompt,
> initial, new RegexValidator(pattern));
> return (dialog.open() == Window.OK ? dialog.getValue() : def);
> }
>
> public String stringInput(String title, String prompt) {
> return stringInput(title, prompt, null, null, null);
> }
>
> public String stringInput(String prompt) {
> return stringInput("Input String", prompt);
> }
>
> private class RegexValidator implements IInputValidator {
>
> private final String pattern;
>
> public RegexValidator(String pattern) {
> this.pattern = pattern;
> }
>
> @Override
> public String isValid(String newText) {
> return (pattern == null || newText.matches(pattern) ? null :
> "Input does not match " + pattern);
> }
> }
> }
>


Laurent Fasani - Obeo
Need training or professional services for Sirius?
http://www.obeodesigner.com/sirius
Previous Topic:Problems Getting Started w. Sirius 4.0/Neon on a Mac
Next Topic:Issue with compartments using Vertical Stacks
Goto Forum:
  


Current Time: Thu Apr 25 09:37:01 GMT 2024

Powered by FUDForum. Page generated in 0.03143 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top