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

Complexity

Total Complexity 35

Size/Duplication

Total Lines 338
Duplicated Lines 39.94 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 35
eloc 204
c 1
b 0
f 1
dl 135
loc 338
rs 9.6

20 Methods

Rating   Name   Duplication   Size   Complexity  
$SelectionListener$.widgetSelected(SelectionEvent) 0 3 ?
A getVocabularyRowFactoryWriteVocab(DataTable) 0 3 1
A getFields() 0 2 1
A addSelectionListenerToControls(SelectionAdapter) 0 1 1
A getData() 0 22 3
A getVocabularyDataTable() 0 4 1
A getDimensionRowFactoryWrite(DataTable) 0 5 1
A getDimensionDataTable() 0 6 1
A DataCubeStepDialog(Shell,Object,TransMeta,String) 0 7 1
A getFields(int) 19 19 5
A getRowFactoryRead(DataTable) 0 4 1
A ok() 0 25 4
B buildContents(Control,ModifyListener) 0 68 6
$SelectionListener$.widgetDefaultSelected(SelectionEvent) 0 2 ?
A cancel() 0 4 1
$ShellAdapter$.shellClosed(ShellEvent) 2 2 ?
C open() 116 116 8
$SelectionAdapter$.widgetDefaultSelected(SelectionEvent) 2 2 ?
$Listener$.handleEvent(Event) 2 2 ?
$ModifyListener$.modifyText(ModifyEvent) 3 3 ?

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.List;
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.graphics.Rectangle;
17
import org.eclipse.swt.layout.FormAttachment;
18
import org.eclipse.swt.layout.FormData;
19
import org.eclipse.swt.layout.FormLayout;
20
import org.eclipse.swt.widgets.Button;
21
import org.eclipse.swt.widgets.Composite;
22
import org.eclipse.swt.widgets.Control;
23
import org.eclipse.swt.widgets.Display;
24
import org.eclipse.swt.widgets.Event;
25
import org.eclipse.swt.widgets.Label;
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.exception.KettleStepException;
31
import org.pentaho.di.core.row.RowMetaInterface;
32
import org.pentaho.di.core.row.ValueMetaInterface;
33
import org.pentaho.di.core.util.StringUtil;
34
import org.pentaho.di.i18n.BaseMessages;
35
import org.pentaho.di.trans.TransMeta;
36
import org.pentaho.di.trans.step.BaseStepMeta;
37
import org.pentaho.di.trans.step.StepDialogInterface;
38
import org.pentaho.di.ui.core.widget.ColumnInfo;
39
import org.pentaho.di.ui.core.widget.TableView;
40
import org.pentaho.di.ui.core.widget.TextVar;
41
import org.pentaho.di.ui.trans.step.BaseStepDialog;
42
43
import br.ufrj.ppgi.greco.kettle.plugin.tools.datatable.DataTable;
44
import br.ufrj.ppgi.greco.kettle.plugin.tools.swthelper.SwtHelper;
45
46
public class DataCubeStepDialog extends BaseStepDialog implements StepDialogInterface {
47
	
48
	private static Class<?> PKG = DataCubeStepMeta.class;
49
50
	private DataCubeStepMeta input;
51
	private SwtHelper swthlp;
52
	private String dialogTitle;
53
54
	private TableView wDimensionTable;
55
	private TableView wVocabularyTable;
56
	//private Button wKeepInputFields;
57
	private TextVar wDataCubeOutputFieldName;
58
	protected String[][] defaultPrefixes = { { "@base", "http://example.cubeviz.org/datacube/" },
59
			{ "owl", "http://www.w3.org/2002/07/owl#" }, { "rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#" },
60
			{ "rdfs", "http://www.w3.org/2000/01/rdf-schema#" }, { "dc", "http://purl.org/dc/elements/1.1/" },
61
			{ "skos", "http://www.w3.org/2004/02/skos/core#" },
62
			{ "sdmx_code", "http://purl.org/linked-data/sdmx/2009/code#" },
63
			{ "sdmx_dimension", "http://purl.org/linked-data/sdmx/2009/dimension#" },
64
			{ "sdmx-attr", "http://purl.org/linked-data/sdmx/2009/attribute#" },
65
			{ "sdmx-meas", "http://purl.org/linked-data/sdmx/2009/measure#" },
66
			{ "cube", "http://purl.org/linked-data/cube#" }, { "ex", "http://meu.exemplo/datacube/" },
67
			{ "exProp", "http://meu.exemplo/datacube/properties/" } };
68
69
	public DataCubeStepDialog(Shell parent, Object stepMeta, TransMeta transMeta, String stepname) {
70
		super(parent, (BaseStepMeta) stepMeta, transMeta, stepname);
71
72
		input = (DataCubeStepMeta) baseStepMeta;
73
		swthlp = new SwtHelper(transMeta, this.props);
74
75
		dialogTitle = BaseMessages.getString(PKG, "DataCubeStep.Title");
76
	}
77
78
	private Control buildContents(Control lastControl, ModifyListener defModListener) {
79
80
		CTabFolder wTabFolder = swthlp.appendTabFolder(shell, lastControl, 90);
81
		CTabItem item = new CTabItem(wTabFolder, SWT.NONE);
82
83
		item.setText(BaseMessages.getString(PKG, "DataCubeStep.Tab.Dimension"));
84
		Composite cpt = swthlp.appendComposite(wTabFolder, lastControl);
85
		ColumnInfo[] columns = new ColumnInfo[] {
86
				new ColumnInfo(BaseMessages.getString(PKG, "DataCubeStep.Tab.Dimension.ColumnA"), ColumnInfo.COLUMN_TYPE_CCOMBO, this.getFields(), true),
87
				new ColumnInfo(BaseMessages.getString(PKG, "DataCubeStep.Tab.Dimension.ColumnB"), ColumnInfo.COLUMN_TYPE_TEXT),
88
				new ColumnInfo(BaseMessages.getString(PKG, "DataCubeStep.Tab.Dimension.ColumnC"), ColumnInfo.COLUMN_TYPE_TEXT),
89
				new ColumnInfo(BaseMessages.getString(PKG, "DataCubeStep.Tab.Dimension.ColumnD"), ColumnInfo.COLUMN_TYPE_TEXT) };
90
		wDimensionTable = swthlp.appendTableView(cpt, null, columns, defModListener, 98);
91
92
		item.setControl(cpt);
93
94
		item = new CTabItem(wTabFolder, SWT.NONE);
95
		item.setText(BaseMessages.getString(PKG, "DataCubeStep.Tab.Vocabulary"));
96
		cpt = swthlp.appendComposite(wTabFolder, lastControl);
97
98
		columns = new ColumnInfo[] { new ColumnInfo(BaseMessages.getString(PKG, "DataCubeStep.Tab.Vocabulary.ColumnA"), ColumnInfo.COLUMN_TYPE_TEXT),
99
				new ColumnInfo(BaseMessages.getString(PKG, "DataCubeStep.Tab.Vocabulary.ColumnB"), ColumnInfo.COLUMN_TYPE_TEXT) };
100
		wVocabularyTable = swthlp.appendTableView(cpt, null, columns, defModListener, 90);
101
102
		swthlp.appendButtonsRow(cpt, wVocabularyTable, new String[] { BaseMessages.getString(PKG, "DataCubeStep.Btn.Clear"), 
103
				BaseMessages.getString(PKG, "DataCubeStep.Btn.Default") },
104
				new SelectionListener[] { new SelectionListener() {
105
106
					public void widgetSelected(SelectionEvent arg0) {
107
						wVocabularyTable.removeAll();
108
						input.setChanged();
109
					}
110
111
					public void widgetDefaultSelected(SelectionEvent arg0) {
112
						input.setChanged();
113
					}
114
				}, new SelectionListener() {
115
116
					public void widgetSelected(SelectionEvent arg0) {
117
						for (String[] row : defaultPrefixes)
118
							wVocabularyTable.add(row);
119
					}
120
121
					public void widgetDefaultSelected(SelectionEvent arg0) {
122
						input.setChanged();
123
					}
124
				} });
125
126
		item.setControl(cpt);
127
128
		item = new CTabItem(wTabFolder, SWT.NONE);
129
		item.setText(BaseMessages.getString(PKG, "DataCubeStep.Tab.Output") );
130
		cpt = swthlp.appendComposite(wTabFolder, lastControl);
131
		/*wKeepInputFields = swthlp.appendCheckboxRow(cpt, null, "Repassar campos de entrada para saída",
132
				new SelectionListener() {
133
					public void widgetDefaultSelected(SelectionEvent arg0) {
134
						widgetSelected(arg0);
135
					}
136
137
					public void widgetSelected(SelectionEvent e) {
138
						input.setChanged(true);
139
					}
140
				});*/
141
		wDataCubeOutputFieldName = swthlp.appendTextVarRow(cpt, null, BaseMessages.getString(PKG, "DataCubeStep.Tab.Output.Field") , defModListener);
142
		item.setControl(cpt);
143
144
		wTabFolder.setSelection(0);
145
		return wTabFolder;
146
	}
147
148
	private String[] getFields() {
149
		return getFields(-1);
150
	}
151
152 View Code Duplication
	private String[] getFields(int type) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
153
154
		List<String> result = new ArrayList<String>();
155
156
		try {
157
			RowMetaInterface inRowMeta = this.transMeta.getPrevStepFields(stepname);
158
159
			List<ValueMetaInterface> fields = inRowMeta.getValueMetaList();
160
161
			for (ValueMetaInterface field : fields) {
162
				if (field.getType() == type || type == -1)
163
					result.add(field.getName());
164
			}
165
166
		} catch (KettleStepException e) {
167
			e.printStackTrace();
168
		}
169
170
		return result.toArray(new String[result.size()]);
171
	}
