1 /* 2 * 3 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * - Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * - Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * - Neither the name of Oracle nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 import javax.swing.*; 34 import javax.swing.event.*; 35 import javax.swing.text.*; 36 import javax.swing.border.*; 37 import javax.swing.colorchooser.*; 38 import javax.swing.filechooser.*; 39 import javax.accessibility.*; 40 41 import java.awt.*; 42 import java.awt.event.*; 43 import java.beans.*; 44 import java.util.*; 45 import java.io.*; 46 import java.applet.*; 47 import java.net.*; 48 49 /** 50 * A generic SwingSet2 demo module 51 * 52 * @author Jeff Dinkins 53 */ 54 public class DemoModule extends JFrame { 55 56 // The preferred size of the demo 57 private int PREFERRED_WIDTH = 680; 58 private int PREFERRED_HEIGHT = 600; 59 60 Border loweredBorder = new CompoundBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED), 61 new EmptyBorder(5,5,5,5)); 62 63 // Premade convenience dimensions, for use wherever you need 'em. 64 public static Dimension HGAP2 = new Dimension(2,1); 65 public static Dimension VGAP2 = new Dimension(1,2); 66 67 public static Dimension HGAP5 = new Dimension(5,1); 68 public static Dimension VGAP5 = new Dimension(1,5); 69 70 public static Dimension HGAP10 = new Dimension(10,1); 71 public static Dimension VGAP10 = new Dimension(1,10); 72 73 public static Dimension HGAP15 = new Dimension(15,1); 74 public static Dimension VGAP15 = new Dimension(1,15); 75 76 public static Dimension HGAP20 = new Dimension(20,1); 77 public static Dimension VGAP20 = new Dimension(1,20); 78 79 public static Dimension HGAP25 = new Dimension(25,1); 80 public static Dimension VGAP25 = new Dimension(1,25); 81 82 public static Dimension HGAP30 = new Dimension(30,1); 83 public static Dimension VGAP30 = new Dimension(1,30); 84 85 private SwingSet2 swingset = null; 86 private JPanel panel = null; 87 private String resourceName = null; 88 private String iconPath = null; 89 private String sourceCode = null; 90 DemoModule(SwingSet2 swingset)91 public DemoModule(SwingSet2 swingset) { 92 this(swingset, null, null); 93 } 94 DemoModule(SwingSet2 swingset, String resourceName, String iconPath)95 public DemoModule(SwingSet2 swingset, String resourceName, String iconPath) { 96 UIManager.put("swing.boldMetal", Boolean.FALSE); 97 panel = new JPanel(); 98 panel.setLayout(new BorderLayout()); 99 100 this.resourceName = resourceName; 101 this.iconPath = iconPath; 102 this.swingset = swingset; 103 104 loadSourceCode(); 105 } 106 getResourceName()107 public String getResourceName() { 108 return resourceName; 109 } 110 getDemoPanel()111 public JPanel getDemoPanel() { 112 return panel; 113 } 114 getSwingSet2()115 public SwingSet2 getSwingSet2() { 116 return swingset; 117 } 118 119 getString(String key)120 public String getString(String key) { 121 122 if (getSwingSet2() != null) { 123 return getSwingSet2().getString(key); 124 }else{ 125 return "nada"; 126 } 127 } 128 getMnemonic(String key)129 public char getMnemonic(String key) { 130 return (getString(key)).charAt(0); 131 } 132 createImageIcon(String filename, String description)133 public ImageIcon createImageIcon(String filename, String description) { 134 if(getSwingSet2() != null) { 135 return getSwingSet2().createImageIcon(filename, description); 136 } else { 137 String path = "/resources/images/" + filename; 138 return new ImageIcon(getClass().getResource(path), description); 139 } 140 } 141 142 getSourceCode()143 public String getSourceCode() { 144 return sourceCode; 145 } 146 loadSourceCode()147 public void loadSourceCode() { 148 if(getResourceName() != null) { 149 String filename = getResourceName() + ".java"; 150 sourceCode = new String("<html><body bgcolor=\"#ffffff\"><pre>"); 151 InputStream is; 152 InputStreamReader isr; 153 URL url; 154 155 try { 156 url = getClass().getResource(filename); 157 is = url.openStream(); 158 isr = new InputStreamReader(is, "UTF-8"); 159 BufferedReader reader = new BufferedReader(isr); 160 161 // Read one line at a time, htmlize using super-spiffy 162 // html java code formating utility from www.CoolServlets.com 163 String line = reader.readLine(); 164 while(line != null) { 165 sourceCode += line + " \n "; 166 line = reader.readLine(); 167 } 168 sourceCode += new String("</pre></body></html>"); 169 } catch (Exception ex) { 170 sourceCode = "Could not load file: " + filename; 171 } 172 } 173 } 174 getName()175 public String getName() { 176 return getString(getResourceName() + ".name"); 177 }; 178 getIcon()179 public Icon getIcon() { 180 return createImageIcon(iconPath, getResourceName() + ".name"); 181 }; 182 getToolTip()183 public String getToolTip() { 184 return getString(getResourceName() + ".tooltip"); 185 }; 186 mainImpl()187 public void mainImpl() { 188 JFrame frame = new JFrame(getName()); 189 frame.getContentPane().setLayout(new BorderLayout()); 190 frame.getContentPane().add(getDemoPanel(), BorderLayout.CENTER); 191 getDemoPanel().setPreferredSize(new Dimension(PREFERRED_WIDTH, PREFERRED_HEIGHT)); 192 frame.pack(); 193 frame.setVisible(true); 194 } 195 createHorizontalPanel(boolean threeD)196 public JPanel createHorizontalPanel(boolean threeD) { 197 JPanel p = new JPanel(); 198 p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); 199 p.setAlignmentY(TOP_ALIGNMENT); 200 p.setAlignmentX(LEFT_ALIGNMENT); 201 if(threeD) { 202 p.setBorder(loweredBorder); 203 } 204 return p; 205 } 206 createVerticalPanel(boolean threeD)207 public JPanel createVerticalPanel(boolean threeD) { 208 JPanel p = new JPanel(); 209 p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); 210 p.setAlignmentY(TOP_ALIGNMENT); 211 p.setAlignmentX(LEFT_ALIGNMENT); 212 if(threeD) { 213 p.setBorder(loweredBorder); 214 } 215 return p; 216 } 217 main(String[] args)218 public static void main(String[] args) { 219 DemoModule demo = new DemoModule(null); 220 demo.mainImpl(); 221 } 222 init()223 public void init() { 224 getContentPane().setLayout(new BorderLayout()); 225 getContentPane().add(getDemoPanel(), BorderLayout.CENTER); 226 } 227 updateDragEnabled(boolean dragEnabled)228 void updateDragEnabled(boolean dragEnabled) {} 229 } 230