Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1c36dd8de3c64fbb7bbab34572c8e5851fa674c7 (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
/*******************************************************************************
 * Copyright (c) 2010 Tasktop Technologies 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:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.commons.ui.team;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import org.eclipse.core.databinding.observable.Diffs;
import org.eclipse.core.databinding.observable.IDiff;
import org.eclipse.core.databinding.property.INativePropertyListener;
import org.eclipse.core.databinding.property.IProperty;
import org.eclipse.core.databinding.property.ISimplePropertyListener;
import org.eclipse.core.databinding.property.NativePropertyListener;
import org.eclipse.core.databinding.property.value.SimpleValueProperty;
import org.eclipse.mylyn.commons.repositories.RepositoryLocation;

/**
 * @author Steffen Pingel
 */
public class RepositoryLocationValueProperty extends SimpleValueProperty {

	private class PrivatePropertyChangeListener extends NativePropertyListener implements PropertyChangeListener {

		public PrivatePropertyChangeListener(IProperty property, ISimplePropertyListener listener) {
			super(property, listener);
		}

		@Override
		protected void doAddTo(Object source) {
			((RepositoryLocation) source).addChangeListener(this);
		}

		@Override
		protected void doRemoveFrom(Object source) {
			((RepositoryLocation) source).removeChangeListener(this);
		}

		public void propertyChange(PropertyChangeEvent evt) {
			if (evt.getPropertyName() == null || key.equals(evt.getPropertyName())) {
				Object oldValue = evt.getOldValue();
				Object newValue = evt.getNewValue();
				IDiff diff;
				if (evt.getPropertyName() == null || oldValue == null || newValue == null) {
					diff = null;
				} else {
					diff = Diffs.createValueDiff(oldValue, newValue);
				}
				fireChange(evt.getSource(), diff);
			}
		}

	}

	private final String key;

	private final String defaultValue;

	public RepositoryLocationValueProperty(String key, String defaultValue) {
		this.key = key;
		this.defaultValue = defaultValue;
	}

	public Object getValueType() {
		return String.class;
	}

	@Override
	protected Object doGetValue(Object source) {
//		if ("uri".equals(key)) {
//			URI uri = ((RepositoryLocation) source).getUri();
//			return (uri != null) ? uri.toString() : uri;
//		}
		String value = ((RepositoryLocation) source).getProperty(key);
		return (value != null) ? value : defaultValue;
	}

	@Override
	protected void doSetValue(Object source, Object value) {
//		if ("uri".equals(key)) {
//			try {
//				((RepositoryLocation) source).setUri(new URI((String) value));
//			} catch (URISyntaxException e) {
//				// ignore
//			}
//		} else {
		((RepositoryLocation) source).setProperty(key, (value != null) ? value.toString() : null);
//		}
	}

	@Override
	public INativePropertyListener adaptListener(final ISimplePropertyListener listener) {
		return new PrivatePropertyChangeListener(this, listener);
	}

}

Back to the top