001: package net.refractions.udig.catalog.internal.ui.actions;
002:
003: import java.net.URL;
004: import java.util.ArrayList;
005: import java.util.Iterator;
006: import java.util.List;
007: import java.util.Map;
008:
009: import net.refractions.udig.catalog.CatalogPlugin;
010: import net.refractions.udig.catalog.ICatalog;
011: import net.refractions.udig.catalog.IService;
012: import net.refractions.udig.catalog.IServiceFactory;
013: import net.refractions.udig.catalog.IServiceInfo;
014: import net.refractions.udig.catalog.ui.CatalogUIPlugin;
015: import net.refractions.udig.ui.PlatformGIS;
016:
017: import org.eclipse.core.runtime.IProgressMonitor;
018: import org.eclipse.core.runtime.ISafeRunnable;
019: import org.eclipse.jface.action.IAction;
020: import org.eclipse.jface.viewers.ISelection;
021: import org.eclipse.jface.viewers.IStructuredSelection;
022: import org.eclipse.ui.IWorkbenchPart;
023: import org.eclipse.ui.actions.ActionDelegate;
024:
025: /**
026: * Resets a selection of services
027: *
028: * @author jgarnett
029: * @since 0.8
030: */
031: public class ResetService extends ActionDelegate {
032:
033: IStructuredSelection current;
034:
035: public void run(IAction action) {
036: if ((current == null)) {
037: return;
038: }
039: PlatformGIS.run(new ISafeRunnable() {
040:
041: public void handleException(Throwable exception) {
042: CatalogUIPlugin.log(
043: "Error resetting: " + current, exception); //$NON-NLS-1$
044: }
045:
046: public void run() throws Exception {
047: List<IService> servers = new ArrayList<IService>();
048: for (Iterator selection = current.iterator(); selection
049: .hasNext();) {
050: try {
051: servers.add((IService) selection.next());
052: } catch (ClassCastException huh) {
053: CatalogUIPlugin
054: .trace("Should not happen: " + huh); //$NON-NLS-1$
055: }
056: }
057: reset(servers, null);
058: }
059:
060: });
061: }
062:
063: public static void reset(List<IService> servers,
064: IProgressMonitor monitor) {
065: IServiceFactory locator = CatalogPlugin.getDefault()
066: .getServiceFactory();
067: ICatalog connections = CatalogPlugin.getDefault()
068: .getLocalCatalog();
069:
070: for (IService original : servers) {
071: try {
072: final URL ID = original.getIdentifier();
073: CatalogUIPlugin
074: .trace("Reset service " + original.getIdentifier()); //$NON-NLS-1$
075:
076: Map<java.lang.String, java.io.Serializable> connectionParams = original
077: .getConnectionParams();
078:
079: IService replacement = null; // unknown
080: TEST: for (IService candidate : locator.acquire(ID,
081: connectionParams)) {
082: try {
083: CatalogUIPlugin.trace(ID + " : connecting"); //$NON-NLS-1$
084: IServiceInfo info = candidate.getInfo(monitor);
085: CatalogUIPlugin.trace(ID
086: + " : found " + info.getTitle()); //$NON-NLS-1$
087: replacement = candidate;
088: break TEST;
089: } catch (Throwable t) {
090: CatalogUIPlugin.trace(ID
091: + " : ... " + t.getLocalizedMessage()); //$NON-NLS-1$
092:
093: }
094: }
095: if (replacement == null) {
096: return; // not available - should update existing service to error?
097: }
098: connections.replace(ID, replacement);
099: } catch (Throwable failed) {
100: CatalogUIPlugin.log("Reset failed", failed); //$NON-NLS-1$
101: }
102: }
103: }
104:
105: /**
106: * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
107: * org.eclipse.jface.viewers.ISelection)
108: */
109: public void selectionChanged(IAction action, ISelection selection) {
110: if (!selection.isEmpty()
111: && selection instanceof IStructuredSelection) {
112: current = (IStructuredSelection) selection;
113: }
114: }
115:
116: /**
117: * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction,
118: * org.eclipse.ui.IWorkbenchPart)
119: */
120: public void setActivePart(IAction action, IWorkbenchPart targetPart) {
121: }
122: }
|