Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4edcb1ae0b7b820ade67ffc98b74dd481664f6b2 (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
/*****************************************************************************
 * Copyright (c) 2010 CEA LIST.
 *
 *    
 * 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:
 *  Patrick Tessier (CEA LIST) Patrick.tessier@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.pastemanager.service;

import java.awt.datatransfer.Clipboard;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.common.core.command.UnexecutableCommand;
import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
import org.eclipse.papyrus.uml.pastemanager.Activator;
import org.osgi.framework.Bundle;

/**
 * this singleton looks for in extension point to find a pasteCommandProvider
 * and can provide a paste command
 */


public class PasteCommandService {

	protected static final String PASTECOMMANDPROVIDER_EXTENSION_ID = "org.eclipse.papyrus.uml.pastemanager.pasteCommandProvider";

	protected static final String REALIZATION = "realization";

	protected static final String PRIORITY = "priority";

	protected static PasteCommandService instance = null;

	public static PasteCommandService getInstance() {
		if(instance == null) {
			instance = new PasteCommandService();
		}
		return instance;
	}


	/**
	 * Load an instance of a class
	 * 
	 * @param element
	 *        the extension point
	 * @param classAttribute
	 *        the name of the class to load
	 * @return the loaded Class
	 * @throws Exception
	 *         if the class is not loaded
	 */
	protected static Object createExtension(final IConfigurationElement element, final String classAttribute) throws Exception {
		try {
			Bundle extensionBundle = Platform.getBundle(element.getDeclaringExtension().getNamespaceIdentifier());
			Class clazz = extensionBundle.loadClass(classAttribute);
			Object obj = clazz.newInstance();
			return obj;
		} catch (Exception e) {
			throw new Exception("unable to create Extension" + e);
		}
	}

	protected Map<String, IPasteCommandProvider> pasteCommandProviderMap;

	
	private PasteCommandService() {
		// Reading data from plugins
		IConfigurationElement[] configElements = Platform.getExtensionRegistry().getConfigurationElementsFor(PASTECOMMANDPROVIDER_EXTENSION_ID);
		pasteCommandProviderMap = new HashMap<String, IPasteCommandProvider>();
		for(int i = 0; i < configElements.length; i++) {
			getPasteCommandProvider(configElements[i]);
		}

	}

	/**
	 * Load one rule
	 * 
	 * @param element
	 *        the extension point
	 */
	protected void getPasteCommandProvider(IConfigurationElement element) {
		IPasteCommandProvider pasteCommandProvider = null;
		try {
			pasteCommandProvider = (IPasteCommandProvider)createExtension(element, element.getAttribute(REALIZATION));
			String priority = (String)element.getAttribute(PRIORITY);
			pasteCommandProviderMap.put(priority, pasteCommandProvider);

		} catch (Exception e) {
			Activator.log.error("- " + pasteCommandProvider + " can not be loaded: " + e.getLocalizedMessage(), e);
		}
	}

	protected IPasteCommandProvider lookForProvider() {
		String[] priority = { "Highest", "High", "Medium", "Low", "Lowest" };
		IPasteCommandProvider selectedProvider = null;
		int i = 0;
		while(selectedProvider == null && i < priority.length) {
			selectedProvider = pasteCommandProviderMap.get(priority[i]);
			i++;
		}
		return selectedProvider;
	}

	/**
	 * return the paste command to execute by taking account parameter
	 * 
	 * @param targetEditPart
	 *        the target where object will be paste
	 * @param systemClipboard
	 *        contains info form the system copy paste
	 * @param papyrusCliboard
	 *        the list of views to paste
	 * @return a command
	 */
	public ICommand getPasteViewCommand(GraphicalEditPart targetEditPart, Clipboard systemClipboard, Collection<Object> papyrusCliboard) {
		IPasteCommandProvider selectedProvider = lookForProvider();
		if(selectedProvider == null) {
			return UnexecutableCommand.INSTANCE;
		}
		return selectedProvider.getPasteViewCommand(targetEditPart, systemClipboard, papyrusCliboard);
	}
	
	/**
	 * return the paste command to execute by taking account parameter. It copy also element of the semantic model
	 * 
	 * @param targetEditPart
	 *        the target where object will be paste
	 * @param systemClipboard
	 *        contains info form the system copy paste
	 * @param papyrusCliboard
	 *        the list of views to paste
	 * @return a command
	 */
	public ICommand getPasteWithModelCommand(GraphicalEditPart targetEditPart, Clipboard systemClipboard, Collection<Object> papyrusCliboard) {
		IPasteCommandProvider selectedProvider = lookForProvider();
		if(selectedProvider == null) {
			return UnexecutableCommand.INSTANCE;
		}
		return selectedProvider.getPasteWithModelCommand(targetEditPart, systemClipboard, papyrusCliboard);
	}
}

Back to the top