Skip to main content
summaryrefslogtreecommitdiffstats
blob: d51a6d5d088a6d4c2ee8df54f11bcf8ded347834 (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
/*******************************************************************************
 * Copyright (c) 2003, 2005 IBM Corporation and others.
 * 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.j2ee.internal.command;


import java.util.Collection;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CommandWrapper;
import org.eclipse.emf.common.command.StrictCompoundCommand;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.command.AddCommand;
import org.eclipse.emf.edit.command.PasteFromClipboardCommand;
import org.eclipse.jst.j2ee.common.internal.util.IDUtility;
import org.eclipse.jst.j2ee.internal.provider.J2EEUIEditingDomain;


public class J2EEPasteFromClipboardOverrideCommand extends PasteFromClipboardCommand {
	private J2EECopyFromClipboardCommand copyCommand;
	private Command addBindingsCommand;
	private Command addExtensionsCommand;

	public J2EEPasteFromClipboardOverrideCommand(PasteFromClipboardCommand p) {
		super(p.getDomain(), p.getOwner(), p.getFeature(), p.getIndex(), false);
	}

	public void doExecute() {
		super.doExecute();
		executeAddBindings();
		executeAddExtensions();
		J2EEClipboard result = (J2EEClipboard) doGetResult();
		for (int i = 0; i < result.size(); i++) {
			EObject o = (EObject) result.get(i);
			if (result.getBinding(o) != null || result.getExtension(o) != null)
				IDUtility.setDefaultID(o, true);
		}
	}

	public Collection doGetAffectedObjects() {
		return copyCommand.getAffectedObjects();
	}

	public Collection doGetResult() {
		return copyCommand.getResult();
	}

	public void doRedo() {
		super.doRedo();
		if (addBindingsCommand != null)
			addBindingsCommand.redo();
		if (addExtensionsCommand != null)
			addExtensionsCommand.redo();
	}

	public void doUndo() {
		super.doUndo();
		if (addBindingsCommand != null)
			addBindingsCommand.undo();
		if (addExtensionsCommand != null)
			addExtensionsCommand.undo();
	}

	protected void executeAddBindings() {
		if (addBindingsCommand != null && addBindingsCommand.canExecute())
			addBindingsCommand.execute();
	}

	protected void executeAddExtensions() {
		if (addExtensionsCommand != null && addExtensionsCommand.canExecute())
			addExtensionsCommand.execute();
	}

	public J2EEClipboard getCopiedClipoard() {
		return (J2EEClipboard) copyCommand.getResult();
	}

	protected J2EEClipboard getJ2EEClipboard() {
		return (J2EEClipboard) domain.getClipboard();
	}

	protected boolean prepare() {
		if (getJ2EEClipboard() == null)
			return false;
		command = new StrictCompoundCommand();

		copyCommand = new J2EECopyFromClipboardCommand((J2EEUIEditingDomain) domain);
		command.append(copyCommand);

		command.append(new CommandWrapper() {
			protected Command createCommand() {
				Command addCommand = AddCommand.create(getDomain(), getOwner(), getFeature(), copyCommand.getResult(), getIndex());
				return addCommand;
			}
		});
		prepareBindingCommand(copyCommand);
		prepareExtensionCommand(copyCommand);

		boolean result;
		if (optimize) {
			// This will determine canExecute as efficiently as possible.
			//
			result = optimizedCanExecute();
		} else {
			// This will actually execute the copy command in order to check if the add can execute.
			//
			result = command.canExecute();
		}

		return result;
	}

	protected void prepareBindingCommand(final J2EECopyFromClipboardCommand cmd) {
		if (!getJ2EEClipboard().hasBindings())
			return;
		//TODO make adaptable command
		//	addBindingsCommand = new CommandWrapper() {
		//		protected Command createCommand() {
		//			Object bindingOwner = BindingAndExtensionHelper.getBindingAddOwner((EObject)getOwner());
		//			Collection bindingsCopies = getCopiedClipoard().getBindings().values();
		//			Command addCommand = AddCommand.create(getDomain(), bindingOwner, null, bindingsCopies,
		// CommandParameter.NO_INDEX);
		//			return addCommand;
		//		}
		//	};
	}

	protected void prepareExtensionCommand(final J2EECopyFromClipboardCommand cmd) {
		if (!getJ2EEClipboard().hasExtensions())
			return;
		// TODO make adaptable command
		//	addExtensionsCommand = new CommandWrapper() {
		//		protected Command createCommand() {
		//			Object extensionOwner =
		// BindingAndExtensionHelper.getExtensionAddOwner((EObject)getOwner());
		//			Collection extensionsCopies = getCopiedClipoard().getExtensions().values();
		//			Command addCommand = AddCommand.create(getDomain(), extensionOwner, null,
		// extensionsCopies, CommandParameter.NO_INDEX);
		//			return addCommand;
		//		}
		//	};
	}
}

Back to the top