001: package net.refractions.udig.catalog.internal.ui;
002:
003: import java.io.Serializable;
004: import java.net.UnknownHostException;
005: import java.util.Iterator;
006: import java.util.Map;
007: import java.util.Map.Entry;
008:
009: import net.refractions.udig.catalog.IService;
010: import net.refractions.udig.catalog.ui.UDIGConnectionPage;
011: import net.refractions.udig.catalog.ui.internal.Messages;
012: import net.refractions.udig.catalog.ui.workflow.ConnectionState;
013: import net.refractions.udig.catalog.ui.workflow.WorkflowWizardDialog;
014: import net.refractions.udig.catalog.ui.workflow.WorkflowWizardPage;
015: import net.refractions.udig.catalog.ui.workflow.Workflow.Listener;
016: import net.refractions.udig.catalog.ui.workflow.Workflow.State;
017: import net.refractions.udig.ui.PlatformGIS;
018:
019: import org.eclipse.jface.wizard.IWizard;
020: import org.eclipse.jface.wizard.IWizardPage;
021: import org.eclipse.swt.graphics.Image;
022: import org.eclipse.swt.widgets.Composite;
023: import org.eclipse.swt.widgets.Display;
024: import org.geotools.data.DataSourceException;
025:
026: public class ConnectionPage extends WorkflowWizardPage implements
027: UDIGConnectionPage, Listener {
028:
029: /** underlying import page **/
030: UDIGConnectionPage page;
031:
032: public ConnectionPage() {
033: super ("connection"); //set name later //$NON-NLS-1$
034: }
035:
036: /**
037: * @return The decorated page.
038: *
039: */
040: public UDIGConnectionPage getDecoratedPage() {
041: return page;
042: }
043:
044: public Map<String, Serializable> getParams() {
045: return page.getParams();
046: }
047:
048: @Override
049: public void setState(State state) {
050: super .setState(state);
051:
052: //create the specific connection page
053: page = ((ConnectionState) state).getConnectionFactory()
054: .createConnectionPage();
055:
056: //do some setup
057: page.setWizard(getWizard());
058:
059: //we do the instance check to allow the connection page to
060: // optionally extend DataPipelinePage.
061: if (page instanceof WorkflowWizardPage) {
062: ((WorkflowWizardPage) page).setState(state);
063: }
064:
065: //add a listener to the workflow to determine when the workflow
066: // moves back, when this happens, we need to forget about the
067: // decorated page
068: state.getWorkflow().addListener(this );
069:
070: }
071:
072: @Override
073: public void shown() {
074: super .shown();
075:
076: if (page != null && page instanceof WorkflowWizardPage) {
077: ((WorkflowWizardPage) page).shown();
078: }
079: }
080:
081: public void createControl(Composite parent) {
082: page.createControl(parent);
083:
084: setControl(page.getControl());
085:
086: }
087:
088: @Override
089: public boolean isPageComplete() {
090: boolean complete = page.isPageComplete();
091: if (complete) {
092: //set some context for the connection state
093: ConnectionState state = (ConnectionState) getState();
094: state.setParams(page.getParams());
095: }
096:
097: return complete;
098: }
099:
100: @Override
101: public String getName() {
102: return page.getName();
103: }
104:
105: @Override
106: public Image getImage() {
107: return page.getImage();
108: }
109:
110: @Override
111: public String getDescription() {
112: return page.getDescription();
113: }
114:
115: @Override
116: public String getTitle() {
117: return page.getTitle();
118: }
119:
120: @Override
121: public String getMessage() {
122: return page.getMessage();
123: }
124:
125: @Override
126: public IWizardPage getNextPage() {
127: IWizardPage newPage = this .page.getNextPage();
128: return newPage;
129: }
130:
131: @Override
132: public void setWizard(IWizard newWizard) {
133: super .setWizard(newWizard);
134: if (page != null)
135: page.setWizard(newWizard);
136: }
137:
138: @Override
139: public void setVisible(boolean visible) {
140: if (getControl() != null)
141: super .setVisible(visible);
142: }
143:
144: public void forward(State current, State prev) {
145: // TODO Auto-generated method stub
146:
147: }
148:
149: public void backward(State current, State next) {
150: if (next != null && next instanceof ConnectionState) {
151: //reset decorated page
152: page = null;
153:
154: getControl().getDisplay().syncExec(new Runnable() {
155: public void run() {
156: getControl().dispose();
157: }
158: });
159:
160: setControl(null);
161: }
162: }
163:
164: public void statePassed(State state) {
165: PlatformGIS.syncInDisplayThread(new Runnable() {
166: public void run() {
167: setErrorMessage(null);
168: }
169: });
170: }
171:
172: public void stateFailed(State state) {
173: if (state instanceof ConnectionState) {
174: ConnectionState connectionState = (ConnectionState) state;
175: Map<IService, Throwable> errors = connectionState
176: .getErrors();
177: Iterator<Entry<IService, Throwable>> iterator = errors
178: .entrySet().iterator();
179: if (iterator.hasNext()) {
180: Entry<IService, Throwable> entry = iterator.next();
181: Throwable t = entry.getValue();
182: final String message = formatException(entry.getKey(),
183: t);
184: if (Display.getCurrent() == null) {
185: Display.getDefault().asyncExec(new Runnable() {
186: public void run() {
187: page.setErrorMessage(message);
188: }
189: });
190: } else
191: page.setErrorMessage(message);
192: }
193: }
194: }
195:
196: private String formatException(IService key, Throwable t) {
197: if (t instanceof UnknownHostException) {
198: return key.getIdentifier().getHost()
199: + Messages.ConnectionPage_illegalHost;
200: }
201: if (t instanceof DataSourceException) {
202: String message = t.getMessage();
203: if (message.contains("user") && message.contains("does not exist")) //$NON-NLS-1$ //$NON-NLS-2$
204: return Messages.ConnectionPage_badUsername;
205: if (message.contains("password")) //$NON-NLS-1$
206: return Messages.ConnectionPage_badPassword;
207: }
208: return Messages.ConnectionPage_genericError;
209: }
210:
211: public void started(State first) {
212:
213: }
214:
215: public void finished(State last) {
216: if (!(getContainer() instanceof WorkflowWizardDialog)
217: && getContainer() != null) {
218: getWizard().performFinish();
219: }
220: }
221:
222: }
|