/* * * TestJXLayer.java version 0.1 * * Copyright (C) 2008 Piet Blok. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Created on 11 jun 2008 */ package org.pbjar.jxlayer.test; import java.util.List; import javax.swing.Action; import java.io.IOException; import javax.swing.JCheckBoxMenuItem; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JScrollPane; import javax.swing.JSplitPane; import javax.swing.JTextPane; import javax.swing.SwingUtilities; import org.jdesktop.jxlayer.JXLayer; /** * TODO Program description * * @author Piet Blok */ public class TestShared { /** * @param args */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { try { new TestShared().test(); } catch (IOException e) { e.printStackTrace(); } } }); } @SuppressWarnings("unchecked") public void test() throws IOException { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(600, 400); frame.setLocationRelativeTo(null); JMenuBar menubar = new JMenuBar(); frame.setJMenuBar(menubar); GeneralLayerUI[] layerUIs = new GeneralLayerUI[] { new MouseDrawingUI(), new HideCursorUI(500), new MagnifierUI(), }; JMenu menu = new JMenu("General UI"); menubar.add(menu); for (GeneralLayerUI layerUI : layerUIs) { addActions(menu, layerUI.getName(), layerUI.getActions()); } JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, createTarget(frame, "Left", menubar, layerUIs), createTarget( frame, "Right", menubar, layerUIs)); splitPane.setDividerLocation(frame.getWidth() / 2); frame.add(splitPane); frame.setVisible(true); } private void addActions(JMenu parentMenu, String name, List actionList) { JMenu menu; if (actionList.size() > 1) { menu = new JMenu(name); parentMenu.add(menu); } else { menu = parentMenu; } for (Action action : actionList) { if (action instanceof FlipFlop) { JCheckBoxMenuItem item = new JCheckBoxMenuItem(action); menu.add(item); item.setSelected(((FlipFlop) action).getState()); } else { menu.add(action); } } } @SuppressWarnings("unchecked") private JComponent createTarget(JFrame frame, String id, JMenuBar menubar, GeneralLayerUI[] layerUIs) { JTextPane originalComponent = new JTextPane() { private static final long serialVersionUID = 1L; @Override public boolean getScrollableTracksViewportWidth() { return true; } }; try { originalComponent.setPage(this.getClass().getResource( "SharedTest.html")); } catch (IOException e) { e.printStackTrace(); } originalComponent.setEditable(false); JMenu menu = new JMenu(id); menubar.add(menu); JComponent wrappingTarget = originalComponent; for (GeneralLayerUI layerUI : layerUIs) { wrappingTarget = new JXLayer(wrappingTarget, layerUI); addActions(menu, layerUI.getName(), layerUI .getActions((JXLayer) wrappingTarget)); } return new JScrollPane(wrappingTarget); } }