Path: blob/master/test/jdk/sanity/client/lib/SwingSet2/src/DemoModule.java
41161 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.BorderLayout;24import java.awt.Dimension;25import java.io.BufferedReader;26import java.io.InputStream;27import java.io.InputStreamReader;28import java.net.URL;2930import javax.swing.BoxLayout;31import javax.swing.Icon;32import javax.swing.ImageIcon;33import javax.swing.JApplet;34import javax.swing.JFrame;35import javax.swing.JPanel;36import javax.swing.UIManager;37import javax.swing.border.Border;38import javax.swing.border.CompoundBorder;39import javax.swing.border.EmptyBorder;40import javax.swing.border.SoftBevelBorder;4142/**43* A generic SwingSet2 demo module44*45* @author Jeff Dinkins46*/47public class DemoModule extends JApplet {4849// The preferred size of the demo50private int PREFERRED_WIDTH = 680;51private int PREFERRED_HEIGHT = 600;5253Border loweredBorder = new CompoundBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED),54new EmptyBorder(5,5,5,5));5556// Premade convenience dimensions, for use wherever you need 'em.57public static Dimension HGAP2 = new Dimension(2,1);58public static Dimension VGAP2 = new Dimension(1,2);5960public static Dimension HGAP5 = new Dimension(5,1);61public static Dimension VGAP5 = new Dimension(1,5);6263public static Dimension HGAP10 = new Dimension(10,1);64public static Dimension VGAP10 = new Dimension(1,10);6566public static Dimension HGAP15 = new Dimension(15,1);67public static Dimension VGAP15 = new Dimension(1,15);6869public static Dimension HGAP20 = new Dimension(20,1);70public static Dimension VGAP20 = new Dimension(1,20);7172public static Dimension HGAP25 = new Dimension(25,1);73public static Dimension VGAP25 = new Dimension(1,25);7475public static Dimension HGAP30 = new Dimension(30,1);76public static Dimension VGAP30 = new Dimension(1,30);7778private SwingSet2 swingset = null;79private JPanel panel = null;80private String resourceName = null;81private String iconPath = null;82private String sourceCode = null;8384public DemoModule(SwingSet2 swingset) {85this(swingset, null, null);86}8788public DemoModule(SwingSet2 swingset, String resourceName, String iconPath) {89UIManager.put("swing.boldMetal", Boolean.FALSE);90panel = new JPanel();91panel.setLayout(new BorderLayout());9293this.resourceName = resourceName;94this.iconPath = iconPath;95this.swingset = swingset;9697loadSourceCode();98}99100public String getResourceName() {101return resourceName;102}103104public JPanel getDemoPanel() {105return panel;106}107108public SwingSet2 getSwingSet2() {109return swingset;110}111112113public String getString(String key) {114115if (getSwingSet2() != null) {116return getSwingSet2().getString(key);117}else{118return "nada";119}120}121122public char getMnemonic(String key) {123return (getString(key)).charAt(0);124}125126public ImageIcon createImageIcon(String filename, String description) {127if(getSwingSet2() != null) {128return getSwingSet2().createImageIcon(filename, description);129} else {130String path = "/resources/images/" + filename;131return new ImageIcon(getClass().getResource(path), description);132}133}134135136public String getSourceCode() {137return sourceCode;138}139140public void loadSourceCode() {141if(getResourceName() != null) {142String filename = getResourceName() + ".java";143sourceCode = new String("<html><body bgcolor=\"#ffffff\"><pre>");144InputStream is;145InputStreamReader isr;146URL url;147148try {149url = getClass().getResource(filename);150is = url.openStream();151isr = new InputStreamReader(is, "UTF-8");152BufferedReader reader = new BufferedReader(isr);153154// Read one line at a time, htmlize using super-spiffy155// html java code formating utility from www.CoolServlets.com156String line = reader.readLine();157while(line != null) {158sourceCode += line + " \n ";159line = reader.readLine();160}161sourceCode += new String("</pre></body></html>");162} catch (Exception ex) {163sourceCode = "Could not load file: " + filename;164}165}166}167168public String getName() {169return getString(getResourceName() + ".name");170};171172public Icon getIcon() {173return createImageIcon(iconPath, getResourceName() + ".name");174};175176public String getToolTip() {177return getString(getResourceName() + ".tooltip");178};179180public void mainImpl() {181JFrame frame = new JFrame(getName());182frame.getContentPane().setLayout(new BorderLayout());183frame.getContentPane().add(getDemoPanel(), BorderLayout.CENTER);184getDemoPanel().setPreferredSize(new Dimension(PREFERRED_WIDTH, PREFERRED_HEIGHT));185frame.pack();186frame.show();187}188189public JPanel createHorizontalPanel(boolean threeD) {190JPanel p = new JPanel();191p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));192p.setAlignmentY(TOP_ALIGNMENT);193p.setAlignmentX(LEFT_ALIGNMENT);194if(threeD) {195p.setBorder(loweredBorder);196}197return p;198}199200public JPanel createVerticalPanel(boolean threeD) {201JPanel p = new JPanel();202p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));203p.setAlignmentY(TOP_ALIGNMENT);204p.setAlignmentX(LEFT_ALIGNMENT);205if(threeD) {206p.setBorder(loweredBorder);207}208return p;209}210211public static void main(String[] args) {212DemoModule demo = new DemoModule(null);213demo.mainImpl();214}215216public void init() {217getContentPane().setLayout(new BorderLayout());218getContentPane().add(getDemoPanel(), BorderLayout.CENTER);219}220221void updateDragEnabled(boolean dragEnabled) {}222}223224