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