172
173
174
	private void addSelectionListenerToControls(SelectionAdapter lsDef) {
175
176
	}
177
178 View Code Duplication
	@Override
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
179
	public String open() {
180
181
		Shell parent = getParent();
182
		Display display = parent.getDisplay();
183
184
		shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MIN | SWT.MAX);
185
		props.setLook(shell);
186
		setShellImage(shell, input);
187
188
		// ModifyListener padrao
189
		ModifyListener lsMod = new ModifyListener() {
190
191
			@Override
192
			public void modifyText(ModifyEvent e) {
193
				input.setChanged();
194
			}
195
		};
196
		boolean changed = input.hasChanged();
197
198
		FormLayout formLayout = new FormLayout();
199
		formLayout.marginWidth = Const.FORM_MARGIN;
200
		formLayout.marginHeight = Const.FORM_MARGIN;
201
202
		shell.setLayout(formLayout);
203
204
		shell.setText(dialogTitle);
205
206
		int middle = props.getMiddlePct();
207
		int margin = Const.MARGIN;
208
209
		// Adiciona um label e um input text no topo do dialog shell
210
		wlStepname = new Label(shell, SWT.RIGHT);
211
		wlStepname.setText(BaseMessages.getString(PKG, "DataCubeStep.StepNameField.Label"));
