001: package com.quantum.csv.wizard;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.util.Arrays;
006: import java.util.Enumeration;
007: import java.util.Iterator;
008: import java.util.List;
009: import java.util.Vector;
010: import java.util.zip.ZipEntry;
011: import java.util.zip.ZipFile;
012:
013: import org.eclipse.jface.dialogs.MessageDialog;
014: import org.eclipse.jface.viewers.IStructuredContentProvider;
015: import org.eclipse.jface.viewers.StructuredSelection;
016: import org.eclipse.jface.viewers.TableViewer;
017: import org.eclipse.jface.viewers.Viewer;
018: import org.eclipse.jface.wizard.WizardPage;
019: import org.eclipse.swt.SWT;
020: import org.eclipse.swt.events.ModifyEvent;
021: import org.eclipse.swt.events.ModifyListener;
022: import org.eclipse.swt.events.SelectionAdapter;
023: import org.eclipse.swt.events.SelectionEvent;
024: import org.eclipse.swt.layout.GridData;
025: import org.eclipse.swt.layout.GridLayout;
026: import org.eclipse.swt.widgets.Button;
027: import org.eclipse.swt.widgets.Composite;
028: import org.eclipse.swt.widgets.FileDialog;
029: import org.eclipse.swt.widgets.Group;
030: import org.eclipse.swt.widgets.Label;
031: import org.eclipse.swt.widgets.Text;
032:
033: import com.quantum.ImageStore;
034: import com.quantum.QuantumPlugin;
035: import com.quantum.flatfiles.MessageUtil;
036: import com.quantum.util.QuantumUtil;
037: import com.quantum.view.bookmark.BookmarkView;
038: import com.quantum.view.bookmark.EntityNode;
039: import com.quantum.view.bookmark.GroupNode;
040: import com.quantum.view.widget.SimpleLabelProvider;
041:
042: /**
043: * @author BC Holmes
044: * @author Julen
045: */
046: public class ImportCSVDetailsPage extends WizardPage {
047:
048: public class ContentProviderImpl implements
049: IStructuredContentProvider {
050:
051: public Object[] getElements(Object inputElement) {
052: if (inputElement instanceof StructuredSelection) {
053: Object[] entities = ((StructuredSelection) inputElement)
054: .toArray();
055: return entities;
056: } else if (inputElement instanceof List) {
057: return ((List) inputElement).toArray();
058: } else {
059: return null;
060: }
061: }
062:
063: public void dispose() {
064: }
065:
066: public void inputChanged(Viewer viewer, Object oldInput,
067: Object newInput) {
068: }
069: }
070:
071: private String fileName;
072: private boolean hasHeaderRow = false; // If the CVS files have a header row with the names of the columns
073: private boolean ignoreHeaderRow = true; // If the header has to be ignored nd only the position used
074: private boolean deletePreviousContent = false; // If the previous content of the tables has to be deleted
075: private TableViewer tableViewer;
076: private ContentProviderImpl contentProvider;
077: private Button ignoreHeaderRowButton = null; // Declared here because it needs to be accessed from an anonymous local class
078: private boolean isZip = false;
079: private Vector matchedEntities = new Vector(); // Holds the EntityNodes that will be imported
080: List allSelectedTables = new Vector(); // Holds all the EntityNodes selected by the user
081: private String eol = System.getProperty("line.separator");
082: private char columnSeparator = ',';
083:
084: protected void refreshTable() {
085: }
086:
087: public void dispose() {
088: super .dispose();
089: }
090:
091: /**
092: * @param pageName
093: */
094: protected ImportCSVDetailsPage(String pageName) {
095: super (pageName);
096:
097: setTitle(MessageUtil.getString(ImportCSVDetailsPage.class,
098: "title"));
099: setDescription(MessageUtil.getString(
100: ImportCSVDetailsPage.class, "description"));
101: }
102:
103: public void createControl(Composite parent) {
104:
105: final Composite parentf = parent;
106:
107: Composite composite = new Composite(parent, SWT.NONE);
108: composite.setLayout(new GridLayout(1, false));
109:
110: composite.setLayoutData(new GridData(GridData.FILL_BOTH));
111:
112: Label label = new Label(composite, SWT.NONE);
113: label.setText(MessageUtil.getString(ImportCSVDetailsPage.class,
114: "selectedEntities"));
115: label.setLayoutData(new GridData(
116: GridData.VERTICAL_ALIGN_BEGINNING));
117:
118: this .tableViewer = new TableViewer(composite);
119: this .tableViewer.setLabelProvider(new SimpleLabelProvider(
120: ImageStore.getImage(ImageStore.TABLE, QuantumPlugin
121: .getDefault())));
122: this .tableViewer
123: .setContentProvider(this .contentProvider = new ContentProviderImpl());
124:
125: // Update the allSelectedTables with the tables selected, expanding the GroupNodes if needed
126: List selectedTables = BookmarkView.getInstance().getSelection()
127: .toList();
128: for (Iterator iter = selectedTables.iterator(); iter.hasNext();) {
129: Object element = iter.next();
130: if (element instanceof GroupNode) {
131: GroupNode groupNode = (GroupNode) element;
132: allSelectedTables.addAll(Arrays.asList(groupNode
133: .getChildren()));
134: } else if (element instanceof EntityNode
135: && ((EntityNode) element).isTable()) {
136: allSelectedTables.add(element);
137: }
138: }
139: // The input of the viewer is matchedTables, that will be empty until a file is selected
140: this .tableViewer.setInput(matchedEntities);
141: this .tableViewer.getControl().setLayoutData(
142: new GridData(GridData.FILL_BOTH));
143:
144: Group group = new Group(composite, SWT.NONE);
145: Composite groupIn = new Composite(group, SWT.NONE);
146: groupIn.setLayout(new GridLayout(2, false));
147: groupIn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
148:
149: label = new Label(groupIn, SWT.NONE);
150: label.setText("End-of-Line characters (OS default): ");
151:
152: Text eolText = new Text(groupIn, SWT.BORDER);
153: eolText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
154: eolText.setText(QuantumUtil.unTrasposeEscape(this .eol));
155: eolText.addModifyListener(new ModifyListener() {
156: public void modifyText(ModifyEvent event) {
157: setEol(((Text) event.getSource()).getText());
158: updateState();
159: }
160: });
161:
162: new Label(groupIn, SWT.NONE).setText("Column separator:");
163:
164: final Text columnSeparatorText = new Text(groupIn, SWT.BORDER);
165: columnSeparatorText.setText(",");
166: columnSeparatorText.setTextLimit(1);
167: columnSeparatorText.setLayoutData(new GridData(
168: GridData.FILL_HORIZONTAL));
169: columnSeparatorText.addModifyListener(new ModifyListener() {
170: public void modifyText(ModifyEvent event) {
171: String columnSeparatorStr = ((Text) event.getSource())
172: .getText();
173: // Warn the user that the selected column separator is to be only one char
174: // as setTextLimit is set to 1, only will set red when length == 0
175: if (columnSeparatorStr.length() != 1) {
176: ((Text) event.getSource())
177: .setBackground(parentf.getDisplay()
178: .getSystemColor(SWT.COLOR_RED));
179: } else {
180: ((Text) event.getSource()).setBackground(parentf
181: .getDisplay().getSystemColor(
182: SWT.COLOR_WHITE));
183: }
184: if (columnSeparatorStr.length() > 0)
185: columnSeparator = columnSeparatorStr.charAt(0);
186: else
187: columnSeparator = ',';
188: }
189: });
190:
191: group.setText(MessageUtil.getString(ImportCSVDetailsPage.class,
192: "options"));
193: group.setLayout(new GridLayout(1, false));
194: group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
195:
196: Button headerRow = new Button(group, SWT.CHECK);
197: ignoreHeaderRowButton = new Button(group, SWT.CHECK);
198: changeButtonActive(hasHeaderRow, ignoreHeaderRowButton);
199:
200: Button deletePreviousContentButton = new Button(group,
201: SWT.CHECK);
202:
203: headerRow.setText(MessageUtil
204: .getString(getClass(), "headerRow"));
205: headerRow.setSelection(hasHeaderRow);
206: headerRow.setLayoutData(new GridData(
207: GridData.HORIZONTAL_ALIGN_BEGINNING));
208: headerRow.addSelectionListener(new SelectionAdapter() {
209: public void widgetSelected(SelectionEvent event) {
210: hasHeaderRow = ((Button) event.getSource())
211: .getSelection();
212: changeButtonActive(hasHeaderRow, ignoreHeaderRowButton);
213: }
214: });
215: ignoreHeaderRowButton.setText(MessageUtil.getString(getClass(),
216: "ignoreHeaderRowButton"));
217: ignoreHeaderRowButton.setSelection(ignoreHeaderRow);
218: ignoreHeaderRowButton.setLayoutData(new GridData(
219: GridData.HORIZONTAL_ALIGN_BEGINNING));
220: ignoreHeaderRowButton
221: .addSelectionListener(new SelectionAdapter() {
222: public void widgetSelected(SelectionEvent event) {
223: ignoreHeaderRow = ((Button) event.getSource())
224: .getSelection();
225: }
226: });
227: deletePreviousContentButton.setText(MessageUtil.getString(
228: getClass(), "deletePreviousContentButton"));
229: deletePreviousContentButton.setSelection(deletePreviousContent);
230: deletePreviousContentButton.setLayoutData(new GridData(
231: GridData.HORIZONTAL_ALIGN_BEGINNING));
232: deletePreviousContentButton
233: .addSelectionListener(new SelectionAdapter() {
234: public void widgetSelected(SelectionEvent event) {
235: deletePreviousContent = ((Button) event
236: .getSource()).getSelection();
237: if (deletePreviousContent) {
238: MessageDialog
239: .openInformation(getShell(),
240: "Important Warning",
241: "This will delete ALL your data in the imported tables. Think 'bout it.");
242: }
243: }
244: });
245:
246: Label blankArea = new Label(composite, SWT.NONE);
247: blankArea.setText("");
248:
249: createDestinationArea(composite);
250:
251: setControl(composite);
252: updateState();
253: }
254:
255: /**
256: * @param button
257: */
258: private void changeButtonActive(boolean value, Button button) {
259: if (!value) {
260: button.setSelection(false);
261: button.setEnabled(false);
262: } else {
263: button.setEnabled(true);
264: }
265: }
266:
267: private void createDestinationArea(Composite composite) {
268: Composite fileArea = new Composite(composite, SWT.NULL);
269: fileArea.setLayout(new GridLayout(3, false));
270: fileArea.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
271: | GridData.VERTICAL_ALIGN_BEGINNING));
272: Label label = new Label(fileArea, SWT.NONE);
273: label.setText(MessageUtil.getString(ImportCSVDetailsPage.class,
274: "fileName"));
275:
276: final Text fileNameText = new Text(fileArea, SWT.BORDER);
277: fileNameText.setLayoutData(new GridData(
278: GridData.FILL_HORIZONTAL));
279: fileNameText.addModifyListener(new ModifyListener() {
280: public void modifyText(ModifyEvent event) {
281: String filename = ((Text) event.getSource()).getText();
282: setFileName(filename);
283: updateState();
284: }
285: });
286:
287: Button button = new Button(fileArea, SWT.NONE);
288: button.setText(MessageUtil.getString(
289: ImportCSVDetailsPage.class, "browse"));
290: button.addSelectionListener(new SelectionAdapter() {
291: public void widgetSelected(SelectionEvent event) {
292: FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
293: String extension = "*.zip;*.csv";
294: dialog.setFilterExtensions(new String[] { extension });
295: dialog.setFilterNames(new String[] { MessageUtil
296: .getString(ImportCSVDetailsPage.class,
297: "zip+csvFiles") });
298: String filename = dialog.open();
299: if (filename != null) {
300: fileNameText.setText(filename);
301: setFileName(filename);
302: updateState();
303: }
304: }
305: });
306: }
307:
308: protected void updateState() {
309: boolean pageComplete = (this .fileName != null && !(new File(
310: this .fileName)).isDirectory());
311: Vector fileNames = new Vector();
312: // If there is a file name
313: if (pageComplete) {
314: // Get the extension to know if we are treating a zip file or a csv file
315: String extension = "";
316: int lastPoint = this .fileName.lastIndexOf('.');
317: if (lastPoint >= 0 && lastPoint < this .fileName.length())
318: extension = this .fileName.substring(lastPoint + 1);
319: pageComplete &= (extension.equals("zip") || extension
320: .equals("csv"));
321: // If it's a zip file, fill the fileNames Vector with the names of all the entries in it, minus extension
322: if (extension.equals("zip")) {
323: isZip = true;
324: try {
325: ZipFile zipFile = new ZipFile(this .fileName);
326: Enumeration entries = zipFile.entries();
327: while (entries.hasMoreElements()) {
328: ZipEntry zipEntry = (ZipEntry) entries
329: .nextElement();
330: fileNames
331: .add(stripExtension(zipEntry.getName()));
332: }
333: zipFile.close();
334: } catch (IOException e) {
335: // TODO Auto-generated catch block
336: e.printStackTrace();
337: }
338: } else { // If not a zip, add only the name of the csv file, if really a csv
339: isZip = false;
340: if (extension.equals("csv"))
341: fileNames.add(stripExtension(this .fileName));
342: }
343: matchedEntities.clear();
344: // Match the fileNames and the allSelectedTables to see which names are equal, and should be imported
345:
346: // If we have one selected table and only one filename, we suppose the user knows what it's doing
347: if (fileNames.size() == 1 && allSelectedTables.size() == 1) {
348: matchedEntities.addAll(allSelectedTables);
349: } else if (fileNames.size() > 0) {
350: for (Iterator iter = fileNames.iterator(); iter
351: .hasNext();) {
352: String fileName = (String) iter.next();
353: for (Iterator iterator = this .allSelectedTables
354: .iterator(); iterator.hasNext();) {
355: EntityNode element = (EntityNode) iterator
356: .next();
357: if (element.getName().equals(fileName))
358: matchedEntities.add(element);
359: }
360: }
361: }
362: this .tableViewer.refresh();
363: }
364: setPageComplete(pageComplete);
365:
366: }
367:
368: /**
369: * @param name
370: * @return
371: */
372: private String stripExtension(String name) {
373: int lastPoint = name.lastIndexOf('.');
374: if (lastPoint >= 0)
375: return name.substring(0, lastPoint);
376: else
377: return name;
378: }
379:
380: /**
381: * @return Returns the deletePreviousContent.
382: */
383: public boolean deletePreviousContent() {
384: return deletePreviousContent;
385: }
386:
387: /**
388: * @param deletePreviousContent The deletePreviousContent to set.
389: */
390: public void setDeletePreviousContent(boolean deletePreviousContent) {
391: this .deletePreviousContent = deletePreviousContent;
392: }
393:
394: /**
395: * @return Returns the ignoreHeaderRow.
396: */
397: public boolean ignoreHeaderRow() {
398: return ignoreHeaderRow;
399: }
400:
401: /**
402: * @param ignoreHeaderRow The ignoreHeaderRow to set.
403: */
404: public void setIgnoreHeaderRow(boolean ignoreHeaderRow) {
405: this .ignoreHeaderRow = ignoreHeaderRow;
406: }
407:
408: public String getFileName() {
409: return this .fileName;
410: }
411:
412: protected void setFileName(String fileName) {
413: this .fileName = fileName;
414: }
415:
416: /**
417: * @return Returns the writeHeaderRow.
418: */
419: public boolean hasHeaderRow() {
420: return hasHeaderRow;
421: }
422:
423: /**
424: * @param writeHeaderRow The writeHeaderRow to set.
425: */
426: public void setWriteHeaderRow(boolean writeHeaderRow) {
427: this .hasHeaderRow = writeHeaderRow;
428: }
429:
430: /**
431: * @return Returns if the selected file is a zip file or not.
432: */
433: public boolean isZip() {
434: return isZip;
435: }
436:
437: /**
438: * @return Returns the matched Entities.
439: */
440: public Vector getMatchedEntities() {
441: return matchedEntities;
442: }
443:
444: public void setEol(String eol) {
445: this .eol = eol;
446: }
447:
448: /**
449: * @return Returns the eol.
450: */
451: public String getEol() {
452: return eol;
453: }
454:
455: public char getColumnSeparator() {
456: return columnSeparator;
457: }
458:
459: }
|