blob: b87e8086f348761d3c842324ee6c8fd636784052 [file] [log] [blame]
/*
* Copyright (c) 2003, 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM - Initial API and implementation
*
* $Id: CreateExtensionAction.java,v 1.3 2004/06/02 17:55:34 khussey Exp $
*/
package org.eclipse.uml2.examples.ui.actions;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.IdentityCommand;
import org.eclipse.emf.common.command.UnexecutableCommand;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.ui.celleditor.FeatureEditorDialog;
import org.eclipse.jface.action.IAction;
import org.eclipse.uml2.Stereotype;
import org.eclipse.uml2.UML2Package;
import org.eclipse.uml2.edit.util.ChangeCommand;
import org.eclipse.uml2.examples.ui.ExamplesUIPlugin;
/**
*
*/
public class CreateExtensionAction
extends UML2CommandAction {
public CreateExtensionAction() {
super();
}
/*
* (non-Javadoc)
*
* @see org.eclipse.emf.edit.ui.action.CommandAction#createActionCommand(org.eclipse.emf.edit.domain.EditingDomain,
* java.util.Collection)
*/
protected Command createActionCommand(EditingDomain editingDomain,
Collection collection) {
if (1 == collection.size()
&& Stereotype.class.isInstance(collection.toArray()[0])) {
return IdentityCommand.INSTANCE;
}
return UnexecutableCommand.INSTANCE;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
*/
public void run(IAction action) {
if (UnexecutableCommand.INSTANCE != command) {
final Stereotype stereotype = (Stereotype) collection.toArray()[0];
List choiceOfValues = new ArrayList();
for (Iterator referencedMetaclasses = stereotype.getProfile()
.getReferencedMetaclasses().iterator(); referencedMetaclasses
.hasNext();) {
EClassifier eClassifier = UML2Package.eINSTANCE
.getEClassifier(((org.eclipse.uml2.Class) referencedMetaclasses
.next()).getName());
if (!stereotype.getAllExtendedEClasses().contains(eClassifier)) {
choiceOfValues.add(eClassifier);
}
}
Collections.sort(choiceOfValues, new Comparator() {
public int compare(Object o1, Object o2) {
return getLabelProvider().getText(o1).compareTo(
getLabelProvider().getText(o2));
}
});
String label = ExamplesUIPlugin.getDefault().getString(
"_UI_CreateExtensionActionCommand_label"); //$NON-NLS-1$
final FeatureEditorDialog dialog = new FeatureEditorDialog(
editorPart.getSite().getShell(), getLabelProvider(),
stereotype, UML2Package.eINSTANCE.getStereotype(),
Collections.EMPTY_LIST, label, choiceOfValues);
dialog.open();
if (FeatureEditorDialog.OK == dialog.getReturnCode()) {
editingDomain.getCommandStack().execute(
new ChangeCommand(editingDomain, new Runnable() {
public void run() {
for (Iterator eClasses = dialog.getResult()
.iterator(); eClasses.hasNext();) {
stereotype.createExtension((EClass) eClasses
.next(), false);
}
}
}, label));
}
}
}
}