212
		props.setLook(wlStepname);
213
214
		fdlStepname = new FormData();
215
		fdlStepname.left = new FormAttachment(0, 0);
216
		fdlStepname.right = new FormAttachment(middle, -margin);
217
		fdlStepname.top = new FormAttachment(0, margin);
218
		wlStepname.setLayoutData(fdlStepname);
219
220
		wStepname = new Text(shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
221
		wStepname.setText(stepname);
222
		props.setLook(wStepname);
223
224
		wStepname.addModifyListener(lsMod);
225
		fdStepname = new FormData();
226
		fdStepname.left = new FormAttachment(middle, 0);
227
		fdStepname.top = new FormAttachment(0, margin);
228
		fdStepname.right = new FormAttachment(100, 0);
229
		wStepname.setLayoutData(fdStepname);
230
		Control lastControl = wStepname;
231
232
		// Chama metodo que adiciona os widgets especificos da janela
233
		lastControl = buildContents(lastControl, lsMod);
234
235
		// Bottom buttons
236
		wOK = new Button(shell, SWT.PUSH);
237
		wOK.setText(BaseMessages.getString(PKG, "DataCubeStep.Btn.OK")); //$NON-NLS-1$
238
		wCancel = new Button(shell, SWT.PUSH);
239
		wCancel.setText(BaseMessages.getString(PKG, "DataCubeStep.Btn.Cancel")); //$NON-NLS-1$
240
		setButtonPositions(new Button[] { wOK, wCancel }, margin, lastControl);
241
242
		// Add listeners
243
		lsCancel = new Listener() {
244
			public void handleEvent(Event e) {
245
				cancel();
246
			}
247
		};
248
		lsOK = new Listener() {
249
			public void handleEvent(Event e) {
250
				ok();
251
			}
252
		};
253
254
		wCancel.addListener(SWT.Selection, lsCancel);
255
		wOK.addListener(SWT.Selection, lsOK);
256
257
		// It closes the window affirmatively when the user press enter in one
258
		// of the text input fields
259
		lsDef = new SelectionAdapter() {
260
			public void widgetDefaultSelected(SelectionEvent e) {
261
				ok();
262
			}
263
		};
264
265
		wStepname.addSelectionListener(lsDef);
266
		addSelectionListenerToControls(lsDef);
267
268
		// Detect X or ALT-F4 or something that kills this window...
269
		shell.addShellListener(new ShellAdapter() {
270
			public void shellClosed(ShellEvent e) {
271
				cancel();
272
			}
273
		});
274
275
		// Populate the data of the controls
276
		getData();
277
278
		// Set the shell size, based upon previous time...
279
		setSize();
280
281
		// Alarga um pouco mais a janela
282
		Rectangle shellBounds = shell.getBounds();
283
		shellBounds.width += 5;
284
		shell.setBounds(shellBounds);
285
286
		input.setChanged(changed);
287
288
		shell.open();
289
		while (!shell.isDisposed()) {
290
			if (!display.readAndDispatch())
291
				display.sleep();
292
		}
293
		return stepname;
294
	}
