001    /**
002     * Copyright (c) 2009, Piet Blok
003     * All rights reserved.
004     *
005     * Redistribution and use in source and binary forms, with or without
006     * modification, are permitted provided that the following conditions
007     * are met:
008     *
009     *   * Redistributions of source code must retain the above copyright
010     *     notice, this list of conditions and the following disclaimer.
011     *   * Redistributions in binary form must reproduce the above
012     *     copyright notice, this list of conditions and the following
013     *     disclaimer in the documentation and/or other materials provided
014     *     with the distribution.
015     *   * Neither the name of the copyright holder nor the names of the
016     *     contributors may be used to endorse or promote products derived
017     *     from this software without specific prior written permission.
018     *
019     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
020     * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
021     * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
022     * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
023     * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
024     * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
025     * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
026     * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
027     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
028     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
029     * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030     */
031    
032    package org.pbjar.jxlayer.demo;
033    
034    import java.awt.BorderLayout;
035    import java.awt.event.ActionEvent;
036    import java.beans.PropertyChangeEvent;
037    import java.beans.PropertyChangeListener;
038    
039    import javax.swing.AbstractAction;
040    import javax.swing.Action;
041    import javax.swing.JCheckBox;
042    import javax.swing.JDialog;
043    import javax.swing.JFrame;
044    import javax.swing.JToolBar;
045    import javax.swing.SwingUtilities;
046    
047    import org.jdesktop.jxlayer.JXLayer;
048    import org.pbjar.jxlayer.plaf.ext.TransformUI;
049    import org.pbjar.jxlayer.repaint.RepaintManagerUtils;
050    
051    /**
052     * A control dialog for the Transformation demo's.
053     * 
054     * @author Piet Blok
055     */
056    public class ControlDialog extends JDialog {
057    
058        private static final long serialVersionUID = 1L;
059    
060        private enum X {
061            enableUIAction,
062    
063            useScrollAction,
064    
065            autoPackAction,
066    
067            packNowAction,
068    
069            rpmTree;
070        }
071    
072        /**
073         * Create a ControlDialog.
074         * 
075         * @param layer
076         *            a JXLayer with BufferedTransformPort and
077         *            SimpleBufferredLayerUI set
078         * @param testframe
079         *            the demo application
080         */
081        public ControlDialog(JXLayer<?> layer, final TestGUI testframe) {
082            super((JFrame) null, "Control transform");
083            final TransformUI transformUI = (TransformUI) layer.getUI();
084            /*
085             * Create the required actions in an array.
086             */
087            final Action[] actions = new Action[X.values().length];
088    
089            actions[X.autoPackAction.ordinal()] = new AbstractAction("Auto pack") {
090    
091                private static final long serialVersionUID = 1L;
092    
093                {
094                    this.putValue(Action.SELECTED_KEY, false);
095                }
096    
097                @Override
098                public void actionPerformed(ActionEvent e) {
099                    testframe.repaintGUI((Boolean) this
100                            .getValue(Action.SELECTED_KEY));
101                }
102            };
103    
104            actions[X.enableUIAction.ordinal()] = new AbstractAction("Enable") {
105    
106                private static final long serialVersionUID = 1L;
107    
108                {
109                    this.putValue(Action.SELECTED_KEY, transformUI.isEnabled());
110                }
111    
112                @Override
113                public void actionPerformed(ActionEvent e) {
114                    transformUI.setEnabled((Boolean) this
115                            .getValue(Action.SELECTED_KEY));
116                    testframe.repaintGUI((Boolean) actions[X.autoPackAction
117                            .ordinal()].getValue(Action.SELECTED_KEY));
118                }
119            };
120    
121            actions[X.useScrollAction.ordinal()] = new AbstractAction("Scroll bars") {
122    
123                private static final long serialVersionUID = 1L;
124    
125                {
126                    this.putValue(Action.SELECTED_KEY, true);
127                }
128    
129                @Override
130                public void actionPerformed(ActionEvent e) {
131                    boolean scroll = (Boolean) this.getValue(Action.SELECTED_KEY);
132                    testframe.setScroller(scroll);
133                    testframe.repaintGUI((Boolean) actions[X.autoPackAction
134                            .ordinal()].getValue(Action.SELECTED_KEY));
135                }
136            };
137    
138            actions[X.packNowAction.ordinal()] = new AbstractAction("Pack") {
139    
140                private static final long serialVersionUID = 1L;
141    
142                @Override
143                public void actionPerformed(ActionEvent e) {
144                    testframe.repaintGUI(true);
145                }
146            };
147    
148            actions[X.rpmTree.ordinal()] = RepaintManagerUtils
149                    .createRPDisplayAction();
150    
151            /*
152             * Create a ToolBar with the actions.
153             */
154            JToolBar toolBar = new JToolBar();
155            this.add(toolBar, BorderLayout.PAGE_START);
156            for (Action action : actions) {
157                if (action.getValue(Action.SELECTED_KEY) == null) {
158                    toolBar.add(action);
159                } else {
160                    toolBar.add(new JCheckBox(action));
161                }
162            }
163            /*
164             * Create a general property change listener.
165             */
166            PropertyChangeListener listener = new PropertyChangeListener() {
167    
168                @Override
169                public void propertyChange(PropertyChangeEvent evt) {
170                    SwingUtilities.invokeLater(new Runnable() {
171    
172                        @Override
173                        public void run() {
174                            testframe.repaintGUI((Boolean) actions[X.autoPackAction
175                                    .ordinal()].getValue(Action.SELECTED_KEY));
176                        }
177                    });
178                }
179            };
180    
181            TransformWheelButtons twbs = new TransformWheelButtons(layer, listener);
182            this.add(twbs, BorderLayout.CENTER);
183            toolBar.add(twbs.getTipDisplayButton());
184        }
185    }