ner$.handleEvent(Event)
last analyzed

Size

Total Lines 2
Code Lines 2

Duplication

Lines 2
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 2
loc 2
1
package br.ufrj.ppgi.greco.kettle;
2
3
import java.util.ArrayList;
4
import java.util.Arrays;
5
6
import org.eclipse.swt.SWT;
7
import org.eclipse.swt.custom.CTabFolder;
8
import org.eclipse.swt.custom.CTabItem;
9
import org.eclipse.swt.events.ModifyEvent;
10
import org.eclipse.swt.events.ModifyListener;
11
import org.eclipse.swt.events.SelectionAdapter;
12
import org.eclipse.swt.events.SelectionEvent;
13
import org.eclipse.swt.events.SelectionListener;
14
import org.eclipse.swt.events.ShellAdapter;
15
import org.eclipse.swt.events.ShellEvent;
16
import org.eclipse.swt.layout.FormAttachment;
17
import org.eclipse.swt.layout.FormData;
18
import org.eclipse.swt.layout.FormLayout;
19
import org.eclipse.swt.widgets.Button;
20
import org.eclipse.swt.widgets.Composite;
21
import org.eclipse.swt.widgets.Control;
22
import org.eclipse.swt.widgets.Display;
23
import org.eclipse.swt.widgets.Event;
24
import org.eclipse.swt.widgets.Label;
25
import org.eclipse.swt.widgets.List;
26
import org.eclipse.swt.widgets.Listener;
27
import org.eclipse.swt.widgets.Shell;
28
import org.eclipse.swt.widgets.Text;
29
import org.pentaho.di.core.Const;
30
import org.pentaho.di.core.row.ValueMetaInterface;
31
import org.pentaho.di.core.util.StringUtil;
32
import org.pentaho.di.i18n.BaseMessages;
33
import org.pentaho.di.trans.TransMeta;
34
import org.pentaho.di.trans.step.BaseStepMeta;
35
import org.pentaho.di.trans.step.StepDialogInterface;
36
import org.pentaho.di.ui.core.widget.ColumnInfo;
37
import org.pentaho.di.ui.core.widget.TableView;
38
import org.pentaho.di.ui.core.widget.TextVar;
39
import org.pentaho.di.ui.trans.step.BaseStepDialog;
40
41
import br.ufrj.ppgi.greco.kettle.plugin.tools.swthelper.SwtHelper;
42
43
public class SparqlStepDialog extends BaseStepDialog implements StepDialogInterface {
44
	
45
	private static Class<?> PKG = SparqlStepMeta.class;
46
47
	private SparqlStepMeta input;
48
	private SwtHelper swthlp;
49
	private String dialogTitle;
50
51
	// TODO Adicionar variaveis dos widgets
52
	private TextVar wEndpointUri;
53
	private TextVar wDefaultGraph;
54
	private Text wQueryString;
55
	private TextVar wPrefixVar;
56
	private List wList;
57
	private Text wParserMessage;
58
	private CTabFolder wTabFolder;
59
	private TableView wPrefixes;
60
	protected String[][] defaultPrefixes = { { "xsd", "http://www.w3.org/2001/XMLSchema#" },
61
			{ "rdfs", "http://www.w3.org/2000/01/rdf-schema#" },
62
			{ "rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#" }, { "owl", "http://www.w3.org/2002/07/owl#" },
63
			{ "dbpedia", "http://dbpedia.org/ontology/" }, { "dbpprop", "http://dbpedia.org/property/" },
64
			{ "dc", "http://purl.org/dc/elements/1.1/" }, { "foaf", "http://xmlns.com/foaf/0.1/" },
65
			{ "vcard", "http://www.w3.org/2006/vcard/ns#" }, { "cc", "http://creativecommons.org/ns#" },
66
			{ "skos", "http://www.w3.org/2004/02/skos/core#" } };
67
68
	public SparqlStepDialog(Shell parent, Object stepMeta, TransMeta transMeta, String stepname) {
69
		super(parent, (BaseStepMeta) stepMeta, transMeta, stepname);
70
71
		input = (SparqlStepMeta) baseStepMeta;
72
		swthlp = new SwtHelper(transMeta, this.props);
73
74
		// TODO: Additional initialization here
75
		dialogTitle = BaseMessages.getString(PKG, "SparqlStep.Title");
76
		// ...
77
	}
78
79
	// TODO Criar widgets especificos da janela
80
	private Control buildContents(Control lastControl, ModifyListener defModListener) {
81
82
		wTabFolder = swthlp.appendTabFolder(shell, lastControl, 90);
83
84
		// Create Tab
85
		CTabItem item = new CTabItem(wTabFolder, SWT.NONE);
86
		item.setText(BaseMessages.getString(PKG, "SparqlStep.Tab.SPARQL"));
87
		Composite cpt = swthlp.appendComposite(wTabFolder, null);
88
		wEndpointUri = swthlp.appendTextVarRow(cpt, null, BaseMessages.getString(PKG, "SparqlStep.Tab.SPARQL.Endpoint"), defModListener);
89
		wDefaultGraph = swthlp.appendTextVarRow(cpt, wEndpointUri, BaseMessages.getString(PKG, "SparqlStep.Tab.SPARQL.Graph"), defModListener);
90
		item.setControl(cpt);
91
92
		// Create Tab
93
		item = new CTabItem(wTabFolder, SWT.NONE);
94
		item.setText(BaseMessages.getString(PKG, "SparqlStep.Tab.Prefix"));
95
		ColumnInfo[] columns = new ColumnInfo[] { new ColumnInfo(BaseMessages.getString(PKG, "SparqlStep.Tab.Prefix.ColumnA"), ColumnInfo.COLUMN_TYPE_TEXT),
96
				new ColumnInfo(BaseMessages.getString(PKG, "SparqlStep.Tab.Prefix.ColumnB"), ColumnInfo.COLUMN_TYPE_TEXT) };
97
		cpt = swthlp.appendComposite(wTabFolder, null);
98
		wPrefixes = swthlp.appendTableView(cpt, null, columns, defModListener, 90);
99
100
		swthlp.appendButtonsRow(cpt, wPrefixes, new String[] { BaseMessages.getString(PKG, "SparqlStep.Tab.Prefix.Clear"),  BaseMessages.getString(PKG, "SparqlStep.Tab.Prefix.Default") },
101
				new SelectionListener[] { new SelectionListener() {
102
103
					public void widgetSelected(SelectionEvent arg0) {
104
						wPrefixes.removeAll();
105
						input.setChanged();
106
					}
107
108
					public void widgetDefaultSelected(SelectionEvent arg0) {
109
						input.setChanged();
110
					}
111
				}, new SelectionListener() {
112
113
					public void widgetSelected(SelectionEvent arg0) {
114
						for (String[] row : defaultPrefixes)
115
							wPrefixes.add(row);
116
					}
117
118
					public void widgetDefaultSelected(SelectionEvent arg0) {
119
						input.setChanged();
120
					}
121
				} });
122
		item.setControl(cpt);
123
124
		// Create Tab
125
		item = new CTabItem(wTabFolder, SWT.NONE);
126
		item.setText(BaseMessages.getString(PKG, "SparqlStep.Tab.Query"));
127
		cpt = swthlp.appendComposite(wTabFolder, null);
128
		Label wLabel = swthlp.appendLabel(cpt, null, BaseMessages.getString(PKG, "SparqlStep.Tab.Query.Label"));
129
		wQueryString = swthlp.appendMultiTextVarRow(cpt, wLabel, new ModifyListener() {
130
			public void modifyText(ModifyEvent arg0) {
131
				input.setChanged();
132
				wParserMessage.setText("");
133
			}
134
		}, 55);
135
136
		SelectionListener[] listeners = new SelectionListener[] { new SelectionListener() {
137
138
			public void widgetSelected(SelectionEvent arg0) {
139
				widgetDefaultSelected(arg0);
140
			}
141
142
			public void widgetDefaultSelected(SelectionEvent arg0) {
143
				validateButton();
144
			}
145
		} };
146
		Composite ctl = swthlp.appendButtonsRow(cpt, wQueryString, new String[] { BaseMessages.getString(PKG, "SparqlStep.Tab.Query.Validate") }, listeners);
147
148
		wParserMessage = swthlp.appendMultiTextVarRow(cpt, ctl, defModListener, 100);
149
		wParserMessage.setEditable(false);
150
		item.setControl(cpt);
151
152
		// Create Tab
153
		item = new CTabItem(wTabFolder, SWT.NONE);
154
		item.setText(BaseMessages.getString(PKG, "SparqlStep.Tab.Output"));
155
		cpt = swthlp.appendComposite(wTabFolder, null);
156
		wPrefixVar = swthlp.appendTextVarRow(cpt, null, BaseMessages.getString(PKG, "SparqlStep.Tab.Output.Field"), new ModifyListener() {
157
158
			public void modifyText(ModifyEvent arg0) {
159
				validateButton();
160
			}
161
		});
162
		wList = swthlp.appendListRow(cpt, wPrefixVar, BaseMessages.getString(PKG, "SparqlStep.Tab.Output.FieldName"), new SelectionListener() {
163
164
			public void widgetSelected(SelectionEvent arg0) {
165
			}
166
167
			public void widgetDefaultSelected(SelectionEvent arg0) {
168
			}
169
		}, 50);
170
		wList.setEnabled(false);
171
172
		/*
173
		 * listeners = new SelectionListener[] { new SelectionListener() {
174
		 * 
175
		 * @Override public void widgetSelected(SelectionEvent arg0) {
176
		 * widgetDefaultSelected(arg0); }
177
		 * 
178
		 * @Override public void widgetDefaultSelected(SelectionEvent arg0) {
179
		 * validateButton(); } } }; ctl = swthlp.appendButtonsRow( cpt, wList,
180
		 * new String [] { "Refazer" }, listeners);
181
		 */
182
		item.setControl(cpt);
183
184
		wTabFolder.setSelection(0);
185
186
		// Return the last created control here
187
		return wTabFolder;
188
	}
189
190
	private void validateButton() {
191
192
		try {
193
			String fullQueryStr = SparqlStepUtils.toFullQueryString(this.getListOfPrefixesFromTableView(),
194
					wQueryString.getText());
195
196
			// update message
197
			wParserMessage.setText(SparqlStepUtils.validateSparql(fullQueryStr));
198
199
			// update output variable list
200
			wList.removeAll();
201
202
			java.util.List<ValueMetaInterface> outVars = SparqlStepUtils.generateOutputVars(wPrefixVar.getText(),
203
					fullQueryStr);
204
			if (outVars != null) {
205
				for (int i = 0; i < outVars.size(); i++) {
206
					ValueMetaInterface field = outVars.get(i);
207
					wList.add(field.getName() + " : " + field.getTypeDesc());
208
				}
209
			}
210
		} catch (Throwable e) {
211
			e.printStackTrace();
212
		}
213
	}
214
215
	// TODO Adicionar listeners para widgets tratarem Enter
216
	// The will close the window affirmatively when the user press Enter in one
217
	// of these text input fields
218
	private void addSelectionListenerToControls(SelectionAdapter lsDef) {
219
		wEndpointUri.addSelectionListener(lsDef);
220
		wDefaultGraph.addSelectionListener(lsDef);
221
		wPrefixVar.addSelectionListener(lsDef);
222
	}
223
224 View Code Duplication
	public String open() {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
225
226
		Shell parent = getParent();
227
		Display display = parent.getDisplay();
228
229
		shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MIN | SWT.MAX);
230
		props.setLook(shell);
231
		setShellImage(shell, input);
232
233
		// ModifyListener padrao
234
		ModifyListener lsMod = new ModifyListener() {
235
236
			public void modifyText(ModifyEvent e) {
237
				input.setChanged();
238
			}
239
		};
240
		boolean changed = input.hasChanged();
241
242
		FormLayout formLayout = new FormLayout();
243
		formLayout.marginWidth = Const.FORM_MARGIN;
244
		formLayout.marginHeight = Const.FORM_MARGIN;
245
246
		shell.setLayout(formLayout);
247
248
		shell.setText(dialogTitle);
249
250
		int middle = props.getMiddlePct();
251
		int margin = Const.MARGIN;
252
253
		// Adiciona um label e um input text no topo do dialog shell
254
		wlStepname = new Label(shell, SWT.RIGHT);
255
		wlStepname.setText(BaseMessages.getString(PKG, "SparqlStep.StepNameField.Label"));
256
		props.setLook(wlStepname);
257
258
		fdlStepname = new FormData();
259
		fdlStepname.left = new FormAttachment(0, 0);
260
		fdlStepname.right = new FormAttachment(middle, -margin);
261
		fdlStepname.top = new FormAttachment(0, margin);
262
		wlStepname.setLayoutData(fdlStepname);
263
264
		wStepname = new Text(shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
265
		wStepname.setText(stepname);
266
		props.setLook(wStepname);
267
268
		wStepname.addModifyListener(lsMod);
269
		fdStepname = new FormData();
270
		fdStepname.left = new FormAttachment(middle, 0);
271
		fdStepname.top = new FormAttachment(0, margin);
272
		fdStepname.right = new FormAttachment(100, 0);
273
		wStepname.setLayoutData(fdStepname);
274
		Control lastControl = wStepname;
275
276
		// Chama metodo que adiciona os widgets especificos da janela
277
		lastControl = buildContents(lastControl, lsMod);
278
279
		// Bottom buttons
280
		wOK = new Button(shell, SWT.PUSH);
281
		wOK.setText(BaseMessages.getString(PKG, "SparqlStep.Btn.OK")); //$NON-NLS-1$
282
		wCancel = new Button(shell, SWT.PUSH);
283
		wCancel.setText(BaseMessages.getString(PKG, "SparqlStep.Btn.Cancel")); //$NON-NLS-1$
284
		setButtonPositions(new Button[] { wOK, wCancel }, margin, lastControl);
285
286
		// Add listeners
287
		lsCancel = new Listener() {
288
			public void handleEvent(Event e) {
289
				cancel();
290
			}
291
		};
292
		lsOK = new Listener() {
293
			public void handleEvent(Event e) {
294
				ok();
295
			}
296
		};
297
298
		wCancel.addListener(SWT.Selection, lsCancel);
299
		wOK.addListener(SWT.Selection, lsOK);
300
301
		// It closes the window affirmatively when the user press enter in one
302
		// of the text input fields
303
		lsDef = new SelectionAdapter() {
304
			public void widgetDefaultSelected(SelectionEvent e) {
305
				ok();
306
			}
307
		};
308
		wStepname.addSelectionListener(lsDef);
309
		addSelectionListenerToControls(lsDef);
310
311
		// Detect X or ALT-F4 or something that kills this window...
312
		shell.addShellListener(new ShellAdapter() {
313
			public void shellClosed(ShellEvent e) {
314
				cancel();
315
			}
316
		});
317
318
		// Populate the data of the controls
319
		getData();
320
321
		// Set the shell size, based upon previous time...
322
		setSize();
323
324
		input.setChanged(changed);
325
326
		shell.open();
327
		while (!shell.isDisposed()) {
328
			if (!display.readAndDispatch())
329
				display.sleep();
330
		}
331
		return stepname;
332
	}
333
334 View Code Duplication
	private void getData() {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
335
336
		try {
337
			wStepname.selectAll();
338
339
			// TODO Recuperar dados do StepMeta e adiciona na GUI
340
			String endpointUri = input.getEndpointUri();
341
			if (endpointUri != null)
342
				wEndpointUri.setText(endpointUri);
343
344
			String defaultGraph = input.getDefaultGraph();
345
			if (defaultGraph != null)
346
				wDefaultGraph.setText(defaultGraph);
347
348
			String queryStr = input.getQueryString();
349
			if (queryStr != null)
350
				wQueryString.setText(queryStr);
351
352
			java.util.List<java.util.List<String>> prefixes = input.getPrefixes();
353
			if (prefixes != null) {
354
				wPrefixes.removeAll();
355
356
				for (java.util.List<String> list : prefixes) {
357
					wPrefixes.add(list.get(0), list.get(1));
358
				}
359
360
				wPrefixes.remove(0);
361
			}
362
363
			String prefix = input.getVarPrefix();
364
			if (prefix != null)
365
				wPrefixVar.setText(prefix);
366
367
			validateButton();
368
		} catch (Throwable t) {
369
370
		}
371
	}
372
373
	protected void cancel() {
374
		stepname = null;
375
		input.setChanged(changed);
376
		dispose();
377
	}
378
379
	protected void ok() {
380
		if (StringUtil.isEmpty(wStepname.getText()))
381
			return;
382
383
		stepname = wStepname.getText(); // return value
384
385
		// TODO Pegar dados da GUI e colocar no StepMeta
386
		input.setEndpointUri(wEndpointUri.getText());
387
		input.setDefaultGraph(wDefaultGraph.getText());
388
		input.setQueryString(wQueryString.getText());
389
390
		try {
391
			input.setPrefixes(getListOfPrefixesFromTableView());
392
		} catch (Exception e) {
393
		}
394
395
		input.setVarPrefix(wPrefixVar.getText());
396
397
		// Fecha janela
398
		dispose();
399
	}
400
401
	private java.util.List<java.util.List<String>> getListOfPrefixesFromTableView() {
402
		ArrayList<java.util.List<String>> prefixes = new ArrayList<java.util.List<String>>();
403
		for (int iRow = 0; iRow < wPrefixes.getItemCount(); iRow++) {
404
			String[] row = wPrefixes.getItem(iRow);
405
			prefixes.add(Arrays.asList(row[0], row[1]));
406
		}
407
		return prefixes;
408
	}
409
}
410