001: package com.quantum.flatfiles.wizard;
002:
003: import java.beans.PropertyChangeEvent;
004: import java.beans.PropertyChangeListener;
005: import java.sql.SQLException;
006:
007: import org.eclipse.jface.viewers.CheckStateChangedEvent;
008: import org.eclipse.jface.viewers.CheckboxTreeViewer;
009: import org.eclipse.jface.viewers.ITreeContentProvider;
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.SelectionAdapter;
014: import org.eclipse.swt.events.SelectionEvent;
015: import org.eclipse.swt.graphics.Image;
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:
021: import com.quantum.ImageStore;
022: import com.quantum.flatfiles.MessageUtil;
023: import com.quantum.model.Bookmark;
024: import com.quantum.model.DatabaseObject;
025: import com.quantum.model.Entity;
026: import com.quantum.model.Schema;
027: import com.quantum.model.Table;
028: import com.quantum.ui.dialog.ExceptionDisplayDialog;
029: import com.quantum.ui.dialog.Executable;
030: import com.quantum.ui.dialog.SQLExceptionDialog;
031: import com.quantum.ui.dialog.SQLInvocationUtil;
032: import com.quantum.util.connection.ConnectionException;
033: import com.quantum.view.widget.SimpleLabelProvider;
034:
035: /**
036: * @author BC Holmes
037: */
038: public class ExportDatabaseObjectsWizardPage extends WizardPage {
039:
040: public class ContentProvider implements ITreeContentProvider {
041:
042: private DatabaseObject[] objects;
043:
044: /**
045: * @param page
046: */
047: public Object[] getChildren(Object parentElement) {
048: Object[] result = null;
049: if (parentElement == null) {
050: } else if (parentElement instanceof DatabaseObject[]) {
051: result = this .objects = (DatabaseObject[]) parentElement;
052: } else if (parentElement instanceof Schema[]) {
053: result = (Object[]) parentElement;
054: } else if (parentElement instanceof Schema) {
055: final Schema schema = (Schema) parentElement;
056: if (this .objects == null) {
057: result = this .objects = (getBookmark() == null ? null
058: : (DatabaseObject[]) SQLInvocationUtil
059: .execute(getShell(), bookmark,
060: new Executable() {
061: public Object execute()
062: throws SQLException,
063: ConnectionException {
064: return getBookmark()
065: .getObjectsForSchema(
066: schema,
067: Entity.TABLE_TYPE);
068: }
069: }));
070: } else {
071: result = this .objects;
072: }
073: }
074: return result == null ? new Object[0] : result;
075: }
076:
077: public Object getParent(final Object element) {
078: if (element instanceof Schema) {
079: return null;
080: } else if (element instanceof Table) {
081: final Table table = (Table) element;
082: final Bookmark bookmark = table.getBookmark();
083: return SQLInvocationUtil.execute(getShell(), bookmark,
084: new Executable() {
085: public Object execute()
086: throws SQLException,
087: ConnectionException {
088: return getBookmark().getSchema(
089: table.getSchema());
090: }
091: });
092: } else {
093: return null;
094: }
095: }
096:
097: public boolean hasChildren(Object element) {
098: if (element instanceof Schema) {
099: return true;
100: } else {
101: return false;
102: }
103: }
104:
105: public Object[] getElements(Object inputElement) {
106: return getChildren(inputElement);
107: }
108:
109: public void dispose() {
110: }
111:
112: public void inputChanged(Viewer viewer, Object oldInput,
113: Object newInput) {
114: this .objects = null;
115: }
116: }
117:
118: class CheckStateListener extends SimpleCheckStateListener {
119: public CheckStateListener(CheckboxTreeViewer viewer) {
120: super (viewer);
121: }
122:
123: public void checkStateChanged(CheckStateChangedEvent event) {
124: super .checkStateChanged(event);
125: setPageComplete(ExportDatabaseObjectsWizardPage.this .treeViewer
126: .getCheckedElements().length > 0);
127: }
128: }
129:
130: public class LabelProvider extends SimpleLabelProvider {
131:
132: public String getText(Object element) {
133: if (element instanceof Table) {
134: return ((Table) element).getName();
135: } else {
136: return super .getText(element);
137: }
138: }
139:
140: public Image getImage(Object element) {
141: if (element instanceof Schema) {
142: return ImageStore.getImage(ImageStore.SCHEMA);
143: } else if (element instanceof Table) {
144: return ImageStore.getImage(ImageStore.TABLE);
145: } else {
146: return null;
147: }
148: }
149: }
150:
151: private Bookmark bookmark;
152: private CheckboxTreeViewer treeViewer;
153: private ContentProvider contentProvider = new ContentProvider();
154: private CheckStateListener checkStateListener;
155:
156: private PropertyChangeListener listener = new PropertyChangeListener() {
157: public void propertyChange(PropertyChangeEvent event) {
158: if ("connected".equals(event.getPropertyName())) {
159: setInput();
160: }
161: }
162: };
163:
164: public ExportDatabaseObjectsWizardPage(String pageName) {
165: super (pageName);
166: }
167:
168: public void createControl(Composite pageContainer) {
169:
170: Composite composite = new Composite(pageContainer, SWT.NULL);
171: composite.setLayout(new GridLayout());
172: composite.setLayoutData(new GridData(GridData.FILL_BOTH));
173:
174: this .treeViewer = new CheckboxTreeViewer(composite, SWT.CHECK
175: | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
176: this .treeViewer.setContentProvider(this .contentProvider);
177: this .treeViewer.setLabelProvider(new LabelProvider());
178: setInput();
179:
180: this .checkStateListener = new CheckStateListener(
181: this .treeViewer);
182: this .treeViewer.addCheckStateListener(this .checkStateListener);
183:
184: GridData data = new GridData(GridData.FILL_BOTH);
185: data.heightHint = 200;
186: data.widthHint = 400;
187: this .treeViewer.getControl().setLayoutData(data);
188:
189: Composite buttons = new Composite(composite, SWT.NULL);
190: buttons.setLayout(new GridLayout(2, false));
191: buttons.setLayoutData(new GridData());
192:
193: Button selectAll = new Button(buttons, SWT.NONE);
194: selectAll.setText(MessageUtil
195: .getString(getClass(), "selectAll"));
196: selectAll.addSelectionListener(new SelectionAdapter() {
197: public void widgetSelected(SelectionEvent event) {
198: selectAll();
199: }
200: });
201:
202: Button deselectAll = new Button(buttons, SWT.NONE);
203: deselectAll.setText(MessageUtil.getString(getClass(),
204: "deselectAll"));
205: deselectAll.addSelectionListener(new SelectionAdapter() {
206: public void widgetSelected(SelectionEvent event) {
207: deselectAll();
208: }
209: });
210:
211: setControl(composite);
212: }
213:
214: /**
215: *
216: */
217: protected void refreshTree() {
218: if (this .treeViewer != null && this .bookmark != null) {
219: this .treeViewer.refresh(this .bookmark);
220: }
221:
222: }
223:
224: protected void deselectAll() {
225: changeSelection(false);
226: }
227:
228: private void changeSelection(boolean selected) {
229: Object[] objects = (Object[]) this .treeViewer.getInput();
230: for (int i = 0, length = (objects == null ? 0 : objects.length); i < length; i++) {
231: this .treeViewer.setSubtreeChecked(objects[i], selected);
232: }
233: setPageComplete(this .treeViewer.getCheckedElements().length > 0);
234: }
235:
236: protected void selectAll() {
237: changeSelection(true);
238: }
239:
240: public Bookmark getBookmark() {
241: return this .bookmark;
242: }
243:
244: public void setBookmark(Bookmark bookmark) {
245: if (this .bookmark != null) {
246: this .bookmark.removePropertyChangeListener(this .listener);
247: }
248: this .bookmark = bookmark;
249: if (this .bookmark != null) {
250: this .bookmark.addPropertyChangeListener(this .listener);
251: }
252: setInput();
253: }
254:
255: public void dispose() {
256: super .dispose();
257: if (this .bookmark != null) {
258: this .bookmark.removePropertyChangeListener(this .listener);
259: }
260: if (this .treeViewer != null) {
261: this .treeViewer
262: .removeCheckStateListener(this .checkStateListener);
263: }
264: }
265:
266: private void setInput() {
267: try {
268: if (this .bookmark != null && this .bookmark.isConnected()) {
269: if (bookmark.getDatabase()
270: .supportsSchemasInTableDefinitions()) {
271: this .treeViewer.setInput(bookmark.getSchemas());
272: } else {
273: this .treeViewer.setInput(bookmark
274: .getObjectsForSchema(bookmark
275: .getDefaultSchema(),
276: Entity.TABLE_TYPE));
277: }
278: refreshTree();
279: selectAll();
280: }
281: } catch (ConnectionException e) {
282: if (e.getCause() != null
283: && e.getCause() instanceof SQLException) {
284: SQLExceptionDialog.openException(getShell(),
285: this .bookmark, (SQLException) e.getCause());
286: } else {
287: ExceptionDisplayDialog.openError(getShell(), null,
288: null, e);
289: }
290: } catch (SQLException e) {
291: SQLExceptionDialog.openException(getShell(), this .bookmark,
292: e);
293: }
294: }
295:
296: public Object[] getCheckedElements() {
297: return this.treeViewer == null ? null : this.treeViewer
298: .getCheckedElements();
299: }
300: }
|