1/*
2 * Copyright (c) 2011, 2012, 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
26package com.apple.laf;
27
28import java.beans.*;
29import java.util.*;
30
31import javax.swing.JComponent;
32
33public class ClientPropertyApplicator<T extends JComponent, N> implements PropertyChangeListener {
34    private final Map<String, Property<N>> properties = new HashMap<String, Property<N>>();
35
36    @SuppressWarnings("unchecked")
37    public ClientPropertyApplicator(final Property<N>... propertyList) {
38        for (final Property<N> p : propertyList) {
39            properties.put(p.name, p);
40        }
41    }
42
43    void applyProperty(final N target, final String propName, final Object value) {
44        final Property<N> property = properties.get(propName);
45        if (property != null) {
46            property.applyProperty(target, value);
47        }
48    }
49
50    public void attachAndApplyClientProperties(final T target) {
51        target.addPropertyChangeListener(this);
52        final N obj = convertJComponentToTarget(target);
53        if (obj == null) {
54            return;
55        }
56
57        final Set<String> propNames = properties.keySet();
58        for (final String propName : propNames) {
59            final Object value = target.getClientProperty(propName);
60            if (value == null) {
61                continue;
62            }
63            applyProperty(obj, propName, value);
64        }
65    }
66
67    public void removeFrom(final T target) {
68        target.removePropertyChangeListener(this);
69    }
70
71    @Override
72    @SuppressWarnings("unchecked")
73    public void propertyChange(final PropertyChangeEvent evt) {
74        final N obj = convertJComponentToTarget((T)evt.getSource());
75        if (obj == null) return;
76        applyProperty(obj, evt.getPropertyName(), evt.getNewValue());
77    }
78
79    @SuppressWarnings("unchecked")
80    public N convertJComponentToTarget(final T component) {
81        return (N)component; // naive implementation
82    }
83
84    public abstract static class Property<X> {
85        final String name;
86
87        public Property(final String name) {
88            this.name = name;
89        }
90
91        public abstract void applyProperty(final X target, final Object value);
92    }
93}
94