Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5dd4827c04fa7d20014d73b5659b002d5bdc03c6 (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
/*****************************************************************************
 * Copyright (c) 2010 CEA LIST.
 *
 * 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:
 *   Nicolas Guyomar (Mia-Software) - initial API and implementation
 *   Nicolas Guyomar (Mia-Software) - Bug 334546 - [celleditors] no border on Text field
 *   Gregoire Dupe (Mia-Software) - Bug 339998 - public methods of AbstractCellEditorComposite have to be protected
 *   Nicolas Bros (Mia-Software) - Bug 334539 - [celleditors] change listener
 *****************************************************************************/
package org.eclipse.papyrus.emf.facet.widgets.celleditors;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

public abstract class AbstractCellEditorComposite<T> extends Composite implements IWidget<T>,
		IWidget2<T> {

	private final List<IListener> commitListeners;
	private final List<IListener> changeListeners;
	@Deprecated
	private final List<Listener> compatibilityCommitListeners;
	private final IValidator defaultValidator = new IValidator() {
		public IStatus validate(final Object object) {
			return Status.OK_STATUS;
		}
	};

	public AbstractCellEditorComposite(final Composite parent) {
		this(parent, SWT.NONE);
	}

	@SuppressWarnings("deprecation")
	/* backwards-compatibility */
	public AbstractCellEditorComposite(final Composite parent, final int style) {
		super(parent, style);
		this.compatibilityCommitListeners = new ArrayList<Listener>();
		this.commitListeners = new ArrayList<IListener>();
		this.changeListeners = new ArrayList<IListener>();
	}

	/** @deprecated use {@link AbstractCellEditorComposite#addCommitListener(IListener)} */
	@Deprecated
	public void addCommitListener(final Listener listener) {
		synchronized (this.compatibilityCommitListeners) {
			if (!this.compatibilityCommitListeners.contains(listener)) {
				this.compatibilityCommitListeners.add(listener);
			}
		}
	}

	/** @deprecated use {@link AbstractCellEditorComposite#removeCommitListener(IListener)} */
	@Deprecated
	public void removeCommitListener(final Listener listener) {
		synchronized (this.compatibilityCommitListeners) {
			this.compatibilityCommitListeners.remove(listener);
		}
	}

	public void addCommitListener(final IListener listener) {
		synchronized (this.commitListeners) {
			if (!this.commitListeners.contains(listener)) {
				this.commitListeners.add(listener);
			}
		}
	}

	public void removeCommitListener(final IListener listener) {
		synchronized (this.commitListeners) {
			this.commitListeners.remove(listener);
		}
	}

	@SuppressWarnings("deprecation")
	/* backwards-compatibility */
	protected final void fireCommit() {
		synchronized (this.compatibilityCommitListeners) {
			for (Listener commitListener : new ArrayList<Listener>(
					this.compatibilityCommitListeners)) {
				commitListener.handleEvent(new Event());
			}
		}
		synchronized (this.commitListeners) {
			for (IListener commitListener : new ArrayList<IListener>(this.commitListeners)) {
				commitListener.handleEvent();
			}
		}
	}

	public void addChangeListener(final IListener listener) {
		synchronized (this.changeListeners) {
			if (!this.changeListeners.contains(listener)) {
				this.changeListeners.add(listener);
			}
		}
	}

	public void removeChangeListener(final IListener listener) {
		synchronized (this.changeListeners) {
			this.changeListeners.remove(listener);
		}
	}

	protected final void fireChanged() {
		synchronized (this.changeListeners) {
			for (IListener changeListener : new ArrayList<IListener>(this.changeListeners)) {
				changeListener.handleEvent();
			}
		}
	}

	public IValidator getValidator() {
		// always valid by default
		return this.defaultValidator;
	}

	protected final void close() {
		if (!isDisposed()) {
			getParent().forceFocus();
			dispose();
		}
	}
}

Back to the top