001: package com.quantum.csv.wizard;
002:
003: import java.io.File;
004:
005: import org.eclipse.jface.preference.IPreferenceStore;
006: import org.eclipse.jface.viewers.IStructuredContentProvider;
007: import org.eclipse.jface.viewers.StructuredSelection;
008: import org.eclipse.jface.viewers.TableViewer;
009: import org.eclipse.jface.viewers.Viewer;
010: import org.eclipse.jface.wizard.WizardPage;
011: import org.eclipse.swt.SWT;
012: import org.eclipse.swt.events.ModifyEvent;
013: import org.eclipse.swt.events.ModifyListener;
014: import org.eclipse.swt.events.SelectionAdapter;
015: import org.eclipse.swt.events.SelectionEvent;
016: import org.eclipse.swt.layout.GridData;
017: import org.eclipse.swt.layout.GridLayout;
018: import org.eclipse.swt.widgets.Button;
019: import org.eclipse.swt.widgets.Composite;
020: import org.eclipse.swt.widgets.FileDialog;
021: import org.eclipse.swt.widgets.Group;
022: import org.eclipse.swt.widgets.Label;
023: import org.eclipse.swt.widgets.Text;
024:
025: import com.quantum.ImageStore;
026: import com.quantum.QuantumPlugin;
027: import com.quantum.flatfiles.MessageUtil;
028: import com.quantum.flatfiles.QuantumFlatFilesPlugin;
029: import com.quantum.flatfiles.preferences.CSVPreferences;
030: import com.quantum.model.Entity;
031: import com.quantum.view.bookmark.BookmarkView;
032: import com.quantum.view.bookmark.EntityNode;
033: import com.quantum.view.widget.SimpleLabelProvider;
034:
035: /**
036: * @author BC Holmes
037: * @author Julen
038: */
039: public class ExportCSVDetailsPage extends WizardPage {
040:
041: public class ContentProviderImpl implements
042: IStructuredContentProvider {
043:
044: public Object[] getElements(Object inputElement) {
045: if (inputElement instanceof StructuredSelection) {
046: Object[] entities = ((StructuredSelection) inputElement)
047: .toArray();
048: return entities;
049: } else {
050: return null;
051: }
052: }
053:
054: public void dispose() {
055: }
056:
057: public void inputChanged(Viewer viewer, Object oldInput,
058: Object newInput) {
059: }
060: }
061:
062: private String fileName;
063: private boolean writeHeaderRow = true;
064: private TableViewer tableViewer;
065: private ContentProviderImpl contentProvider;
066: private Entity[] entities;
067: private char columnSeparator = ',';
068: private IPreferenceStore store = QuantumFlatFilesPlugin
069: .getDefault().getPreferenceStore();
070:
071: protected void refreshTable() {
072: }
073:
074: public void dispose() {
075: super .dispose();
076: }
077:
078: /**
079: * @param pageName
080: */
081: protected ExportCSVDetailsPage(String pageName) {
082: super (pageName);
083:
084: setTitle(MessageUtil.getString(ExportCSVDetailsPage.class,
085: "title"));
086: setDescription(MessageUtil.getString(
087: ExportCSVDetailsPage.class, "description"));
088: }
089:
090: public void createControl(Composite parent) {
091:
092: writeHeaderRow = store.getBoolean(CSVPreferences.WRITE_HEADERS);
093:
094: final Composite parentf = parent;
095:
096: Composite composite = new Composite(parent, SWT.NONE);
097: composite.setLayout(new GridLayout(1, false));
098:
099: composite.setLayoutData(new GridData(GridData.FILL_BOTH));
100:
101: Label label = new Label(composite, SWT.NONE);
102: label.setText(MessageUtil.getString(ExportCSVDetailsPage.class,
103: "selectedEntities"));
104: label.setLayoutData(new GridData(
105: GridData.VERTICAL_ALIGN_BEGINNING));
106:
107: this .tableViewer = new TableViewer(composite);
108: this .tableViewer.setLabelProvider(new SimpleLabelProvider(
109: ImageStore.getImage(ImageStore.TABLE, QuantumPlugin
110: .getDefault())));
111: this .tableViewer
112: .setContentProvider(this .contentProvider = new ContentProviderImpl());
113: this .tableViewer.setInput(BookmarkView.getInstance()
114: .getSelection());
115: this .tableViewer.getControl().setLayoutData(
116: new GridData(GridData.FILL_BOTH));
117:
118: Object[] nodes = this .contentProvider.getElements(BookmarkView
119: .getInstance().getSelection());
120: this .entities = new Entity[nodes.length];
121: // Add to the entities only the tables and views selected
122: for (int i = 0; i < nodes.length; i++) {
123: Object node = nodes[i];
124: if (node instanceof EntityNode) {
125: EntityNode entityNode = (EntityNode) node;
126: if (entityNode.isTable() || entityNode.isView())
127: entities[i] = entityNode.getEntity();
128: }
129: }
130:
131: // Define the options group
132: Group group = new Group(composite, SWT.NONE);
133: group.setText(MessageUtil.getString(ExportCSVDetailsPage.class,
134: "options"));
135: group.setLayout(new GridLayout(1, false));
136: group.setLayoutData(new GridData(SWT.FILL, SWT.NONE, false,
137: false));
138:
139: // Define the button to select if there is a header row
140: Button headerRow = new Button(group, SWT.CHECK);
141: headerRow.setText(MessageUtil
142: .getString(getClass(), "headerRow"));
143: headerRow.setSelection(writeHeaderRow);
144: headerRow.setLayoutData(new GridData(SWT.BEGINNING, SWT.NONE,
145: false, false));
146: headerRow.addSelectionListener(new SelectionAdapter() {
147: public void widgetSelected(SelectionEvent event) {
148: writeHeaderRow = ((Button) event.getSource())
149: .getSelection();
150: }
151: });
152:
153: Composite columnSeparatorGrid = new Composite(group, SWT.NONE);
154: columnSeparatorGrid.setLayout(new GridLayout(2, false));
155:
156: // Define the selection of column separator
157: new Label(columnSeparatorGrid, SWT.NONE)
158: .setText("Column separator:");
159:
160: final Text columnSeparatorText = new Text(columnSeparatorGrid,
161: SWT.BORDER);
162: String defaultColumnSeparator = store
163: .getString(CSVPreferences.COLUMN_SEPARATOR);
164: if (defaultColumnSeparator == "") {
165: defaultColumnSeparator = ",";
166: }
167: columnSeparator = defaultColumnSeparator.charAt(0);
168: columnSeparatorText.setText(defaultColumnSeparator.substring(0,
169: 1));
170: columnSeparatorText.setTextLimit(1);
171: columnSeparatorText.setLayoutData(new GridData(
172: GridData.FILL_HORIZONTAL));
173: columnSeparatorText.addModifyListener(new ModifyListener() {
174: public void modifyText(ModifyEvent event) {
175: String columnSeparatorStr = ((Text) event.getSource())
176: .getText();
177: // Warn the user that the selected column separator is to be only one char
178: // as setTextLimit is set to 1, only will set red when length == 0
179: if (columnSeparatorStr.length() != 1) {
180: ((Text) event.getSource())
181: .setBackground(parentf.getDisplay()
182: .getSystemColor(SWT.COLOR_RED));
183: } else {
184: ((Text) event.getSource()).setBackground(parentf
185: .getDisplay().getSystemColor(
186: SWT.COLOR_WHITE));
187: }
188: if (columnSeparatorStr.length() > 0)
189: columnSeparator = columnSeparatorStr.charAt(0);
190: else
191: columnSeparator = ',';
192: }
193: });
194:
195: Label blankArea = new Label(composite, SWT.NONE);
196: blankArea.setText("");
197:
198: createDestinationArea(composite);
199:
200: setControl(composite);
201: updateState();
202: }
203:
204: private void createDestinationArea(Composite composite) {
205: Composite fileArea = new Composite(composite, SWT.NULL);
206: fileArea.setLayout(new GridLayout(3, false));
207: fileArea.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
208: | GridData.VERTICAL_ALIGN_BEGINNING));
209: Label label = new Label(fileArea, SWT.NONE);
210: label.setText(MessageUtil.getString(ExportCSVDetailsPage.class,
211: "fileName"));
212:
213: final Text fileNameText = new Text(fileArea, SWT.BORDER);
214: // Set the default filename from the preferences
215: String defaultFileName = store
216: .getString(CSVPreferences.DEFAULT_PATH)
217: + store.getString(CSVPreferences.DEFAULT_FILE_NAME);
218: fileNameText.setText(defaultFileName);
219: if (defaultFileName != "") {
220: setFileName(appendExtension(defaultFileName));
221: updateState();
222: }
223: fileNameText.setLayoutData(new GridData(
224: GridData.FILL_HORIZONTAL));
225: fileNameText.addModifyListener(new ModifyListener() {
226: public void modifyText(ModifyEvent event) {
227: String filename = ((Text) event.getSource()).getText();
228: // If the filename has no extension, we concat the default extension
229:
230: setFileName(appendExtension(filename));
231: updateState();
232: }
233: });
234:
235: Button button = new Button(fileArea, SWT.NONE);
236: button.setText(MessageUtil.getString(
237: ExportCSVDetailsPage.class, "browse"));
238: button.addSelectionListener(new SelectionAdapter() {
239: public void widgetSelected(SelectionEvent event) {
240: FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
241: String extension = entities.length > 1 ? "*.zip"
242: : "*.csv";
243: dialog.setFilterExtensions(new String[] { extension });
244: dialog.setFilterNames(new String[] { MessageUtil
245: .getString(ExportCSVDetailsPage.class,
246: entities.length > 1 ? "zipFiles"
247: : "csvFiles") });
248: String filename = dialog.open();
249: if (filename != null) {
250: fileNameText.setText(filename);
251: setFileName(filename);
252: updateState();
253: }
254: }
255: });
256: }
257:
258: protected String appendExtension(String filename) {
259: String extension = entities.length > 1 ? ".zip" : ".csv";
260: filename = filename != "" && filename.indexOf('.') < 0 ? filename
261: + extension
262: : filename;
263: return filename;
264: }
265:
266: protected void updateState() {
267: boolean pageComplete = (this .entities != null && this .entities.length > 0);
268: pageComplete &= (this .fileName != null && this .fileName != "" && !(new File(
269: this .fileName)).isDirectory());
270:
271: setPageComplete(pageComplete);
272: }
273:
274: public String getFileName() {
275: return this .fileName;
276: }
277:
278: protected void setFileName(String fileName) {
279: this .fileName = fileName;
280: }
281:
282: /**
283: * @return Returns the entities.
284: */
285: public Entity[] getEntities() {
286: return entities;
287: }
288:
289: /**
290: * @param entities The entities to set.
291: */
292: public void setEntities(Entity[] entities) {
293: this .entities = entities;
294: }
295:
296: /**
297: * @return Returns the writeHeaderRow.
298: */
299: public boolean isWriteHeaderRow() {
300: return writeHeaderRow;
301: }
302:
303: /**
304: * @param writeHeaderRow The writeHeaderRow to set.
305: */
306: public void setWriteHeaderRow(boolean writeHeaderRow) {
307: this .writeHeaderRow = writeHeaderRow;
308: }
309:
310: public char getColumnSeparator() {
311: return columnSeparator;
312: }
313:
314: }
|