Skip to main content
summaryrefslogtreecommitdiffstats
blob: d59816b5d192c1175955855960a668e2f771b5dc (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
/*******************************************************************************
 * Copyright (c) 2008, 2009 Oracle. 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:
 *     Oracle - initial API and implementation
 *******************************************************************************/
package org.eclipse.jpt.eclipselink.core.internal.context.persistence;

import java.util.Iterator;

import org.eclipse.jpt.core.context.persistence.PersistenceUnit;
import org.eclipse.jpt.utility.model.Model;
import org.eclipse.jpt.utility.model.event.ListChangeEvent;
import org.eclipse.jpt.utility.model.event.PropertyChangeEvent;
import org.eclipse.jpt.utility.model.listener.ListChangeListener;

/**
 * PersistenceUnitPropertyListListener Notifies the given parent when a change
 * occured in JPT PersistenceUnit properties
 */
public class PersistenceUnitPropertyListListener implements ListChangeListener
{
	private PersistenceUnitProperties parent;

	// ********** constructors / initialization **********
	public PersistenceUnitPropertyListListener(PersistenceUnitProperties parent) {
		this.parent = parent;
	}

	// ********** Behavior **********
	protected boolean add(PersistenceUnit.Property newItem, Model source) {
		if (this.model().itemIsProperty(newItem)) {
			String propertyName = this.model().propertyIdFor(newItem);
			this.model().propertyChanged(
				new PropertyChangeEvent(source, propertyName, null, newItem));
			return true;
		}
		return false;
	}

	protected boolean remove(PersistenceUnit.Property item, Model source) {
		if (this.model().itemIsProperty(item)) {
			String propertyName = this.model().propertyIdFor(item);
			this.model().propertyChanged(
				new PropertyChangeEvent(source, propertyName, item, null)); // oldItem is the removed property
			return true;
		}
		return false;
	}

	// replace
	protected PersistenceUnit.Property set(PersistenceUnit.Property newItem, Model source) {
		if (this.model().itemIsProperty(newItem)) {
			String propertyName = this.model().propertyIdFor(newItem);
			this.model().propertyChanged(
				new PropertyChangeEvent(source, propertyName, null, newItem)); // oldItem unknown
			return newItem;
		}
		return null;
	}

	// ********** ListChangeListener implementation **********
	public void itemsAdded(ListChangeEvent e) {
		for (Iterator<PersistenceUnit.Property> stream = this.items(e); stream.hasNext();) {
			this.add(stream.next(), e.getSource());
		}
	}

	public void itemsRemoved(ListChangeEvent e) {
		for (Iterator<PersistenceUnit.Property> stream = this.items(e); stream.hasNext();) {
			this.remove(stream.next(), e.getSource());
		}
	}

	public void itemsReplaced(ListChangeEvent e) {
		// ItemAspectListValueModelAdapter(270) does not provide old value
		for (Iterator<PersistenceUnit.Property> newStream = this.items(e); newStream.hasNext();) {
			this.set(newStream.next(), e.getSource());
		}
	}

	public void itemsMoved(ListChangeEvent e) {
		throw new UnsupportedOperationException("source: " + e.getSource() + " - aspect: " + e.getAspectName());
	}

	public void listCleared(ListChangeEvent e) {
		throw new UnsupportedOperationException("source: " + e.getSource() + " - aspect: " + e.getAspectName());
	}

	public void listChanged(ListChangeEvent e) {
		throw new UnsupportedOperationException("source: " + e.getSource() + " - aspect: " + e.getAspectName());
	}

	// ********** standard methods **********
	@Override
	public String toString() {
		return this.getClass().getSimpleName() + "( " + this.model().getClass().getSimpleName() + ")";
	}

	// ********** internal methods **********
	private PersistenceUnitProperties model() {
		return this.parent;
	}

	@SuppressWarnings("unchecked")
	private Iterator<PersistenceUnit.Property> items(ListChangeEvent event) {
		return (Iterator<PersistenceUnit.Property>) event.items();
	}
}

Back to the top