Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4ccdeb1d9f21aa8fcb8a0efdd5239e0a8a65f52b (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
155
156
/*****************************************************************************
 * Copyright (c) 2013 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.infra.gmfdiag.common.editpolicies;

import org.eclipse.core.databinding.observable.ChangeEvent;
import org.eclipse.core.databinding.observable.IChangeListener;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.draw2d.Border;
import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ResizableCompartmentEditPart;
import org.eclipse.gmf.runtime.draw2d.ui.figures.OneLineBorder;
import org.eclipse.gmf.runtime.gef.ui.internal.editpolicies.GraphicalEditPolicyEx;
import org.eclipse.gmf.runtime.notation.BooleanValueStyle;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.gmfdiag.common.databinding.custom.CustomBooleanStyleObservableValue;
import org.eclipse.papyrus.infra.gmfdiag.common.editpart.IPapyrusEditPart;

/**
 * this edit policy has in charge to display the border of node NodeNamedElement
 * associated figure has to be a {@link NodeNamedElementFigure}
 */
public class BorderDisplayEditPolicy extends GraphicalEditPolicyEx implements IChangeListener {

	public static final String DISPLAY_BORDER = "displayBorder";

	/** key for this edit policy */
	public final static String BORDER_DISPLAY_EDITPOLICY = "BORDER_DISPLAY_EDITPOLICY";

	protected IObservableValue styleObservable;

	/**
	 * 
	 * {@inheritDoc}
	 */
	@Override
	public void activate() {
		// retrieve the view and the element managed by the edit part
		View view = (View)getHost().getModel();
		if(view == null) {
			return;
		}

		styleObservable = new CustomBooleanStyleObservableValue(view, EMFHelper.resolveEditingDomain(view), DISPLAY_BORDER);
		styleObservable.addChangeListener(this);

		getHost().refresh();
	}


	/**
	 * 
	 * @param currentView
	 * @return the current Style that reperesent the boder
	 */
	protected BooleanValueStyle getMaintainSymbolRatioStyle(View currentView) {
		return (BooleanValueStyle)currentView.getNamedStyle(NotationPackage.eINSTANCE.getBooleanValueStyle(), DISPLAY_BORDER);
	}

	/**
	 * 
	 * {@inheritDoc}
	 */
	@Override
	public void deactivate() {
		if(styleObservable != null) {
			styleObservable.removeChangeListener(this);
			styleObservable.dispose();
			styleObservable = null;
		}
	}

	@Override
	public void handleChange(ChangeEvent event) {
		getHost().refresh();
	}

	@Override
	public void refresh() {
		refreshBorderDisplay();
	}

	protected IFigure getPrimaryShape() {
		EditPart host = getHost();
		if(host instanceof IPapyrusEditPart) {
			IPapyrusEditPart graphicalHost = (IPapyrusEditPart)host;
			return graphicalHost.getPrimaryShape();
		}

		return getHostFigure();
	}

	protected View getNotationView() {
		EditPart host = getHost();
		if(host instanceof org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart) {
			return ((org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart)host).getNotationView();
		}

		View view = (View)host.getAdapter(View.class);
		return view;
	}

	protected Border defaultBorder;

	protected void refreshBorderDisplay() {
		View view = getNotationView();
		if(view == null) {
			return;
		}

		BooleanValueStyle displayBorderStyle = (BooleanValueStyle)view.getNamedStyle(NotationPackage.eINSTANCE.getBooleanValueStyle(), BorderDisplayEditPolicy.DISPLAY_BORDER);

		if(displayBorderStyle != null && !displayBorderStyle.isBooleanValue()) {
			if(defaultBorder == null) {
				defaultBorder = getPrimaryShape().getBorder();
			}
			getPrimaryShape().setBorder(null);

			for(Object currentEditPart : getHost().getChildren()) {
				if(currentEditPart instanceof ResizableCompartmentEditPart) {
					((ResizableCompartmentEditPart)currentEditPart).getFigure().setBorder(null);
				}

			}


		} else {
			if(defaultBorder != null) {
				getPrimaryShape().setBorder(defaultBorder);
			}
			defaultBorder = null;

			for(Object currentEditPart : getHost().getChildren()) {
				if(currentEditPart instanceof ResizableCompartmentEditPart) {
					((ResizableCompartmentEditPart)currentEditPart).getFigure().setBorder(new OneLineBorder());
				}
			}


		}
	}

}

Back to the top