Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 799bdb338213606bfd8f2031ba43d9915f9f51bb (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*****************************************************************************
 * 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.gmf.editpoliciesstates.view;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.editparts.AbstractEditPart;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.IContentProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.papyrus.infra.gmfdiag.dnd.policy.CustomizableDropEditPolicy;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

/**
 * this class is used to see all editpolicies attached to a editpart
 * It very specify for papyrus.
 *
 *
 */
public class EditPoliciesStateView extends ViewPart {

	/**
	 * this a object that represents a line into the table
	 */
	class EditPolicyDescriptor {
		public String role = "NO ROLE";
		public EditPolicy policy = null;
	}


	private ArrayList<EditPolicyDescriptor> editPolicyList = new ArrayList<EditPolicyDescriptor>();
	private ISelectionListener myEditPartlistener;
	private TreeViewer viewer;

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
	 */
	@Override
	public void setFocus() {
		Viewer viewer = getViewer();
		if (viewer != null && !viewer.getControl().isDisposed()) {

			viewer.getControl().setFocus();
		}
	}

	/**
	 * Create the main tree control
	 *
	 * @param parent
	 * @return Tree
	 */
	protected Tree createTree(Composite parent) {
		Tree tree = new Tree(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI
				| SWT.FULL_SELECTION);
		tree.setLinesVisible(true);
		return tree;
	}

	/**
	 * Return the viewer.
	 *
	 * @return TreeViewer
	 */
	protected TreeViewer getViewer() {
		return viewer;
	}

	/**
	 *
	 * Constructor.
	 *
	 */
	public EditPoliciesStateView() {
		myEditPartlistener = new ISelectionListener() {

			public void selectionChanged(IWorkbenchPart part, ISelection selection) {

				if (selection instanceof IStructuredSelection) {
					Object[] policiesValue = null;
					Object selectedobject = ((IStructuredSelection) selection).getFirstElement();
					if (selectedobject instanceof org.eclipse.gef.GraphicalEditPart) {
						org.eclipse.gef.GraphicalEditPart graphicalEP = ((org.eclipse.gef.GraphicalEditPart) selectedobject);
						Field policiesFiled = null;
						try {
							policiesFiled = AbstractEditPart.class.getDeclaredField("policies");
						} catch (NoSuchFieldException e) {
							e.printStackTrace();
						} catch (SecurityException e) {
							e.printStackTrace();
						}
						policiesFiled.setAccessible(true);

						try {
							policiesValue = (Object[]) policiesFiled.get(graphicalEP);
						} catch (IllegalArgumentException e) {
							e.printStackTrace();
						} catch (IllegalAccessException e) {
							e.printStackTrace();
						}
						editPolicyList.clear();

						for (int i = 0; i < policiesValue.length; i = i + 2) {
							EditPolicyDescriptor epd = new EditPolicyDescriptor();
							if (policiesValue[i] instanceof String) {
								epd.role = (String) policiesValue[i];
								editPolicyList.add(epd);
							}


							if (policiesValue[i + 1] instanceof EditPolicy) {
								epd.policy = (EditPolicy) policiesValue[i + 1];
							}
						}
						if (viewer != null) {
							viewer.setInput(editPolicyList);
						}
					}

				}
			}
		};
		ISelectionService selectionService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();
		selectionService.addSelectionListener(myEditPartlistener);

	}

	protected void createColumns() {
		viewer.getTree().setHeaderVisible(true);
		viewer.getTree().setLinesVisible(true);

		TreeColumn tcName = new TreeColumn(viewer.getTree(), SWT.LEFT);
		tcName.setText("Name");
		tcName.setWidth(200);
		TreeColumn tcDescription = new TreeColumn(viewer.getTree(), SWT.LEFT);
		tcDescription.setText("Description");
		tcDescription.setWidth(800);
	}

	@Override
	public void createPartControl(final org.eclipse.swt.widgets.Composite parent) {
		final IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager();
		tbm.add(getClearViewAction());
		viewer = new TreeViewer(createTree(parent));
		viewer.setContentProvider(getContentProvider());
		viewer.setLabelProvider(getLabelProvider());
		createColumns();

	}

	protected ITableLabelProvider getLabelProvider() {
		return new EditpolicyLabelProvider();
	}

	protected Action getClearViewAction() {
		return new Action() {

			@Override
			public void run() {
				clear();
			}

			@Override
			public boolean isEnabled() {
				return true;
			}

			@Override
			public String getToolTipText() {
				return getText();
			}

			@Override
			public String getText() {
				return "Clear the view";
			}

		};
	}


	protected IContentProvider getContentProvider() {
		return new ITreeContentProvider() {

			public Object[] getElements(final Object inputElement) {
				if (inputElement instanceof Collection<?>) {
					return ((Collection<?>) inputElement).toArray();
				}
				return new Object[0];

			}

			public void inputChanged(final Viewer viewer, final Object oldInput, final Object newInput) {
				// nothing
			}

			public void dispose() {
				// nothing
			}

			public boolean hasChildren(final Object element) {

				return getChildren(element).length != 0;
			}

			public Object getParent(final Object element) {
				return null;
			}

			public Object[] getChildren(final Object parentElement) {
				if (parentElement instanceof EditPolicyDescriptor) {
					if (((EditPolicyDescriptor) parentElement).policy != null) {
						if (((EditPolicyDescriptor) parentElement).policy instanceof CustomizableDropEditPolicy) {
							Field defaultDropEditPolicyField;
							try {
								defaultDropEditPolicyField = CustomizableDropEditPolicy.class.getDeclaredField("defaultDropEditPolicy");

								Field defaultCreationEditPolicyField = CustomizableDropEditPolicy.class.getDeclaredField("defaultCreationEditPolicy");
								defaultDropEditPolicyField.setAccessible(true);
								defaultCreationEditPolicyField.setAccessible(true);

								EditPolicyDescriptor edp1 = new EditPolicyDescriptor();
								EditPolicyDescriptor edp2 = new EditPolicyDescriptor();
								edp1.policy = (EditPolicy) defaultDropEditPolicyField.get(((EditPolicyDescriptor) parentElement).policy);
								edp2.policy = (EditPolicy) defaultCreationEditPolicyField.get(((EditPolicyDescriptor) parentElement).policy);
								edp1.role = "defaultDropEditPolicy";
								edp2.role = "defaultCreationEditPolicy";
								Object[] result = new Object[2];
								result[0] = edp1;
								result[1] = edp2;
								return result;
							} catch (NoSuchFieldException e) {
								e.printStackTrace();
							} catch (SecurityException e) {
								e.printStackTrace();
							} catch (IllegalArgumentException e) {
								e.printStackTrace();
							} catch (IllegalAccessException e) {
								e.printStackTrace();
							}
						}
					}

				}
				return new Object[0];
			}
		};
	}

	@Override
	public void dispose() {
		super.dispose();
		ISelectionService selectionService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();
		selectionService.removeSelectionListener(myEditPartlistener);
		clear();
	}

	private void clear() {
		this.editPolicyList.clear();
	}
}

Back to the top