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
|
/*******************************************************************************
* Copyright (c) 2011, 2012 Wind River Systems, Inc. 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:
* Wind River Systems - initial API and implementation
*******************************************************************************/
package org.eclipse.tcf.te.runtime.events;
import java.util.EventObject;
import org.eclipse.tcf.te.runtime.activator.CoreBundleActivator;
import org.eclipse.tcf.te.runtime.interfaces.tracing.ITraceIds;
/**
* Common change event implementation.
*/
public class ChangeEvent extends EventObject {
private static final long serialVersionUID = -7859159130977760588L;
public static final String ID_ADDED = "added"; //$NON-NLS-1$
public static final String ID_REMOVED = "removed"; //$NON-NLS-1$
public static final String ID_CHANGED = "changed"; //$NON-NLS-1$
private Object eventId;
private Object oldValue;
private Object newValue;
/**
* Constructor.
*
* @param source The source object. Must not be <code>null</code>.
* @param eventId The event id. Must not be <code>null</code>.
* @param oldValue The old value.
* @param newValue The new value.
*
* @exception IllegalArgumentException if eventId == null.
*/
public ChangeEvent(Object source, Object eventId, Object oldValue, Object newValue) {
super(source);
if (eventId == null) {
throw new IllegalArgumentException("null eventId"); //$NON-NLS-1$
}
this.eventId = eventId;
this.oldValue = oldValue;
this.newValue = newValue;
}
/**
* Returns the event id.
*
* @return The event id.
*/
public final Object getEventId() {
return eventId;
}
/**
* Returns the old value.
*
* @return The old value or <code>null</code>.
*/
public final Object getOldValue() {
return oldValue;
}
/**
* Returns the new value.
*
* @return The new value or <code>null</code>.
*/
public final Object getNewValue() {
return newValue;
}
/*
* Formats a value due to its type.
*/
private Object formatValue(Object value) {
Object formattedValue = value;
if (value != null && value.getClass().isArray()) {
StringBuilder str = new StringBuilder();
str.append("{"); //$NON-NLS-1$
for (int i = 0; i < ((Object[]) value).length; i++) {
if (i > 0) {
str.append(","); //$NON-NLS-1$
}
str.append(formatValue(((Object[]) value)[i]));
}
str.append("}"); //$NON-NLS-1$
formattedValue = str.toString();
}
return formattedValue;
}
/*
* (non-Javadoc)
* @see java.util.EventObject#toString()
*/
@Override
public String toString() {
StringBuilder toString = new StringBuilder(getClass().getName());
String prefix = ""; //$NON-NLS-1$
// if tracing the event, formating them a little bit better readable.
if (CoreBundleActivator.getTraceHandler().isSlotEnabled(0, ITraceIds.TRACE_EVENTS)) {
prefix = "\n\t\t"; //$NON-NLS-1$
}
toString.append(prefix + "{eventId="); //$NON-NLS-1$
toString.append(eventId);
toString.append("," + prefix + "source="); //$NON-NLS-1$ //$NON-NLS-2$
toString.append(source);
toString.append("," + prefix + "oldValue="); //$NON-NLS-1$ //$NON-NLS-2$
toString.append(formatValue(oldValue));
toString.append("," + prefix + "newValue="); //$NON-NLS-1$ //$NON-NLS-2$
toString.append(formatValue(newValue));
toString.append("}"); //$NON-NLS-1$
return toString.toString();
}
}
|