home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / awt / test / visualte.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  12.6 KB  |  428 lines

  1. /*
  2.  * @(#)VisualTest.java    1.9 95/09/12 Sami Shaio
  3.  *
  4.  * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. import java.awt.*;
  20.  
  21. public class VisualTest extends Frame {
  22.     boolean    inReshape = false;
  23.     Menu    componentMenu;
  24.     Menu    backgroundMenu;
  25.     Menu    foregroundMenu;
  26.     Menu    shapeMenu;
  27.     Menu    sizeMenu;
  28.     Menu    fontMenu;
  29.     Menu    enableMenu;
  30.     Menu    familyMenu;
  31.     Menu    containerMenu;
  32.  
  33.     int        currentSize = 10;
  34.     Font    currentFont;
  35.     boolean    enableComponents = true;
  36.     Color    currentForeground;
  37.     Color    currentBackground;
  38.     Container    currentContainer;
  39.     Component    component;
  40.     Font    font10;
  41.     Font    font14;
  42.     Font    font24;
  43.     Font    font36;
  44.  
  45.     public VisualTest() {
  46.     super("VisualTest");
  47.  
  48.     MenuBar mb = new MenuBar();
  49.     currentContainer = this;
  50.     componentMenu = new Menu("Component");
  51.     componentMenu.add(new MenuItem("Button"));
  52.     componentMenu.add(new MenuItem("Checkbox"));
  53.     componentMenu.add(new MenuItem("Choice"));
  54.     componentMenu.add(new MenuItem("Label"));
  55.     componentMenu.add(new MenuItem("List"));
  56.     componentMenu.add(new MenuItem("Panel"));
  57.     componentMenu.add(new MenuItem("TextArea"));
  58.     componentMenu.add(new MenuItem("TextField"));
  59.     componentMenu.add(new MenuItem("HScrollbar"));
  60.     componentMenu.add(new MenuItem("VScrollbar"));
  61.     mb.add(componentMenu);
  62.  
  63.     enableMenu = new Menu("Enable/Disable");
  64.     enableMenu.add(new MenuItem("Enable"));
  65.     enableMenu.add(new MenuItem("Disable"));
  66.     mb.add(enableMenu);
  67.  
  68.     fontMenu = new Menu("Font");
  69.     familyMenu = new Menu("Family");
  70.     familyMenu.add(new MenuItem("Courier"));
  71.     familyMenu.add(new MenuItem("Dialog"));
  72.     familyMenu.add(new MenuItem("TimesRoman"));
  73.     familyMenu.add(new MenuItem("Helvetica"));
  74.     familyMenu.add(new MenuItem("Symbol"));
  75.     fontMenu.add(familyMenu);
  76.  
  77.     sizeMenu = new Menu("Size");
  78.     sizeMenu.add(new MenuItem("10"));
  79.     font10 = new Font("Helvetica", Font.PLAIN, 10);
  80.     sizeMenu.add(new MenuItem("14"));
  81.     font14 = new Font("Helvetica", Font.PLAIN, 14);
  82.     sizeMenu.add(new MenuItem("24"));
  83.     font24 = new Font("Helvetica", Font.PLAIN, 24);
  84.     sizeMenu.add(new MenuItem("36"));
  85.     font36 = new Font("Helvetica", Font.PLAIN, 36);
  86.     fontMenu.add(sizeMenu);
  87.  
  88.     mb.add(fontMenu);
  89.  
  90.     shapeMenu = new Menu("Move/Reshape");
  91.     shapeMenu.add(new CheckboxMenuItem("Move"));
  92.     shapeMenu.add(new CheckboxMenuItem("Reshape"));
  93.     mb.add(shapeMenu);
  94.  
  95.     foregroundMenu = new Menu("Foreground");
  96.     foregroundMenu.add(new CheckboxMenuItem("default"));
  97.     foregroundMenu.add(new CheckboxMenuItem("red"));
  98.     foregroundMenu.add(new CheckboxMenuItem("green"));
  99.     foregroundMenu.add(new CheckboxMenuItem("blue"));
  100.     mb.add(foregroundMenu);
  101.  
  102.     backgroundMenu = new Menu("Background");
  103.     backgroundMenu.add(new CheckboxMenuItem("default"));
  104.     backgroundMenu.add(new CheckboxMenuItem("red"));
  105.     backgroundMenu.add(new CheckboxMenuItem("green"));
  106.     backgroundMenu.add(new CheckboxMenuItem("blue"));
  107.     mb.add(backgroundMenu);
  108.  
  109.     containerMenu = new Menu("Container");
  110.     containerMenu.add(new MenuItem("FlowLayout"));
  111.     containerMenu.add(new MenuItem("GridLayout02"));
  112.     containerMenu.add(new MenuItem("GridLayout20"));
  113.     containerMenu.add(new MenuItem("GridLayout03"));
  114.     containerMenu.add(new MenuItem("GridLayout30"));
  115.     containerMenu.add(new MenuItem("BorderLayout"));
  116.     mb.add(containerMenu);
  117.  
  118.     setMenuBar(mb);
  119.     setLayout(null);
  120.  
  121.     currentFont = font10;
  122.     currentForeground = getForeground();
  123.     currentBackground = getBackground();
  124.     enableComponents = true;
  125.     resize(500, 300);
  126.     show();
  127.     }
  128.  
  129.     public boolean handleEvent(Event e) {
  130.     switch (e.id) {
  131.       case Event.WINDOW_DESTROY:
  132.         System.exit(0);
  133.         return true;
  134.       case Event.MOUSE_DOWN:
  135.         currentContainer = this;
  136.         setCurrentComponent(e.x, e.y);
  137.         /* fall into next case */
  138.       case Event.MOUSE_DRAG:
  139.         if (component != null) {
  140.         if (inReshape) {
  141.             Rectangle bounds = component.bounds();
  142.             component.resize(Math.abs(e.x-bounds.x), Math.abs(e.y-bounds.y));
  143.             component.validate();
  144.         } else {
  145.             component.move(e.x, e.y);
  146.         }
  147.         }
  148.         return true;
  149.       case Event.MOUSE_UP:
  150.         currentContainer.validate();
  151.         return true;
  152.       default:
  153.         return super.handleEvent(e);
  154.     }
  155.     }
  156.  
  157.     void setAttributes(Component c) {
  158.     if (c instanceof Container) {
  159.         return;
  160.     }
  161.     c.setForeground(currentForeground);
  162.     c.setBackground(currentBackground);
  163.     c.setFont(currentFont);
  164.     if (enableComponents) {
  165.         c.enable();
  166.     } else {
  167.         c.disable();
  168.     }
  169.     }
  170.  
  171.     int computeDistance(int x, int y, Rectangle r) {
  172.     int mx;
  173.     int my;
  174.  
  175.     mx = x - (r.x + (r.width / 2));
  176.     my = y - (r.y + (r.height / 2));
  177.  
  178.     return (mx*mx) + (my*my);
  179.     }
  180.  
  181.     void setCurrentComponent(int x, int y) {
  182.     int n = countComponents();
  183.     int distance = -1;
  184.  
  185.     for (int i=0; i<n; i++) {
  186.         Component c = getComponent(i);
  187.         Rectangle b = c.bounds();
  188.         int       d;
  189.         
  190.         d = computeDistance(x, y, b);
  191.         if (distance == -1 || d < distance) {
  192.         distance = d;
  193.         component = c;
  194.         }
  195.     }
  196.     }
  197.  
  198.     void setAttributes() {
  199.     int n = countComponents();
  200.  
  201.     for (int i=0; i < n; i++) {
  202.         setAttributes(getComponent(i));
  203.     }
  204.     }
  205.  
  206.     public boolean action(Event e, Object arg) {
  207.     if (e.target instanceof MenuItem) {
  208.         Menu menu = (Menu)(((MenuItem)e.target).getParent());
  209.         String label = (String)arg;
  210.  
  211.         if (menu == backgroundMenu) {
  212.         if (label.equals("red")) {
  213.             currentBackground = Color.red;
  214.         } else if (label.equals("green")) {
  215.             currentBackground = Color.green;
  216.         } else if (label.equals("blue")) {
  217.             currentBackground = Color.blue;
  218.         } else if (label.equals("default")) {
  219.             currentBackground = Color.lightGray;
  220.         }
  221.         } else if (menu == foregroundMenu) {
  222.         if (label.equals("red")) {
  223.             currentForeground = Color.red.darker();
  224.         } else if (label.equals("green")) {
  225.             currentForeground = Color.green.darker();
  226.         } else if (label.equals("blue")) {
  227.             currentForeground = Color.blue.darker();
  228.         } else if (label.equals("default")) {
  229.             currentForeground = Color.black;
  230.         }
  231.         } else if (menu == shapeMenu) {
  232.         if (label.equals("Move")) {
  233.             inReshape = false;
  234.         } else if (label.equals("Reshape")) {
  235.             inReshape = true;
  236.         }
  237.         } else if (menu == sizeMenu) {
  238.         if (label.equals("10")) {
  239.             currentFont = font10;
  240.         } else if (label.equals("14")) {
  241.             currentFont = font14;
  242.         } else if (label.equals("24")) {
  243.             currentFont = font24;
  244.         } else if (label.equals("36")) {
  245.             currentFont = font36;
  246.         }
  247.         } else if (menu == familyMenu) {
  248.         font10 = new Font(label, Font.PLAIN, 10);
  249.         font14 = new Font(label, Font.PLAIN, 14);
  250.         font24 = new Font(label, Font.PLAIN, 24);
  251.         font36 = new Font(label, Font.PLAIN, 36);
  252.         switch (currentSize) {
  253.           case 10:
  254.           default:
  255.             currentFont = font10;
  256.             break;
  257.           case 14:
  258.             currentFont = font14;
  259.             break;
  260.           case 24:
  261.             currentFont = font24;
  262.             break;
  263.           case 36:
  264.             currentFont = font36;
  265.             break;
  266.         }
  267.         } else if (menu == enableMenu) {
  268.         if (label.equals("Enable")) {
  269.             enableComponents = true;
  270.         } else if (label.equals("Disable")) {
  271.             enableComponents = false;
  272.         }
  273.         } else if (menu == componentMenu) {
  274.         Component component;
  275.  
  276.         if (label.equalsIgnoreCase("Button")) {
  277.             component = new Button("Button");
  278.         } else if (label.equalsIgnoreCase("Label")) {
  279.             component = new Label("label");
  280.         } else if (label.equalsIgnoreCase("TextField")) {
  281.             component = new TextField("textfield");
  282.         } else if (label.equalsIgnoreCase("Choice")) {
  283.             component = new Choice();
  284.             ((Choice)component).addItem("Choice");
  285.         } else if (label.equalsIgnoreCase("List")) {
  286.             component = new List(4, false);
  287.             ((List)component).addItem("List1");
  288.             ((List)component).addItem("List2");
  289.             ((List)component).addItem("List3");
  290.             ((List)component).addItem("List4");
  291.             ((List)component).addItem("List5");
  292.             currentContainer.add(component);
  293.         } else if (label.equalsIgnoreCase("TextArea")) {
  294.             component = new TextArea(5, 15);
  295.             ((TextArea)component).setText("TextArea");
  296.         } else if (label.equalsIgnoreCase("Checkbox")) {
  297.             component = new Checkbox("Checkbox");
  298.         } else if (label.equalsIgnoreCase("Panel")) {
  299.             component = new VPanel(this);
  300.         } else if (label.equalsIgnoreCase("HScrollbar")) {
  301.             component = new Scrollbar(Scrollbar.HORIZONTAL);
  302.         } else if (label.equalsIgnoreCase("VScrollbar")) {
  303.             component = new Scrollbar(Scrollbar.VERTICAL);
  304.         } else {
  305.             component = new Button("Button");
  306.         }
  307.         if (! (component instanceof Container)) {
  308.             Dimension d = component.preferredSize();
  309.             component.reshape(10, 10, d.width, d.height);
  310.         }
  311.         currentContainer.add(component);
  312.         currentContainer.validate();
  313.         } else if (menu == containerMenu) {
  314.         if (currentContainer != this) {
  315.             if (label.equalsIgnoreCase("FlowLayout")) {
  316.             currentContainer.setLayout(new FlowLayout());
  317.             } else if (label.equalsIgnoreCase("GridLayout02")) {
  318.             currentContainer.setLayout(new GridLayout(0,2));
  319.             } else if (label.equalsIgnoreCase("GridLayout20")) {
  320.             currentContainer.setLayout(new GridLayout(2,0));
  321.             } else if (label.equalsIgnoreCase("GridLayout03")) {
  322.             currentContainer.setLayout(new GridLayout(0,3));
  323.             } else if (label.equalsIgnoreCase("GridLayout30")) {
  324.             currentContainer.setLayout(new GridLayout(3, 0));
  325.             } else if (label.equalsIgnoreCase("BorderLayout")) {
  326.             currentContainer.setLayout(new BorderLayout());
  327.             Component comp1;
  328.             Component comp2;
  329.             Component comp3;
  330.             Component comp4;
  331.             Component comp5;
  332.             switch (currentContainer.countComponents()) {
  333.               case 1:
  334.                 comp1 = currentContainer.getComponent(0);
  335.                 currentContainer.remove(comp1);
  336.                 currentContainer.add("Center", comp1);
  337.                 break;
  338.               case 2:
  339.                 comp1 = currentContainer.getComponent(0);
  340.                 comp2 = currentContainer.getComponent(1);
  341.  
  342.                 currentContainer.remove(comp1);
  343.                 currentContainer.remove(comp2);
  344.  
  345.                 currentContainer.add("North", comp1);
  346.                 currentContainer.add("Center", comp2);
  347.                 break;
  348.               case 3:
  349.                 comp1 = currentContainer.getComponent(0);
  350.                 comp2 = currentContainer.getComponent(1);
  351.                 comp3 = currentContainer.getComponent(2);
  352.                 currentContainer.remove(comp1);
  353.                 currentContainer.remove(comp2);
  354.                 currentContainer.remove(comp3);
  355.  
  356.                 currentContainer.add("North", comp1);
  357.                 currentContainer.add("South", comp2);
  358.                 currentContainer.add("Center", comp3);
  359.                 break;
  360.               case 4:
  361.                 comp1 = currentContainer.getComponent(0);
  362.                 comp2 = currentContainer.getComponent(1);
  363.                 comp3 = currentContainer.getComponent(2);
  364.                 comp4 = currentContainer.getComponent(3);
  365.  
  366.                 currentContainer.remove(comp1);
  367.                 currentContainer.remove(comp2);
  368.                 currentContainer.remove(comp3);
  369.                 currentContainer.remove(comp4);
  370.  
  371.                 currentContainer.add("North", comp1);
  372.                 currentContainer.add("South", comp2);
  373.                 currentContainer.add("East", comp3);
  374.                 currentContainer.add("Center", comp4);
  375.                 break;
  376.               case 5:
  377.               default:
  378.                 comp1 = currentContainer.getComponent(0);
  379.                 comp2 = currentContainer.getComponent(1);
  380.                 comp3 = currentContainer.getComponent(2);
  381.                 comp4 = currentContainer.getComponent(3);
  382.                 comp5 = currentContainer.getComponent(4);
  383.  
  384.                 currentContainer.remove(comp1);
  385.                 currentContainer.remove(comp2);
  386.                 currentContainer.remove(comp3);
  387.                 currentContainer.remove(comp4);
  388.                 currentContainer.remove(comp5);
  389.  
  390.                 currentContainer.add("North", comp1);
  391.                 currentContainer.add("South", comp2);
  392.                 currentContainer.add("East", comp3);
  393.                 currentContainer.add("West", comp4);
  394.                 currentContainer.add("Center", comp5);
  395.                 break;
  396.             }
  397.             }
  398.             currentContainer.validate();
  399.         }
  400.         }
  401.         setAttributes();
  402.         return true;
  403.     }
  404.     return false;
  405.     }
  406.  
  407.     public static void main(String args[]) {
  408.     new VisualTest();
  409.     }
  410. }
  411.  
  412. class VPanel extends Panel {
  413.     VisualTest    target;
  414.  
  415.     public VPanel(VisualTest target) {
  416.     this.target = target;
  417.     setBackground(target.getBackground().darker());
  418.     resize(100, 100);
  419.     }
  420.  
  421.     public boolean mouseDown(Event evt, int x, int y) {
  422.     target.currentContainer = this;
  423.     target.containerMenu.enable();
  424.     return true;
  425.     }
  426. }
  427.  
  428.