Skip to content

Package: ProgressPasteFromClipboardCommand$3

ProgressPasteFromClipboardCommand$3

nameinstructionbranchcomplexitylinemethod
run(IProgressMonitor)
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
{...}
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2016 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Johannes Faltermeier - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.edit.spi.swt.commands;
15:
16: import java.lang.reflect.InvocationTargetException;
17: import java.util.ArrayList;
18: import java.util.Collection;
19: import java.util.Iterator;
20: import java.util.List;
21:
22: import org.eclipse.core.runtime.IProgressMonitor;
23: import org.eclipse.emf.common.command.Command;
24: import org.eclipse.emf.common.command.CommandWrapper;
25: import org.eclipse.emf.edit.command.AddCommand;
26: import org.eclipse.emf.edit.command.PasteFromClipboardCommand;
27: import org.eclipse.emf.edit.domain.EditingDomain;
28: import org.eclipse.jface.dialogs.ProgressMonitorDialog;
29: import org.eclipse.jface.operation.IRunnableWithProgress;
30: import org.eclipse.swt.widgets.Display;
31:
32: /**
33: * Extension of the {@link PasteFromClipboardCommand} which enabled progress reporting.
34: *
35: * @author Johannes Faltermeier
36: * @since 1.11
37: *
38: */
39: public class ProgressPasteFromClipboardCommand extends PasteFromClipboardCommand implements IProgressMonitorProvider {
40:
41:         private IProgressMonitor monitor;
42:
43:         /**
44:          * Constructs a new {@link ProgressPasteFromClipboardCommand}.
45:          *
46:          * @param domain the {@link EditingDomain}
47:          * @param owner the owner object
48:          * @param feature the feature
49:          * @param index the index
50:          */
51:         public ProgressPasteFromClipboardCommand(EditingDomain domain, Object owner, Object feature, int index) {
52:                 super(domain, owner, feature, index);
53:         }
54:
55:         /**
56:          * Constructs a new {@link ProgressPasteFromClipboardCommand}.
57:          *
58:          * @param domain the {@link EditingDomain}
59:          * @param owner the owner object
60:          * @param feature the feature
61:          * @param index the index
62:          * @param optimize whether to call the optimized can execute method
63:          */
64:         public ProgressPasteFromClipboardCommand(EditingDomain domain, Object owner, Object feature, int index,
65:                 boolean optimize) {
66:                 super(domain, owner, feature, index, optimize);
67:         }
68:
69:         @Override
70:         protected boolean prepare() {
71:                 {
72:                         // Create a strict compound command to do a copy and then add the result
73:                         //
74:                         command = new ProgressStrictCompoundCommand(this);
75:
76:                         // Create a command to copy the clipboard.
77:                         //
78:                         final Command copyCommand = ProgressCopyCommandFactory.create(domain, domain.getClipboard(), this);
79:
80:                         // REUSED CLASS
81:                         command.append(copyCommand);
82:
83:                         // Create a proxy that will create an add command.
84:                         //
85:                         command.append(new CommandWrapper() {
86:                                 protected Collection<Object> original;
87:                                 protected Collection<Object> copy;
88:
89:                                 @Override
90:                                 protected Command createCommand() {
91:                                         original = domain.getClipboard();
92:                                         copy = new ArrayList<Object>(copyCommand.getResult());
93:
94:                                         // Use the original to do the add, but only if it's of the same type as the copy.
95:                                         // This ensures that if there is conversion being done as part of the copy,
96:                                         // as would be the case for a cross domain copy in the mapping framework,
97:                                         // that we do actually use the converted instance.
98:                                         //
99:                                         if (original.size() == copy.size()) {
100:                                                 for (Iterator<Object> i = original.iterator(), j = copy.iterator(); i.hasNext();) {
101:                                                         final Object originalObject = i.next();
102:                                                         final Object copyObject = j.next();
103:                                                         if (originalObject.getClass() != copyObject.getClass()) {
104:                                                                 original = null;
105:                                                                 break;
106:                                                         }
107:                                                 }
108:                                         }
109:
110:                                         final Command addCommand = AddCommand.create(domain, owner, feature,
111:                                                 original == null ? copy : original, index);
112:                                         if (IProgressMonitorConsumer.class.isInstance(addCommand)) {
113:                                                 IProgressMonitorConsumer.class.cast(addCommand)
114:                                                         .setIProgressMonitorAccessor(ProgressPasteFromClipboardCommand.this);
115:                                         }
116:                                         return addCommand;
117:                                 }
118:
119:                                 @Override
120:                                 public void execute() {
121:                                         if (original != null) {
122:                                                 domain.setClipboard(copy);
123:                                         }
124:                                         super.execute();
125:                                 }
126:
127:                                 @Override
128:                                 public void undo() {
129:                                         super.undo();
130:                                         if (original != null) {
131:                                                 domain.setClipboard(original);
132:                                         }
133:                                 }
134:
135:                                 @Override
136:                                 public void redo() {
137:                                         if (original != null) {
138:                                                 domain.setClipboard(copy);
139:                                         }
140:                                         super.redo();
141:                                 }
142:                         });
143:
144:                         boolean result;
145:                         if (optimize) {
146:                                 // This will determine canExecute as efficiently as possible.
147:                                 //
148:                                 result = optimizedCanExecute();
149:                         } else {
150:                                 // This will actually execute the copy command in order to check if the add can execute.
151:                                 //
152:                                 result = command.canExecute();
153:                         }
154:
155:                         return result;
156:                 }
157:         }
158:         // END REUSED CLASS
159:
160:         @Override
161:         public void doExecute() {
162:                 doExecuteWithProgress("Pasting elements..."); //$NON-NLS-1$
163:         }
164:
165:         /**
166:          * Does the same as {@link #doExecute()} but also displays a progress dialog.
167:          *
168:          * @param task the name of the task
169:          */
170:         protected void doExecuteWithProgress(final String task) {
171:                 try {
172:                         final ProgressMonitorDialog progressMonitorDialog = new ProgressMonitorDialog(
173:                                 Display.getDefault().getActiveShell());
174:                         progressMonitorDialog.run(
175:                                 false, /* if we fork auto UI updates of EMF.edit.ui do not work, so stay on UI thread */
176:                                 false,
177:                                 new IRunnableWithProgress() {
178:                                         @Override
179:                                         public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
180:                                                 ProgressPasteFromClipboardCommand.this.monitor = monitor;
181:                                                 monitor.beginTask(task, getTotalWorkExecute());
182:                                                 ProgressPasteFromClipboardCommand.super.doExecute();
183:                                                 ProgressPasteFromClipboardCommand.this.monitor = null;
184:                                         }
185:                                 });
186:                 } catch (final InvocationTargetException ex) {
187:                 } catch (final InterruptedException ex) {
188:                 }
189:         }
190:
191:         /**
192:          * @return the total work required to execute the paste operation
193:          */
194:         protected int getTotalWorkExecute() {
195:                 int work = domain.getClipboard().size();
196:                 final List<Command> commandList = command.getCommandList();
197:                 work += commandList.size();
198:                 for (final Command subCommand : commandList) {
199:                         if (!ProgressCompoundCommand.class.isInstance(subCommand)) {
200:                                 continue;
201:                         }
202:                         work += ProgressCompoundCommand.class.cast(subCommand).getCommandList().size();
203:                 }
204:                 return work;
205:         }
206:
207:         @Override
208:         public void doUndo() {
209:                 doUndoWithProgress("Undoing paste operation"); //$NON-NLS-1$
210:         }
211:
212:         /**
213:          * Does the same as {@link #doUndo()} but also displays a progress dialog.
214:          *
215:          * @param task the name of the task
216:          */
217:         protected void doUndoWithProgress(final String task) {
218:                 try {
219:                         final ProgressMonitorDialog progressMonitorDialog = new ProgressMonitorDialog(
220:                                 Display.getDefault().getActiveShell());
221:                         progressMonitorDialog.run(
222:                                 false, /* if we fork auto UI updates of EMF.edit.ui do not work, so stay on UI thread */
223:                                 false,
224:                                 new IRunnableWithProgress() {
225:                                         @Override
226:                                         public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
227:                                                 ProgressPasteFromClipboardCommand.this.monitor = monitor;
228:                                                 monitor.beginTask(task, 1);
229:                                                 ProgressPasteFromClipboardCommand.super.doUndo();
230:                                                 ProgressPasteFromClipboardCommand.this.monitor = null;
231:                                         }
232:                                 });
233:                 } catch (final InvocationTargetException ex) {
234:                 } catch (final InterruptedException ex) {
235:                 }
236:         }
237:
238:         @Override
239:         public void doRedo() {
240:                 doRedoWithProgress("Redoing paste operation."); //$NON-NLS-1$
241:         }
242:
243:         /**
244:          * Does the same as {@link #doRedo()} but also displays a progress dialog.
245:          *
246:          * @param task the name of the task
247:          */
248:         protected void doRedoWithProgress(final String task) {
249:                 try {
250:                         final ProgressMonitorDialog progressMonitorDialog = new ProgressMonitorDialog(
251:                                 Display.getDefault().getActiveShell());
252:                         progressMonitorDialog.run(
253:                                 false, /* if we fork auto UI updates of EMF.edit.ui do not work, so stay on UI thread */
254:                                 false,
255:                                 new IRunnableWithProgress() {
256:                                         @Override
257:                                         public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
258:                                                 ProgressPasteFromClipboardCommand.this.monitor = monitor;
259:                                                 monitor.beginTask(task, 1);
260:                                                 ProgressPasteFromClipboardCommand.super.doRedo();
261:                                                 ProgressPasteFromClipboardCommand.this.monitor = null;
262:                                         }
263:                                 });
264:                 } catch (final InvocationTargetException ex) {
265:                 } catch (final InterruptedException ex) {
266:                 }
267:         }
268:
269:         @Override
270:         public IProgressMonitor getProgressMonitor() {
271:                 return monitor;
272:         }
273:
274: }