Skip to main content
summaryrefslogtreecommitdiffstats
blob: da330f59755aa3dc384221a42ca70b847909d79a (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*******************************************************************************
 * Copyright (c) 2006 IBM 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:
 *     IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse
 *******************************************************************************/

package org.eclipse.linuxtools.systemtap.graphingapi.ui.wizards.graph;

import java.text.MessageFormat;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.linuxtools.internal.systemtap.graphingapi.ui.Localization;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;

/**
 * @since 2.1
 */
public class SelectGraphAndSeriesWizardPage extends WizardPage implements Listener {
	public SelectGraphAndSeriesWizardPage() {
		super("selectGraphAndSeries"); //$NON-NLS-1$
		setTitle(Localization.getString("SelectGraphAndSeriesWizardPage.SelectGraphAndSeries")); //$NON-NLS-1$
	}

	@Override
	public void createControl(Composite parent) {
		wizard = (SelectGraphAndSeriesWizard)getWizard();
		model = wizard.model;
		edit = wizard.isEditing();

		//Set the layout data
		Composite comp = new Composite(parent, SWT.NULL);
		comp.setLayout(new GridLayout());

		Group cmpGraphOptsGraph = new Group(comp, SWT.SHADOW_ETCHED_IN);
		cmpGraphOptsGraph.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, false, false));
		RowLayout rowLayout = new RowLayout();
		rowLayout.type = SWT.HORIZONTAL;
		rowLayout.spacing = 10;
		cmpGraphOptsGraph.setLayout(rowLayout);
		cmpGraphOptsGraph.setText(Localization.getString("SelectGraphAndSeriesWizardPage.Graph")); //$NON-NLS-1$

		String[] graphIDs = GraphFactory.getAvailableGraphs(wizard.model.getDataSet());
		btnGraphs = new Button[graphIDs.length];
		for(int i=0; i<btnGraphs.length; i++) {
			btnGraphs[i] = new Button(cmpGraphOptsGraph, SWT.RADIO);
			btnGraphs[i].setImage(GraphFactory.getGraphImage(graphIDs[i]));
			btnGraphs[i].addListener(SWT.Selection, this);
			btnGraphs[i].setData(graphIDs[i]);
			btnGraphs[i].setToolTipText(GraphFactory.getGraphName(btnGraphs[i].getData().toString()) + "\n\n" + //$NON-NLS-1$
					GraphFactory.getGraphDescription(btnGraphs[i].getData().toString()));
			if (wizard.isEditing() && graphIDs[i].equals(wizard.model.getGraphID())) {
				btnGraphs[i].setSelection(true);
			}
		}

		Group cmpGraphOptsSeries = new Group(comp, SWT.SHADOW_ETCHED_IN);
		cmpGraphOptsSeries.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		cmpGraphOptsSeries.setLayout(layout);

		Label lblTitle = new Label(cmpGraphOptsSeries, SWT.NONE);
		lblTitle.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
		lblTitle.setText(Localization.getString("SelectGraphAndSeriesWizardPage.Title")); //$NON-NLS-1$
		txtTitle = new Text(cmpGraphOptsSeries, SWT.BORDER);
		txtTitle.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		if (edit) {
			txtTitle.setText(model.getGraphData().title);
		}
		txtTitle.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				checkErrors();
			}
		});

		//Add the data series widgets
		String[] labels = model.getSeries();

		cboYItems = new Combo[!edit ? labels.length : Math.max(labels.length, model.getYSeries().length)];
		lblYItems = new Label[cboYItems.length];
		deleted = new boolean[cboYItems.length + 1];

		Label lblXItem = new Label(cmpGraphOptsSeries, SWT.NONE);
		lblXItem.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
		lblXItem.setText(Localization.getString("SelectGraphAndSeriesWizardPage.XSeries")); //$NON-NLS-1$
		cboXItem = new Combo(cmpGraphOptsSeries, SWT.DROP_DOWN|SWT.READ_ONLY);
		cboXItem.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		cboXItem.addSelectionListener(new ComboSelectionListener());
		cboXItem.add(Localization.getString("SelectGraphAndSeriesWizardPage.RowID")); //$NON-NLS-1$

		for(int i=0; i<cboYItems.length; i++) {
			lblYItems[i] = new Label(cmpGraphOptsSeries, SWT.NONE);
			lblYItems[i].setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
			lblYItems[i].setText(MessageFormat.format(Localization.getString("SelectGraphAndSeriesWizardPage.YSeries"), new Integer(i))); //$NON-NLS-1$
			cboYItems[i] = new Combo(cmpGraphOptsSeries, SWT.DROP_DOWN|SWT.READ_ONLY);
			cboYItems[i].setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
			cboYItems[i].addSelectionListener(new ComboSelectionListener());

			if(i>0) {
				cboYItems[i].add(Localization.getString("SelectGraphAndSeriesWizardPage.NA")); //$NON-NLS-1$
				cboYItems[i].setVisible(false);
				lblYItems[i].setVisible(false);
			}
		}

		for(int j,i=0; i<labels.length; i++) {
			cboXItem.add(labels[i]);
			for(j=0; j<lblYItems.length; j++)
				cboYItems[j].add(labels[i]);
		}

		int selected;
		if (!edit) {
			cboXItem.select(0);
			cboYItems[0].select(0);
		} else {
			selected = model.getXSeries();
			if (selected < labels.length){
				cboXItem.select(selected + 1);
			} else {
				cboXItem.add(Localization.getString("SelectGraphAndSeriesWizardPage.Deleted"), 0); //$NON-NLS-1$
				cboXItem.select(0);
				deleted[0] = true;
			}
			selected = model.getYSeries()[0];
			if (selected < labels.length) {
				cboYItems[0].select(selected);
			} else {
				cboYItems[0].add(Localization.getString("SelectGraphAndSeriesWizardPage.Deleted"), 0); //$NON-NLS-1$
				cboYItems[0].select(0);
				deleted[1] = true;
			}
		}
		boolean cvisible = true;
		for(int i=1; i<cboYItems.length; i++) {
			if (!edit || model.getYSeries().length <= i) {
				cboYItems[i].select(selected = 0);
			} else {
				selected = model.getYSeries()[i];
				if (selected < labels.length){
					cboYItems[i].select(selected + 1);
				} else {
					cboYItems[i].add(Localization.getString("SelectGraphAndSeriesWizardPage.Deleted"), 0); //$NON-NLS-1$
					cboYItems[i].select(0);
					deleted[i+1] = true;
				}
			}
			cboYItems[i].setVisible(cvisible);
			lblYItems[i].setVisible(cvisible);
			cvisible = (selected > 0);
		}

		//Select one of the graph types by default, rather than blank choice
		if (!edit) {
			btnGraphs[0].setSelection(true);
			saveDataToModelGraph(graphIDs[0]);
		}
		else {
			saveDataToModelGraph(wizard.model.getGraphID());
		}

		setControl(comp);
		checkErrors();
	}

	@Override
	public void handleEvent(Event event) {
		if(event.widget instanceof Button) {
			saveDataToModelGraph(((Button)event.widget).getData().toString());
			wizard.getContainer().updateButtons();
		}
	}

	@Override
	public boolean canFlipToNextPage() {
		return false;
	}

	@Override
	public boolean isPageComplete() {
		return saveDataToModelSeries();
	}

	/**
	 * Saves the choice of graph type to the model.
	 * @param selected The ID of the selected graph.
	 */
	private void saveDataToModelGraph(String selected) {
		model.setGraph(selected);
	}

	/**
	 * Saves all information pertaining to series data & naming to the model.
	 * @return <code>true</code> if there are no conflicts in series data selection,
	 * <code>false</code> otherwise. In the case of the latter, no data is saved.
	 */
	private boolean saveDataToModelSeries() {
		if(getErrorMessage() == null) {
			model.setTitle(txtTitle.getText());

			if(null != txtKey && txtKey.isEnabled())
				model.setKey(txtKey.getText());
			else
				model.setKey(null);

			model.setXSeries(cboXItem.getSelectionIndex()-1);

			int i, count;
			for(i=1, count=1; i<cboYItems.length && 0 != cboYItems[i].getSelectionIndex(); i++) {
				count++;
			}

			int[] ySeries = new int[count];
			ySeries[0] = cboYItems[0].getSelectionIndex();
			for(i=1; i<count; i++) {
				ySeries[i] = cboYItems[i].getSelectionIndex()-1;
			}
			model.setYSeries(ySeries);
			return true;
		}
		return false;
	}

	private void markAsDuplicate(Combo item, Boolean bad) {
		item.setForeground(item.getDisplay().getSystemColor(bad ? SWT.COLOR_RED : SWT.COLOR_BLACK));
	}

	/**
	 * Checks for conflicts in data selection. (An example of a conflict
	 * is two Y-series fields set to the same output value.)
	 * @return <code>true</code> if there is no conflict, <code>false</code> otherwise.
	 * Also visually marks conficting series.
	 */
	private boolean isSeriesUnique() {
		boolean foundDuplicate = false;

		// Undo duplicate marking, as it is to be updated.
		markAsDuplicate(cboXItem, false);
		for (int i = 0; i < cboYItems.length; i++) {
			markAsDuplicate(cboYItems[i], false);
		}

		for(int j,i=0; i<cboYItems.length; i++) {
			if(cboYItems[i].isVisible() && !deleted[i+1]) {
				for(j=i+1; j<cboYItems.length; j++) {
					try {
						if(!deleted[j+1] && cboYItems[j].isVisible() && cboYItems[i].getItem(cboYItems[i].getSelectionIndex())
								.equals(cboYItems[j].getItem(cboYItems[j].getSelectionIndex()))) {
								markAsDuplicate(cboYItems[i], true);
								markAsDuplicate(cboYItems[j], true);
								foundDuplicate = true;
						}
					} catch (Exception e) {
						// If a cboYItem has no item selected, don't mark any duplicates. Ignore.
					}
				}
				try {
					if(!deleted[0] && cboYItems[i].getItem(cboYItems[i].getSelectionIndex()).equals(cboXItem.getItem(cboXItem.getSelectionIndex()))) {
						markAsDuplicate(cboYItems[i], true);
						markAsDuplicate(cboXItem, true);
						foundDuplicate = true;
					}
				} catch (Exception e) {
					// Ignore for same reason as above.
				}
			}
		}
		return !foundDuplicate;
	}

	/**
	 * Checks for deleted/unselected series entries.
	 * @return <code>true if some value is not selected, <code>false</code> otherwise.
	 */
	private boolean isSeriesDeleted() {
		for (int i = 0; i < deleted.length; i++) {
			if (deleted[i]) {
				return true;
			}
		}
		return false;
	}

	@Override
	public void dispose() {
		super.dispose();
		if(null != btnGraphs)
			for(int i=0; i<btnGraphs.length; i++)
				btnGraphs[i].dispose();
		btnGraphs = null;

		if(null != txtTitle)
			txtTitle.dispose();
		txtTitle = null;

		if(null != txtKey)
			txtKey.dispose();
		txtKey = null;
		if(null != btnKey)
			btnKey.dispose();
		btnKey = null;
		if(null != lblKey)
			lblKey.dispose();
		lblKey = null;

		if(null != cboXItem)
			cboXItem.dispose();
		cboXItem = null;
		if(null != cboYItems) {
			for(int i=0; i<cboYItems.length; i++) {
				if(null != cboYItems[i])
					cboYItems[i].dispose();
				cboYItems[i] = null;
				if(null != lblYItems[i])
					lblYItems[i].dispose();
				lblYItems[i] = null;
			}
		}
		cboYItems = null;
		lblYItems = null;
		model = null;
	}

	/**
	 * This class is responsible for updating the menu elements whenever
	 * the user interacts with them. Namely, it checks for naming errors
	 * and invalid series selections, and handles display of Y-series combo boxes.
	 */
	private class ComboSelectionListener implements SelectionListener {
		@Override
		public void widgetDefaultSelected(SelectionEvent e) {}

		@Override
		public void widgetSelected(SelectionEvent e) {
			Combo source = (Combo) e.getSource();
			if(cboXItem.equals(source)) {
				if (deleted[0] && cboXItem.getSelectionIndex() != 0) {
					cboXItem.remove(0);
					deleted[0] = false;
				}
			}
			else {
				for (int i = 0; i < cboYItems.length; i++) {
					if (deleted[i+1] && cboYItems[i].equals(source) && cboYItems[i].getSelectionIndex() != 0) {
						cboYItems[i].remove(0);
						deleted[i+1] = false;
						break;
					}
				}
				boolean setVisible = true;
				if(GraphFactory.isMultiGraph(model.getGraphID())) {
					for(int i=1; i<cboYItems.length; i++) {
						cboYItems[i].setVisible(setVisible);
						lblYItems[i].setVisible(setVisible);
						if (!setVisible && deleted[i+1]) {
							cboYItems[i].remove(0);
							deleted[i+1] = false;
							cboYItems[i].select(0);
						}
						if(deleted[i+1] || (cboYItems[i].getSelectionIndex() > 0 && cboYItems[i].isVisible()))
							setVisible = true;
						else
							setVisible = false;
					}
				}
			}

			checkErrors();
		}
	}

	private void checkErrors(){
		if(txtTitle.getText().length() == 0) {
			setErrorMessage(Localization.getString("SelectGraphAndSeriesWizardPage.TitleNotSet")); //$NON-NLS-1$
		} else if(!isSeriesUnique()) {
			setErrorMessage(Localization.getString("SelectGraphAndSeriesWizardPage.SeriesNotUnique")); //$NON-NLS-1$
		}
		else if(isSeriesDeleted()) {
			setErrorMessage(Localization.getString("SelectGraphAndSeriesWizardPage.SeriesDeleted")); //$NON-NLS-1$
		} else {
			setErrorMessage(null);
		}
		getWizard().getContainer().updateButtons();
	}

	private Button[] btnGraphs;
	private SelectGraphAndSeriesWizard wizard;

	private Text txtTitle;		//TODO: Move this to another page once graphs get more detail
	private Text txtKey;
	private Button btnKey;
	private Label lblKey;
	private Combo cboXItem;
	private Combo[] cboYItems;
	private Label[] lblYItems;
	private GraphModel model;
	private boolean[] deleted;
	private boolean edit;
}

Back to the top