Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/java/beans/beancontext/BeanContextChildSupport.java
41159 views
1
/*
2
* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package java.beans.beancontext;
27
28
import java.beans.PropertyChangeEvent;
29
import java.beans.PropertyChangeListener;
30
import java.beans.PropertyChangeSupport;
31
import java.beans.PropertyVetoException;
32
import java.beans.VetoableChangeListener;
33
import java.beans.VetoableChangeSupport;
34
import java.io.IOException;
35
import java.io.ObjectInputStream;
36
import java.io.ObjectOutputStream;
37
import java.io.Serial;
38
import java.io.Serializable;
39
40
/**
41
* <p>
42
* This is a general support class to provide support for implementing the
43
* BeanContextChild protocol.
44
*
45
* This class may either be directly subclassed, or encapsulated and delegated
46
* to in order to implement this interface for a given component.
47
* </p>
48
*
49
* @author Laurence P. G. Cable
50
* @since 1.2
51
*
52
* @see java.beans.beancontext.BeanContext
53
* @see java.beans.beancontext.BeanContextServices
54
* @see java.beans.beancontext.BeanContextChild
55
*/
56
57
public class BeanContextChildSupport implements BeanContextChild, BeanContextServicesListener, Serializable {
58
59
/**
60
* Use serialVersionUID from JDK 1.2 for interoperability.
61
*/
62
@Serial
63
private static final long serialVersionUID = 6328947014421475877L;
64
65
/**
66
* construct a BeanContextChildSupport where this class has been
67
* subclassed in order to implement the JavaBean component itself.
68
*/
69
70
public BeanContextChildSupport() {
71
super();
72
73
beanContextChildPeer = this;
74
75
pcSupport = new PropertyChangeSupport(beanContextChildPeer);
76
vcSupport = new VetoableChangeSupport(beanContextChildPeer);
77
}
78
79
/**
80
* construct a BeanContextChildSupport where the JavaBean component
81
* itself implements BeanContextChild, and encapsulates this, delegating
82
* that interface to this implementation
83
* @param bcc the underlying bean context child
84
*/
85
86
public BeanContextChildSupport(BeanContextChild bcc) {
87
super();
88
89
beanContextChildPeer = (bcc != null) ? bcc : this;
90
91
pcSupport = new PropertyChangeSupport(beanContextChildPeer);
92
vcSupport = new VetoableChangeSupport(beanContextChildPeer);
93
}
94
95
/**
96
* Sets the {@code BeanContext} for
97
* this {@code BeanContextChildSupport}.
98
* @param bc the new value to be assigned to the {@code BeanContext}
99
* property
100
* @throws PropertyVetoException if the change is rejected
101
*/
102
public synchronized void setBeanContext(BeanContext bc) throws PropertyVetoException {
103
if (bc == beanContext) return;
104
105
BeanContext oldValue = beanContext;
106
BeanContext newValue = bc;
107
108
if (!rejectedSetBCOnce) {
109
if (rejectedSetBCOnce = !validatePendingSetBeanContext(bc)) {
110
throw new PropertyVetoException(
111
"setBeanContext() change rejected:",
112
new PropertyChangeEvent(beanContextChildPeer, "beanContext", oldValue, newValue)
113
);
114
}
115
116
try {
117
fireVetoableChange("beanContext",
118
oldValue,
119
newValue
120
);
121
} catch (PropertyVetoException pve) {
122
rejectedSetBCOnce = true;
123
124
throw pve; // re-throw
125
}
126
}
127
128
if (beanContext != null) releaseBeanContextResources();
129
130
beanContext = newValue;
131
rejectedSetBCOnce = false;
132
133
firePropertyChange("beanContext",
134
oldValue,
135
newValue
136
);
137
138
if (beanContext != null) initializeBeanContextResources();
139
}
140
141
/**
142
* Gets the nesting {@code BeanContext}
143
* for this {@code BeanContextChildSupport}.
144
* @return the nesting {@code BeanContext} for
145
* this {@code BeanContextChildSupport}.
146
*/
147
public synchronized BeanContext getBeanContext() { return beanContext; }
148
149
/**
150
* Add a PropertyChangeListener for a specific property.
151
* The same listener object may be added more than once. For each
152
* property, the listener will be invoked the number of times it was added
153
* for that property.
154
* If {@code name} or {@code pcl} is null, no exception is thrown
155
* and no action is taken.
156
*
157
* @param name The name of the property to listen on
158
* @param pcl The {@code PropertyChangeListener} to be added
159
*/
160
public void addPropertyChangeListener(String name, PropertyChangeListener pcl) {
161
pcSupport.addPropertyChangeListener(name, pcl);
162
}
163
164
/**
165
* Remove a PropertyChangeListener for a specific property.
166
* If {@code pcl} was added more than once to the same event
167
* source for the specified property, it will be notified one less time
168
* after being removed.
169
* If {@code name} is null, no exception is thrown
170
* and no action is taken.
171
* If {@code pcl} is null, or was never added for the specified
172
* property, no exception is thrown and no action is taken.
173
*
174
* @param name The name of the property that was listened on
175
* @param pcl The PropertyChangeListener to be removed
176
*/
177
public void removePropertyChangeListener(String name, PropertyChangeListener pcl) {
178
pcSupport.removePropertyChangeListener(name, pcl);
179
}
180
181
/**
182
* Add a VetoableChangeListener for a specific property.
183
* The same listener object may be added more than once. For each
184
* property, the listener will be invoked the number of times it was added
185
* for that property.
186
* If {@code name} or {@code vcl} is null, no exception is thrown
187
* and no action is taken.
188
*
189
* @param name The name of the property to listen on
190
* @param vcl The {@code VetoableChangeListener} to be added
191
*/
192
public void addVetoableChangeListener(String name, VetoableChangeListener vcl) {
193
vcSupport.addVetoableChangeListener(name, vcl);
194
}
195
196
/**
197
* Removes a {@code VetoableChangeListener}.
198
* If {@code pcl} was added more than once to the same event
199
* source for the specified property, it will be notified one less time
200
* after being removed.
201
* If {@code name} is null, no exception is thrown
202
* and no action is taken.
203
* If {@code vcl} is null, or was never added for the specified
204
* property, no exception is thrown and no action is taken.
205
*
206
* @param name The name of the property that was listened on
207
* @param vcl The {@code VetoableChangeListener} to be removed
208
*/
209
public void removeVetoableChangeListener(String name, VetoableChangeListener vcl) {
210
vcSupport.removeVetoableChangeListener(name, vcl);
211
}
212
213
/**
214
* A service provided by the nesting BeanContext has been revoked.
215
*
216
* Subclasses may override this method in order to implement their own
217
* behaviors.
218
* @param bcsre The {@code BeanContextServiceRevokedEvent} fired as a
219
* result of a service being revoked
220
*/
221
public void serviceRevoked(BeanContextServiceRevokedEvent bcsre) { }
222
223
/**
224
* A new service is available from the nesting BeanContext.
225
*
226
* Subclasses may override this method in order to implement their own
227
* behaviors
228
* @param bcsae The BeanContextServiceAvailableEvent fired as a
229
* result of a service becoming available
230
*
231
*/
232
public void serviceAvailable(BeanContextServiceAvailableEvent bcsae) { }
233
234
/**
235
* Gets the {@code BeanContextChild} associated with this
236
* {@code BeanContextChildSupport}.
237
*
238
* @return the {@code BeanContextChild} peer of this class
239
*/
240
public BeanContextChild getBeanContextChildPeer() { return beanContextChildPeer; }
241
242
/**
243
* Reports whether or not this class is a delegate of another.
244
*
245
* @return true if this class is a delegate of another
246
*/
247
public boolean isDelegated() { return !this.equals(beanContextChildPeer); }
248
249
/**
250
* Report a bound property update to any registered listeners. No event is
251
* fired if old and new are equal and non-null.
252
* @param name The programmatic name of the property that was changed
253
* @param oldValue The old value of the property
254
* @param newValue The new value of the property
255
*/
256
public void firePropertyChange(String name, Object oldValue, Object newValue) {
257
pcSupport.firePropertyChange(name, oldValue, newValue);
258
}
259
260
/**
261
* Report a vetoable property update to any registered listeners.
262
* If anyone vetos the change, then fire a new event
263
* reverting everyone to the old value and then rethrow
264
* the PropertyVetoException. <P>
265
*
266
* No event is fired if old and new are equal and non-null.
267
*
268
* @param name The programmatic name of the property that is about to
269
* change
270
*
271
* @param oldValue The old value of the property
272
* @param newValue - The new value of the property
273
*
274
* @throws PropertyVetoException if the recipient wishes the property
275
* change to be rolled back.
276
*/
277
public void fireVetoableChange(String name, Object oldValue, Object newValue) throws PropertyVetoException {
278
vcSupport.fireVetoableChange(name, oldValue, newValue);
279
}
280
281
/**
282
* Called from setBeanContext to validate (or otherwise) the
283
* pending change in the nesting BeanContext property value.
284
* Returning false will cause setBeanContext to throw
285
* PropertyVetoException.
286
* @param newValue the new value that has been requested for
287
* the BeanContext property
288
* @return {@code true} if the change operation is to be vetoed
289
*/
290
public boolean validatePendingSetBeanContext(BeanContext newValue) {
291
return true;
292
}
293
294
/**
295
* This method may be overridden by subclasses to provide their own
296
* release behaviors. When invoked any resources held by this instance
297
* obtained from its current BeanContext property should be released
298
* since the object is no longer nested within that BeanContext.
299
*/
300
301
protected void releaseBeanContextResources() {
302
// do nothing
303
}
304
305
/**
306
* This method may be overridden by subclasses to provide their own
307
* initialization behaviors. When invoked any resources required by the
308
* BeanContextChild should be obtained from the current BeanContext.
309
*/
310
311
protected void initializeBeanContextResources() {
312
// do nothing
313
}
314
315
/**
316
* Write the persistence state of the object.
317
*
318
* @param oos the {@code ObjectOutputStream} to write
319
* @throws IOException if an I/O error occurs
320
*/
321
@Serial
322
private void writeObject(ObjectOutputStream oos) throws IOException {
323
324
/*
325
* don't serialize if we are delegated and the delegator is not also
326
* serializable.
327
*/
328
329
if (!equals(beanContextChildPeer) && !(beanContextChildPeer instanceof Serializable))
330
throw new IOException("BeanContextChildSupport beanContextChildPeer not Serializable");
331
332
else
333
oos.defaultWriteObject();
334
335
}
336
337
338
/**
339
* Restore a persistent object, must wait for subsequent setBeanContext()
340
* to fully restore any resources obtained from the new nesting BeanContext.
341
*
342
* @param ois the {@code ObjectInputStream} to read
343
* @throws ClassNotFoundException if the class of a serialized object could
344
* not be found
345
* @throws IOException if an I/O error occurs
346
*/
347
@Serial
348
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
349
ois.defaultReadObject();
350
}
351
352
/*
353
* fields
354
*/
355
356
/**
357
* The {@code BeanContext} in which
358
* this {@code BeanContextChild} is nested.
359
*/
360
@SuppressWarnings("serial") // Not statically typed as Serializable
361
public BeanContextChild beanContextChildPeer;
362
363
/**
364
* The {@code PropertyChangeSupport} associated with this
365
* {@code BeanContextChildSupport}.
366
*/
367
protected PropertyChangeSupport pcSupport;
368
369
/**
370
* The {@code VetoableChangeSupport} associated with this
371
* {@code BeanContextChildSupport}.
372
*/
373
protected VetoableChangeSupport vcSupport;
374
375
/**
376
* The bean context.
377
*/
378
protected transient BeanContext beanContext;
379
380
/**
381
* A flag indicating that there has been
382
* at least one {@code PropertyChangeVetoException}
383
* thrown for the attempted setBeanContext operation.
384
*/
385
protected transient boolean rejectedSetBCOnce;
386
387
}
388
389