Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5a1146a321414ed01f6a84d8cafc2a4b33d4f0a3 (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
/*
 * Copyright (c) 2007, 2008 Borland Software Corporation
 * 
 * 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:
 *    Dmitry Stadnik (Borland) - initial API and implementation
 *    Artem Tikhomirov (Borland) - refactoring (https://bugs.eclipse.org/230014)
 */

«IMPORT 'http://www.eclipse.org/gmf/2008/GenModel'»
«IMPORT 'http://www.eclipse.org/emf/2002/Ecore'»

«DEFINE className FOR gmfgen::GenDiagram»ResultView«ENDDEFINE»

«DEFINE Class FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
	public static class «EXPAND className» «EXPAND supertypes» «EXPAND implements»{
		«EXPAND xpt::Common::generatedMemberComment»
		public static final String VIEW_ID = "«getMetricViewID()»";«EXPAND xpt::Common::nonNLS»

		«EXPAND xpt::Common::generatedMemberComment»
		private static int MAX_VISIBLE_KEY_CHAR_COUNT = 8;

		«EXPAND viewerField-»

		«EXPAND inputField-»

		«EXPAND setInputMethod-»

		«EXPAND adjustLayoutMethod-»

		«EXPAND createPartControlMethod-»

		«EXPAND handleOpenMethod-»

		«EXPAND calcMetricMaxValueStrLenMapMethod-»

		«EXPAND setFocusMethod-»

		«EXPAND labelProviderClass-»

		«EXPAND additions-»
	}
«ENDDEFINE»

«DEFINE supertypes FOR gmfgen::GenDiagram»extends org.eclipse.ui.part.ViewPart«ENDDEFINE»
«DEFINE implements FOR gmfgen::GenDiagram»implements org.eclipse.jface.viewers.IOpenListener«ENDDEFINE»
«DEFINE additions FOR gmfgen::GenDiagram»«ENDDEFINE»

«DEFINE viewerField FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
	private org.eclipse.jface.viewers.TableViewer viewer;
«ENDDEFINE»

«DEFINE inputField FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
	private org.eclipse.emf.ecore.resource.Resource diagramResource;
«ENDDEFINE»


«DEFINE setInputMethod FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
	void setInput(org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramWorkbenchPart diagramPart) {
		diagramResource = diagramPart.getDiagram().eResource();
		setTitleToolTip(diagramResource.getURI().path());
		java.util.List metrics = calculateMetrics(diagramPart);
		adjustLayout(metrics);
		viewer.setInput(metrics); 
	}
«ENDDEFINE»

«DEFINE adjustLayoutMethod FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
	private void adjustLayout(java.util.List metricResultList) {
		java.util.Map maxValStrMap = calcMetricMaxValueStrLenMap(metricResultList);
		org.eclipse.swt.widgets.Table table = viewer.getTable();
		org.eclipse.jface.viewers.TableLayout layout = new org.eclipse.jface.viewers.TableLayout();
		org.eclipse.swt.graphics.GC gc = new org.eclipse.swt.graphics.GC(table);

		gc.setFont(org.eclipse.jface.resource.JFaceResources.getDialogFont());
		int padding = gc.stringExtent("X").x * 2;«EXPAND xpt::Common::nonNLS»
		for (int i = 0; i < getMetricKeys().length; i++) {
			final String nextKey = getMetricKeys()[i];
			String valueStr = (String) maxValStrMap.get(nextKey);
			int minWidth = valueStr != null ? gc.stringExtent(valueStr).x + padding : 20;
			layout.addColumnData(new org.eclipse.jface.viewers.ColumnPixelData(minWidth, true));
		}
		gc.dispose();

		layout.addColumnData(new org.eclipse.jface.viewers.ColumnWeightData(1, 50, true));
		viewer.getTable().setLayout(layout);
		viewer.getTable().layout(true, true);
	}
«ENDDEFINE»

«DEFINE createPartControlMethod FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
	public void createPartControl(org.eclipse.swt.widgets.Composite parent) {
		this.viewer = new org.eclipse.jface.viewers.TableViewer(parent, org.eclipse.swt.SWT.FULL_SELECTION);
		final org.eclipse.swt.widgets.Table table = viewer.getTable();
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		for (int i = 0; i < getMetricKeys().length; i++) {
			org.eclipse.swt.widgets.TableColumn column = new org.eclipse.swt.widgets.TableColumn(table, org.eclipse.swt.SWT.NONE);
			column.setAlignment(org.eclipse.swt.SWT.RIGHT);
			column.setMoveable(true);
			column.setText(getMetricKeys()[i]);
			column.setToolTipText(getMetricToolTips()[i]);
		}

		org.eclipse.swt.widgets.TableColumn objectColumn = new org.eclipse.swt.widgets.TableColumn(table, org.eclipse.swt.SWT.NONE);
		objectColumn.setText("Element");
		objectColumn.setToolTipText("Measurement element");

		viewer.setLabelProvider(new Labels());
		viewer.setContentProvider(new org.eclipse.jface.viewers.ArrayContentProvider());
		viewer.addOpenListener(this);

		org.eclipse.swt.events.SelectionListener headerSelListener = new org.eclipse.swt.events.SelectionListener() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				table.setSortColumn((org.eclipse.swt.widgets.TableColumn) e.getSource());  
				table.setSortDirection((table.getSortDirection() != org.eclipse.swt.SWT.DOWN) ? org.eclipse.swt.SWT.DOWN : org.eclipse.swt.SWT.UP);
				viewer.refresh();
			}
			public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
			}
		};
		org.eclipse.swt.widgets.TableColumn[] columns = viewer.getTable().getColumns();
		for (int i = 0; i < columns.length; i++) {
			columns[i].addSelectionListener(headerSelListener);
		}

		viewer.setSorter(new org.eclipse.jface.viewers.ViewerSorter() {
			public int compare(org.eclipse.jface.viewers.Viewer viewer, Object e1, Object e2) {
				org.eclipse.swt.widgets.TableColumn c = table.getSortColumn();
				int result = 0;
				if (c != null) {
					Metric mc1 = ((ElementMetrics) e1).getMetricByKey(c.getText());
					Metric mc2 = ((ElementMetrics) e2).getMetricByKey(c.getText());
					result = (mc1 != null && mc2 != null) ?
						mc1.compareTo(mc2) : (mc1 == null ? -1 : 1);
				} else {
					result = ((ElementMetrics) e1).targetElementQName.compareTo(((ElementMetrics) e2).targetElementQName);
				}
				return table.getSortDirection() == org.eclipse.swt.SWT.DOWN ? result : -result;
			}
		});
		«EXPAND createPartControlMethod_refreshInput FOR editorGen.editor-»
		}
