001: package com.quantum.flatfiles.wizard;
002:
003: import java.io.File;
004:
005: import com.quantum.QuantumPlugin;
006: import com.quantum.flatfiles.MessageUtil;
007: import com.quantum.flatfiles.QuantumFlatFilesPlugin;
008: import com.quantum.flatfiles.converter.ConverterFactory;
009: import com.quantum.flatfiles.preferences.CSVPreferences;
010: import com.quantum.sql.SQLResultSetResults;
011: import com.quantum.sql.Scrollable;
012: import com.quantum.view.widget.ComboViewer;
013: import com.quantum.wizards.PropertyChangeWizardPage;
014:
015: import org.eclipse.jface.preference.IPreferenceStore;
016: import org.eclipse.jface.viewers.ISelectionChangedListener;
017: import org.eclipse.jface.viewers.IStructuredContentProvider;
018: import org.eclipse.jface.viewers.IStructuredSelection;
019: import org.eclipse.jface.viewers.LabelProvider;
020: import org.eclipse.jface.viewers.SelectionChangedEvent;
021: import org.eclipse.jface.viewers.StructuredSelection;
022: import org.eclipse.jface.viewers.Viewer;
023: import org.eclipse.swt.SWT;
024: import org.eclipse.swt.events.ModifyEvent;
025: import org.eclipse.swt.events.ModifyListener;
026: import org.eclipse.swt.events.SelectionAdapter;
027: import org.eclipse.swt.events.SelectionEvent;
028: import org.eclipse.swt.layout.GridData;
029: import org.eclipse.swt.layout.GridLayout;
030: import org.eclipse.swt.widgets.Button;
031: import org.eclipse.swt.widgets.Composite;
032: import org.eclipse.swt.widgets.FileDialog;
033: import org.eclipse.swt.widgets.Group;
034: import org.eclipse.swt.widgets.Label;
035: import org.eclipse.swt.widgets.Text;
036:
037: /**
038: * @author BC Holmes
039: */
040: class ExportResultSetDataWizardPage extends PropertyChangeWizardPage {
041:
042: private final SQLResultSetResults results;
043:
044: private Text fileName;
045: private Button overwrite;
046: private String exportType = ConverterFactory.EXCEL_SPREADSHEET;
047:
048: /**
049: * @param pageName
050: */
051: protected ExportResultSetDataWizardPage(String pageName,
052: SQLResultSetResults results) {
053: super (pageName);
054: setTitle(MessageUtil.getString(getClass(), "title"));
055: setDescription(MessageUtil.getString(getClass(), "description"));
056: this .results = results;
057: }
058:
059: public void createControl(Composite parent) {
060: setPageComplete(false);
061:
062: Composite composite = new Composite(parent, SWT.NULL);
063: composite.setLayout(new GridLayout(3, false));
064: composite.setLayoutData(new GridData(GridData.FILL_BOTH));
065:
066: createRowsPart(composite);
067:
068: Label label = new Label(composite, SWT.NULL);
069: label.setText(MessageUtil.getString(getClass(), "fileName"));
070: this .fileName = new Text(composite, SWT.BORDER | SWT.SINGLE);
071: this .fileName.setLayoutData(new GridData(
072: GridData.FILL_HORIZONTAL));
073:
074: Button browse = new Button(composite, SWT.PUSH);
075: browse.setText(MessageUtil.getString(getClass(), "browse"));
076: browse.setLayoutData(new GridData(
077: GridData.HORIZONTAL_ALIGN_FILL));
078: browse.addSelectionListener(new SelectionAdapter() {
079: public void widgetSelected(SelectionEvent event) {
080: FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
081: String filename = dialog.open();
082: if (filename != null) {
083: ExportResultSetDataWizardPage.this .fileName
084: .setText(filename);
085: validate(filename);
086: }
087: }
088: });
089:
090: this .fileName.addModifyListener(new ModifyListener() {
091: public void modifyText(ModifyEvent event) {
092: validate(((Text) event.getSource()).getText());
093: }
094: });
095:
096: label = new Label(composite, SWT.NULL);
097: label
098: .setText(MessageUtil.getString(getClass(),
099: "exportFormat"));
100:
101: ComboViewer viewer = new ComboViewer(composite);
102: viewer.getControl().setLayoutData(
103: new GridData(GridData.FILL_HORIZONTAL));
104: viewer.setContentProvider(new IStructuredContentProvider() {
105: public void dispose() {
106: }
107:
108: public void inputChanged(Viewer viewer, Object oldInput,
109: Object newInput) {
110: }
111:
112: public Object[] getElements(Object inputElement) {
113: if (inputElement instanceof String[]) {
114: return (String[]) inputElement;
115: } else {
116: return null;
117: }
118: }
119: });
120: viewer.setLabelProvider(new LabelProvider() {
121: public String getText(Object element) {
122: return element == null ? "" : MessageUtil.getString(
123: ConverterFactory.class, (String) element);
124: }
125: });
126: viewer.setInput(ConverterFactory.getConverterTypes());
127: viewer.setSelection(new StructuredSelection(
128: ConverterFactory.EXCEL_SPREADSHEET));
129: viewer
130: .addSelectionChangedListener(new ISelectionChangedListener() {
131: public void selectionChanged(
132: SelectionChangedEvent event) {
133: IStructuredSelection selection = (IStructuredSelection) event
134: .getSelection();
135: setExportType(selection.isEmpty() ? null
136: : (String) selection.getFirstElement());
137: }
138: });
139:
140: Label filler = new Label(composite, SWT.NULL);
141: filler.setText("");
142: filler.setLayoutData(new GridData(
143: GridData.HORIZONTAL_ALIGN_BEGINNING));
144:
145: IPreferenceStore store = QuantumPlugin.getDefault()
146: .getPreferenceStore();
147: boolean confirmOverwrite = store
148: .getBoolean(com.quantum.PluginPreferences.EXPORT_CONFIRM_OVERWRITE);
149:
150: this .overwrite = new Button(composite, SWT.CHECK);
151: overwrite.setSelection(confirmOverwrite);
152: this .overwrite.setText(MessageUtil.getString(getClass(),
153: "overwrite"));
154: GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
155: gridData.horizontalSpan = 3;
156: this .overwrite.setLayoutData(gridData);
157:
158: setControl(composite);
159: }
160:
161: protected void setExportType(String exportType) {
162: this .exportType = exportType;
163: IPreferenceStore store = QuantumFlatFilesPlugin.getDefault()
164: .getPreferenceStore();
165: // Set the default filename for CSV from the preferences
166: String defaultCSVFileName = store
167: .getString(CSVPreferences.DEFAULT_PATH)
168: + store.getString(CSVPreferences.DEFAULT_FILE_NAME);
169: if (exportType.equals(ConverterFactory.COMMA_SEPARATED_VALUES)) {
170: if (this .fileName.getText().equals("")) {
171: this .fileName.setText(defaultCSVFileName);
172: }
173: } else {
174: // If the text equals the default for CSV, and CSV is not selected, it's
175: // likely that's a left-over. We blank the text.
176: if (this .fileName.getText().equals(defaultCSVFileName)) {
177: this .fileName.setText("");
178: }
179: }
180: }
181:
182: private void validate(String fileName) {
183: File file = new File(fileName);
184: if (file.exists() && file.isFile()) {
185: setErrorMessage(null);
186: setPageComplete(true);
187: } else if (!file.exists() && !fileName.endsWith(File.separator)) {
188: setErrorMessage(null);
189: setPageComplete(true);
190: } else {
191: setErrorMessage(MessageUtil.getString(getClass(),
192: "invalidFileName"));
193: setPageComplete(false);
194: }
195: }
196:
197: /**
198: * @param composite
199: */
200: private void createRowsPart(Composite composite) {
201: Group group = new Group(composite, SWT.NULL);
202: group.setText(MessageUtil.getString(getClass(), "rows"));
203: GridData data = new GridData(GridData.FILL_HORIZONTAL);
204: data.horizontalSpan = 3;
205: group.setLayout(new GridLayout());
206: group.setLayoutData(data);
207:
208: Button allRowsButton = new Button(group, SWT.RADIO);
209: allRowsButton.setText(MessageUtil.getString(getClass(),
210: "allRows"));
211:
212: Button currentRowsButton = new Button(group, SWT.RADIO);
213: if (this .results instanceof Scrollable) {
214: Scrollable scrollable = (Scrollable) this .results;
215: currentRowsButton.setText(MessageUtil.getString(getClass(),
216: "currentRowsWithRange", new Object[] {
217: new Integer(scrollable.getStart()),
218: new Integer(scrollable.getEnd()) }));
219: if (scrollable.hasNextPage()
220: || scrollable.hasPreviousPage()) {
221: currentRowsButton.setSelection(true);
222: } else {
223: currentRowsButton.setEnabled(false);
224: }
225: } else {
226: currentRowsButton.setText(MessageUtil.getString(getClass(),
227: "currentRows"));
228: currentRowsButton.setEnabled(false);
229: }
230: }
231:
232: public String getFileName() {
233: return this .fileName == null ? null : this .fileName.getText();
234: }
235:
236: public boolean overwriteFile() {
237: return this .overwrite == null ? false : this .overwrite
238: .getSelection();
239: }
240:
241: public String getExportType() {
242: return exportType;
243: }
244:
245: }
|