Skip to main content
summaryrefslogtreecommitdiffstats
blob: ca3a3555260dfe30a3e652c445635cf60850dc70 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*******************************************************************************
 * Copyright (c) 2003, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
/*
 * Created on Jun 10, 2004
 */
package org.eclipse.jst.j2ee.internal.webservice;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider;
import org.eclipse.jst.j2ee.internal.webservice.helper.WebServicesManager;
import org.eclipse.jst.j2ee.webservice.wsclient.ComponentScopedRefs;
import org.eclipse.jst.j2ee.webservice.wsclient.Handler;
import org.eclipse.jst.j2ee.webservice.wsclient.ServiceRef;
import org.eclipse.jst.j2ee.webservice.wsdd.WebServiceDescription;
import org.eclipse.jst.j2ee.webservice.wsdd.WebServices;

/**
 * @author jlanuti
 */
public class WebServicesNavigatorSynchronizer extends AdapterFactoryContentProvider implements Adapter {


	private static WebServicesNavigatorSynchronizer INSTANCE = null;
	protected Notifier target = null;
	private WebServicesNavigatorContentProvider contentProvider = null;

	/**
	 * Constructor
	 */
	public WebServicesNavigatorSynchronizer(AdapterFactory adapterFactory, WebServicesNavigatorContentProvider provider) {
		super(adapterFactory);
		contentProvider = provider;
	}

	public static WebServicesNavigatorSynchronizer getInstance() {
		return INSTANCE;
	}

	public static WebServicesNavigatorSynchronizer createInstance(AdapterFactory adapterFactory, WebServicesNavigatorContentProvider provider) {
		INSTANCE = new WebServicesNavigatorSynchronizer(adapterFactory, provider);
		return INSTANCE;
	}

	public static void disposeInstance() {
		INSTANCE = null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.common.notify.Adapter#getTarget()
	 */
	public Notifier getTarget() {
		return target;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.common.notify.Adapter#isAdapterForType(java.lang.Object)
	 */
	public boolean isAdapterForType(Object type) {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.common.notify.Adapter#setTarget(org.eclipse.emf.common.notify.Notifier)
	 */
	public void setTarget(Notifier newTarget) {
		target = newTarget;
	}

	@Override
	public void notifyChanged(final Notification notification) {
		if (notification.isTouch()) {
			//There is nothing that is required since nothing changed.
			return;
		}
		
		EObject notifier = (EObject) notification.getNotifier();
		if (notifier instanceof WebServices) {
			//Do nothing.
		} else if (isServiceRefModification(notification)) {
			// Handle service ref edits
			contentProvider.getViewer().refresh(notifier);
		} else if (isServiceRefAddOrRemove(notification)) {
			//Handle service ref adds or removes
			contentProvider.getViewer().refresh();
		} else {
			// Handle web service changes by getting the WSDD parent
			while (!(notifier instanceof WebServiceDescription) && notifier != null) {
				notifier = notifier.eContainer();
			}
			// Refresh the associated wsdl service for the WSDD parent
			if (notifier instanceof WebServiceDescription) {
				EObject wsdl = WebServicesManager.getInstance().getWSDLServiceForWebService((WebServiceDescription)notifier);
				contentProvider.getViewer().refresh(wsdl);
			}
			super.notifyChanged(notification);
		}
	}
	
	private boolean isServiceRefAddOrRemove(Notification notification) {
		//Note this check is not handling ADD_MANY or REMOVE_MANY.
		Object value = null;
		switch (notification.getEventType()) {
			case Notification.ADD:
				value = notification.getNewValue();
				break;
			case Notification.REMOVE:
				value = notification.getOldValue();
				break;
		}
		return value != null && (value instanceof ServiceRef || value instanceof ComponentScopedRefs);
	}
	
	private boolean isServiceRefModification(Notification notification) {
		Object notifier = notification.getNotifier();
		return notifier instanceof ServiceRef || notifier instanceof Handler;
	}
}

Back to the top