Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 44eaa08ea6dd4e4cc1c373cac45f852474f919d6 (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
170
171
172
173
174
175
176
177
178
179
/*******************************************************************************
 * Copyright (c) 2013 Atos
 * 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:
 *     Arthur Daussy - initial implementation
 *******************************************************************************/
package org.eclipse.papyrus.team.collaborative.strategy.ui.actions;

import java.util.Collection;
import java.util.Set;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.papyrus.team.collaborative.core.ICollaborativeManager;
import org.eclipse.papyrus.team.collaborative.core.IExtendedURI;
import org.eclipse.papyrus.team.collaborative.core.participants.locker.IUnlocker;
import org.eclipse.papyrus.team.collaborative.core.participants.version.ICommitter;
import org.eclipse.papyrus.team.collaborative.core.reports.CollabStatus;
import org.eclipse.papyrus.team.collaborative.core.strategy.ILockingStrategy;
import org.eclipse.papyrus.team.collaborative.core.strategy.ILockingStrategy.Descriptor;
import org.eclipse.papyrus.team.collaborative.strategy.MatchingURIObject;
import org.eclipse.papyrus.team.collaborative.strategy.ui.ICollabColors;
import org.eclipse.papyrus.team.collaborative.strategy.ui.dialogs.PreviewDialog;
import org.eclipse.papyrus.team.collaborative.strategy.ui.providers.ExtensivePartitionNameLabelProvider;
import org.eclipse.papyrus.team.collaborative.strategy.utils.UIUtils;
import org.eclipse.swt.widgets.Display;


/**
 * Handler for the Unlock action in Papyrus
 */
public class UnlockAction extends Action {

	/** The strategy. */
	protected ILockingStrategy.Descriptor strategy;

	/**
	 * Instantiates a new unlock action.
	 * 
	 * @param strategy
	 *        the strategy
	 */
	public UnlockAction(Descriptor strategy) {
		super();
		this.strategy = strategy;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.action.Action#run()
	 */
	@Override
	public void run() {
		if(!UIUtils.saveAllDirtyEditor().isOK()) {
			return;
		}
		Collection<EObject> selection = UIUtils.getSelection();
		if(!selection.isEmpty()) {
			IStatus status = doSafeUnlock(selection, strategy);
			if(!status.isOK()) {
				UIUtils.errorDialog(status, "Error during Unlock");
			}
		}
		super.run();
	}

	/**
	 * Do unlock.
	 * 
	 * @param selection
	 *        The {@link EObject} selected by the user
	 * @param strategy
	 *        the strategy use to compute the business object set to unlock
	 * @throws CollabException
	 *         the collab exception
	 */
	public static IStatus doSafeUnlock(Collection<EObject> selection, ILockingStrategy.Descriptor strategy) {
		EObject firstSelectedObject = selection.iterator().next();
		ResourceSet resourceSet = firstSelectedObject.eResource().getResourceSet();

		//Execute the strategy to define the functional object to lock
		Set<IExtendedURI> objectToLock = strategy.getStrategy().getBusinessObject(selection);
		//First check there no modification uncommitted
		ICommitter committer = ICollaborativeManager.INSTANCE.getCommitter(objectToLock, resourceSet);
		if(committer == null) {
			return CollabStatus.createErrorStatus("Unable to find an Committer");
		}

		IStatus preventUnlockingWithUntrackedModification = preventUnlockingWithUntrackedModification(resourceSet, objectToLock, committer, firstSelectedObject);
		if(!preventUnlockingWithUntrackedModification.isOK()) {
			return preventUnlockingWithUntrackedModification;
		}
		//Reset the resourceSet because the editor may have been reloaded
		resourceSet = UIUtils.getCurrentResourceSet();

		IUnlocker unlocker = ICollaborativeManager.INSTANCE.getUnlocker(objectToLock, resourceSet);
		if(unlocker == null) {
			return CollabStatus.createErrorStatus("Unable to find a unlocker for:\n " + objectToLock);
		}
		return doUnlock(resourceSet, unlocker);
	}

	protected static IStatus doUnlock(ResourceSet resourceSet, IUnlocker unlocker) {
		Set<IExtendedURI> needUnlock = unlocker.getExtendedSet();
		//Check if the lock can be taken
		ExtensivePartitionNameLabelProvider labelProvider = new ExtensivePartitionNameLabelProvider(new MatchingURIObject(needUnlock),UIUtils.getModelExplorerLavelProvider());
		labelProvider.setColor(ICollabColors.UNLOCK_COLLOR);
		PreviewDialog unlockPreviewDialog = new PreviewDialog(Display.getDefault().getActiveShell(), labelProvider, "Unlock Preview", "Element in green will be unlocked");
		Collection<EObject> objectsToReveal = UIUtils.getLeafSemanticElement(needUnlock, resourceSet);
		if(objectsToReveal != null && !objectsToReveal.isEmpty()) {
			unlockPreviewDialog.setObjectsToReveal(objectsToReveal);
		}
		if(unlockPreviewDialog.open() == PreviewDialog.OK) {
			IStatus lockResult = unlocker.unlock();
			UIUtils.refreshModelExplorer(needUnlock, resourceSet);
			return lockResult;
		} else {
			return CollabStatus.CANCEL_STATUS;
		}
	}


	/**
	 * Prevent unlocking with untracked modification.
	 * 
	 * @param resourceSet
	 *        the resource set
	 * @param objectToLock
	 *        the object to lock
	 * @param committer
	 *        the committer
	 * @param selected
	 *        the selected
	 * @return the i status
	 */
	private static IStatus preventUnlockingWithUntrackedModification(ResourceSet resourceSet, Set<IExtendedURI> objectToLock, ICommitter committer, EObject selected) {
		Set<IExtendedURI> committableResource = committer.getExtendedSet();
		if(!committableResource.isEmpty()) {
			//The user shall either commit or revert it's changes
			MessageDialog dialog = new MessageDialog(Display.getCurrent().getActiveShell(), "Modification in progess", null, "You have untrackted modification. You either have to commit it or revert it(Everything will be lost) to release the lock", MessageDialog.WARNING, new String[]{ "Commit", "Revert" }, 0);
			int dialogResult = dialog.open();
			if(dialogResult == 0) {
				//then commit
				return CommitHandler.doCommitFromBuilder(resourceSet, committer, committableResource, true, null);
			} else if(dialogResult == 1) {
				//then revert
				return RevertHandler.doRevert(committableResource, resourceSet);
			} else {
				//cancel
				return Status.CANCEL_STATUS;
			}
		}
		return Status.OK_STATUS;
	}



	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.action.Action#getText()
	 */
	@Override
	public String getText() {
		return strategy.getName();
	}



}

Back to the top