001    /**
002     * Copyright (c) 2008-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.io.IOException;
035    import java.util.List;
036    
037    import javax.swing.Action;
038    import javax.swing.JCheckBoxMenuItem;
039    import javax.swing.JComponent;
040    import javax.swing.JFrame;
041    import javax.swing.JMenu;
042    import javax.swing.JMenuBar;
043    import javax.swing.JScrollPane;
044    import javax.swing.JTextPane;
045    import javax.swing.SwingUtilities;
046    
047    import org.jdesktop.jxlayer.JXLayer;
048    import org.pbjar.jxlayer.plaf.misc.GeneralLayerUI;
049    import org.pbjar.jxlayer.plaf.misc.HideCursorUI;
050    import org.pbjar.jxlayer.plaf.misc.MagnifierUI;
051    import org.pbjar.jxlayer.plaf.misc.MouseDrawingUI;
052    
053    /**
054     * Test the wrapping of JXLayer's into one another.
055     * 
056     * <p>
057     * Run a web start demo: <a
058     * href="http://www.pbjar.org/blogs/jxlayer/version_2/WrappingDemo.jnlp"> <IMG
059     * style="CLEAR: right" alt="Web Start Wrapped JXLayer"
060     * src="http://javadesktop.org/javanet_images/webstart.small2.gif"
061     * align="middle" border="1" /> </a>
062     * </p>
063     * 
064     * @author Piet Blok
065     */
066    public class TestWrapped {
067    
068        /**
069         * Run the progtam.
070         * 
071         * @param args
072         *            not used.
073         */
074        public static void main(String[] args) {
075            SwingUtilities.invokeLater(new Runnable() {
076    
077                @Override
078                public void run() {
079                    try {
080                        new TestWrapped().test();
081                    } catch (IOException e) {
082                        e.printStackTrace();
083                    }
084                }
085            });
086        }
087    
088        private TestWrapped() {
089    
090        }
091    
092        private void addActions(JMenu menu, String name, List<Action> actionList) {
093            for (Action action : actionList) {
094                if (action.getValue(Action.SELECTED_KEY) != null) {
095                    JCheckBoxMenuItem item = new JCheckBoxMenuItem(action);
096                    menu.add(item);
097                } else {
098                    menu.add(action);
099                }
100            }
101        }
102    
103        @SuppressWarnings("unchecked")
104        private JComponent createTarget(JFrame frame, String id, JMenu menubar,
105                GeneralLayerUI<JComponent, ?>[] layerUIs) {
106            JTextPane originalComponent = new JTextPane() {
107    
108                private static final long serialVersionUID = 1L;
109    
110                @Override
111                public boolean getScrollableTracksViewportWidth() {
112                    return true;
113                }
114    
115            };
116            try {
117                originalComponent.setPage(this.getClass().getResource(
118                        "WrapTest.html"));
119            } catch (IOException e) {
120                e.printStackTrace();
121            }
122            originalComponent.setEditable(false);
123    
124            JComponent wrappingTarget = originalComponent;
125    
126            for (GeneralLayerUI<JComponent, ?> layerUI : layerUIs) {
127                wrappingTarget = new JXLayer<JComponent>(wrappingTarget, layerUI);
128                JMenu menu = new JMenu(layerUI.getName());
129                menubar.add(menu);
130                addActions(menu, layerUI.getName(), layerUI.getActions());
131                addActions(menu, layerUI.getName(), layerUI
132                        .getActions((JXLayer<JComponent>) wrappingTarget));
133            }
134    
135            return new JScrollPane(wrappingTarget);
136    
137        }
138    
139        @SuppressWarnings("unchecked")
140        private void test() throws IOException {
141            JFrame frame = new JFrame();
142            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
143            frame.setSize(600, 400);
144            frame.setLocationRelativeTo(null);
145            JMenuBar menubar = new JMenuBar();
146            frame.setJMenuBar(menubar);
147    
148            GeneralLayerUI<JComponent, ?>[] layerUIs = new GeneralLayerUI[] {
149    
150            new MouseDrawingUI(true),
151    
152            new HideCursorUI(500, true),
153    
154            new MagnifierUI(true),
155    
156            };
157    
158            JMenu menu = new JMenu("Options");
159            menubar.add(menu);
160    
161            frame.add(createTarget(frame, "Target", menu, layerUIs));
162            frame.setVisible(true);
163        }
164    
165    }