Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1b0dec3ae1bce184ba783bfe4a929540d193a46f (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
/*****************************************************************************
 * Copyright (c) 2011 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.customization.factory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.papyrus.customization.Activator;
import org.eclipse.papyrus.customization.messages.Messages;
import org.eclipse.papyrus.customization.model.customizationplugin.CustomizableElement;
import org.eclipse.papyrus.customization.model.customizationplugin.CustomizationPluginPackage;
import org.eclipse.papyrus.customization.model.customizationplugin.FileBasedCustomizableElement;
import org.eclipse.papyrus.customization.model.customizationplugin.Profile;
import org.eclipse.papyrus.customization.plugin.PluginEditor;
import org.eclipse.papyrus.infra.widgets.util.FileUtil;
import org.w3c.dom.Element;


public class ProfileExtensionFactory extends FileBasedExtensionFactory {

	public ProfileExtensionFactory() {
		super(Messages.ProfileExtensionFactory_Profile, "org.eclipse.papyrus.uml.extensionpoints.UMLProfile", "path", "profile", true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	}

	@Override
	public void addElement(CustomizableElement element, PluginEditor editor) {
		super.addElement(element, editor);

		editor.getManifestEditor().addDependency("org.eclipse.papyrus.uml.extensionpoints"); //$NON-NLS-1$
	}

	@Override
	protected Element createExtension(FileBasedCustomizableElement element, PluginEditor editor) {
		Element extension = super.createExtension(element, editor);
		Profile profile = (Profile) element;

		extension.setAttribute("name", profile.getName()); //$NON-NLS-1$

		if (profile.getDescription() != null) {
			extension.setAttribute("description", profile.getDescription()); //$NON-NLS-1$
		}

		if (profile.getIconpath() != null && !profile.getIconpath().trim().equals("")) {
			//extension.setAttribute("iconpath", profile.getIconpath()); //$NON-NLS-1$
			copyIcon(profile.getIconpath(), editor);
			extension.setAttribute("iconpath", getIconPath(profile.getIconpath())); //$NON-NLS-1$
		}

		if (profile.getProvider() != null) {
			extension.setAttribute("provider", profile.getProvider()); //$NON-NLS-1$
		}

		return extension;
	}

	protected void copyIcon(String path, PluginEditor editor) {
		File sourceFile = FileUtil.getFile(path);
		File targetFile = FileUtil.getWorkspaceFile("/" + editor.getProject().getName() + "/" + getIconPath(path)); //$NON-NLS-1$ //$NON-NLS-2$
		if (!targetFile.getParentFile().exists()) {
			targetFile.getParentFile().mkdirs();
		}

		try {
			copy(new FileInputStream(sourceFile), targetFile);
		} catch (IOException ex) {
			Activator.log.error(ex);
		}

		editor.getBuildEditor().addToBuild("icons/"); //$NON-NLS-1$
	}

	protected String getIconPath(String path) {
		return "icons/" + getFileName(path); //$NON-NLS-1$
	}

	@Override
	protected String getFileName(String path) {
		String fileName;
		path = path.replace("\\", "/");
		if (path.indexOf("/") < 0) { //$NON-NLS-1$
			fileName = path;
		} else {
			fileName = path.substring(path.lastIndexOf("/") + 1, path.length()); //$NON-NLS-1$
		}
		return fileName;
	}

	@Override
	protected String getTargetPath(FileBasedCustomizableElement element) {
		return "/umlProfile/" + getFileName(element); //$NON-NLS-1$
	}

	public EClass getCustomizableElementClass() {
		return CustomizationPluginPackage.eINSTANCE.getProfile();
	}

}

Back to the top