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: AdvancedEncodingDialog.java
13   * Created: 2009-03-25
14   * Author: bmilos
15   * $Id$
16   */
17  
18  package pl.edu.agh.cast.importer.wizard.dialog;
19  
20  import java.nio.charset.Charset;
21  import java.util.Set;
22  
23  import org.eclipse.jface.viewers.TableViewer;
24  import org.eclipse.swt.SWT;
25  import org.eclipse.swt.events.SelectionAdapter;
26  import org.eclipse.swt.events.SelectionEvent;
27  import org.eclipse.swt.layout.GridData;
28  import org.eclipse.swt.layout.GridLayout;
29  import org.eclipse.swt.widgets.Button;
30  import org.eclipse.swt.widgets.Combo;
31  import org.eclipse.swt.widgets.Composite;
32  import org.eclipse.swt.widgets.Dialog;
33  import org.eclipse.swt.widgets.Display;
34  import org.eclipse.swt.widgets.Group;
35  import org.eclipse.swt.widgets.Shell;
36  import org.eclipse.ui.ISharedImages;
37  import org.eclipse.ui.PlatformUI;
38  
39  import pl.edu.agh.cast.importer.base.data.RawTabularData;
40  import pl.edu.agh.cast.importer.wizard.page.FileParamsSelectionPage;
41  import pl.edu.agh.cast.importer.wizard.util.Messages;
42  import pl.edu.agh.cast.importer.wizard.utils.AbstractWizardPageHelper;
43  import pl.edu.agh.cast.importer.wizard.utils.TableViewerHelper;
44  
45  /**
46   * Dialog of the import wizard, which serves for advanced encoding support.
47   *
48   * @author AGH CAST Team
49   */
50  public class AdvancedEncodingDialog extends Dialog {
51  
52  	private static final int DEFAULT_DIALOG_STYLE = SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM;
53  
54  	private FileParamsSelectionPage mediator;
55  
56  	private Shell shell;
57  
58  	private Group fileEncodingGroup;
59  
60  	private Combo fileEncodingCombo;
61  
62  	private Group filePreviewGroup;
63  
64  	private TableViewer previewTableViewer;
65  
66  	private RawTabularData previewData;
67  
68  	private Charset selectedEncoding;
69  
70  	private Composite buttonsBarComposite;
71  
72  	private Button cancelBtn;
73  
74  	private Button saveBtn;
75  
76  	/**
77  	 * The default constructor.
78  	 *
79  	 * @param parent
80  	 *            the parent shell
81  	 * @param mediator
82  	 *            the mediating wizard page
83  	 * @param selectedEncoding
84  	 *            the previously selected encoding
85  	 * @param data
86  	 *            the preview data
87  	 */
88  	public AdvancedEncodingDialog(Shell parent, FileParamsSelectionPage mediator, Charset selectedEncoding,
89  	        RawTabularData data) {
90  		super(parent, DEFAULT_DIALOG_STYLE);
91  		this.mediator = mediator;
92  		this.previewData = data;
93  		this.selectedEncoding = selectedEncoding;
94  	}
95  
96  	/**
97  	 * Opens the dialog.
98  	 */
99  	public void open() {
100 		// Create the dialog window
101 		shell = new Shell(getParent(), DEFAULT_DIALOG_STYLE);
102 		shell.setText(Messages.AdvancedEncodingDialog_PageTitle);
103 		shell.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_DEF_VIEW));
104 		shell.layout();
105 		createContents();
106 		shell.pack();
107 		shell.setLocation(getParent().toDisplay(100, 100));
108 		shell.open();
109 
110 		Display display = getParent().getDisplay();
111 		while (!shell.isDisposed()) {
112 			if (!display.readAndDispatch()) {
113 				display.sleep();
114 			}
115 		}
116 	}
117 
118 	/**
119 	 * Creates the dialog contents.
120 	 */
121 	private void createContents() {
122 		GridLayout thisLayout = new GridLayout();
123 		shell.setLayout(thisLayout);
124 		shell.setSize(520, 200);
125 
126 		fileEncodingGroup = new Group(shell, SWT.NONE);
127 		GridLayout fileEncodingGroupLayout = new GridLayout();
128 		fileEncodingGroup.setLayout(fileEncodingGroupLayout);
129 		GridData fileEncodingGroupLData = new GridData();
130 		fileEncodingGroupLData.horizontalAlignment = GridData.FILL;
131 		fileEncodingGroupLData.grabExcessHorizontalSpace = true;
132 		fileEncodingGroupLData.verticalAlignment = GridData.END;
133 		fileEncodingGroup.setLayoutData(fileEncodingGroupLData);
134 		fileEncodingGroup.setText(Messages.FileTypeSelectionPageComposite_FileEncoding);
135 
136 		fileEncodingCombo = new Combo(fileEncodingGroup, SWT.READ_ONLY);
137 		GridData fileEncodingComboLData = new GridData();
138 		fileEncodingComboLData.horizontalAlignment = GridData.FILL;
139 		fileEncodingComboLData.grabExcessHorizontalSpace = true;
140 		fileEncodingComboLData.grabExcessVerticalSpace = true;
141 		fileEncodingCombo.setLayoutData(fileEncodingComboLData);
142 
143 		fileEncodingCombo.setItems(getAvailableCharsets());
144 		fileEncodingCombo.addSelectionListener(new SelectionAdapter() {
145 			@Override
146 			public void widgetSelected(SelectionEvent event) {
147 				handleFileEncodingComboSelection();
148 			}
149 		});
150 
151 		filePreviewGroup = new Group(shell, SWT.NONE);
152 		GridLayout filePreviewGroupLayout = new GridLayout();
153 		filePreviewGroup.setLayout(filePreviewGroupLayout);
154 		GridData filePreviewGroupLData = new GridData();
155 		filePreviewGroupLData.verticalAlignment = GridData.FILL;
156 		filePreviewGroupLData.horizontalAlignment = GridData.FILL;
157 		filePreviewGroupLData.widthHint = 500;
158 		filePreviewGroupLData.heightHint = 180;
159 		filePreviewGroupLData.grabExcessVerticalSpace = true;
160 		filePreviewGroupLData.grabExcessHorizontalSpace = true;
161 		filePreviewGroup.setLayoutData(filePreviewGroupLData);
162 		filePreviewGroup.setText(Messages.InitImportPageComposite_FilePreview);
163 
164 		previewTableViewer = new TableViewer(filePreviewGroup, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
165 		GridData previewTableViewerLData = new GridData();
166 		previewTableViewerLData.verticalAlignment = GridData.FILL;
167 		previewTableViewerLData.horizontalAlignment = GridData.FILL;
168 		previewTableViewerLData.widthHint = 480;
169 		previewTableViewerLData.heightHint = 160;
170 		previewTableViewerLData.grabExcessHorizontalSpace = true;
171 		previewTableViewerLData.grabExcessVerticalSpace = true;
172 		previewTableViewer.getControl().setLayoutData(previewTableViewerLData);
173 
174 		buttonsBarComposite = new Composite(shell, SWT.NONE);
175 		GridLayout buttonsBarCompositeLayout = new GridLayout();
176 		buttonsBarCompositeLayout.makeColumnsEqualWidth = true;
177 		buttonsBarCompositeLayout.numColumns = 2;
178 		GridData buttonsBarCompositeLData = new GridData();
179 		buttonsBarCompositeLData.horizontalSpan = 2;
180 		buttonsBarCompositeLData.verticalAlignment = GridData.BEGINNING;
181 		buttonsBarCompositeLData.horizontalAlignment = GridData.END;
182 		buttonsBarCompositeLData.grabExcessHorizontalSpace = true;
183 		buttonsBarCompositeLData.grabExcessVerticalSpace = true;
184 		buttonsBarCompositeLData.minimumWidth = 180;
185 		buttonsBarCompositeLData.minimumHeight = 30;
186 		buttonsBarComposite.setLayoutData(buttonsBarCompositeLData);
187 		buttonsBarComposite.setLayout(buttonsBarCompositeLayout);
188 
189 		saveBtn = new Button(buttonsBarComposite, SWT.PUSH | SWT.CENTER);
190 		GridData saveBtnLData = new GridData();
191 		saveBtnLData.verticalAlignment = GridData.BEGINNING;
192 		saveBtnLData.horizontalAlignment = GridData.BEGINNING;
193 		saveBtnLData.widthHint = 80;
194 		saveBtnLData.heightHint = 25;
195 		saveBtn.setLayoutData(saveBtnLData);
196 		saveBtn.setText(Messages.DialogCompositeButtons_Save);
197 		saveBtn.addSelectionListener(new SelectionAdapter() {
198 			@Override
199 			public void widgetSelected(SelectionEvent event) {
200 				handleSaveAction();
201 			}
202 		});
203 
204 		cancelBtn = new Button(buttonsBarComposite, SWT.PUSH | SWT.CENTER);
205 		GridData cancelBtnLData = new GridData();
206 		cancelBtnLData.verticalAlignment = GridData.BEGINNING;
207 		cancelBtnLData.horizontalAlignment = GridData.BEGINNING;
208 		cancelBtnLData.widthHint = 80;
209 		cancelBtnLData.heightHint = 25;
210 		cancelBtn.setLayoutData(cancelBtnLData);
211 		cancelBtn.setText(Messages.DialogCompositeButtons_Cancel);
212 		cancelBtn.addSelectionListener(new SelectionAdapter() {
213 			@Override
214 			public void widgetSelected(SelectionEvent event) {
215 				handleCancelAction();
216 			}
217 		});
218 
219 		setSelectedEncoding(selectedEncoding);
220 		refreshFilePreview(previewData);
221 	}
222 
223 	/*
224 	 * -----------------------------------------------------------------------------------------------------------
225 	 * --------------------------ACTION METHODS------------------------------------------------------------
226 	 * -----------------------------------------------------------------------------------------------------------
227 	 */
228 
229 	private void handleFileEncodingComboSelection() {
230 		refreshFilePreview(null);
231 	}
232 
233 	private void handleSaveAction() {
234 		if (mediator != null) {
235 			mediator.setSelectedEncoding(getSelectedEncoding());
236 		}
237 		shell.close();
238 	}
239 
240 	private void handleCancelAction() {
241 		shell.close();
242 	}
243 
244 	/*----------------------------------------------------------------------------------------------------------*/
245 
246 	/*
247 	 * -----------------------------------------------------------------------------------------------------------
248 	 * --------------------------HELPER METHODS------------------------------------------------------------
249 	 * -----------------------------------------------------------------------------------------------------------
250 	 */
251 
252 	private String[] getAvailableCharsets() {
253 		Set<String> availableCharsets = Charset.availableCharsets().keySet();
254 		return availableCharsets.toArray(new String[availableCharsets.size()]);
255 	}
256 
257 	/**
258 	 * Refreshes the file preview.
259 	 */
260 	private void refreshFilePreview(RawTabularData cachedData) {
261 		if (mediator == null) {
262 			return;
263 		}
264 
265 		if (cachedData == null) {
266 			previewData = mediator
267 			        .loadPreview(getSelectedEncoding(), null, AbstractWizardPageHelper.PREVIEW_ROWS_LIMIT_PARTIAL);
268 		} else {
269 			previewData = cachedData;
270 		}
271 
272 		if (previewData != null) {
273 			// create preview table
274 			previewTableViewer.getTable().setEnabled(true);
275 
276 			TableViewerHelper.initTable(previewTableViewer, previewData);
277 
278 			previewTableViewer.getTable().setLinesVisible(true);
279 			TableViewerHelper.adjustColumnWidth(previewTableViewer);
280 
281 		} else {
282 
283 			previewTableViewer.getTable().setEnabled(false);
284 			previewTableViewer.getTable().setLinesVisible(false);
285 			previewTableViewer.getTable().clearAll();
286 		}
287 	}
288 
289 	/*----------------------------------------------------------------------------------------------------------*/
290 
291 	/*
292 	 * -----------------------------------------------------------------------------------------------------------
293 	 * --------------------------GETTERS AND SETTERS------------------------------------------------------
294 	 * -----------------------------------------------------------------------------------------------------------
295 	 */
296 
297 	/**
298 	 * Retrieves the selected charset.
299 	 *
300 	 * @return selected charset
301 	 */
302 	public Charset getSelectedEncoding() {
303 		String[] availableEncodings = fileEncodingCombo.getItems();
304 		int selectedIndex = fileEncodingCombo.getSelectionIndex();
305 		if (selectedIndex == -1) {
306 			return null;
307 		}
308 		String selectedCharsetName = fileEncodingCombo.getItem(selectedIndex);
309 
310 		for (String encoding : availableEncodings) {
311 			if (selectedCharsetName.equals(encoding)) {
312 				return Charset.availableCharsets().get(selectedCharsetName);
313 			}
314 		}
315 		return null;
316 	}
317 
318 	private void setSelectedEncoding(Charset charset) {
319 		if (charset != null) {
320 			String[] availableEncodings = fileEncodingCombo.getItems();
321 
322 			for (int encodingIndex = 0; encodingIndex < availableEncodings.length; encodingIndex++) {
323 				if (charset.displayName().equals(availableEncodings[encodingIndex])) {
324 					fileEncodingCombo.select(encodingIndex);
325 					break;
326 				}
327 			}
328 		}
329 	}
330 
331 	/*----------------------------------------------------------------------------------------------------------*/
332 
333 }