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
|
/*******************************************************************************
* Copyright (c) 2009 THALES GLOBAL SERVICES.
* 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:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.ui.tools.internal.actions.control;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.ISaveablePart2;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.eclipse.sirius.common.ui.tools.api.util.SWTUtil;
import org.eclipse.sirius.business.api.session.Session;
import org.eclipse.sirius.business.api.session.SessionManager;
import org.eclipse.sirius.business.api.session.SessionStatus;
import org.eclipse.sirius.provider.SiriusEditPlugin;
import org.eclipse.sirius.ui.tools.api.control.SiriusControlHandler;
import org.eclipse.sirius.ui.tools.api.control.SiriusUncontrolHandler;
/**
* A specific control action handling representations.
*
* @author cbrun
*/
public class DesignerControlAction extends ControlAction {
/**
* Create a new action to control the models.
*
*/
public DesignerControlAction() {
super();
}
/**
* {@inheritDoc}
*/
@Override
public void run() {
final boolean controlling = this.command == null;
final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
int choice = ISaveablePart2.YES;
final Session session = SessionManager.INSTANCE.getSession(this.eObject);
if (session != null) {
if (session.getStatus() == SessionStatus.DIRTY) {
/* Show a dialog. */
choice = SWTUtil.showSaveDialog(session, "Representations file", true);
}
if (choice == ISaveablePart2.YES) {
try {
new ProgressMonitorDialog(PlatformUI.getWorkbench().getDisplay().getActiveShell()).run(false, false, new WorkspaceModifyOperation() {
@Override
protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
try {
monitor.beginTask((controlling ? "Control" : "Uncontrol") + " resources", 2);
if (session.isOpen()) {
monitor.subTask("Session saving");
session.save(new SubProgressMonitor(monitor, 1));
}
if (controlling) {
new SiriusControlHandler().performControl(shell, eObject, new SubProgressMonitor(monitor, 1));
} else {
new SiriusUncontrolHandler().performUncontrol(shell, eObject, new SubProgressMonitor(monitor, 1));
}
} finally {
monitor.done();
}
}
});
} catch (InvocationTargetException e) {
SiriusEditPlugin.getPlugin().getLog().log(new Status(IStatus.ERROR, SiriusEditPlugin.ID, e.getLocalizedMessage(), e));
} catch (InterruptedException e) {
SiriusEditPlugin.getPlugin().getLog().log(new Status(IStatus.ERROR, SiriusEditPlugin.ID, e.getLocalizedMessage(), e));
}
}
}
updateSelection(getStructuredSelection());
}
/**
* {@inheritDoc}
*/
@Override
public boolean updateSelection(final IStructuredSelection selection) {
// At each selection changed, we must reset the eObject to avoid a
// potential memory leak
eObject = null;
this.selection = null;
setEditingDomain(null);
final Iterator<?> it = selection.iterator();
while (it.hasNext() && getEditingDomain() == null) {
final Object next = it.next();
if (next instanceof EObject) {
this.eObject = (EObject) next;
setEditingDomain(TransactionUtil.getEditingDomain(eObject));
break;
}
}
return getEditingDomain() != null && super.updateSelection(selection);
}
@Override
public boolean isEnabled() {
return super.isEnabled() && SessionManager.INSTANCE.getSession(this.eObject) != null;
}
}
|