Search in sources :

Example 1 with NamedElement

use of org.obeonetwork.dsl.cinematic.NamedElement in project InformationSystem by ObeoNetwork.

the class FlowEditLabelSwitch method caseSubflowState.

@Override
public EObject caseSubflowState(SubflowState subflowState) {
    String flowName = newLabel.trim();
    // Check if the name has changed
    if (flowName != null && subflowState.getSubflow() != null && subflowState.getSubflow().getName() != null) {
        if (flowName.equalsIgnoreCase(subflowState.getSubflow().getName().trim())) {
            // Nothing to do
            return subflowState;
        }
    }
    List<Flow> foundFlows = FlowsUtil.getFlowsWithName(subflowState, flowName);
    if (foundFlows.isEmpty()) {
        // No flow found, let's notify the user
        MessageDialog.openWarning(Display.getCurrent().getActiveShell(), "Unable to find flow", "No found flow with this name");
    } else if (foundFlows.size() == 1) {
        subflowState.setSubflow(foundFlows.get(0));
    } else {
        // Several flows with the same name, we let the user choose the correct one
        Object result = UiUtil.getUserChoiceWithinList("Select a flow", "Select a flow among the flows with the specified name", foundFlows.toArray(), new LabelProvider() {

            @Override
            public Image getImage(Object element) {
                URL imageURL = null;
                if (element instanceof Flow) {
                    imageURL = (URL) CinematicEditPlugin.INSTANCE.getImage("full/obj16/Flow");
                }
                if (imageURL != null) {
                    ImageDescriptor imgDesc = ImageDescriptor.createFromURL(imageURL);
                    if (imgDesc != null) {
                        return imgDesc.createImage();
                    }
                }
                return null;
            }

            @Override
            public String getText(Object element) {
                String label = "";
                if (element instanceof Flow) {
                    Flow flow = (Flow) element;
                    label = getLabel(flow);
                }
                return label;
            }

            private String getLabel(EObject eObject) {
                String label = "";
                if (eObject.eContainer() != null) {
                    String parentLabel = getLabel(eObject.eContainer());
                    if (parentLabel != null) {
                        label = parentLabel + "::";
                    }
                }
                if (eObject instanceof NamedElement) {
                    label += ((NamedElement) eObject).getName();
                }
                return label;
            }
        });
        if (result instanceof Flow) {
            subflowState.setSubflow((Flow) result);
        }
    }
    return subflowState;
}
Also used : EObject(org.eclipse.emf.ecore.EObject) EObject(org.eclipse.emf.ecore.EObject) ImageDescriptor(org.eclipse.jface.resource.ImageDescriptor) LabelProvider(org.eclipse.jface.viewers.LabelProvider) NamedElement(org.obeonetwork.dsl.cinematic.NamedElement) URL(java.net.URL) Flow(org.obeonetwork.dsl.cinematic.flow.Flow)

Example 2 with NamedElement

use of org.obeonetwork.dsl.cinematic.NamedElement in project InformationSystem by ObeoNetwork.

the class CinematicEcoreServices method duplicateCinematicElement.

@SuppressWarnings("unchecked")
public static EObject duplicateCinematicElement(EObject context) {
    EObject clone = CopierUtils.copy(context);
    if (clone instanceof NamedElement) {
        NamedElement namedElement = (NamedElement) clone;
        namedElement.setName(namedElement.getName() + " copy");
    }
    EStructuralFeature containmentFeature = context.eContainingFeature();
    Object feature = context.eContainer().eGet(containmentFeature, true);
    if (feature instanceof Collection) {
        ((Collection<EObject>) feature).add(clone);
    }
    return clone;
}
Also used : EObject(org.eclipse.emf.ecore.EObject) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) Collection(java.util.Collection) EObject(org.eclipse.emf.ecore.EObject) NamedElement(org.obeonetwork.dsl.cinematic.NamedElement)

Example 3 with NamedElement

use of org.obeonetwork.dsl.cinematic.NamedElement in project InformationSystem by ObeoNetwork.

the class FlowEditLabelSwitch method caseTransition.

