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: FixedWidthColumnSelectionPage.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.LinkedList;
21  import java.util.List;
22  
23  import org.eclipse.swt.SWT;
24  import org.eclipse.swt.widgets.Composite;
25  
26  import pl.edu.agh.cast.importer.base.data.RawTabularData;
27  import pl.edu.agh.cast.importer.base.tokenizer.ITokenizerOption;
28  import pl.edu.agh.cast.importer.base.tokenizer.TokenizerOption;
29  import pl.edu.agh.cast.importer.base.tokenizer.fixedwidth.FixedWidthTokenizer;
30  import pl.edu.agh.cast.importer.wizard.util.Messages;
31  
32  /**
33   * Page of the import wizard, which serves for selection of columns for a fixed width data file.
34   *
35   * @author AGH CAST Team
36   */
37  public class FixedWidthColumnSelectionPage extends AbstractImportWizardPage {
38  
39  	private FixedWidthColumnSelectionPageComposite composite;
40  
41  	private String filePathCache;
42  
43  	/**
44  	 * The default constructor.
45  	 */
46  	public FixedWidthColumnSelectionPage() {
47  		super(Messages.ImporterWizard_FixedWidthColumnSelectionPageTitle,
48  		        Messages.ImporterWizard_FixedWidthColumnSelectionPageDescription);
49  	}
50  
51  	/*
52  	 * -----------------------------------------------------------------------------------------------------------
53  	 * --------------------------OVERRIDED METHODS----------------------------------------------------------------
54  	 * -----------------------------------------------------------------------------------------------------------
55  	 */
56  
57  	/**
58  	 * {@inheritDoc}
59  	 *
60  	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
61  	 */
62  	@Override
63  	public void createControl(Composite parent) {
64  		composite = new FixedWidthColumnSelectionPageComposite(parent, SWT.NULL, this);
65  		setControl(composite);
66  
67  		refreshSelectedData();
68  	}
69  
70  	@Override
71  	protected boolean isComplete() {
72  		return true;
73  	}
74  
75  	/**
76  	 * {@inheritDoc}
77  	 *
78  	 * @see pl.edu.agh.cast.importer.wizard.page.AbstractImportWizardPage#initPage()
79  	 */
80  	@Override
81  	public void initPage() {
82  		refreshSelectedData();
83  	}
84  
85  	/**
86  	 * {@inheritDoc}
87  	 *
88  	 * @see pl.edu.agh.cast.importer.wizard.page.AbstractImportWizardPage#refreshImportData()
89  	 */
90  	@Override
91  	public void refreshImportData() {
92  		getImportProcess().setTokenizerOptions(getSelectedTokenizerOptions());
93  	}
94  
95  	/*----------------------------------------------------------------------------------------------------------*/
96  
97  	/*
98  	 * -----------------------------------------------------------------------------------------------------------
99  	 * --------------------------HELPER METHODS-------------------------------------------------------------------
100 	 * -----------------------------------------------------------------------------------------------------------
101 	 */
102 
103 	private void refreshSelectedData() {
104 		if (composite == null) {
105 			return;
106 		}
107 
108 		if (filePathCache != null && getImportProcess().getFilePath().equals(filePathCache)) {
109 			refreshImportData();
110 		}
111 
112 		final RawTabularData preview = getImportProcess().getTokenizePreview();
113 		composite.refreshFilePreviewGroupLabel(getImportProcess().getFilePath());
114 		final FixedWidthTokenizer tokenizer = (FixedWidthTokenizer)getImportProcess().getTokenizer();
115 		composite.refreshFilePreview(tokenizer.getInputFileLines().subList(0, preview.size()), tokenizer
116 		        .getLineCutPoints());
117 
118 		filePathCache = getImportProcess().getFilePath();
119 	}
120 
121 	/**
122 	 * Notifies about comment option value change.
123 	 */
124 	void notifyCommentOptionChanged() {
125 		refreshSelectedData();
126 	}
127 
128 	private List<ITokenizerOption> getSelectedTokenizerOptions() {
129 		final List<ITokenizerOption> options = new LinkedList<ITokenizerOption>();
130 
131 		if (composite.getCommentCharOptionValue() != null) {
132 			options.add(new TokenizerOption(FixedWidthTokenizer.COMMENT_CHAR_OPTION_NAME, composite
133 			        .getCommentCharOptionValue()));
134 		}
135 
136 		options.add(new TokenizerOption(FixedWidthTokenizer.CUT_POINTS_OPTION_NAME, FixedWidthTokenizer
137 		        .pointListToString(composite.getColumnBeaksIndices())));
138 
139 		return options;
140 	}
141 	/*----------------------------------------------------------------------------------------------------------*/
142 
143 }