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: AbstractConversionRuleConfigDialog.java
13   * Created: 2009-04-30
14   * Author: bmilos
15   * $Id$
16   */
17  
18  package pl.edu.agh.cast.importer.wizard.dialog.rule;
19  
20  import java.util.HashMap;
21  import java.util.LinkedList;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.eclipse.osgi.util.NLS;
26  import org.eclipse.swt.SWT;
27  import org.eclipse.swt.layout.GridData;
28  import org.eclipse.swt.widgets.Composite;
29  import org.eclipse.swt.widgets.Dialog;
30  import org.eclipse.swt.widgets.Display;
31  import org.eclipse.swt.widgets.Shell;
32  
33  import pl.edu.agh.cast.importer.base.converter.rules.IConversionRule;
34  import pl.edu.agh.cast.importer.base.data.TabularData;
35  import pl.edu.agh.cast.importer.wizard.page.AbstractConversionRulesSelectionPage;
36  import pl.edu.agh.cast.importer.wizard.util.Messages;
37  import pl.edu.agh.cast.importer.wizard.utils.ButtonTableCreator;
38  import pl.edu.agh.cast.ui.util.MsgBoxHelper;
39  
40  /**
41   * Abstract class for conversion rules configuration dialogs of the importer wizard.
42   *
43   * @author AGH CAST Team
44   */
45  public abstract class AbstractConversionRuleConfigDialog extends Dialog {
46  
47  	private static final int DEFAULT_DIALOG_STYLE = SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM;
48  
49  	/**
50  	 * The dialog shell.
51  	 */
52  	protected Shell shell;
53  
54  	/**
55  	 * The mediating conversion rules selection page.
56  	 */
57  	protected AbstractConversionRulesSelectionPage mediator;
58  
59  	/**
60  	 * The tabular data to be converted.
61  	 */
62  	protected TabularData data;
63  
64  	/**
65  	 * The additional parameters for the conversion rule.
66  	 */
67  	protected String[] labelParams;
68  
69  	/**
70  	 * The conversion rule.
71  	 */
72  	protected IConversionRule rule;
73  
74  	/**
75  	 * The default constructor.
76  	 *
77  	 * @param parent
78  	 *            the parent shell
79  	 */
80  	public AbstractConversionRuleConfigDialog(Shell parent) {
81  		super(parent, DEFAULT_DIALOG_STYLE);
82  	}
83  
84  	/**
85  	 * Opens the dialog.
86  	 */
87  	public void open() {
88  		// Create the dialog window
89  		shell = new Shell(getParent(), DEFAULT_DIALOG_STYLE);
90  		shell.layout();
91  		shell.setText(Messages.AbstractConversionRuleConfigDialog_Title);
92  		if (createContents()) {
93  			shell.pack();
94  			shell.setLocation(getParent().toDisplay(100, 100));
95  			shell.open();
96  
97  			Display display = getParent().getDisplay();
98  			while (!shell.isDisposed()) {
99  				if (!display.readAndDispatch()) {
100 					display.sleep();
101 				}
102 			}
103 		}
104 	}
105 
106 	/**
107 	 * Sets necessary data for this conversion rule configuration dialog.
108 	 *
109 	 * @param mediatingPage
110 	 *            the mediating page
111 	 * @param tabData
112 	 *            the tabular data
113 	 * @param params
114 	 *            the additional parameters
115 	 * @param convRule
116 	 *            the conversion rule
117 	 */
118 	public void setData(AbstractConversionRulesSelectionPage mediatingPage, TabularData tabData, String[] params,
119 	        IConversionRule convRule) {
120 		this.mediator = mediatingPage;
121 		this.data = tabData;
122 		this.labelParams = params;
123 		this.rule = convRule;
124 	}
125 
126 	/**
127 	 * Calls the {@link ButtonTableCreator#createTable(Composite, GridData, TabularData, Map)} method of the specified
128 	 * table creator, which is to be created on the specified composite.
129 	 *
130 	 * @param container
131 	 *            the container for the new table that is to be created
132 	 * @param tableCreator
133 	 *            the button table creator
134 	 * @param columnType
135 	 *            the type of columns to be included in the table
136 	 * @param columnTypeName
137 	 *            the name of the column type
138 	 * @return <code>true</code> of the table was successfully created, <code>false</code> otherwise
139 	 */
140 	protected boolean createTable(Composite container, ButtonTableCreator tableCreator, Class<?> columnType,
141 	        String columnTypeName) {
142 		List<Class<?>> columnTypes = data.getColumnTypes();
143 		GridData tableLData = new GridData(SWT.FILL, SWT.FILL, true, true);
144 
145 		if (columnTypes == null) {
146 			tableCreator.createTable(container, tableLData, data, null);
147 		} else {
148 			List<Integer> colIndices = new LinkedList<Integer>();
149 
150 			for (int columnIdx = 0; columnIdx < columnTypes.size(); columnIdx++) {
151 				if (columnType.isAssignableFrom(columnTypes.get(columnIdx))) {
152 					colIndices.add(columnIdx);
153 				}
154 			}
155 
156 			if (colIndices.isEmpty()) {
157 				String colName = ""; //$NON-NLS-1$
158 				if (columnTypeName != null) {
159 					colName = columnTypeName.toLowerCase();
160 				}
161 
162 				MsgBoxHelper.showWarningBox(container,
163 				        Messages.AbstractSingleColumnConversionRuleConfigDialog_NoColumnBoxTitle, NLS.bind(
164 				                Messages.AbstractSingleColumnConversionRuleConfigDialog_NoColumnBoxMsg, colName));
165 				return false;
166 			}
167 
168 			/*
169 			 * Construction of a mapping of dialog tabular data column indices (always increased by 1 because of the row
170 			 * number column in the config dialog) to the indices of columns of the "entire" tabular data (containing
171 			 * only the columns selected at the {@link AnalyzersSelectionPage}).
172 			 */
173 			Map<Integer, Integer> dialogToDataIndices = new HashMap<Integer, Integer>();
174 			for (int dialogColIdx = 0; dialogColIdx < colIndices.size(); dialogColIdx++) {
175 				dialogToDataIndices.put(dialogColIdx + 1, colIndices.get(dialogColIdx));
176 			}
177 
178 			tableCreator.createTable(container, tableLData, data.copyDataLeavingColumns(colIndices),
179 			        dialogToDataIndices);
180 		}
181 		return true;
182 	}
183 
184 	/*
185 	 * -----------------------------------------------------------------------------------------------------------
186 	 * --------------------------ABSTRACT METHODS---------------------------------------------------------
187 	 * -----------------------------------------------------------------------------------------------------------
188 	 */
189 
190 	protected abstract boolean createContents();
191 
192 	/*----------------------------------------------------------------------------------------------------------*/
193 
194 }