001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.mobility.svgcore.export;
042:
043: import java.awt.event.ActionEvent;
044: import java.awt.event.ActionListener;
045: import java.util.logging.Level;
046: import javax.swing.JComboBox;
047: import javax.swing.JComponent;
048: import javax.swing.JSlider;
049: import javax.swing.JSpinner;
050: import javax.swing.event.ChangeEvent;
051: import javax.swing.event.ChangeListener;
052: import org.netbeans.modules.mobility.svgcore.composer.SceneManager;
053:
054: /**
055: * Simple data binding between Swing components
056: *
057: * @author Pavel Benes
058: */
059:
060: public class ComponentGroup implements ActionListener, ChangeListener {
061:
062: public static abstract class ComponentWrapper {
063: protected final JComponent m_delegate;
064:
065: public abstract float getValue();
066:
067: public abstract void setValue(float value);
068:
069: protected ComponentWrapper(JComponent delegate) {
070: m_delegate = delegate;
071: }
072:
073: protected void addListener(ComponentGroup group) {
074: if (m_delegate instanceof JSpinner) {
075: ((JSpinner) m_delegate).addChangeListener(group);
076: } else if (m_delegate instanceof JSlider) {
077: ((JSlider) m_delegate).addChangeListener(group);
078: } else if (m_delegate instanceof JComboBox) {
079: ((JComboBox) m_delegate).addActionListener(group);
080: }
081: }
082:
083: public static ComponentWrapper wrap(JComponent comp) {
084: if (comp instanceof JSpinner) {
085: return wrap((JSpinner) comp);
086: } else if (comp instanceof JSlider) {
087: return wrap((JSlider) comp);
088: } else {
089: throw new IllegalArgumentException("Could not wrap "
090: + comp);
091: }
092: }
093:
094: public static ComponentWrapper wrap(JSpinner spinner) {
095: return new ComponentWrapper(spinner) {
096: public float getValue() {
097: return ((Number) ((JSpinner) m_delegate).getValue())
098: .floatValue();
099: }
100:
101: public void setValue(float value) {
102: JSpinner spinner = (JSpinner) m_delegate;
103: Object prevValue = spinner.getValue();
104: if (prevValue instanceof Integer) {
105: spinner
106: .setValue(new Integer(Math.round(value)));
107: } else {
108: spinner.setValue(new Double(value));
109: }
110: }
111: };
112: }
113:
114: public static ComponentWrapper wrap(JSlider slider) {
115: return new SliderWrapper(slider);
116: }
117: }
118:
119: public static class SliderWrapper extends ComponentWrapper {
120: protected SliderWrapper(JSlider slider) {
121: super (slider);
122: }
123:
124: public float getValue() {
125: return ((JSlider) m_delegate).getValue() / 100.0f;
126: }
127:
128: public void setValue(float value) {
129: ((JSlider) m_delegate).setValue(Math.round(value * 100));
130: }
131: }
132:
133: private final ComponentWrapper[] m_wrappers;
134: private boolean m_isUpdateInProgress = false;
135:
136: public ComponentGroup(Object... objects) {
137: m_wrappers = new ComponentWrapper[objects.length];
138:
139: for (int i = 0; i < objects.length; i++) {
140: Object o = objects[i];
141: if (o instanceof JComponent) {
142: m_wrappers[i] = ComponentWrapper.wrap((JComponent) o);
143: } else if (o instanceof ComponentWrapper) {
144: m_wrappers[i] = (ComponentWrapper) o;
145: } else {
146: throw new IllegalArgumentException("Illegal object");
147: }
148: m_wrappers[i].addListener(this );
149: }
150: }
151:
152: public ComponentWrapper findWrapper(JComponent comp) {
153: for (ComponentWrapper wrapper : m_wrappers) {
154: if (wrapper.m_delegate == comp) {
155: return wrapper;
156: }
157: }
158: return null;
159: }
160:
161: public float getValue() {
162: return m_wrappers[0].getValue();
163: }
164:
165: public boolean valueChanged(JComponent comp) {
166: for (int i = 0; i < m_wrappers.length; i++) {
167: if (m_wrappers[i].m_delegate == comp) {
168: return valueChanged(i);
169: }
170: }
171: SceneManager.log(Level.SEVERE, "Component " + comp
172: + " not found!"); //NOI18N
173: return false;
174: }
175:
176: public boolean valueChanged(int index) {
177: if (!m_isUpdateInProgress) {
178: try {
179: m_isUpdateInProgress = true;
180: float value = m_wrappers[index].getValue();
181: for (int i = 0; i < m_wrappers.length; i++) {
182: if (i != index) {
183: m_wrappers[i].setValue(value);
184: }
185: }
186: return true;
187: } finally {
188: m_isUpdateInProgress = false;
189: }
190: } else {
191: return false;
192: }
193: }
194:
195: public void actionPerformed(ActionEvent e) {
196: JComponent comp = (JComponent) e.getSource();
197:
198: if (valueChanged(comp)) {
199: refresh(comp);
200: }
201: }
202:
203: public void stateChanged(ChangeEvent e) {
204: JComponent comp = (JComponent) e.getSource();
205:
206: if (valueChanged(comp)) {
207: refresh(comp);
208: }
209: }
210:
211: protected void refresh(JComponent comp) {
212: }
213: }
|