getFields()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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