br.ufrj.ppgi.greco.kettle.GraphSparqlStepDialog   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 319
Duplicated Lines 43.89 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 34
eloc 194
c 1
b 0
f 1
dl 140
loc 319
rs 9.68

15 Methods

Rating   Name   Duplication   Size   Complexity  
A addSelectionListenerToControls(SelectionAdapter) 0 3 1
B getData() 32 32 7
$ModifyListener$.modifyText(ModifyEvent) 0 3 ?
A getListOfPrefixesFromTableView() 0 7 2
$SelectionListener$.widgetSelected(SelectionEvent) 0 3 ?
$ShellAdapter$.shellClosed(ShellEvent) 2 2 ?
A validateButton() 0 8 2
C buildContents(Control,ModifyListener) 0 81 9
$SelectionListener$.widgetDefaultSelected(SelectionEvent) 0 2 ?
A cancel() 0 4 1
A GraphSparqlStepDialog(Shell,Object,TransMeta,String) 0 8 1
C open() 108 108 8
A ok() 0 20 3
$SelectionAdapter$.widgetDefaultSelected(SelectionEvent) 2 2 ?
$Listener$.handleEvent(Event) 2 2 ?

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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