@Override
public EObject caseTransition(Transition transition) {
    String eventPart = null;
    String guard = null;
    Collection<String> notFoundEventNames = new ArrayList<String>();
    // We search for '[' and ']' characters
    int openingBracketPos = newLabel.indexOf("[");
    int closingBracketPos = newLabel.lastIndexOf("]");
    if (openingBracketPos != -1 && openingBracketPos != newLabel.length() - 1) {
        // we consider there is a closing bracket at the end of the string
        if (closingBracketPos == -1 || closingBracketPos < openingBracketPos) {
            closingBracketPos = newLabel.length();
        }
        guard = newLabel.substring(openingBracketPos + 1, closingBracketPos).trim();
        // We now consider the rest of the string i.e. trigger events
        eventPart = newLabel.substring(0, openingBracketPos).trim();
    } else {
        guard = null;
        eventPart = newLabel.trim();
    }
    // Get the containing flow
    if (transition.eContainer() != null && transition.eContainer() instanceof Flow) {
        Flow flow = (Flow) transition.eContainer();
        String[] events = eventPart.split(",");
        // Retrieve associated events and possible event types
        Collection<Event> associatedEvents = FlowsUtil.getAssociatedEvents(flow);
        Collection<WidgetEventTypeAndAbstractViewElement> possibleEvents = FlowsUtil.getPossibleWidgetEvents(flow);
        Collection<Object> eventsToBeAssociated = new ArrayList<Object>();
        for (String event : events) {
            List<Object> candidatesEvents = new ArrayList<Object>();
            String eventName = event.trim();
            if (eventName != null && !eventName.equals("")) {
                // Test if the event was already associated
                boolean alreadyAssociatedEvent = false;
                for (Event alreadyAssociated : transition.getOn()) {
                    if (eventName.equals(alreadyAssociated.getName().trim())) {
                        alreadyAssociatedEvent = true;
                        break;
                    }
                }
                if (alreadyAssociatedEvent) {
                    break;
                }
                for (Event associatedEvent : associatedEvents) {
                    if (eventName.equalsIgnoreCase(associatedEvent.getName().trim())) {
                        candidatesEvents.add(associatedEvent);
                    }
                }
                for (WidgetEventTypeAndAbstractViewElement widgetEventTypeAndElement : possibleEvents) {
                    if (eventName.equalsIgnoreCase(widgetEventTypeAndElement.getWidgetEventType().getName().trim())) {
                        candidatesEvents.add(widgetEventTypeAndElement);
                    }
                }
            }
            // Now, let's see if we had some results
            if (candidatesEvents.isEmpty()) {
                notFoundEventNames.add(eventName);
            } else if (candidatesEvents.size() == 1 && candidatesEvents.get(0) instanceof Event) {
                eventsToBeAssociated.add((Event) candidatesEvents.get(0));
            } else if (candidatesEvents.size() == 1 && candidatesEvents.get(0) instanceof WidgetEventTypeAndAbstractViewElement) {
                WidgetEventTypeAndAbstractViewElement info = (WidgetEventTypeAndAbstractViewElement) candidatesEvents.get(0);
                boolean create = MessageDialog.openQuestion(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Create a new Event ?", "An event named '" + eventName + "' does not exist yet but a ViewEvent could be created for the widget '" + info.getAbstractViewElement().getName() + "'.\n" + "Do you want to create this event ?");
                if (create) {
                    eventsToBeAssociated.add(info);
                }
            } else {
                Object result = UiUtil.getUserChoiceWithinList("Select an event", "You could pick an existing event or create a new one for '" + eventName + "'", candidatesEvents.toArray(), new LabelProvider() {

                    @Override
                    public Image getImage(Object element) {
                        URL imageURL = null;
                        if (element instanceof FlowEvent) {
                            imageURL = (URL) CinematicEditPlugin.INSTANCE.getImage("full/obj16/FlowEvent");
                        } else if (element instanceof ViewEvent) {
                            imageURL = (URL) CinematicEditPlugin.INSTANCE.getImage("full/obj16/ViewEvent");
                        } else if (element instanceof WidgetEventTypeAndAbstractViewElement) {
                            imageURL = (URL) CinematicEditPlugin.INSTANCE.getImage("full/obj16/NewViewEvent");
                        }
                        if (imageURL != null) {
                            ImageDescriptor imgDesc = ImageDescriptor.createFromURL(imageURL);
                            if (imgDesc != null) {
                                return imgDesc.createImage();
                            }
                        }
                        return null;
                    }

                    @Override
                    public String getText(Object element) {
                        String label = null;
                        if (element instanceof Event) {
                            Event event = (Event) element;
                            label = getLabel(event);
                        } else if (element instanceof WidgetEventTypeAndAbstractViewElement) {
                            AbstractViewElement viewElement = ((WidgetEventTypeAndAbstractViewElement) element).getAbstractViewElement();
                            WidgetEventType eventType = ((WidgetEventTypeAndAbstractViewElement) element).getWidgetEventType();
                            label = getLabel(viewElement) + "::" + eventType.getName() + " (will create a new event)";
                        }
                        return label;
                    }

                    private String getLabel(EObject eObject) {
                        String label = "";
                        if (eObject.eContainer() != null) {
                            String parentLabel = getLabel(eObject.eContainer());
                            if (parentLabel != null) {
                                label = parentLabel + "::";
                            }
                        }
                        if (eObject instanceof NamedElement) {
                            label += ((NamedElement) eObject).getName();
                        }
                        return label;
                    }
                });
                if (result != null) {
                    eventsToBeAssociated.add(result);
                }
            }
        }
        // Warn and ask the user if we have to continue
        boolean doIt = true;
        if (!notFoundEventNames.isEmpty()) {
            String label = "";
            for (String name : notFoundEventNames) {
                label += "'" + name + "',";
            }
            // Remove last comma
            label = label.substring(0, label.length() - 1);
            doIt = MessageDialog.openQuestion(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Edit the transition", "No event found for " + label + ".\n" + "Edit the transition anyway ?");
        }
        if (doIt) {
            transition.setGuard(guard);
            // Let's associate the events now
            if (!eventsToBeAssociated.isEmpty()) {
                transition.getOn().clear();
                for (Object object : eventsToBeAssociated) {
                    if (object instanceof Event) {
                        transition.getOn().add((Event) object);
                    } else if (object instanceof WidgetEventTypeAndAbstractViewElement) {
                        AbstractViewElement viewElement = ((WidgetEventTypeAndAbstractViewElement) object).getAbstractViewElement();
                        WidgetEventType eventType = ((WidgetEventTypeAndAbstractViewElement) object).getWidgetEventType();
                        ViewEvent event = ViewFactory.eINSTANCE.createViewEvent();
                        event.setName(eventType.getName());
                        event.setType(eventType);
                        viewElement.getEvents().add(event);
                        transition.getOn().add(event);
                    }
                }
            }
        }
    }
    return transition;
}
Also used : ArrayList(java.util.ArrayList) Image(org.eclipse.swt.graphics.Image) URL(java.net.URL) AbstractViewElement(org.obeonetwork.dsl.cinematic.view.AbstractViewElement) WidgetEventTypeAndAbstractViewElement(org.obeonetwork.dsl.cinematic.design.services.flows.WidgetEventTypeAndAbstractViewElement) ViewEvent(org.obeonetwork.dsl.cinematic.view.ViewEvent) EObject(org.eclipse.emf.ecore.EObject) ImageDescriptor(org.eclipse.jface.resource.ImageDescriptor) Flow(org.obeonetwork.dsl.cinematic.flow.Flow) FlowEvent(org.obeonetwork.dsl.cinematic.flow.FlowEvent) WidgetEventTypeAndAbstractViewElement(org.obeonetwork.dsl.cinematic.design.services.flows.WidgetEventTypeAndAbstractViewElement) WidgetEventType(org.obeonetwork.dsl.cinematic.toolkits.WidgetEventType) ViewEvent(org.obeonetwork.dsl.cinematic.view.ViewEvent) Event(org.obeonetwork.dsl.cinematic.Event) FlowEvent(org.obeonetwork.dsl.cinematic.flow.FlowEvent) EObject(org.eclipse.emf.ecore.EObject) LabelProvider(org.eclipse.jface.viewers.LabelProvider) NamedElement(org.obeonetwork.dsl.cinematic.NamedElement)

Aggregations

EObject (org.eclipse.emf.ecore.EObject)3 NamedElement (org.obeonetwork.dsl.cinematic.NamedElement)3 URL (java.net.URL)2 ImageDescriptor (org.eclipse.jface.resource.ImageDescriptor)2 LabelProvider (org.eclipse.jface.viewers.LabelProvider)2 Flow (org.obeonetwork.dsl.cinematic.flow.Flow)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)1 Image (org.eclipse.swt.graphics.Image)1 Event (org.obeonetwork.dsl.cinematic.Event)1 WidgetEventTypeAndAbstractViewElement (org.obeonetwork.dsl.cinematic.design.services.flows.WidgetEventTypeAndAbstractViewElement)1 FlowEvent (org.obeonetwork.dsl.cinematic.flow.FlowEvent)1 WidgetEventType (org.obeonetwork.dsl.cinematic.toolkits.WidgetEventType)1 AbstractViewElement (org.obeonetwork.dsl.cinematic.view.AbstractViewElement)1 ViewEvent (org.obeonetwork.dsl.cinematic.view.ViewEvent)1