Path: blob/master/src/demo/share/jfc/SwingSet2/DemoModule.java
41149 views
/*1*2* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/3132import javax.swing.*;33import javax.swing.event.*;34import javax.swing.text.*;35import javax.swing.border.*;36import javax.swing.colorchooser.*;37import javax.swing.filechooser.*;38import javax.accessibility.*;3940import java.awt.*;41import java.awt.event.*;42import java.beans.*;43import java.util.*;44import java.io.*;45import java.applet.*;46import java.net.*;4748/**49* A generic SwingSet2 demo module50*51* @author Jeff Dinkins52*/53public class DemoModule extends JFrame {5455// The preferred size of the demo56private int PREFERRED_WIDTH = 680;57private int PREFERRED_HEIGHT = 600;5859Border loweredBorder = new CompoundBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED),60new EmptyBorder(5,5,5,5));6162// Premade convenience dimensions, for use wherever you need 'em.63public static Dimension HGAP2 = new Dimension(2,1);64public static Dimension VGAP2 = new Dimension(1,2);6566public static Dimension HGAP5 = new Dimension(5,1);67public static Dimension VGAP5 = new Dimension(1,5);6869public static Dimension HGAP10 = new Dimension(10,1);70public static Dimension VGAP10 = new Dimension(1,10);7172public static Dimension HGAP15 = new Dimension(15,1);73public static Dimension VGAP15 = new Dimension(1,15);7475public static Dimension HGAP20 = new Dimension(20,1);76public static Dimension VGAP20 = new Dimension(1,20);7778public static Dimension HGAP25 = new Dimension(25,1);79public static Dimension VGAP25 = new Dimension(1,25);8081public static Dimension HGAP30 = new Dimension(30,1);82public static Dimension VGAP30 = new Dimension(1,30);8384private SwingSet2 swingset = null;85private JPanel panel = null;86private String resourceName = null;87private String iconPath = null;88private String sourceCode = null;8990public DemoModule(SwingSet2 swingset) {91this(swingset, null, null);92}9394public DemoModule(SwingSet2 swingset, String resourceName, String iconPath) {95UIManager.put("swing.boldMetal", Boolean.FALSE);96panel = new JPanel();97panel.setLayout(new BorderLayout());9899this.resourceName = resourceName;100this.iconPath = iconPath;101this.swingset = swingset;102103loadSourceCode();104}105106public String getResourceName() {107return resourceName;108}109110public JPanel getDemoPanel() {111return panel;112}113114public SwingSet2 getSwingSet2() {115return swingset;116}117118119public String getString(String key) {120121if (getSwingSet2() != null) {122return getSwingSet2().getString(key);123}else{124return "nada";125}126}127128public char getMnemonic(String key) {129return (getString(key)).charAt(0);130}131132public ImageIcon createImageIcon(String filename, String description) {133if(getSwingSet2() != null) {134return getSwingSet2().createImageIcon(filename, description);135} else {136String path = "/resources/images/" + filename;137return new ImageIcon(getClass().getResource(path), description);138}139}140141142public String getSourceCode() {143return sourceCode;144}145146public void loadSourceCode() {147if(getResourceName() != null) {148String filename = getResourceName() + ".java";149sourceCode = new String("<html><body bgcolor=\"#ffffff\"><pre>");150InputStream is;151InputStreamReader isr;152URL url;153154try {155url = getClass().getResource(filename);156is = url.openStream();157isr = new InputStreamReader(is, "UTF-8");158BufferedReader reader = new BufferedReader(isr);159160// Read one line at a time, htmlize using super-spiffy161// html java code formating utility from www.CoolServlets.com162String line = reader.readLine();163while(line != null) {164sourceCode += line + " \n ";165line = reader.readLine();166}167sourceCode += new String("</pre></body></html>");168} catch (Exception ex) {169sourceCode = "Could not load file: " + filename;170}171}172}173174public String getName() {175return getString(getResourceName() + ".name");176};177178public Icon getIcon() {179return createImageIcon(iconPath, getResourceName() + ".name");180};181182public String getToolTip() {183return getString(getResourceName() + ".tooltip");184};185186public void mainImpl() {187JFrame frame = new JFrame(getName());188frame.getContentPane().setLayout(new BorderLayout());189frame.getContentPane().add(getDemoPanel(), BorderLayout.CENTER);190getDemoPanel().setPreferredSize(new Dimension(PREFERRED_WIDTH, PREFERRED_HEIGHT));191frame.pack();192frame.setVisible(true);193}194195public JPanel createHorizontalPanel(boolean threeD) {196JPanel p = new JPanel();197p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));198p.setAlignmentY(TOP_ALIGNMENT);199p.setAlignmentX(LEFT_ALIGNMENT);200if(threeD) {201p.setBorder(loweredBorder);202}203return p;204}205206public JPanel createVerticalPanel(boolean threeD) {207JPanel p = new JPanel();208p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));209p.setAlignmentY(TOP_ALIGNMENT);210p.setAlignmentX(LEFT_ALIGNMENT);211if(threeD) {212p.setBorder(loweredBorder);213}214return p;215}216217public static void main(String[] args) {218DemoModule demo = new DemoModule(null);219demo.mainImpl();220}221222public void init() {223getContentPane().setLayout(new BorderLayout());224getContentPane().add(getDemoPanel(), BorderLayout.CENTER);225}226227void updateDragEnabled(boolean dragEnabled) {}228}229230231