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: FixedWidthColumnSelectionPageComposite.java
13   * Created: 2009-03-16
14   * Author: bmilos
15   * $Id$
16   */
17  
18  package pl.edu.agh.cast.importer.wizard.page;
19  
20  import java.util.List;
21  
22  import org.eclipse.swt.SWT;
23  import org.eclipse.swt.events.FocusAdapter;
24  import org.eclipse.swt.events.FocusEvent;
25  import org.eclipse.swt.events.SelectionAdapter;
26  import org.eclipse.swt.events.SelectionEvent;
27  import org.eclipse.swt.events.VerifyEvent;
28  import org.eclipse.swt.events.VerifyListener;
29  import org.eclipse.swt.layout.GridData;
30  import org.eclipse.swt.layout.GridLayout;
31  import org.eclipse.swt.widgets.Button;
32  import org.eclipse.swt.widgets.Composite;
33  import org.eclipse.swt.widgets.Group;
34  import org.eclipse.swt.widgets.Label;
35  import org.eclipse.swt.widgets.Text;
36  
37  import pl.edu.agh.cast.importer.wizard.util.Messages;
38  import pl.edu.agh.cast.importer.wizard.utils.AbstractWizardPageHelper;
39  
40  /**
41   * The main composite of the {@link FixedWidthColumnSelectionPage}.
42   *
43   * @author AGH CAST Team
44   */
45  public class FixedWidthColumnSelectionPageComposite extends Composite {
46  
47  	private Group filePreviewGroup;
48  
49  	private Label createLineLbl;
50  
51  	private Label deleteLineLbl;
52  
53  	private ColumnBreaksSelectionComposite columnBreaksSelectionComposite;
54  
55  	private Composite instructionsComposite;
56  
57  	private Composite optionsComposite;
58  
59  	private Text commentCharTxt;
60  
61  	private Button commentCharChbox;
62  
63  	private FixedWidthColumnSelectionPage mediator;
64  
65  	/**
66  	 * The default constructor.
67  	 *
68  	 * @param parent
69  	 *            the parent composite
70  	 * @param style
71  	 *            the style of widget to construct
72  	 * @param mediator
73  	 *            the mediating wizard page
74  	 */
75  	public FixedWidthColumnSelectionPageComposite(Composite parent, int style, FixedWidthColumnSelectionPage mediator) {
76  		super(parent, style);
77  		this.mediator = mediator;
78  		initGUI();
79  	}
80  
81  	private void initGUI() {
82  		GridLayout thisLayout = new GridLayout();
83  		this.setLayout(thisLayout);
84  		this.setSize(800, 600);
85  
86  		instructionsComposite = new Composite(this, SWT.NONE);
87  		GridLayout instructionsCompositeLayout = new GridLayout();
88  		instructionsCompositeLayout.makeColumnsEqualWidth = true;
89  		GridData instructionsCompositeLData = new GridData();
90  		instructionsCompositeLData.grabExcessHorizontalSpace = true;
91  		instructionsCompositeLData.verticalAlignment = GridData.FILL;
92  		instructionsCompositeLData.horizontalAlignment = GridData.FILL;
93  		instructionsComposite.setLayoutData(instructionsCompositeLData);
94  		instructionsComposite.setLayout(instructionsCompositeLayout);
95  
96  		createLineLbl = new Label(instructionsComposite, SWT.NONE);
97  		createLineLbl.setText(Messages.FixedWidthColumnSelectionPageComposite_CreateColumnBreakLbl);
98  
99  		deleteLineLbl = new Label(instructionsComposite, SWT.NONE);
100 		deleteLineLbl.setText(Messages.FixedWidthColumnSelectionPageComposite_DeleteColumnBreakLbl);
101 
102 		optionsComposite = new Composite(this, SWT.NONE);
103 		GridLayout optionsCompositeLayout = new GridLayout();
104 		optionsCompositeLayout.numColumns = 2;
105 		optionsCompositeLayout.makeColumnsEqualWidth = true;
106 		optionsCompositeLayout.horizontalSpacing = 20;
107 		optionsCompositeLayout.verticalSpacing = 10;
108 		GridData optionsCompositeLData = new GridData();
109 		optionsCompositeLData.grabExcessVerticalSpace = true;
110 		optionsCompositeLData.grabExcessHorizontalSpace = true;
111 		optionsComposite.setLayout(optionsCompositeLayout);
112 		optionsComposite.setLayoutData(optionsCompositeLData);
113 
114 		commentCharChbox = new Button(optionsComposite, SWT.CHECK | SWT.LEFT);
115 		commentCharChbox.setText(Messages.DelimiterSelectionPageComposite_CommentChar);
116 		GridData commentCharChboxLData = new GridData();
117 		commentCharChboxLData.horizontalAlignment = GridData.FILL;
118 		commentCharChboxLData.grabExcessHorizontalSpace = true;
119 		commentCharChbox.setLayoutData(commentCharChboxLData);
120 		commentCharChbox.setToolTipText(Messages.DelimiterSelectionPageComposite_CommentCharTooltip);
121 		commentCharChbox.addSelectionListener(new SelectionAdapter() {
122 			@Override
123 			public void widgetSelected(SelectionEvent event) {
124 				handleCommentCharValueChanged();
125 			}
126 		});
127 
128 		commentCharTxt = new Text(optionsComposite, SWT.BORDER);
129 		GridData commentCharTxtLData = new GridData();
130 		commentCharTxtLData.heightHint = 15;
131 		commentCharTxtLData.horizontalAlignment = GridData.FILL;
132 		commentCharTxtLData.grabExcessHorizontalSpace = true;
133 		commentCharTxt.setLayoutData(commentCharTxtLData);
134 		commentCharTxt.setEnabled(false);
135 		commentCharTxt.addVerifyListener(new VerifyListener() {
136 			public void verifyText(VerifyEvent event) {
137 				AbstractWizardPageHelper.handleCommentCharVerification(event);
138 			}
139 		});
140 		commentCharTxt.addFocusListener(new FocusAdapter() {
141 			public void focusLost(FocusEvent e) {
142 				handleCommentCharValueChanged();
143 			}
144 		});
145 
146 		filePreviewGroup = new Group(this, SWT.NONE);
147 		GridLayout filePreviewGroupLayout = new GridLayout();
148 		GridData filePreviewGroupLData = new GridData();
149 		filePreviewGroupLData.horizontalSpan = 3;
150 		filePreviewGroupLData.verticalAlignment = GridData.FILL;
151 		filePreviewGroupLData.horizontalAlignment = GridData.FILL;
152 		filePreviewGroupLData.minimumWidth = 550;
153 		filePreviewGroupLData.minimumHeight = 150;
154 		filePreviewGroupLData.grabExcessVerticalSpace = true;
155 		filePreviewGroupLData.grabExcessHorizontalSpace = true;
156 		filePreviewGroup.setLayout(filePreviewGroupLayout);
157 		filePreviewGroup.setLayoutData(filePreviewGroupLData);
158 		filePreviewGroup.setText(Messages.FixedWidthColumnSelectionPageComposite_FilePreview);
159 
160 		GridData columnBreaksSelectionCompositeLData = new GridData();
161 		columnBreaksSelectionCompositeLData.grabExcessHorizontalSpace = true;
162 		columnBreaksSelectionCompositeLData.grabExcessVerticalSpace = true;
163 		columnBreaksSelectionCompositeLData.horizontalAlignment = GridData.FILL;
164 		columnBreaksSelectionCompositeLData.verticalAlignment = GridData.FILL;
165 		columnBreaksSelectionCompositeLData.heightHint = 400;
166 		columnBreaksSelectionComposite = new ColumnBreaksSelectionComposite(filePreviewGroup);
167 		columnBreaksSelectionComposite.setLayoutData(columnBreaksSelectionCompositeLData);
168 
169 	}
170 
171 	/*
172 	 * -----------------------------------------------------------------------------------------------------------
173 	 * --------------------------HELPER METHODS------------------------------------------------------------
174 	 * -----------------------------------------------------------------------------------------------------------
175 	 */
176 
177 	private void handleCommentCharValueChanged() {
178 		if (commentCharChbox.getSelection()) {
179 			commentCharTxt.setEnabled(true);
180 		} else {
181 			commentCharTxt.setEnabled(false);
182 		}
183 		mediator.notifyCommentOptionChanged();
184 	}
185 
186 	/**
187 	 * Refreshes the file preview group label by adding to it the selected data file path name.
188 	 *
189 	 * @param selectedFilePath
190 	 *            the selected file path
191 	 */
192 	void refreshFilePreviewGroupLabel(String selectedFilePath) {
193 		if (selectedFilePath != null && selectedFilePath.length() > 0) {
194 			final StringBuilder sb = new StringBuilder();
195 			sb.append(Messages.InitImportPageComposite_FilePreview);
196 			sb.append(": "); //$NON-NLS-1$
197 			sb.append(selectedFilePath);
198 			filePreviewGroup.setText(sb.toString());
199 		}
200 	}
201 
202 	/**
203 	 * Delegates to method {@link ColumnBreaksSelectionComposite#showFilePreview(List, List)}.
204 	 *
205 	 * @param lines
206 	 *            string lines from tokenizer
207 	 * @param cutLinePoints
208 	 *            cut line points
209 	 */
210 	void refreshFilePreview(List<String> lines, List<Integer> cutLinePoints) {
211 		columnBreaksSelectionComposite.showFilePreview(lines, cutLinePoints);
212 	}
213 
214 	/*
215 	 * -----------------------------------------------------------------------------------------------------------
216 	 * --------------------------GETTERS AND SETTERS------------------------------------------------------------
217 	 * -----------------------------------------------------------------------------------------------------------
218 	 */
219 
220 	/**
221 	 * Returns comment char option value. If value is empty - returns null.
222 	 *
223 	 * @return comment char option value
224 	 */
225 	String getCommentCharOptionValue() {
226 		final String commentChar = commentCharTxt.getText();
227 		if (commentCharChbox.getSelection() && commentChar != null && !commentChar.isEmpty()) {
228 			return commentCharTxt.getText();
229 		}
230 		return null;
231 	}
232 
233 	List<Integer> getColumnBeaksIndices() {
234 		return columnBreaksSelectionComposite.getColumnBreaksIndices();
235 	}
236 	/*----------------------------------------------------------------------------------------------------------*/
237 
238 }