/*******************************************************************************
 * Copyright (C) 2007 The University of Manchester   
 * 
 *  Modifications to the initial code base are copyright of their
 *  respective authors, or their employers as appropriate.
 * 
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2.1 of
 *  the License, or (at your option) any later version.
 *    
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *    
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 ******************************************************************************/
package net.sf.taverna.t2.activities.biomoby.servicedescriptions;

import java.util.Arrays;
import java.util.List;

import javax.swing.Icon;

import net.sf.taverna.t2.activities.biomoby.actions.BiomobyAdvancedMenuAction;
import net.sf.taverna.t2.activities.biomoby.query.BiomobyActivityIcon;
import net.sf.taverna.t2.activities.biomoby.query.BiomobyQueryHelper;
import net.sf.taverna.t2.activities.biomoby.ui.AddBiomobyDialogue;
import net.sf.taverna.t2.lang.observer.Observable;
import net.sf.taverna.t2.lang.observer.Observer;
import net.sf.taverna.t2.servicedescriptions.AbstractConfigurableServiceProvider;
import net.sf.taverna.t2.servicedescriptions.CustomizedConfigurePanelProvider;
import net.sf.taverna.t2.servicedescriptions.ServiceDescriptionRegistry;
import net.sf.taverna.t2.servicedescriptions.events.RemovedProviderEvent;
import net.sf.taverna.t2.servicedescriptions.events.ServiceDescriptionRegistryEvent;
import net.sf.taverna.t2.servicedescriptions.impl.ServiceDescriptionRegistryImpl;

import org.biomoby.client.CentralImpl;
import org.biomoby.shared.MobyException;

public class BiomobyServiceProvider extends
		AbstractConfigurableServiceProvider<BiomobyServiceProviderConfig>
		implements
		CustomizedConfigurePanelProvider<BiomobyServiceProviderConfig> {

	public BiomobyServiceProvider() {
		super(new BiomobyServiceProviderConfig());
	}

	public void findServiceDescriptionsAsync(
			FindServiceDescriptionsCallBack callBack) {
		try {
			BiomobyQueryHelper helper = new BiomobyQueryHelper(
					getConfiguration().getEndpoint().toASCIIString(),
					getConfiguration().getNamespace().toASCIIString());
			helper.findServiceDescriptionsAsync(callBack);
		} catch (MobyException ex) {
			callBack.fail("Could not connect to Biomoby endpoint "
					+ getConfiguration().getEndpoint(), ex);
		}
		
		
		BiomobyAdvancedMenuAction.addRegistry(
				getConfiguration().getEndpoint().toASCIIString(),
				getConfiguration().getNamespace().toASCIIString());
		
		// here we add a listener for events regarding BiomobyServiceProvider
		// TODO should find a better place for this so that we dont have to call addRegistry without
		// listening for the add provider event
		ServiceDescriptionRegistry serviceReg = ServiceDescriptionRegistryImpl.getInstance();
		serviceReg.addObserver(new Observer<ServiceDescriptionRegistryEvent>(){
			public void notify(
					Observable<ServiceDescriptionRegistryEvent> subject,
					ServiceDescriptionRegistryEvent event) throws Exception {
				// check for removed provider events
				if (event instanceof RemovedProviderEvent) {
					if (((RemovedProviderEvent) event).getProvider() instanceof BiomobyServiceProvider) {
						BiomobyServiceProvider bsp = (BiomobyServiceProvider) ((RemovedProviderEvent) event).getProvider();
						// remove our menu item
						BiomobyAdvancedMenuAction.removeRegistry(
								bsp.getConfiguration().getEndpoint().toASCIIString(),
								bsp.getConfiguration().getNamespace().toASCIIString());
					}
				}
				
			}});
	}

	@Override
	public List<BiomobyServiceProviderConfig> getDefaultConfigurations() {
		return Arrays.asList(new BiomobyServiceProviderConfig(
				CentralImpl.DEFAULT_ENDPOINT,
				CentralImpl.DEFAULT_NAMESPACE));
	}

	public String getName() {
		return "Biomoby service";
	}

	public Icon getIcon() {
		return BiomobyActivityIcon.getBiomobyIcon();
	}

	@Override
	public String toString() {
		return getName() + " " + getConfiguration().getEndpoint();
	}

	@SuppressWarnings("serial")
	public void createCustomizedConfigurePanel(
			final CustomizedConfigureCallBack<BiomobyServiceProviderConfig> callBack) {
		AddBiomobyDialogue addBiomobyDialogue = new AddBiomobyDialogue() {
			@Override
			protected void addRegistry(String registryEndpoint,
					String registryURI) {
				BiomobyServiceProviderConfig providerConfig = new BiomobyServiceProviderConfig(
						registryEndpoint, registryURI);
				callBack.newProviderConfiguration(providerConfig);
			}
		};
		addBiomobyDialogue.setVisible(true);
	}

	@Override
	protected List<? extends Object> getIdentifyingData() {
		List<String> result;
		result = Arrays.asList(getConfiguration().getEndpoint().toString());
		return result;
	}

}

@@ -2548,16 +2548,42 @@
 ) %7B%0A
+%09%09boolean success = true;%0A
 %09%09try %7B%0A
 %09%09%09B
@@ -2578,16 +2578,16 @@
 %09%09try %7B%0A
-
 %09%09%09Biomo
@@ -2826,16 +2826,36 @@
 n ex) %7B%0A
+%09%09%09success = false;%0A
 %09%09%09callB
@@ -2956,21 +2956,31 @@
 ;%0A%09%09%7D%0A%09%09
+if (success)
 %0A%09%09
-%0A%09
 %09Biomoby
@@ -3007,24 +3007,25 @@
 ddRegistry(%0A
+%09
 %09%09%09%09getConfi
@@ -3066,24 +3066,25 @@
 ring(),%0A%09%09%09%09
+%09
 getConfigura
@@ -3125,16 +3125,17 @@
 g());%0A%09%09
+%09
 %0A%09%09// he

create a menu item only if a valid registry was added to the workbench