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