Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5cf43b584714d2ef6c533c3b7892c508f3195520 (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
/*******************************************************************************
 * Copyright (c) 2003, 2005 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
 *******************************************************************************/

package org.eclipse.jst.j2ee.internal.webservice.adapter;

import java.util.Iterator;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;

public class AdapterCCombo extends AdapterImpl implements SelectionListener {
	private EObject eObject_;
	private EStructuralFeature feature_;
	private EStructuralFeature displayFeature_;
	private CCombo combo_;
	private String defaultDisplayString_;

	public AdapterCCombo(EStructuralFeature feature, EStructuralFeature displayFeature, CCombo combo, String defaultDisplayString) {
		super();
		eObject_ = null;
		feature_ = feature;
		displayFeature_ = displayFeature;
		combo_ = combo;
		combo_.addSelectionListener(this);
		defaultDisplayString_ = (defaultDisplayString != null) ? defaultDisplayString : ""; //$NON-NLS-1$
	}

	public AdapterCCombo(EStructuralFeature feature, EStructuralFeature displayFeature, CCombo combo) {
		this(feature, displayFeature, combo, null);
	}

	public AdapterCCombo(EObject eObject, EStructuralFeature feature, EStructuralFeature displayFeature, CCombo combo) {
		this(eObject, feature, displayFeature, combo, null);
	}

	public AdapterCCombo(EObject eObject, EStructuralFeature feature, EStructuralFeature displayFeature, CCombo combo, String defaultDisplayString) {
		this(feature, displayFeature, combo, defaultDisplayString);
		adapt(eObject);
	}

	public void notifyChanged(Notification msg) {
		Object notifier = msg.getNotifier();
		if (notifier == eObject_) {
			int type = msg.getEventType();
			if (type == Notification.SET || type == Notification.UNSET || type == Notification.ADD || type == Notification.REMOVE || type == Notification.ADD_MANY || type == Notification.REMOVE_MANY)
				populateCombo();
		} else if (msg.getFeature() == displayFeature_)
			populateCombo();
	}

	private void populateCombo() {
		if (eObject_ != null) {
			Object object = eObject_.eGet(feature_);
			if (object != null) {
				if (combo_.isDisposed())
					return;
				int selectionIndex = combo_.getSelectionIndex();
				String text = combo_.getText();
				combo_.removeAll();
				if (object instanceof EList) {
					EList eList = (EList) object;
					for (Iterator it = eList.iterator(); it.hasNext();)
						combo_.add(toDisplayString(((EObject) it.next()).eGet(displayFeature_)));
				} else
					combo_.add(toDisplayString(((EObject) object).eGet(displayFeature_)));
				int newIndex = combo_.indexOf(text);
				if (selectionIndex != -1 && newIndex != -1)
					combo_.select(newIndex);
				else
					combo_.select(0);
			}
		}
	}

	private String toDisplayString(Object object) {
		if (object != null) {
			String s = object.toString();
			if (s != null && s.length() > 0)
				return s;
		}
		return defaultDisplayString_;
	}

	public void widgetDefaultSelected(SelectionEvent e) {
		//Do nothing
	}

	public void widgetSelected(SelectionEvent e) {
		//Do nothing
	}

	public void adapt(EObject eObject) {
		if (eObject_ != null) {
			eObject_.eAdapters().remove(this);
			combo_.removeAll();
		}
		eObject_ = eObject;
		if (eObject_ != null) {
			eObject_.eAdapters().add(this);
			Object object = eObject_.eGet(feature_);
			if (object instanceof EList) {
				for (Iterator it = ((EList) object).iterator(); it.hasNext();) {
					Object item = it.next();
					if (item instanceof EObject)
						((EObject) item).eAdapters().add(this);
				}
			} else if (object instanceof EObject)
				((EObject) object).eAdapters().add(this);
			populateCombo();
		}
	}

	public void dispose() {
		if (eObject_ != null)
			eObject_.eAdapters().remove(this);
		if (combo_ != null && !combo_.isDisposed())
			combo_.removeSelectionListener(this);
	}
}

Back to the top