home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / VisualTest.java < prev    next >
Text File  |  1997-05-20  |  14KB  |  444 lines

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