View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: ComboBoxTableCreator.java
13   * Created: 2009-03-17
14   * Author: elmo, jjarzabe, bmilos
15   * $Id$
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   * SWT table creator, which creates tables with comboboxes as labels and configuration buttons above.
44   * 
45   * @author AGH CAST Team
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 = ""; //$NON-NLS-1$
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  	 * The default constructor.
69  	 * 
70  	 * @param mediator
71  	 *            the mediating wizard page
72  	 */
73  	public ComboBoxTableCreator(AnalyzersSelectionPage mediator) {
74  		this.mediator = mediator;
75  	}
76  
77  	/**
78  	 * Creates table basing on column names and tokenized raw tabular data.
79  	 * 
80  	 * @param container
81  	 *            the table container
82  	 * @param comboItems
83  	 *            the combobox items
84  	 * @param comboData
85  	 *            the combobox data
86  	 * @param rawTabData
87  	 *            the data to be inserted into the table
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 	 * --------------------------HELPER METHODS------------------------------------------------------------
118 	 * -----------------------------------------------------------------------------------------------------------
119 	 */
120 
121 	/**
122 	 * Prepares the table columns.
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 	 * Makes columns headers as comboboxes.
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; // final variable for anonymous access
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 	 * Makes columns headers as comboboxes.
168 	 */
169 	private void makeTableColumnsHeadersAsComboBoxes(String[] comboItems, Object[] comboData, TableItem tableItem) {
170 		combos = new CCombo[data.getColumnsCount()];
171 		// comboBox.itemsCount = columnsLabels.count + 1 (+ 'ignore')
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; // final variable for anonymous access
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 						// selectedComboIndex is decreased by 1, because the first position in the combo is taken up by
188 						// the 'ignore' entry
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 	 * Fills table rows to the pointed size.
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 	 * --------------------------ACTION METHODS------------------------------------------------------------
240 	 * -----------------------------------------------------------------------------------------------------------
241 	 */
242 
243 	/**
244 	 * Selects event for prepared combobox.
245 	 * 
246 	 * @param columnIndex
247 	 *            the index of the data column
248 	 * @param dataType
249 	 *            the selected data type
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 	 * --------------------------HELPER METHODS------------------------------------------------------------
286 	 * -----------------------------------------------------------------------------------------------------------
287 	 */
288 
289 	/**
290 	 * Enables configuration button in column with the specified index.
291 	 * 
292 	 * @param columnIndex
293 	 *            the column index
294 	 */
295 	public void enableConfigButton(int columnIndex) {
296 		configButtons[columnIndex].setEnabled(true);
297 	}
298 
299 	/**
300 	 * Disables configuration button in column with the specified index.
301 	 * 
302 	 * @param columnIndex
303 	 *            the column index
304 	 */
305 	public void disableConfigButton(int columnIndex) {
306 		configButtons[columnIndex].setEnabled(false);
307 	}
308 
309 	/**
310 	 * Delegated method.
311 	 * 
312 	 * {@inheritDoc}
313 	 * 
314 	 * @see AnalyzersSelectionPage#widgetModified()
315 	 */
316 	public void widgetModified() {
317 		mediator.widgetModified();
318 	}
319 
320 	/*----------------------------------------------------------------------------------------------------------*/
321 
322 	/*
323 	 * -----------------------------------------------------------------------------------------------------------
324 	 * --------------------------GETTERS AND SETTERS-----------------------------------------------------
325 	 * -----------------------------------------------------------------------------------------------------------
326 	 */
327 
328 	/**
329 	 * Delegate method.
330 	 * 
331 	 * {@inheritDoc}
332 	 * 
333 	 * @see RawTabularData#getDistinctTokensInColumn(int)
334 	 */
335 	public List<String> getDistinctTokensInColumn(int columnIndex) {
336 		return data.getDistinctTokensInColumn(columnIndex);
337 	}
338 
339 	/**
340 	 * Retrieves the selected combobox data (analyzers).
341 	 * 
342 	 * @return selected combobox data
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 	 * Retrieves the selected captions for tabular data columns.
363 	 * 
364 	 * @return selected column captions
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 }