Commit 8094b214 authored by Dongyue Mou's avatar Dongyue Mou
Browse files

new ui plugin for mira

parent 7fa88c7e
package org.fortiss.af3.mira.ui;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
/**
* The activator class controls the plug-in life cycle.
*/
public class AF3MiraUIActivator extends AbstractUIPlugin {
/** The plug-in ID. */
public static final String PLUGIN_ID = "org.fortiss.af3.mira.ui"; //$NON-NLS-1$
/** The shared instance. */
private static AF3MiraUIActivator plugin;
/** Constructor. */
public AF3MiraUIActivator() {
// do nothing
}
/** {@inheritDoc} */
@Override
public void start(BundleContext context) throws Exception {
super.start(context);
plugin = this;
}
/** {@inheritDoc} */
@Override
public void stop(BundleContext context) throws Exception {
plugin = null;
super.stop(context);
}
/** Returns the shared instance. */
public static AF3MiraUIActivator getDefault() {
return plugin;
}
}
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2011 ForTISS GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.af3.mira.ui.dialog;
import org.eclipse.jface.dialogs.IInputValidator;
import org.fortiss.af3.mira.model.usecase.Actor;
import org.fortiss.af3.mira.model.usecase.UseCase;
/**
*
* @author mou
* @author $Author$
* @version $Rev$
* @ConQAT.Rating RED Hash:
*/
public class ActorNameValidator implements IInputValidator {
/** the use case for the validation */
private UseCase uc;
/** Create an actor-name-validator for the given use case */
public ActorNameValidator(UseCase usecase) {
uc = usecase;
}
/** {@inheritDoc} */
@Override
public String isValid(String newText) {
if (newText == null || newText.trim().length() == 0)
return "The name may not be empty!";
for (Actor a : uc.getActor())
if (a.getName().equals(newText))
return "The name is already used by another actor!";
return null;
}
}
\ No newline at end of file
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2011 ForTISS GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.af3.mira.ui.dialog;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.ResourceManager;
import org.fortiss.af3.mira.model.usecase.Actor;
import org.fortiss.af3.mira.model.usecase.UseCase;
import org.fortiss.af3.mira.model.usecase.UsecaseFactory;
import org.fortiss.af3.mira.ui.AF3MiraUIActivator;
import org.fortiss.af3.mira.ui.dialog.base.ElementTreeSingleSelectDialog;
import org.fortiss.af3.mira.ui.dialog.base.IDialogEditSupport;
import org.fortiss.af3.mira.ui.dialog.base.TreeContentProviderBase;
import org.fortiss.tooling.kernel.service.ICommandStackService;
import org.fortiss.tooling.kernel.ui.service.IModelElementHandlerService;
/**
*
* @author Leonhard Uden
* @author Dongyue Mou
* @author $Author$
* @version $Rev$
* @levd.rating RED Rev:
*/
public class ActorSelectionDialog extends ElementTreeSingleSelectDialog {
/**
* @param parentShell
* the shell object
* @param usecases
* the use cases
* @param actor
* the selected actor
*/
public ActorSelectionDialog(final Shell parentShell,
final List<UseCase> usecases, Actor actor) {
super(parentShell, usecases, actor, new TreeContentProviderBase() {
@Override
public Object getParent(Object element) {
if (element instanceof UseCase)
return null;
else if (element instanceof Actor)
return ((Actor) element).eContainer();
else
throw new IllegalArgumentException();
}
@Override
public Object[] getChildren(Object parentElement) {
if (parentElement instanceof UseCase)
return ((UseCase) parentElement).getActor();
else if (parentElement instanceof Actor)
return new Object[0];
else
throw new IllegalArgumentException();
}
}, new LabelProvider() {
/** {@inheritDoc} */
@Override
public String getText(Object element) {
if (element instanceof UseCase)
return IModelElementHandlerService.INSTANCE
.getModelElementHandler((UseCase) element).getName(
(UseCase) element);
else if (element instanceof Actor)
return ((Actor) element).getName();
else
throw new IllegalArgumentException();
}
/** {@inheritDoc} */
@Override
public Image getImage(Object element) {
if (element instanceof UseCase)
return ResourceManager.getPluginImage(
AF3MiraUIActivator.PLUGIN_ID,
"/icons/Icon_UseCase.png");
else if (element instanceof Actor)
return ResourceManager
.getPluginImage(AF3MiraUIActivator.PLUGIN_ID,
"/icons/Icon_Actor.png");
else
throw new IllegalArgumentException();
}
}, new IDialogEditSupport() {
@Override
public void editElement(Object selected) {
if (selected instanceof Actor) {
final Actor a = (Actor) selected;
final InputDialog input = new InputDialog(parentShell,
"Edit an actor",
"Please type a name for the actor", a.getName(),
new ActorNameValidator((UseCase) a.eContainer()));
if (Window.OK == input.open()) {
ICommandStackService.INSTANCE.runAsCommand(
((EObject) selected).eContainer(),
new Runnable() {
@Override
public void run() {
a.setName(input.getValue());
}
});
}
} else {
MessageDialog.openInformation(parentShell, "Edit an actor",
"Please select an actor");
}
}
@Override
public boolean deleteElement(final Object selected) {
if (MessageDialog.openConfirm(
parentShell,
"Delete Actor",
"Do you want to delete the actor "
+ ((Actor) selected).getName() + "?")) {
ICommandStackService.INSTANCE.runAsCommand(
((EObject) selected).eContainer(), new Runnable() {
@Override
public void run() {
EcoreUtil.delete((EObject) selected);
}
});
return true;
}
return false;
}
@Override
public Object createElement(Object selected) {
if (selected instanceof UseCase) {
final UseCase uc = (UseCase) selected;
final InputDialog input = new InputDialog(parentShell,
"Create an actor",
"Please type a name for the new actor", "Actor_"
+ (uc.getActorLength() + 1),
new ActorNameValidator(uc));
if (Window.OK == input.open()) {
FutureTask<Actor> cmd = new FutureTask<Actor>(
new Callable<Actor>() {
@Override
public Actor call() throws Exception {
Actor a = UsecaseFactory.eINSTANCE
.createActor();
a.setName(input.getValue());
uc.getActorList().add(a);
return a;
}
});
try {
ICommandStackService.INSTANCE.runAsCommand(uc, cmd);
return cmd.get();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
} else {
MessageDialog.openInformation(parentShell,
"Create an actor", "Please select a use case");
}
return null;
}
});
}
/**
* @param parentShell
* the shell
* @param uc
* the use case
* @return the actually selected actor or null if nothing is selected
*/
public static Actor open(Shell parentShell, UseCase uc) {
ActorSelectionDialog asd = new ActorSelectionDialog(parentShell,
Arrays.asList(uc), null);
if (Window.OK == asd.open())
return (Actor) asd.getSelectedElement();
return null;
}
}
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2011 ForTISS GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.af3.mira.ui.dialog;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.ResourceManager;
import org.fortiss.af3.micro.model.architecture.component.Component;
import org.fortiss.af3.micro.model.architecture.component.ComponentArchitecture;
import org.fortiss.af3.mira.ui.dialog.base.ElementTreeSingleSelectDialog;
import org.fortiss.af3.mira.ui.dialog.base.IDialogEditSupport;
import org.fortiss.af3.mira.ui.dialog.base.TreeContentProviderBase;
import org.fortiss.tooling.kernel.extension.data.Prototype;
import org.fortiss.tooling.kernel.model.INamedCommentedElement;
import org.fortiss.tooling.kernel.service.ICommandStackService;
import org.fortiss.tooling.kernel.service.IElementCompositorService;
import org.fortiss.tooling.kernel.service.IPrototypeService;
import org.fortiss.tooling.kernel.ui.extension.IModelElementHandler;
import org.fortiss.tooling.kernel.ui.service.IModelElementHandlerService;
/**
*
* @author Leonhard Uden
* @author Dongyue Mou
* @author $Author$
* @version $Rev$
* @levd.rating RED Rev:
*/
public class ComponentSelectionDialog extends ElementTreeSingleSelectDialog {
/**
* @param parentShell
* the shell object
* @param components
* the root element
* @param selected
* the selected component
*/
public ComponentSelectionDialog(final Shell parentShell,
final List<? extends EObject> components, Component selected) {
super(parentShell, components, selected, new TreeContentProviderBase() {
@Override
public Object getParent(Object element) {
if (element instanceof EObject) {
if (components.contains(element))
return null;
return ((EObject) element).eContainer();
}
throw new IllegalArgumentException();
}
@Override
public Object[] getChildren(Object parentElement) {
if (parentElement instanceof EObject) {
IModelElementHandler<EObject> base = IModelElementHandlerService.INSTANCE
.getModelElementHandler((EObject) parentElement);
List<EObject> subnodes = base
.getSubnodes((EObject) parentElement);
ArrayList<EObject> childrenNodes = new ArrayList<EObject>();
for (EObject eobj : subnodes)
if (eobj instanceof Component)
childrenNodes.add(eobj);
return childrenNodes.toArray();
}
throw new IllegalArgumentException();
}
}, new LabelProvider() {
/** {@inheritDoc} */
@Override
public String getText(Object element) {
if (element instanceof EObject) {
IModelElementHandler<EObject> base = IModelElementHandlerService.INSTANCE
.getModelElementHandler((EObject) element);
if (base == null)
return "Unknown element: " + element;
return base.getName((EObject) element);
}
throw new IllegalArgumentException();
}
/** {@inheritDoc} */
@Override
public Image getImage(Object element) {
if (element instanceof EObject) {
IModelElementHandler<EObject> base = IModelElementHandlerService.INSTANCE
.getModelElementHandler((EObject) element);
if (base == null)
return ResourceManager.getPluginImage(
"org.fortiss.tooling.kernel.ui",
"/icons/unknown.png");
return base.getIcon();
}
throw new IllegalArgumentException();
}
}, new IDialogEditSupport() {
@Override
public void editElement(Object selected) {
if (selected instanceof INamedCommentedElement) {
final INamedCommentedElement c = (INamedCommentedElement) selected;
final InputDialog input = new InputDialog(parentShell,
"Edit", "Please type a name for the component",
c.getName(), null);
if (Window.OK == input.open()) {
ICommandStackService.INSTANCE.runAsCommand(
((EObject) selected).eContainer(),
new Runnable() {
@Override
public void run() {
c.setName(input.getValue());
}
});
}
} else {
MessageDialog.openInformation(parentShell, "Edit an actor",
"Please select an actor");
}
}
@Override
public boolean deleteElement(final Object selected) {
if (selected instanceof EObject) {
final EObject eobj = (EObject) selected;
if (IElementCompositorService.INSTANCE.canDecompose(eobj)) {
if (MessageDialog.openConfirm(parentShell,
"Remove element",
"Do you want to remove this element?")) {
FutureTask<Boolean> cmd = new FutureTask<Boolean>(
new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return IElementCompositorService.INSTANCE
.decompose(eobj);
}
});
ICommandStackService.INSTANCE.runAsCommand(eobj,
cmd);
try {
return cmd.get().booleanValue();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
return false;
}
@Override
public Object createElement(Object selected) {
if (selected instanceof EObject) {
final EObject eobj = (EObject) selected;
List<Prototype> prototypes = IPrototypeService.INSTANCE
.getComposablePrototypes(eobj.getClass());
for (Prototype p : prototypes) {
if (p.getPrototype() instanceof Component) {
final EObject neweobj = p.getPrototypeCopy();
FutureTask<Boolean> cmd = new FutureTask<Boolean>(
new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return IElementCompositorService.INSTANCE
.compose(eobj, neweobj,
null);
}
});
ICommandStackService.INSTANCE.runAsCommand(eobj,
cmd);
try {
if (cmd.get().booleanValue()) {
return neweobj;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
return null;
}
});
}
/**
* @param parentShell
* the shell
* @param ca
* the component architecture
* @return the actually selected component or null if nothing is selected
*/
public static Component open(Shell parentShell, ComponentArchitecture ca) {
ComponentSelectionDialog asd = new ComponentSelectionDialog(
parentShell, Arrays.asList(ca), null);
if (Window.OK == asd.open())
return (Component) asd.getSelectedElement();
return null;
}
}
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2011 ForTISS GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |