01: package net.refractions.udig.catalog.ui.workflow;
02:
03: import java.io.IOException;
04: import java.util.Collection;
05:
06: import net.refractions.udig.catalog.CatalogPlugin;
07: import net.refractions.udig.catalog.ui.ConnectionFactoryManager;
08: import net.refractions.udig.catalog.ui.UDIGConnectionFactory;
09: import net.refractions.udig.catalog.ui.UDIGConnectionFactoryDescriptor;
10: import net.refractions.udig.catalog.ui.internal.Messages;
11: import net.refractions.udig.catalog.ui.workflow.Workflow.State;
12:
13: import org.eclipse.core.runtime.IProgressMonitor;
14:
15: /**
16: * First state in the data import worklow. This state chooses a data source based on the context of
17: * the workflow.
18: *
19: * @author Justin Deoliveira,Refractions Research Inc.,jdeolive@refractions.net
20: */
21: public class DataSourceSelectionState extends Workflow.State {
22:
23: /** the chosen import page * */
24: UDIGConnectionFactoryDescriptor descriptor;
25: private boolean validateService;
26:
27: /**
28: * Create new Instance
29: * @param validateServices indicates whether the service should be probed for its members and metadata.
30: */
31: public DataSourceSelectionState(boolean validateServices) {
32: this .validateService = validateServices;
33: }
34:
35: @Override
36: public void init(IProgressMonitor monitor) throws IOException {
37: super .init(monitor);
38:
39: // based on context, try to choose a single data source
40: Object context = getWorkflow().getContext();
41: if (context == null)
42: return;
43:
44: Collection<UDIGConnectionFactoryDescriptor> descriptors = ConnectionFactoryManager
45: .instance().getConnectionFactoryDescriptors();
46:
47: // determine if any conntection factory can process the context object
48: descriptor = null;
49: for (UDIGConnectionFactoryDescriptor d : descriptors) {
50: UDIGConnectionFactory factory = d.getConnectionFactory();
51: try {
52: if (factory.canProcess(context)) {
53: // if we already have a descriptor, we have a conflict
54: if (descriptor != null) {
55: descriptor = null;
56: return;
57: }
58:
59: descriptor = d;
60: }
61: } catch (Throwable t) {
62: // log and keep going
63: CatalogPlugin.log(t.getLocalizedMessage(), t);
64: }
65:
66: }
67: }
68:
69: @Override
70: public boolean run(IProgressMonitor monitor) throws IOException {
71: monitor.beginTask(getName(), 1);
72: monitor.done();
73: return descriptor != null;
74: }
75:
76: @Override
77: public boolean hasNext() {
78: return true;
79: }
80:
81: @Override
82: public State next() {
83: // move to connection state
84: return new ConnectionState(descriptor, validateService);
85: }
86:
87: public UDIGConnectionFactoryDescriptor getDescriptor() {
88: return descriptor;
89: }
90:
91: public void setDescriptor(UDIGConnectionFactoryDescriptor descriptor) {
92: this .descriptor = descriptor;
93: }
94:
95: @Override
96: public String getName() {
97: return Messages.DataSourceSelectionState_name;
98: }
99: }
|