Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e68a2b0559021bdf18b7c6779caa55513028ca1a (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
//------------------------------------------------------------------------------
// Copyright (c) 2009 Anyware Technologies 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:
//      Anyware Technologies - initial API and implementation
//------------------------------------------------------------------------------
package org.eclipse.papyrus.views.documentation.view.actions;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.epf.richtext.IRichText;
import org.eclipse.epf.richtext.RichTextCommand;
import org.eclipse.epf.richtext.RichTextImages;
import org.eclipse.epf.richtext.actions.RichTextAction;
import org.eclipse.jface.action.IAction;
import org.eclipse.papyrus.views.documentation.view.DocViewPlugin;
import org.eclipse.papyrus.views.documentation.view.DocumentionPartHandlerRegistry;
import org.eclipse.papyrus.views.documentation.view.IDocumentationPartHandler;
import org.eclipse.papyrus.views.documentation.view.Messages;
import org.eclipse.ui.IEditorPart;

/**
 * Adds a link to a topcased model element.
 * 
 * @author Jose Alfredo Serrano
 */
public class AddElementLinkAction extends RichTextAction {

	/**
	 * Creates a new instance.
	 */
	public AddElementLinkAction(IRichText richText) {
		super(richText, IAction.AS_PUSH_BUTTON);
		setImageDescriptor(DocViewPlugin.getDefault().getImageRegistry().getDescriptor("MODEL_LINK")); //$NON-NLS-1$
		setDisabledImageDescriptor(RichTextImages.DISABLED_IMG_DESC_ADD_LINK);
		setToolTipText(Messages.AddElementLinkAction_text);
	}

	/**
	 * Executes the action.
	 * 
	 * @param richText
	 *            a rich text control
	 */
	public void execute(IRichText richText)
	{
		if (richText != null)
		{
			String linkURL = createURL();
			if (linkURL != null && linkURL.length() > 0) {
				richText.executeCommand(RichTextCommand.ADD_LINK, linkURL);
			}
		}
	}
	
	public boolean disableInSourceMode() {
		return false;
	}

	private String createURL()
	{
		EObject selection = null;
		IEditorPart activeEditor = DocViewPlugin.getActiveEditor();
		IDocumentationPartHandler documentationPartHandler = DocumentionPartHandlerRegistry.getInstance().getDocumentationPartHandler(activeEditor);
		if (documentationPartHandler != null) {
			selection = documentationPartHandler.openElementSelectionDialog(activeEditor);
		}
		
		if (selection != null)
		{
			// XXX : HACK Extreme hacking on... 
			// there should be a proper way to handle with spaces and URIs.
			String uri = EcoreUtil.getURI(selection).toString();
			return "http://" + uri.replace(" ", "%20"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		}
		
		return null;
	}
}

Back to the top