1/*
2 * Copyright (c) 2003, 2007, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 4353056
27 * @summary Tests basic IndexPropertyChangeEvent functionality
28 * @author Mark Davidson
29 */
30
31import java.awt.Color;
32
33import java.beans.IndexedPropertyChangeEvent;
34import java.beans.PropertyChangeEvent;
35import java.beans.PropertyChangeListener;
36import java.beans.PropertyChangeSupport;
37
38/**
39 * Tests the basic functionality of IndexedPropertyChangeEvent and
40 * the fireIndexed... methods on PropertyChangeSupport.
41 */
42public class Test4353056 implements PropertyChangeListener {
43    private static final int COUNT = 100;
44    private static final String COLOR = "color";
45    private static final String BOOLEAN = "boolean";
46    private static final String INTEGER = "integer";
47
48    public static void main(String[] args) throws Exception {
49        Test4353056 test = new Test4353056();
50        test.addPropertyChangeListener(test);
51        for (int i = 0; i < COUNT; i++) {
52            boolean even = i % 2 == 0;
53            test.setColor(i, i % 3 == 0 ? Color.RED : Color.BLUE);
54            test.setBoolean(i, even);
55            test.setInteger(i, i);
56        }
57    }
58
59    private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
60
61    private Color color;
62    private boolean flag;
63    private int value;
64
65    private String name;
66    private int index = -1;
67
68    public void addPropertyChangeListener(PropertyChangeListener listener) {
69        this.pcs.addPropertyChangeListener(listener);
70    }
71
72    public void removePropertyChangeListener(PropertyChangeListener listener) {
73        this.pcs.removePropertyChangeListener(listener);
74    }
75
76    /**
77     * Setter for Object indexed property.
78     *
79     * @param index  the property index
80     * @param color  new value
81     */
82    public void setColor(int index, Color color) {
83        Color oldColor = this.color;
84        this.color = color;
85
86        this.index = index;
87        this.name = COLOR;
88
89        this.pcs.fireIndexedPropertyChange(COLOR, index,
90                oldColor, color);
91    }
92
93    /**
94     * Setter for boolean indexed property.
95     *
96     * @param index  the property index
97     * @param flag   new value
98     */
99    public void setBoolean(int index, boolean flag) {
100        boolean oldBool = this.flag;
101        this.flag = flag;
102
103        this.index = index;
104        this.name = BOOLEAN;
105
106        this.pcs.fireIndexedPropertyChange(BOOLEAN, index,
107                oldBool, flag);
108    }
109
110    /**
111     * Setter for integer indexed property.
112     *
113     * @param index  the property index
114     * @param value  new value
115     */
116    public void setInteger(int index, int value) {
117        int oldInt = this.value;
118        this.value = value;
119
120        this.index = index;
121        this.name = INTEGER;
122
123        this.pcs.fireIndexedPropertyChange(INTEGER, index,
124                oldInt, value);
125    }
126
127    public void propertyChange(PropertyChangeEvent event) {
128        Object value = event.getNewValue();
129        if (value.equals(event.getOldValue())) {
130            throw new Error("new value is equal to old one");
131        }
132        if (!this.name.equals(event.getPropertyName())) {
133            throw new Error("unexpected property name");
134        } else if (this.name.equals(COLOR)) {
135            if (!value.equals(this.color)) {
136                throw new Error("unexpected object value");
137            }
138        } else if (this.name.equals(BOOLEAN)) {
139            if (!value.equals(Boolean.valueOf(this.flag))) {
140                throw new Error("unexpected boolean value");
141            }
142        } else if (this.name.equals(INTEGER)) {
143            if (!value.equals(Integer.valueOf(this.value))) {
144                throw new Error("unexpected integer value");
145            }
146        } else {
147            throw new Error("unexpected property name");
148        }
149        if (event instanceof IndexedPropertyChangeEvent) {
150            IndexedPropertyChangeEvent ipce = (IndexedPropertyChangeEvent) event;
151            if (this.index != ipce.getIndex()) {
152                throw new Error("unexpected property index");
153            }
154        } else {
155            throw new Error("unexpected event type");
156        }
157        System.out.println(this.name + " at " + this.index + " is " + value);
158
159        this.name = null;
160        this.index = -1;
161    }
162}
163