001: package net.refractions.udig.catalog.internal.ui;
002:
003: import java.net.URL;
004: import java.text.MessageFormat;
005: import java.util.Collection;
006: import java.util.HashMap;
007: import java.util.List;
008: import java.util.Map;
009:
010: import net.refractions.udig.catalog.CatalogPlugin;
011: import net.refractions.udig.catalog.ICatalog;
012: import net.refractions.udig.catalog.IGeoResource;
013: import net.refractions.udig.catalog.IResolve;
014: import net.refractions.udig.catalog.IService;
015: import net.refractions.udig.catalog.ui.CatalogTreeViewer;
016: import net.refractions.udig.catalog.ui.CatalogUIPlugin;
017: import net.refractions.udig.catalog.ui.ConnectionErrorPage;
018: import net.refractions.udig.catalog.ui.DataSourceSelectionPage;
019: import net.refractions.udig.catalog.ui.internal.Messages;
020: import net.refractions.udig.catalog.ui.workflow.ConnectionErrorState;
021: import net.refractions.udig.catalog.ui.workflow.ConnectionFailurePage;
022: import net.refractions.udig.catalog.ui.workflow.ConnectionFailureState;
023: import net.refractions.udig.catalog.ui.workflow.ConnectionState;
024: import net.refractions.udig.catalog.ui.workflow.DataSourceSelectionState;
025: import net.refractions.udig.catalog.ui.workflow.Workflow;
026: import net.refractions.udig.catalog.ui.workflow.WorkflowWizard;
027: import net.refractions.udig.catalog.ui.workflow.WorkflowWizardDialog;
028: import net.refractions.udig.catalog.ui.workflow.WorkflowWizardPage;
029: import net.refractions.udig.catalog.ui.workflow.Workflow.State;
030: import net.refractions.udig.ui.PlatformGIS;
031:
032: import org.eclipse.core.runtime.IProgressMonitor;
033: import org.eclipse.core.runtime.SubProgressMonitor;
034: import org.eclipse.jface.viewers.StructuredSelection;
035: import org.eclipse.swt.widgets.Display;
036: import org.eclipse.swt.widgets.Shell;
037: import org.eclipse.ui.IWorkbenchWindow;
038: import org.eclipse.ui.PartInitException;
039: import org.eclipse.ui.PlatformUI;
040:
041: public class CatalogImport {
042:
043: Shell shell;
044: private WorkflowWizardDialog dialog;
045: protected WorkflowWizard wizard;
046:
047: public CatalogImport() {
048: init();
049: }
050:
051: void init() {
052: Workflow workflow = createWorkflow();
053: Map<Class<? extends State>, WorkflowWizardPage> map = createPageMapping();
054: wizard = createWorkflowWizard(workflow, map);
055:
056: if (Display.getCurrent() == null) {
057: PlatformGIS.syncInDisplayThread(new Runnable() {
058: public void run() {
059: shell = createShell();
060: }
061: });
062: } else {
063: shell = createShell();
064: }
065:
066: dialog = new WorkflowWizardDialog(shell, wizard);
067: dialog.setBlockOnOpen(true);
068: }
069:
070: /**
071: * Must be called in the Display thread.
072: */
073: Shell createShell() {
074: Display d = PlatformUI.getWorkbench().getDisplay();
075: if (d == null)
076: d = Display.getCurrent();
077: return new Shell(d.getActiveShell());
078: }
079:
080: public WorkflowWizardDialog getDialog() {
081: return dialog;
082: }
083:
084: public void open() {
085: Display.getDefault().asyncExec(new Runnable() {
086: public void run() {
087: dialog.open();
088: };
089: });
090: }
091:
092: public void run(IProgressMonitor monitor, Object context) {
093: dialog.getWorkflowWizard().getWorkflow().setContext(context);
094: String bind = MessageFormat.format(
095: Messages.CatalogImport_monitor_task,
096: new Object[] { format(context) });
097: monitor.beginTask(bind, IProgressMonitor.UNKNOWN);
098: monitor.setTaskName(bind);
099: try {
100: dialog.runHeadless(new SubProgressMonitor(monitor, 100));
101: } finally {
102: monitor.done();
103: }
104: }
105:
106: private String format(Object data) {
107: if (data instanceof URL) {
108: return formatURL((URL) data);
109: }
110: if (data instanceof IGeoResource) {
111: return ((IGeoResource) data).getIdentifier().getRef();
112: }
113: if (data instanceof IResolve) {
114: return formatURL(((IResolve) data).getIdentifier());
115: }
116: return data.toString();
117: }
118:
119: private String formatURL(URL url) {
120: return url.getProtocol() + "://" + url.getPath(); //$NON-NLS-1$
121: }
122:
123: protected Workflow createWorkflow() {
124: DataSourceSelectionState state = new DataSourceSelectionState(
125: false);
126: Workflow workflow = new Workflow(new Workflow.State[] { state });
127:
128: return workflow;
129: }
130:
131: protected Map<Class<? extends State>, WorkflowWizardPage> createPageMapping() {
132: HashMap<Class<? extends State>, WorkflowWizardPage> map = new HashMap<Class<? extends State>, WorkflowWizardPage>();
133:
134: map.put(DataSourceSelectionState.class,
135: new DataSourceSelectionPage());
136: map.put(ConnectionState.class, new ConnectionPage());
137: map.put(ConnectionErrorState.class, new ConnectionErrorPage());
138: map.put(ConnectionFailureState.class,
139: new ConnectionFailurePage());
140: return map;
141: }
142:
143: protected WorkflowWizard createWorkflowWizard(Workflow workflow,
144: Map<Class<? extends State>, WorkflowWizardPage> map) {
145: return new CatalogImportWizard(workflow, map);
146: }
147:
148: public static class CatalogImportWizard extends WorkflowWizard {
149:
150: public CatalogImportWizard(Workflow workflow,
151: Map<Class<? extends State>, WorkflowWizardPage> map) {
152: super (workflow, map);
153: }
154:
155: @Override
156: protected boolean performFinish(IProgressMonitor monitor) {
157: //get the connection state from the pipe
158: ConnectionState connState = getWorkflow().getState(
159: ConnectionState.class);
160:
161: if (connState == null)
162: return false;
163:
164: //add the services to the catalog
165: final Collection<IService> services = connState
166: .getServices();
167: if (services == null || services.isEmpty())
168: return false;
169:
170: //add the services to the catalog
171: ICatalog catalog = CatalogPlugin.getDefault()
172: .getLocalCatalog();
173: for (IService service : services)
174: catalog.add(service);
175:
176: //select the first service
177: //TODO: this has threading issues
178: PlatformGIS.syncInDisplayThread(new Runnable() {
179: public void run() {
180: try {
181: CatalogView view = getCatalogView();
182: if (view != null) {
183: CatalogTreeViewer treeviewer = view
184: .getTreeviewer();
185: treeviewer
186: .setSelection(new StructuredSelection(
187: services.iterator().next()));
188: }
189: } catch (Exception e) {
190: CatalogUIPlugin.log(e.getLocalizedMessage(), e);
191: }
192: }
193: });
194:
195: return true;
196: }
197:
198: protected boolean isShowCatalogView() {
199: return true;
200: }
201:
202: protected CatalogView getCatalogView() throws PartInitException {
203: CatalogView view;
204: if (isShowCatalogView()) {
205: view = (CatalogView) PlatformUI.getWorkbench()
206: .getActiveWorkbenchWindow().getActivePage()
207: .showView(CatalogView.VIEW_ID);
208: } else {
209: view = (CatalogView) PlatformUI.getWorkbench()
210: .getActiveWorkbenchWindow().getActivePage()
211: .findView(CatalogView.VIEW_ID);
212: }
213: return view;
214: }
215: }
216: }
|