295
296
	private void getData() {
297
298
		wStepname.selectAll();
299
300
		DataTable<String> dtable = input.getDimensionTable();
301
		DataTable<String>.RowFactory rf = getRowFactoryRead(dtable);
302
		for (int i = 0; i < dtable.size(); i++) {
303
			wDimensionTable.add(dtable.getRowRange(i, rf).getRow());
304
		}
305
		wDimensionTable.remove(0);
306
		input.setDimensionTable(dtable);
307
308
		dtable = input.getVocabularyTable();
309
		rf = getRowFactoryRead(dtable);
310
		for (int i = 0; i < dtable.size(); i++) {
311
			wVocabularyTable.add(dtable.getRowRange(i, rf).getRow());
312
		}
313
		wVocabularyTable.remove(0);
314
		input.setVocabularyTable(dtable);
315
316
		//wKeepInputFields.setSelection(input.isKeepInputFields());
317
		wDataCubeOutputFieldName.setText(Const.NVL(input.getDataCubeOutputFieldName(), ""));
318
319
	}
320
321
	protected void cancel() {
322
		stepname = null;
323
		input.setChanged(changed);
324
		dispose();
325
	}
326
327
	protected void ok() {
328
		if (StringUtil.isEmpty(wStepname.getText()))
329
			return;
330
331
		stepname = wStepname.getText(); // return value
332
333
		DataTable<String> table = this.getDimensionDataTable();
334
		DataTable<String>.RowFactory rf = getDimensionRowFactoryWrite(table);
335
		for (int i = 0; i < wDimensionTable.getItemCount(); i++) {
336
			table.add(rf.newRow(wDimensionTable.getItem(i)).getFullRow());
337
		}
338
		input.setDimensionTable(table);
339
340
		table = this.getVocabularyDataTable();
341
		rf = this.getVocabularyRowFactoryWriteVocab(table);
342
		for (int i = 0; i < wVocabularyTable.getItemCount(); i++) {
343
			table.add(rf.newRow(wVocabularyTable.getItem(i)).getFullRow());
344
		}
345
		input.setVocabularyTable(table);
346
347
		//input.setKeepInputFields(wKeepInputFields.getSelection());
348
		input.setDataCubeOutputFieldName(this.wDataCubeOutputFieldName.getText());
349
350
		// Fecha janela
351
		dispose();
352
	}
353
354
	private DataTable<String> getDimensionDataTable() {
355
		return new DataTable<String>(DataCubeStepMeta.Field.DIMENSION_TABLE.name(),
356
				DataCubeStepMeta.Field.DIMENSION_TABLE_NAME.name(), 
357
				DataCubeStepMeta.Field.DIMENSION_TABLE_URI.name(),
358
				DataCubeStepMeta.Field.DIMENSION_TABLE_LABEL.name(),
359
				DataCubeStepMeta.Field.DIMENSION_TABLE_TYPE.name());
360
	}
361
362
	private DataTable<String>.RowFactory getDimensionRowFactoryWrite(DataTable<String> table) {
363
		return table.newRowFactory(DataCubeStepMeta.Field.DIMENSION_TABLE_NAME.name(),
364
				DataCubeStepMeta.Field.DIMENSION_TABLE_URI.name(), 
365
				DataCubeStepMeta.Field.DIMENSION_TABLE_LABEL.name(),
366
				DataCubeStepMeta.Field.DIMENSION_TABLE_TYPE.name());
367
	}
368
369
	private DataTable<String> getVocabularyDataTable() {
370
		return new DataTable<String>(DataCubeStepMeta.Field.VOCABULARY_TABLE.name(),
371
				DataCubeStepMeta.Field.VOCABULARY_TABLE_PREFIX.name(),
372
				DataCubeStepMeta.Field.VOCABULARY_TABLE_URI.name());
373
	}
374
375
	private DataTable<String>.RowFactory getVocabularyRowFactoryWriteVocab(DataTable<String> table) {
376
		return table.newRowFactory(DataCubeStepMeta.Field.VOCABULARY_TABLE_PREFIX.name(),
377
				DataCubeStepMeta.Field.VOCABULARY_TABLE_URI.name());
378
	}
379
380
	private DataTable<String>.RowFactory getRowFactoryRead(DataTable<String> table) {
381
		List<String> header = new ArrayList<String>();
382
		header.addAll(table.getHeader());
383
		return table.newRowFactory(header.toArray(new String[0]));
384
	}
385
}
386