Skip to main content
summaryrefslogtreecommitdiffstats
blob: d67857f41cc729e9421783c24e97088bf0b92477 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package org.eclipse.papyrus.uml.compare.ui.transactional.merge.viewer;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.eclipse.compare.CompareConfiguration;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.IUndoableOperation;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.compare.diff.metamodel.DiffElement;
import org.eclipse.emf.compare.ui.viewer.content.ModelContentMergeViewer;
import org.eclipse.emf.compare.ui.viewer.content.part.ModelContentMergeTabFolder;
import org.eclipse.emf.edit.domain.IEditingDomainProvider;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.workspace.AbstractEMFOperation;
import org.eclipse.emf.workspace.EMFOperationCommand;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.papyrus.uml.compare.ui.Activator;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;

public class TransactionalModelContentMergeViewer extends ModelContentMergeViewer {

	private TransactionalEditingDomain domain;
	
	
	private boolean isSynchronizedWithDiagram;

	//duplicate code from tatiana
	public TransactionalModelContentMergeViewer(Composite parent, CompareConfiguration config) {
		super(parent, config);
		IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
		if(editor instanceof IEditingDomainProvider) {
			domain = (TransactionalEditingDomain)((IEditingDomainProvider)editor).getEditingDomain();
		}
	}

	@Override
	protected ModelContentMergeTabFolder createModelContentMergeTabFolder(Composite composite, int side) {
		// TODO Auto-generated method stub
		//		return super.createModelContentMergeTabFolder(composite, side);
		return new TransactionalModelContentMergeTabFolder(this, composite, side);
	}

	/**
	 * Undoes the changes implied by the currently selected {@link DiffElement diff}.
	 */
	protected void copyDiffLeftToRight() {
		if(currentSelection != null) {
			doCopy(currentSelection, true);
		}
		currentSelection.clear();
		switchCopyState(false);
	}

	/**
	 * Applies the changes implied by the currently selected {@link DiffElement diff}.
	 */
	protected void copyDiffRightToLeft() {
		if(currentSelection != null) {
			doCopy(currentSelection, false);
		}
		currentSelection.clear();
		switchCopyState(false);
	}


	protected void doCopy(final List<DiffElement> diffs, final boolean leftToRight) {
		Map<?, ?> transactionOptions = Collections.EMPTY_MAP;
		final Runnable runnable = new Runnable() {

			public void run() {
				// TODO Auto-generated method stub
				TransactionalModelContentMergeViewer.this.copy(diffs, leftToRight);
			}
		};

		//		AbstractCommand cmd = new ChangeC(){
		//
		//			public void execute() {
		//				// TODO Auto-generated method stub
		//				
		//			}
		//
		//			public void redo() {
		//				// TODO Auto-generated method stub
		//				
		//			}
		//			
		//		}
		IUndoableOperation operation = new AbstractEMFOperation(domain, "copy action", transactionOptions) {

			@Override
			protected IStatus doExecute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
				//				try {
				runnable.run();
				return Status.OK_STATUS;
				//				} catch (RuntimeException ex) {
				//					if (ex instanceof OperationCanceledException) {
				//						throw (OperationCanceledException) ex;
				//					} else {
				////						throw new ExecutionException(NLS.bind(Messages.problem_transactionFailed, operationLabel), ex);
				//					}
				//				}
				//				
			}

			//			@Override
			//			public boolean canUndo() {
			////				return transactionOptions.get(Transaction.OPTION_NO_UNDO) != Boolean.TRUE;
			//			}


		};

		Command command = new EMFOperationCommand(domain, operation);
		//		domain.getCommandStack().
		domain.getCommandStack().execute(command);

	}


	@Override
	protected void createToolItems(ToolBarManager tbm) {
		
		final IAction diagramSynchronization = new Action("Diagram Synchronisation", IAction.AS_CHECK_BOX) {

			@Override
			public ImageDescriptor getImageDescriptor() {
				//TODO improve?
				return Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/PapyrusLogo16x16.gif");
			}
		};


		ActionContributionItem actionContributionItem = new ActionContributionItem(diagramSynchronization) {
			@Override
			public void fill(ToolBar parent, int index) {
				// TODO Auto-generated method stub
				super.fill(parent, index);
			}
		};
		//actionContributionItem.setMode(ActionContributionItem.MODE_FORCE_TEXT);
		diagramSynchronization.addPropertyChangeListener(new IPropertyChangeListener() {

			public void propertyChange(PropertyChangeEvent event) {
				//we are here each time the user click on the action!
				diagramSynchronization.setChecked(diagramSynchronization.isChecked());
				isSynchronizedWithDiagram = diagramSynchronization.isChecked();
			}
		});
		tbm.insert(0, actionContributionItem);
		super.createToolItems(tbm);
	}

}

Back to the top