«ENDDEFINE»

«DEFINE createPartControlMethod_refreshInput FOR gmfgen::GenEditorView-»
        org.eclipse.ui.IEditorPart editor = getSite().getPage().getActiveEditor();
        if (editor != null && editor.getClass().equals(«editorGen.editor.getQualifiedClassName()».class)) {
			setInput((«editorGen.editor.getQualifiedClassName()») editor);
        }
«ENDDEFINE»

«DEFINE handleOpenMethod FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
	public void open(org.eclipse.jface.viewers.OpenEvent event) {
		try {
			org.eclipse.ui.IEditorPart editorPart = getSite().getPage().openEditor(
				new org.eclipse.ui.part.FileEditorInput(org.eclipse.emf.workspace.util.WorkspaceSynchronizer.getFile(diagramResource)),
				«editorGen.editor.getQualifiedClassName()».ID);
			if (editorPart == null) {
				return;
			}
			org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramWorkbenchPart diagramPart =
				(org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramWorkbenchPart) editorPart.getAdapter(
					org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramWorkbenchPart.class);
			ElementMetrics selection = (ElementMetrics) ((org.eclipse.jface.viewers.IStructuredSelection) event.getSelection()).getFirstElement();
			String viewID = selection.diagramElementID;
			if (viewID != null) {
				org.eclipse.gmf.runtime.notation.View targetView = (org.eclipse.gmf.runtime.notation.View) diagramPart.getDiagram().eResource().getEObject(viewID);
				if (targetView != null) {
					org.eclipse.gef.EditPart targetEditPart = (org.eclipse.gef.EditPart) diagramPart.getDiagramGraphicalViewer().getEditPartRegistry().get(targetView);
					if (targetEditPart != null) {
						«getDiagramEditorUtilQualifiedClassName()».selectElementsInDiagram(diagramPart, java.util.Collections.singletonList(targetEditPart));
					}
				}
			}
		} catch (org.eclipse.ui.PartInitException e) {
			«editorGen.plugin.getActivatorQualifiedClassName()».getInstance().logError("Can't open diagram editor", e); //$NON-NLS-1$
		}
	}
