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
|
/*****************************************************************************
* Copyright (c) 2016 CEA LIST 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:
* Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
*****************************************************************************/
package org.eclipse.papyrus.uml.ui.editors;
import org.eclipse.core.runtime.Assert;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.window.Window;
import org.eclipse.papyrus.infra.emf.utils.TextReferencesHelper;
import org.eclipse.papyrus.infra.widgets.editors.TreeSelectorDialog;
import org.eclipse.papyrus.infra.widgets.editors.richtext.AbstractToolbarButton;
import org.eclipse.papyrus.infra.widgets.editors.richtext.GenericRichTextEditor;
import org.eclipse.papyrus.infra.widgets.editors.richtext.RichTextUtils;
import org.eclipse.papyrus.infra.widgets.providers.EncapsulatedContentProvider;
import org.eclipse.papyrus.infra.widgets.providers.IStaticContentProvider;
import org.eclipse.papyrus.uml.ui.Activator;
import org.eclipse.papyrus.uml.ui.messages.Messages;
import org.eclipse.swt.widgets.Display;
/**
*
* @author VL222926
*
*/
public class InsertReferenceToolbarButton extends AbstractToolbarButton {
/**
* the name of the button
*/
private static final String BUTTON_NAME = "InsertReferenceButton"; //$NON-NLS-1$
/**
* the name of the command
*/
private static final String BUTTON_COMMAND_NAME = "InsertReferenceCommand"; //$NON-NLS-1$
/**
* the label of the button
*/
private static final String BUTTON_LABEL = Messages.InsertReferenceToolbarButton_Tooltip;
/**
* the path of the icon used for the button
*/
private static final String ICON_PATH = "icons/hyperlink_16x16.gif"; //$NON-NLS-1$
/**
*
* Constructor.
*
* @param richTextEditor
* the richtext editor
*/
public InsertReferenceToolbarButton() {
super(BUTTON_NAME, BUTTON_COMMAND_NAME, BUTTON_LABEL, RichTextUtils.TOOLBAR_GROUP_LINKS, org.eclipse.papyrus.infra.widgets.Activator.getDefault().getURL(ICON_PATH));
}
/**
* Opens the dialog to insert a reference in the rich text content
*
* @see org.eclipse.nebula.widgets.richtext.toolbar.ToolbarButton#execute()
*/
@Override
public Object execute() {
if (!(this.richTextEditor instanceof UMLRichtextEditorWithReferences)) {
Activator.log.warn("The insert references action cannot be executed, because the Richtext editor is not a UMLRichtextEditorWithReferences");//$NON-NLS-1$
} else {
UMLRichtextEditorWithReferences editor = (UMLRichtextEditorWithReferences) this.richTextEditor;
final IStaticContentProvider referenceContentProvider = editor.getContentProvider();
final ILabelProvider labelProvider = editor.getLabelProvider();
final TextReferencesHelper referencesHelper = editor.getTextReferencesHelper();
if (referenceContentProvider != null && labelProvider != null && referencesHelper != null) {
TreeSelectorDialog dialog = new TreeSelectorDialog(Display.getDefault().getActiveShell());
dialog.setContentProvider(new EncapsulatedContentProvider(referenceContentProvider));
dialog.setLabelProvider(labelProvider);
if (dialog.open() == Window.OK) {
Object[] result = dialog.getResult();
if (result.length == 0) {
return null;
}
Object resultElement = result[0];
if (!(resultElement instanceof EObject)) {
return null;
}
EObject objectToReference = (EObject) resultElement;
String newText = referencesHelper.insertReference(objectToReference, "", 0); //$NON-NLS-1$
if (this.richTextEditor.isTagsEnabled()) {
try {
String realName = referencesHelper.replaceReferences(newText).replaceAll("\\<u\\>", "").replaceAll("\\<\\/u\\>", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
String reference = newText.replaceAll("\\{\\@link", "").replaceAll("\\}", "").replaceAll("\\s", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
this.richTextEditor.insertHTML("<a class=\"internal_ref\" href=\"" + reference + "\">" + realName + "</a>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
} catch (Exception e) {
Activator.log.error(e);
this.richTextEditor.insertText(newText);
}
} else {
this.richTextEditor.insertText(newText);
}
}
}
}
return null;
}
/**
* @see org.eclipse.papyrus.infra.widgets.editors.richtext.AbstractToolbarButton#setRichTextEditor(org.eclipse.papyrus.infra.widgets.editors.richtext.GenericRichTextEditor)
*
* @param editor
*/
@Override
public void setRichTextEditor(final GenericRichTextEditor editor) {
Assert.isTrue(editor instanceof UMLRichtextEditorWithReferences);
super.setRichTextEditor(editor);
}
}
|