001: package com.quantum.flatfiles.wizard;
002:
003: import java.beans.PropertyChangeEvent;
004: import java.beans.PropertyChangeListener;
005: import java.io.InputStream;
006:
007: import org.eclipse.jface.viewers.StructuredSelection;
008: import org.eclipse.swt.SWT;
009: import org.eclipse.swt.events.SelectionAdapter;
010: import org.eclipse.swt.events.SelectionEvent;
011: import org.eclipse.swt.layout.GridData;
012: import org.eclipse.swt.layout.GridLayout;
013: import org.eclipse.swt.widgets.Button;
014: import org.eclipse.swt.widgets.Composite;
015: import org.eclipse.swt.widgets.Control;
016: import org.eclipse.swt.widgets.Label;
017: import org.eclipse.swt.widgets.Text;
018: import org.eclipse.swt.widgets.Tree;
019: import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
020:
021: import com.quantum.flatfiles.MessageUtil;
022: import com.quantum.view.widget.ErrorMessageDisplayer;
023: import com.quantum.view.widget.FileSelectionWidget;
024:
025: /**
026: * @author BC Holmes
027: */
028: public class ExportDDLWizardFileSelectionPage extends
029: WizardNewFileCreationPage implements ErrorMessageDisplayer,
030: PropertyChangeListener {
031:
032: private Tree resourceTree;
033: private Text resourceName;
034: private Text workspaceFileName;
035:
036: private final InputStreamProvider inputStreamProvider;
037: private FileSelectionWidget fileSelection;
038: private boolean saveInFileSystem = true;
039:
040: /**
041: * Creates a new wizard page.
042: *
043: * @param pageName the name of the page
044: */
045: public ExportDDLWizardFileSelectionPage(String pageName,
046: InputStreamProvider inputStreamProvider) {
047: super (pageName, new StructuredSelection());
048: this .inputStreamProvider = inputStreamProvider;
049: setFileName("create.sql");
050: setTitle(MessageUtil.getString(getClass(), "title"));
051: setDescription(MessageUtil.getString(getClass(), "description"));
052: }
053:
054: public void createControl(Composite parent) {
055: Composite topLevel1 = new Composite(parent, SWT.NONE);
056: topLevel1.setLayout(new GridLayout(2, false));
057: topLevel1.setLayoutData(new GridData(
058: GridData.VERTICAL_ALIGN_FILL
059: | GridData.HORIZONTAL_ALIGN_FILL));
060: Button fileSystemRadioButton = new Button(topLevel1, SWT.RADIO);
061: GridData gridData = new GridData();
062: gridData.horizontalSpan = 2;
063: fileSystemRadioButton.setLayoutData(gridData);
064: fileSystemRadioButton.setText(MessageUtil.getString(getClass(),
065: "saveInFileSystem"));
066: fileSystemRadioButton
067: .addSelectionListener(new SelectionAdapter() {
068: public void widgetSelected(SelectionEvent event) {
069: Button button = (Button) event.getSource();
070: changeOptions(button.getSelection());
071: }
072: });
073: Label label = new Label(topLevel1, SWT.NONE);
074: label.setText("");
075:
076: this .fileSelection = new FileSelectionWidget(topLevel1,
077: SWT.NONE, this );
078:
079: Button workspaceRadioButton = new Button(topLevel1, SWT.RADIO);
080: gridData = new GridData();
081: gridData.horizontalSpan = 2;
082: workspaceRadioButton.setLayoutData(gridData);
083: workspaceRadioButton.setText(MessageUtil.getString(getClass(),
084: "saveInWorkspace"));
085: workspaceRadioButton
086: .addSelectionListener(new SelectionAdapter() {
087: public void widgetSelected(SelectionEvent event) {
088: Button button = (Button) event.getSource();
089: changeOptions(!button.getSelection());
090: }
091: });
092:
093: label = new Label(topLevel1, SWT.NONE);
094: label.setText("");
095: Composite composite = new Composite(topLevel1, SWT.NONE);
096: composite.setLayout(new GridLayout());
097: composite.setLayoutData(new GridData(GridData.FILL_BOTH));
098: super .createControl(composite);
099:
100: Composite control = (Composite) getControl();
101: findParts(control);
102:
103: setControl(topLevel1);
104:
105: fileSystemRadioButton.setSelection(true);
106: changeOptions(true);
107: this .fileSelection.addPropertyChangeListener(this );
108: }
109:
110: /**
111: * I worry that this approach to finding the sub-components is fragile, but the
112: * Eclipse API doesn't really give me a better alternative.
113: * @param composite
114: */
115: private void findParts(Composite composite) {
116: Control[] controls = composite.getChildren();
117: for (int i = 0, length = (controls == null ? 0
118: : controls.length); i < length; i++) {
119: if (controls[i] instanceof Tree) {
120: this .resourceTree = (Tree) controls[i];
121: } else if (controls[i] instanceof Composite) {
122: findParts((Composite) controls[i]);
123: } else if (controls[i] instanceof Text
124: && this .resourceName != null) {
125: this .workspaceFileName = (Text) controls[i];
126: } else if (controls[i] instanceof Text
127: && this .resourceName == null) {
128: this .resourceName = (Text) controls[i];
129: } else if (controls[i] instanceof Button) {
130: Button button = (Button) controls[i];
131: button.setVisible(false);
132: button.setEnabled(false);
133: }
134: }
135: }
136:
137: private void changeOptions(boolean fileSystem) {
138: if (this .resourceName != null) {
139: this .resourceName.setEnabled(!fileSystem);
140: }
141: if (this .resourceTree != null) {
142: this .resourceTree.setEnabled(!fileSystem);
143: }
144: if (this .workspaceFileName != null) {
145: this .workspaceFileName.setEnabled(!fileSystem);
146: }
147:
148: this .fileSelection.setEnabled(fileSystem);
149: if (fileSystem) {
150: setPageComplete(this .fileSelection.getFile() != null);
151: } else {
152: setPageComplete(super .validatePage());
153: }
154: this .saveInFileSystem = fileSystem;
155: }
156:
157: public void dispose() {
158: super .dispose();
159: this .fileSelection.removePropertyChangeListener(this );
160: }
161:
162: /**
163: * Returns a stream containing the initial contents to be given to new file resource
164: * instances. <b>Subclasses</b> may wish to override. This default implementation
165: * provides no initial contents.
166: *
167: * @return initial contents to be given to new file resource instances
168: */
169: protected InputStream getInitialContents() {
170: return this .inputStreamProvider.getInputStream();
171: }
172:
173: public void propertyChange(PropertyChangeEvent event) {
174: if ("file".equals(event.getPropertyName())) {
175: setPageComplete(event.getNewValue() != null);
176: }
177: }
178:
179: public Object createOutputFile() {
180: if (this.saveInFileSystem) {
181: return this.fileSelection.createFile(getInitialContents());
182: } else {
183: return createNewFile();
184: }
185: }
186: }
|