View Javadoc
1   /*****************************************************************************
2    * Copyright (c) 2015 CEA LIST.
3    *
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the Eclipse Public License v1.0
6    * which accompanies this distribution, and is available at
7    * http://www.eclipse.org/legal/epl-v10.html
8    *
9    * Contributors:
10   *  Benoit Maggi (CEA LIST) benoit.maggi@cea.fr - Initial API and implementation
11   *****************************************************************************/
12  package org.eclipse.papyrus.sysml14.diagram.common.advices;
13  
14  
15  
16  import java.util.HashMap;
17  import java.util.Map;
18  
19  import org.eclipse.core.runtime.OperationCanceledException;
20  import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
21  import org.eclipse.gmf.runtime.common.core.command.ICommand;
22  import org.eclipse.gmf.runtime.common.core.command.UnexecutableCommand;
23  import org.eclipse.gmf.runtime.emf.type.core.edithelper.AbstractEditHelperAdvice;
24  import org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest;
25  import org.eclipse.jface.window.Window;
26  import org.eclipse.papyrus.sysml14.diagram.common.command.ConfigurePartCommand;
27  import org.eclipse.papyrus.sysml14.diagram.common.dialog.CreateOrSelectValuePropertyTypeDialog;
28  import org.eclipse.papyrus.sysml14.service.types.advice.AfterConfigureCommandEditHelperAdvice;
29  import org.eclipse.papyrus.uml.diagram.common.dialogs.CreateOrSelectTypeDialog;
30  import org.eclipse.swt.widgets.Display;
31  import org.eclipse.swt.widgets.Shell;
32  import org.eclipse.uml2.uml.Package;
33  import org.eclipse.uml2.uml.Property;
34  import org.eclipse.uml2.uml.Type;
35  
36  
37  /**
38   * The helperadvice class used for Value
39   * It creates the popup to help the value creation
40   *
41   */
42  public class ValueEditHelperAdvice extends AbstractEditHelperAdvice {
43  	
44  	/**
45  	 * @see org.eclipse.gmf.runtime.emf.type.core.edithelper.AbstractEditHelperAdvice#getBeforeConfigureCommand(org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest)
46  	 *
47  	 * @param request
48  	 * @return
49  	 */
50  	@Override
51  	protected ICommand getBeforeConfigureCommand(ConfigureRequest request) {
52  		configureRequest(request);
53  		return super.getBeforeConfigureCommand(request);		
54  	}
55  	
56  	/**
57  	 * @param request
58  	 */
59  	protected void configureRequest(ConfigureRequest request) {
60  		Map<String, Object> newParameters = new HashMap<String, Object>();
61  		
62  		ICommand configureCommand = null;
63  
64  		Shell shell = Display.getDefault().getActiveShell();
65  		Property property = (Property) request.getElementToConfigure();
66  		Package owner = property.getNearestPackage();
67  
68  		CreateOrSelectTypeDialog dialog = new CreateOrSelectValuePropertyTypeDialog(shell, owner);
69  		
70  		dialog.open();
71  		if (dialog.getReturnCode() == Window.OK) {
72  
73  			final ICommand typeCreationCommand = dialog.getNewTypeCreateCommand();
74  			final Type partType = (Type) dialog.getExistingType();
75  
76  			// Abort if type creation command exists but is not executable
77  			if ((typeCreationCommand != null) && (!typeCreationCommand.canExecute())) {
78  				configureCommand = UnexecutableCommand.INSTANCE;
79  			} else {
80  				configureCommand = CompositeCommand.compose(configureCommand, typeCreationCommand);
81  			}
82  
83  			// Create the configure command that will set the constraint property type
84  			ICommand setTypeCommand = new ConfigurePartCommand(request,partType,typeCreationCommand) ;
85  			configureCommand = CompositeCommand.compose(configureCommand, setTypeCommand);
86  		} else {
87  			throw new OperationCanceledException("Value creation cancelled by user"); //$NON-NLS-1$
88  		}
89  		
90  		newParameters.put(AfterConfigureCommandEditHelperAdvice.AFTER_CONFIGURE_COMMAND, configureCommand);
91  		request.addParameters(newParameters);
92  	}
93  }