001: package com.quantum.dbunit.wizard;
002:
003: import java.io.File;
004: import java.util.List;
005:
006: import org.eclipse.jface.dialogs.MessageDialog;
007: import org.eclipse.jface.viewers.IStructuredContentProvider;
008: import org.eclipse.jface.viewers.StructuredSelection;
009: import org.eclipse.jface.viewers.TableViewer;
010: import org.eclipse.jface.viewers.Viewer;
011: import org.eclipse.jface.wizard.WizardPage;
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.events.ModifyEvent;
014: import org.eclipse.swt.events.ModifyListener;
015: import org.eclipse.swt.events.SelectionAdapter;
016: import org.eclipse.swt.events.SelectionEvent;
017: import org.eclipse.swt.layout.GridData;
018: import org.eclipse.swt.layout.GridLayout;
019: import org.eclipse.swt.widgets.Button;
020: import org.eclipse.swt.widgets.Composite;
021: import org.eclipse.swt.widgets.FileDialog;
022: import org.eclipse.swt.widgets.Group;
023: import org.eclipse.swt.widgets.Label;
024: import org.eclipse.swt.widgets.Text;
025:
026: import com.quantum.ImageStore;
027: import com.quantum.QuantumPlugin;
028: import com.quantum.dbunit.MessageUtil;
029: import com.quantum.model.Bookmark;
030: import com.quantum.model.Entity;
031: import com.quantum.view.bookmark.BookmarkView;
032: import com.quantum.view.bookmark.GroupNode;
033: import com.quantum.view.widget.SimpleLabelProvider;
034:
035: /**
036: * Page of the Import Wizard for dbUnit XML files
037: * @author BC Holmes
038: * @author Julen
039: */
040: public class ImportDbUnitDetailsPage extends WizardPage {
041:
042: public class ContentProviderImpl implements
043: IStructuredContentProvider {
044:
045: public Object[] getElements(Object inputElement) {
046: if (inputElement instanceof StructuredSelection) {
047: Object[] entities = ((StructuredSelection) inputElement)
048: .toArray();
049: return entities;
050: } else if (inputElement instanceof List) {
051: return ((List) inputElement).toArray();
052: } else {
053: return null;
054: }
055: }
056:
057: public void dispose() {
058: }
059:
060: public void inputChanged(Viewer viewer, Object oldInput,
061: Object newInput) {
062: }
063: }
064:
065: private String fileName;
066: private boolean deletePreviousContent = false; // If the previous content of the tables has to be deleted
067: private boolean namesAreQualified = false; // If the table names in the XML files are qualified
068: private TableViewer tableViewer;
069: private Bookmark bookmark = null;
070: private String schema = null;
071:
072: protected void refreshTable() {
073: }
074:
075: public void dispose() {
076: super .dispose();
077: }
078:
079: /**
080: * @param pageName
081: */
082: protected ImportDbUnitDetailsPage(String pageName) {
083: super (pageName);
084:
085: setTitle(MessageUtil.getString(ImportDbUnitDetailsPage.class,
086: "title"));
087: setDescription(MessageUtil.getString(
088: ImportDbUnitDetailsPage.class, "description"));
089: }
090:
091: public void createControl(Composite parent) {
092:
093: List selectedNodes = BookmarkView.getInstance().getSelection()
094: .toList();
095:
096: if (selectedNodes.size() > 0) {
097: Object node = selectedNodes.get(0);
098: if ((node instanceof GroupNode)
099: && ((GroupNode) node).getType().equals(
100: Entity.TABLE_TYPE)) {
101: bookmark = ((GroupNode) node).getBookmark();
102: schema = ((GroupNode) node).getSchema().getName();
103: }
104: }
105:
106: Composite composite = new Composite(parent, SWT.NONE);
107: composite.setLayout(new GridLayout(1, false));
108:
109: composite.setLayoutData(new GridData(GridData.FILL_BOTH));
110:
111: Label label = new Label(composite, SWT.NONE);
112: label.setText(MessageUtil.getString(
113: ImportDbUnitDetailsPage.class, "selectedEntities"));
114: label.setLayoutData(new GridData(
115: GridData.VERTICAL_ALIGN_BEGINNING));
116:
117: this .tableViewer = new TableViewer(composite);
118: this .tableViewer.setLabelProvider(new SimpleLabelProvider(
119: ImageStore.getImage(ImageStore.GROUP, QuantumPlugin
120: .getDefault())));
121: this .tableViewer.setContentProvider(new ContentProviderImpl());
122: this .tableViewer.setInput(selectedNodes);
123:
124: this .tableViewer.getControl().setLayoutData(
125: new GridData(GridData.FILL_BOTH));
126:
127: Group group = new Group(composite, SWT.NONE);
128: group.setText(MessageUtil.getString(
129: ImportDbUnitDetailsPage.class, "options"));
130: group.setLayout(new GridLayout(1, false));
131: group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
132:
133: Button deletePreviousContentButton = new Button(group,
134: SWT.CHECK);
135:
136: deletePreviousContentButton.setText(MessageUtil.getString(
137: getClass(), "deletePreviousContentButton"));
138: deletePreviousContentButton.setSelection(deletePreviousContent);
139: deletePreviousContentButton.setLayoutData(new GridData(
140: GridData.VERTICAL_ALIGN_BEGINNING));
141: deletePreviousContentButton
142: .addSelectionListener(new SelectionAdapter() {
143: public void widgetSelected(SelectionEvent event) {
144: deletePreviousContent = ((Button) event
145: .getSource()).getSelection();
146: if (deletePreviousContent) {
147: MessageDialog
148: .openInformation(getShell(),
149: "Important Warning",
150: "This will delete ALL your data in the imported tables. Think 'bout it.");
151: }
152: }
153: });
154:
155: Button namesAreQualifiedButton = new Button(group, SWT.CHECK);
156:
157: namesAreQualifiedButton.setText(MessageUtil.getString(
158: getClass(), "namesAreQualifiedButton"));
159: namesAreQualifiedButton.setSelection(namesAreQualified);
160: namesAreQualifiedButton.setLayoutData(new GridData(
161: GridData.VERTICAL_ALIGN_BEGINNING));
162: namesAreQualifiedButton
163: .addSelectionListener(new SelectionAdapter() {
164: public void widgetSelected(SelectionEvent event) {
165: namesAreQualified = ((Button) event.getSource())
166: .getSelection();
167: }
168: });
169:
170: Label blankArea = new Label(composite, SWT.NONE);
171: blankArea.setText("");
172:
173: createDestinationArea(composite);
174:
175: setControl(composite);
176: updateState();
177: }
178:
179: private void createDestinationArea(Composite composite) {
180: Composite fileArea = new Composite(composite, SWT.NULL);
181: fileArea.setLayout(new GridLayout(3, false));
182: fileArea.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
183: | GridData.VERTICAL_ALIGN_BEGINNING));
184: Label label = new Label(fileArea, SWT.NONE);
185: label.setText(MessageUtil.getString(
186: ImportDbUnitDetailsPage.class, "fileName"));
187:
188: final Text fileNameText = new Text(fileArea, SWT.BORDER);
189: fileNameText.setLayoutData(new GridData(
190: GridData.FILL_HORIZONTAL));
191: fileNameText.addModifyListener(new ModifyListener() {
192: public void modifyText(ModifyEvent event) {
193: String filename = ((Text) event.getSource()).getText();
194: setFileName(filename);
195: updateState();
196: }
197: });
198:
199: Button button = new Button(fileArea, SWT.NONE);
200: button.setText(MessageUtil.getString(
201: ImportDbUnitDetailsPage.class, "browse"));
202: button.addSelectionListener(new SelectionAdapter() {
203: public void widgetSelected(SelectionEvent event) {
204: FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
205: String extension = "*.xml";
206: dialog.setFilterExtensions(new String[] { extension });
207: dialog.setFilterNames(new String[] { MessageUtil
208: .getString(ImportDbUnitDetailsPage.class,
209: "xmlFiles") });
210: String filename = dialog.open();
211: if (filename != null) {
212: fileNameText.setText(filename);
213: setFileName(filename);
214: updateState();
215: }
216: }
217: });
218: }
219:
220: protected void updateState() {
221: boolean pageComplete = (this .fileName != null && !(new File(
222: this .fileName)).isDirectory());
223: setPageComplete(pageComplete);
224:
225: }
226:
227: /**
228: * @return Returns the deletePreviousContent.
229: */
230: public boolean isDeletePreviousContent() {
231: return deletePreviousContent;
232: }
233:
234: public String getFileName() {
235: return this .fileName;
236: }
237:
238: protected void setFileName(String fileName) {
239: this .fileName = fileName;
240: }
241:
242: /**
243: * @return Returns the bookmark.
244: */
245: public Bookmark getBookmark() {
246: return bookmark;
247: }
248:
249: /**
250: * @return Returns the schema.
251: */
252: public String getSchema() {
253: return schema;
254: }
255:
256: public boolean isNamesAreQualified() {
257: return namesAreQualified;
258: }
259:
260: public void setNamesAreQualified(boolean namesAreQualified) {
261: this.namesAreQualified = namesAreQualified;
262: }
263:
264: }
|