Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9a054c7c12ffebe42283c5af0a11d3fe164eb512 (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
/*****************************************************************************
 * Copyright (c) 2009 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:
 *  Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.uml.properties.tabbedproperties.comments.propertysection;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.domain.IEditingDomainProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.infra.core.utils.EditorUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.Comment;

/**
 * Section with advanced editor for the {@link Comment} in Papyrus
 */
public class CommentPropertySection extends AbstractPropertySection {

	/** advanced editor for the comments */
	protected CommentRichTextEditor richText;

	/** edited comment */
	protected Comment comment;

	/**
	 * Creates a new CommentPropertySection
	 */
	public CommentPropertySection() {
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void createControls(Composite parent, TabbedPropertySheetPage aTabbedPropertySheetPage) {
		super.createControls(parent, aTabbedPropertySheetPage);

		richText = CommentRichTextFormToolkit.createFocusAwareRichTextEditor(getWidgetFactory(), parent, "", comment, SWT.NONE, EditorUtils.getMultiDiagramEditor().getEditorSite());
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setInput(IWorkbenchPart part, ISelection selection) {
		super.setInput(part, selection);
		if(!(selection instanceof IStructuredSelection) || !((part instanceof IEditingDomainProvider) || part.getAdapter(IEditingDomainProvider.class) != null)) {
			return;
		}

		Object firstElement = ((IStructuredSelection)selection).getFirstElement();
		EObject newEObject = resolveSemanticObject(firstElement);

		if(newEObject instanceof Comment) {
			setComment((Comment)newEObject);
			richText.init(comment, null);
			richText.setText(comment.getBody());
		}
	}

	/**
	 * Sets the comment to edit
	 * 
	 * @param comment
	 *        the comment to edit
	 */
	protected void setComment(Comment comment) {
		this.comment = comment;
	}

	/**
	 * Returns the comment to edit
	 * 
	 * @return the comment to edit
	 */
	protected Comment getComment() {
		return comment;
	}

	/**
	 * Resolve semantic element
	 * 
	 * @param object
	 *        the object to resolve
	 * @return <code>null</code> or the semantic element associated to the specified object
	 */
	private EObject resolveSemanticObject(Object object) {
		if(object instanceof EObject) {
			return (EObject)object;
		} else if(object instanceof IAdaptable) {
			IAdaptable adaptable = (IAdaptable)object;
			if(adaptable.getAdapter(EObject.class) != null) {
				return (EObject)adaptable.getAdapter(EObject.class);
			}
		}
		return null;
	}
}

Back to the top