«ENDDEFINE»

«DEFINE calcMetricMaxValueStrLenMapMethod FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
	private static java.util.Map calcMetricMaxValueStrLenMap(java.util.List allMetrics) {
		java.util.Map metric2MaxStrLen = new java.util.HashMap();
		for (int i = 0; i < getMetricKeys().length; i++) {
			String nextKey = getMetricKeys()[i]; 
			int trimPos = Math.min(nextKey.length(), MAX_VISIBLE_KEY_CHAR_COUNT);
			metric2MaxStrLen.put(nextKey, nextKey.substring(0, trimPos));
		}
		for (java.util.Iterator it = allMetrics.iterator(); it.hasNext();) {
			ElementMetrics elementMetrics = (ElementMetrics) it.next();
			for (int i = 0; i < elementMetrics.metrics.length; i++) {
				Metric metric = elementMetrics.metrics[i];
				String valueStr = (String) metric2MaxStrLen.get(metric.key);
				if (valueStr == null || metric.displayValue.length() > valueStr.length()) {
					metric2MaxStrLen.put(metric.key, metric.displayValue);	
				}
			}
		}
		return metric2MaxStrLen;
	}
«ENDDEFINE»

«DEFINE setFocusMethod FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
	public void setFocus() {
	}
«ENDDEFINE»

«DEFINE labelProviderClass FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
	private class Labels extends org.eclipse.jface.viewers.LabelProvider implements org.eclipse.jface.viewers.ITableLabelProvider, org.eclipse.jface.viewers.ITableColorProvider {

		«EXPAND xpt::Common::generatedMemberComment»
		private boolean isElementColumn(int columnIndex) {
			return columnIndex >= getMetricKeys().length;
		}

		«EXPAND xpt::Common::generatedMemberComment»
		public org.eclipse.swt.graphics.Image getColumnImage(Object element, int columnIndex) {
			return isElementColumn(columnIndex) ? ((ElementMetrics) element).elementImage : null;
		}

		«EXPAND xpt::Common::generatedMemberComment»
		public String getColumnText(Object element, int columnIndex) {
			ElementMetrics elementMetrics = (ElementMetrics) element;
			if (columnIndex == getMetricKeys().length) {
				return elementMetrics.targetElementQName;
			}
			final String key = getMetricKeys()[columnIndex];
			Metric metric = elementMetrics.getMetricByKey(key);
			return (metric != null) ? metric.displayValue : "-"; //$NON-NLS-1$
		}

		«EXPAND xpt::Common::generatedMemberComment»
		public org.eclipse.swt.graphics.Color getBackground(Object element, int columnIndex) {
			return null; 
		}

		«EXPAND xpt::Common::generatedMemberComment»
		public org.eclipse.swt.graphics.Color getForeground(Object element, int columnIndex) {
			if (isElementColumn(columnIndex)) {
				return null;
			}
			ElementMetrics columnElement = (ElementMetrics) element;
			final String key = getMetricKeys()[columnIndex];
			Metric metric = columnElement.getMetricByKey(key);
			if (metric != null && metric.value != null) {
				if (metric.highLimit != null && metric.highLimit.longValue() < metric.value.longValue()) {
					return «EXPAND labelProvider_colorMore»;
				} else if (metric.lowLimit != null && metric.lowLimit.longValue() > metric.value.longValue()) {
					return «EXPAND labelProvider_colorLess»;
				}
			}
			return null;
		}
	}		
«ENDDEFINE»

«DEFINE labelProvider_colorMore FOR gmfgen::GenDiagram»org.eclipse.draw2d.ColorConstants.red«ENDDEFINE»
«DEFINE labelProvider_colorLess FOR gmfgen::GenDiagram»org.eclipse.draw2d.ColorConstants.blue«ENDDEFINE»


 

Back to the top