Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: af859ac57e380bd51b050a1cffea82542e11ff52 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*****************************************************************************
 * Copyright (c) 2010 CEA LIST.
 *    
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation
 *  Vincent Lorenzo (CEA-LIST) vincent.lorenzo@cea.fr
 *****************************************************************************/
package org.eclipse.papyrus.properties.runtime.controller;

import static org.eclipse.papyrus.properties.runtime.Activator.log;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.OperationHistoryFactory;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
import org.eclipse.papyrus.properties.runtime.modelhandler.emf.IEMFModelHandler;
import org.eclipse.papyrus.properties.runtime.modelhandler.emf.TransactionUtil;


/**
 * Controller for property editors, that manages EMFT-based models. This means that a transaction will be opened each time the model has to be
 * modified
 */
public abstract class EMFTPropertyEditorController extends EMFPropertyEditorController {

	/** Transactional editing domain used to write into the model */
	private TransactionalEditingDomain editingDomain;

	/** model handler to interact with the model for this controller */
	protected IEMFModelHandler modelHandler;

	/**
	 * Constructor.
	 */
	public EMFTPropertyEditorController() {
		super();
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.properties.runtime.controller.PropertyEditorController#updateModel()
	 * 
	 */
	@Override
	public void updateModel() {
		CompositeCommand cc = new CompositeCommand("Set Value Command"); //$NON-NLS-1$
		Object valueToSet = getEditorValue();
		for(Object obj : this.objectToEdit) {
			EObject elementToEdit = (EObject)obj;
			//build the request
			SetRequest[] req = null;
			req = modelHandler.getSetRequest(getEditingDomain(), elementToEdit, valueToSet);
			if(req == null) {
				break;
			}
			org.eclipse.papyrus.service.edit.service.IElementEditService provider = org.eclipse.papyrus.service.edit.service.ElementEditServiceUtils.getCommandProvider(elementToEdit);
			if(provider != null) {

				ICommand editCommand = null;
				for(SetRequest current : req) {
					editCommand = provider.getEditCommand(current);

					if(editCommand != null && editCommand.canExecute()) {
						cc.add(editCommand);
					}
				}
			}

			if(cc.canExecute() && !(TransactionUtil.isReadTransactionInProgress(getEditingDomain(), true, true))) {
				try {
					OperationHistoryFactory.getOperationHistory().execute(cc, new NullProgressMonitor(), null);
				} catch (ExecutionException e) {
					log.error(e);
				}
				return;
			}
		}

		/*
		 * req was null, or the command was unexecutable
		 * Currently, the handler for stereotype return always null!
		 */
		AbstractTransactionalCommand command = new EMFTControllerCommand();
		if(command.canExecute() && !(TransactionUtil.isReadTransactionInProgress(editingDomain, true, true))) {
			try {
				OperationHistoryFactory.getOperationHistory().execute(command, new NullProgressMonitor(), null);
			} catch (ExecutionException e) {
				log.error(e);
			}
		}
	}

	/**
	 * Returns the editing domain that manages modifications to the model
	 * 
	 * @return the editing domain that manages modifications to the model
	 */
	public TransactionalEditingDomain getEditingDomain() {
		return editingDomain;
	}

	/**
	 * Sets the editingDomain for this controller
	 * 
	 * @param editingDomain
	 *        the editingDomain to set
	 */
	public void setEditingDomain(TransactionalEditingDomain editingDomain) {
		this.editingDomain = editingDomain;
	}

	/**
	 * Command to modify the model, using the property editors
	 */
	protected class EMFTControllerCommand extends AbstractTransactionalCommand {

		/**
		 * Creates the new EMFTControllerCommand.
		 */
		public EMFTControllerCommand() {
			super(editingDomain, "Editing Property", getWorkspaceFiles(getObjectsToEdit())); //$NON-NLS-1$
		}

		/**
		 * @{inheritDoc
		 */
		@Override
		protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
			setValueInModel(getEditorValue());
			return CommandResult.newOKCommandResult(getObjectsToEdit());
		}
	}

}

Back to the top