1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package pl.edu.agh.cast.importer.wizard.utils;
19
20 import java.util.LinkedList;
21 import java.util.List;
22
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.custom.CCombo;
25 import org.eclipse.swt.custom.TableEditor;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.widgets.Button;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Table;
31 import org.eclipse.swt.widgets.TableColumn;
32 import org.eclipse.swt.widgets.TableItem;
33
34 import pl.edu.agh.cast.importer.base.data.RawDataRow;
35 import pl.edu.agh.cast.importer.base.data.RawTabularData;
36 import pl.edu.agh.cast.importer.base.parser.analyzer.IAnalyzer;
37 import pl.edu.agh.cast.importer.base.util.DataTypeInfo;
38 import pl.edu.agh.cast.importer.wizard.dialog.analyzer.AbstractDataTypeConfigDialog;
39 import pl.edu.agh.cast.importer.wizard.page.AnalyzersSelectionPage;
40 import pl.edu.agh.cast.importer.wizard.util.Messages;
41
42
43
44
45
46
47 public class ComboBoxTableCreator {
48
49 private static final int MAX_ROWS_TO_DISPLAY = 50;
50
51 private static final int VISIBLE_ITEMS_COUNT = 10;
52
53 private static final String EMPTY_STRING = "";
54
55 private AnalyzersSelectionPage mediator;
56
57 private RawTabularData data;
58
59 private Table table;
60
61 private Button[] configButtons;
62
63 private CCombo[] combos;
64
65 private DataTypeInfo[] selectedDataTypes;
66
67
68
69
70
71
72
73 public ComboBoxTableCreator(AnalyzersSelectionPage mediator) {
74 this.mediator = mediator;
75 }
76
77
78
79
80
81
82
83
84
85
86
87
88
89 public void createTable(Composite container, String[] comboItems, Object[] comboData, RawTabularData rawTabData) {
90 if (table != null) {
91 table.dispose();
92 }
93 table = new Table(container, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
94 table.setHeaderVisible(false);
95 table.setLinesVisible(true);
96
97 if (rawTabData == null) {
98 return;
99 }
100 this.data = rawTabData;
101 this.selectedDataTypes = new DataTypeInfo[data.getColumnsCount()];
102
103 prepareTableColumns(comboItems);
104
105 TableItem tableItem = new TableItem(table, SWT.NONE);
106 makeFirstTableRowAsConfigButtons(tableItem);
107
108 TableItem tableItem2 = new TableItem(table, SWT.NONE);
109 makeTableColumnsHeadersAsComboBoxes(comboItems, comboData, tableItem2);
110
111 int size = Math.min(MAX_ROWS_TO_DISPLAY, data.size());
112 fillTableRows(tableItem2, size);
113 }
114
115
116
117
118
119
120
121
122
123
124 private void prepareTableColumns(String[] comboItems) {
125 for (int columnIndex = 0; columnIndex < data.getColumnsCount(); columnIndex++) {
126 TableColumn tableColumn = new TableColumn(table, SWT.NONE);
127 tableColumn.setText(comboItems[0]);
128 tableColumn.pack();
129 tableColumn.setWidth(150);
130 }
131 }
132
133
134
135
136 private void makeFirstTableRowAsConfigButtons(TableItem tableItem) {
137 configButtons = new Button[data.getColumnsCount()];
138 for (int columnIndex = 0; columnIndex < data.getColumnsCount(); columnIndex++) {
139 TableEditor editor = new TableEditor(table);
140 Button configBtn = new Button(table, SWT.PUSH);
141 configButtons[columnIndex] = configBtn;
142 configBtn.pack();
143
144 final int columnIndexCp = columnIndex;
145 configBtn.addSelectionListener(new SelectionAdapter() {
146 @Override
147 public void widgetSelected(SelectionEvent event) {
148 handleConfigButtonSelection(columnIndexCp);
149 }
150
151 });
152 editor.grabHorizontal = true;
153
154 configBtn.setText(Messages.ComboBoxTableCreator_ConfigureBtnLabel);
155 configBtn.setEnabled(false);
156 editor.minimumWidth = configBtn.getSize().x;
157 editor.minimumHeight = table.getItemHeight() + table.getBorderWidth();
158 editor.horizontalAlignment = SWT.FILL;
159 editor.verticalAlignment = SWT.FILL;
160 editor.grabVertical = true;
161 editor.grabHorizontal = true;
162 editor.setEditor(configBtn, tableItem, columnIndex);
163 }
164 }
165
166
167
168
169 private void makeTableColumnsHeadersAsComboBoxes(String[] comboItems, Object[] comboData, TableItem tableItem) {
170 combos = new CCombo[data.getColumnsCount()];
171
172 for (int columnIndex = 0; columnIndex < data.getColumnsCount(); columnIndex++) {
173 TableEditor editor = new TableEditor(table);
174 CCombo combo = new CCombo(table, SWT.CHECK | SWT.READ_ONLY | SWT.BORDER);
175 combos[columnIndex] = combo;
176 combo.pack();
177
178 final int columnIndexCp = columnIndex;
179 combo.addSelectionListener(new SelectionAdapter() {
180 @Override
181 public void widgetSelected(SelectionEvent event) {
182 CCombo selectedCombo = (CCombo)event.getSource();
183 int selectedComboIndex = selectedCombo.getSelectionIndex();
184 DataTypeInfo selectedDataType = null;
185
186 if (selectedComboIndex != 0) {
187
188
189 selectedDataType = ((DataTypeInfo[])selectedCombo.getData())[selectedComboIndex - 1];
190 }
191
192 handleComboSelection(columnIndexCp, selectedDataType);
193 }
194
195 });
196 editor.grabHorizontal = true;
197
198 combo.setItems(comboItems);
199 combo.setData(comboData);
200 combo.add(Messages.ComboBoxTableCreator_Ignored, 0);
201 combo.select(0);
202 combo.setVisibleItemCount(VISIBLE_ITEMS_COUNT);
203
204 editor.minimumWidth = combo.getSize().x;
205 editor.minimumHeight = combo.getSize().y;
206 editor.horizontalAlignment = SWT.FILL;
207 editor.verticalAlignment = SWT.FILL;
208 editor.grabVertical = true;
209 editor.grabHorizontal = true;
210 editor.setEditor(combo, tableItem, columnIndex);
211 }
212 }
213
214
215
216
217 private void fillTableRows(TableItem tableItem, int size) {
218 for (int rowsIndex = 0; rowsIndex < size; rowsIndex++) {
219 final TableItem tabItem = new TableItem(table, SWT.NONE);
220
221 for (int columnIndex = 0; columnIndex < data.getColumnsCount(); columnIndex++) {
222 String valueToSet = null;
223 RawDataRow row = data.get(rowsIndex);
224 if (columnIndex < row.size()) {
225 valueToSet = row.get(columnIndex);
226 }
227 if (valueToSet == null) {
228 valueToSet = EMPTY_STRING;
229 }
230 tabItem.setText(columnIndex, valueToSet);
231 }
232 }
233 }
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251 private void handleComboSelection(int columnIndex, DataTypeInfo dataType) {
252 if (dataType == null) {
253 selectedDataTypes[columnIndex] = null;
254 disableConfigButton(columnIndex);
255
256 } else {
257 selectedDataTypes[columnIndex] = new DataTypeInfo(dataType);
258 AbstractDataTypeConfigDialog dialog = mediator.getDataTypeConfigDialogInstance(dataType.getDataTypeId());
259 if (dialog != null) {
260 enableConfigButton(columnIndex);
261 } else {
262 disableConfigButton(columnIndex);
263 }
264 }
265
266 mediator.widgetModified();
267 }
268
269 private void handleConfigButtonSelection(int columnIndex) {
270 DataTypeInfo selectedDataType = selectedDataTypes[columnIndex];
271 if (selectedDataType != null) {
272 AbstractDataTypeConfigDialog dialog = mediator.getDataTypeConfigDialogInstance(selectedDataType
273 .getDataTypeId());
274 if (dialog != null) {
275 dialog.setData(this, columnIndex, getSelectedAnalyzer(columnIndex));
276 dialog.open();
277 }
278 }
279 }
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295 public void enableConfigButton(int columnIndex) {
296 configButtons[columnIndex].setEnabled(true);
297 }
298
299
300
301
302
303
304
305 public void disableConfigButton(int columnIndex) {
306 configButtons[columnIndex].setEnabled(false);
307 }
308
309
310
311
312
313
314
315
316 public void widgetModified() {
317 mediator.widgetModified();
318 }
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335 public List<String> getDistinctTokensInColumn(int columnIndex) {
336 return data.getDistinctTokensInColumn(columnIndex);
337 }
338
339
340
341
342
343
344 public List<Object> getSelectedComboData() {
345 if (selectedDataTypes == null) {
346 return null;
347 }
348
349 List<Object> result = new LinkedList<Object>();
350 for (DataTypeInfo dataType : selectedDataTypes) {
351 if (dataType != null) {
352 result.add(dataType.getAnalyzer());
353 } else {
354 result.add(null);
355 }
356 }
357
358 return result;
359 }
360
361
362
363
364
365
366 public List<String> getSelectedColumnCaptions() {
367 if (selectedDataTypes == null) {
368 return null;
369 }
370
371 List<String> captions = new LinkedList<String>();
372
373 for (DataTypeInfo dataType : selectedDataTypes) {
374 if (dataType != null) {
375 captions.add(dataType.getName());
376 }
377 }
378
379 return captions;
380 }
381
382 private IAnalyzer getSelectedAnalyzer(int columnIndex) {
383 if (selectedDataTypes == null) {
384 return null;
385 }
386
387 DataTypeInfo dataType = selectedDataTypes[columnIndex];
388
389 return dataType.getAnalyzer();
390 }
391
392
393
394 }