home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / sdkjava.exe / SDKJava.cab / Samples / AFC / Statics / Src / SDKCtrlPnl.java < prev    next >
Encoding:
Java Source  |  1998-03-05  |  9.3 KB  |  328 lines

  1. //
  2. // (c) 1998 Microsoft Corporation.  All rights reserved.
  3. //
  4. import java.awt.Event;
  5. import java.awt.Insets;
  6. import java.awt.Image;
  7. import com.ms.ui.*;
  8. import com.ms.fx.*;
  9.  
  10. public class SDKCtrlPnl extends UIPanel implements SDKConsts, SDKCallbacks
  11. {
  12.     private UIGroup main, dspgroup, dtxtgroup;
  13.     private UIPanel otherpnl, padding;
  14.     private UIDrawText dtxt;
  15.     private UIStatus status;
  16.     private SDKInsetPanel display;
  17.     private SDKInsetPanelBL stspnl;
  18.     private ImagePanel imgpnl;
  19.     private UIItem item;
  20.     private String name;
  21.     private Image image;
  22.     private BtnPanel btns;
  23.     private int position, imagepos, type;
  24.     private boolean drawgrp, drawtxt;
  25.  
  26.     public SDKCtrlPnl(UIApplet applet, UIFrame frame)
  27.     {
  28.         SDKImages.init(applet, frame);
  29.  
  30.         setLayout(new UIBorderLayout(0,0));
  31.  
  32.         // Create main UIGroup, add to this UIPanel
  33.         main = new UIGroup("Van de Graafics (STATICS)");
  34.         add(main, "Center");
  35.  
  36.         // Create button UIPanel
  37.         btns = new BtnPanel(this);
  38.  
  39.         // Create UIITem
  40.         image = SDKImages.get(SHIP); name = "Santa Maria";    
  41.         position = UIStatic.CENTERED;
  42.         imagepos = UIItem.ABOVE; type = T_ITEM;
  43.         item = new UIItem(image, name, position, imagepos);
  44.         item.setFont(new FxFont("Helvetica", FxFont.BOLD, 16));
  45.         item.setForeground(new FxColor(255,255,0));
  46.         item.setBackground(new FxTexture(SDKImages.get(OCEAN), FxTexture.STRETCH_NONE, 
  47.                                         0, 0, -1, -1, false, 210, 184, 155));
  48.  
  49.         // Create UIGroup for display and drawtext
  50.         dspgroup = new UIGroup(STR_VOYAGE); dtxtgroup = new UIGroup(STR_TALE);
  51.         dspgroup.setLayout(new UIBorderLayout(0,0));
  52.         dtxtgroup.setLayout(new UIBorderLayout(0,0));
  53.  
  54.         // Create UIStatus for display
  55.         status = new UIStatus("The ship is located in the center");
  56.  
  57.         // Create UIDrawText for display
  58.         dtxt = new UIDrawText(STR_DRAWTEXT);
  59.         dtxt.setWordWrap(IFxTextConstants.wwKeepWordIntact);
  60.         dtxtgroup.add(dtxt, "Center");
  61.  
  62.         // Create UIPanel for UIDrawText and StatusPanel
  63.         otherpnl = new UIPanel();
  64.         otherpnl.setLayout(new UISplitLayout(UISplitLayout.HORIZONTAL, 75));
  65.  
  66.         // Create status and padding UIPanel for UIStatus
  67.         stspnl = new SDKInsetPanelBL(8,0,0,0);
  68.         stspnl.add(status, "Center");
  69.         padding = new UIPanel();
  70.         
  71.         // Create display UIPanel
  72.         display = new SDKInsetPanel(0,10,0,0);
  73.         display.setLayout(new UISplitLayout(UISplitLayout.HORIZONTAL, 240));
  74.  
  75.         // Create image UIPanel
  76.         imgpnl = new ImagePanel(this);
  77.         imgpnl.setLayout(new UIBorderLayout(0,0));
  78.  
  79.         drawgrp = true; drawtxt = true;
  80.         setDisplay(S_FIRSTTIME);
  81.  
  82.         main.setLayout(new UISplitLayout(0,150));
  83.         // Add button panel on left and display panel on right.
  84.         main.add(btns, "nw"); main.add(display, "se");
  85.     }
  86.  
  87.     public void setDisplay(int state)
  88.     {
  89.         switch ( state ) {
  90.         case S_FIRSTTIME:
  91.             dspgroup.add(item, "Center"); 
  92.             imgpnl.add(dspgroup, "Center");
  93.             display.add(imgpnl, "nw"); 
  94.             display.add(otherpnl, "se");
  95.             otherpnl.add(dtxtgroup, "nw"); 
  96.             otherpnl.add(stspnl, "se");
  97.             break;
  98.         case S_GRP:
  99.             drawgrp = true; imgpnl.remove(item);
  100.             dspgroup.add(item, "Center"); imgpnl.add(dspgroup, "Center");
  101.             break;
  102.         case S_NO_GRP:
  103.             drawgrp = false;
  104.             dspgroup.remove(item); imgpnl.remove(dspgroup);
  105.             imgpnl.add(item, "Center");
  106.             break;
  107.         case S_DRAWTXT:
  108.             drawtxt = true;
  109.             display.removeAll();
  110.             display.setLayout(new UISplitLayout(UISplitLayout.HORIZONTAL, 240));
  111.             display.add(imgpnl, "nw"); display.add(otherpnl, "se");
  112.             otherpnl.add(dtxtgroup, "nw"); 
  113.             break;
  114.         case S_NO_DRAWTXT:
  115.             drawtxt = false;
  116.             otherpnl.remove(dtxtgroup); display.removeAll();
  117.             display.setLayout(new UISplitLayout(UISplitLayout.HORIZONTAL, 315));
  118.             display.add(imgpnl, "nw"); display.add(otherpnl, "se");
  119.             break;
  120.         case S_STATUS:
  121.             otherpnl.remove(padding);
  122.             otherpnl.add(stspnl, "se");
  123.             break;
  124.         case S_NO_STATUS:
  125.             otherpnl.remove(stspnl);
  126.             otherpnl.add(padding, "se");
  127.             break;
  128.         }
  129.     }
  130.         
  131.     public void setItemPosition(int pos)
  132.     {
  133.         if ( pos != position ) {
  134.             item.setFlags( pos );
  135.             position = pos;
  136.             String str = new String("The ship is ");
  137.             switch ( pos ) {
  138.             case UIItem.TOPLEFT: str += "located in the top left corner"; break;
  139.             case UIItem.BOTTOMLEFT: str += "located in the bottom left corner"; break;
  140.             case UIItem.LEFT: str += "centered on the left"; break;
  141.             case UIItem.TOPRIGHT: str += "located in the top right corner"; break;
  142.             case UIItem.BOTTOMRIGHT: str += "located in the bottom right corner"; break;
  143.             case UIItem.RIGHT: str += "centered on the right"; break;
  144.             case UIItem.TOP: str += "centered at the top"; break;
  145.             case UIItem.BOTTOM: str += "centered at the bottom"; break;
  146.             case UIItem.CENTERED: str += "located in the center"; break;
  147.             }
  148.             status.setName(str);
  149.         }
  150.     }
  151.  
  152.     public void setItemType(int type)
  153.     {
  154.         if ( type != this.type ) {
  155.             this.type = type;
  156.             switch ( type ) {
  157.             case T_TEXT: item.setName(name); item.setImage(null); break;
  158.             case T_GRAPHIC: item.setName(""); item.setImage(image); break;
  159.             case T_ITEM: item.setName(name); item.setImage(image); break;
  160.             }
  161.         }
  162.     }
  163.  
  164.     public void setImagePos(int imagepos)
  165.     {
  166.         if ( imagepos != this.imagepos ) {
  167.             this.imagepos = imagepos;
  168.             item.setImagePos(imagepos);
  169.         }
  170.     }
  171.  
  172.     public boolean isDrawGrp() { return drawgrp; }
  173.     public boolean isDrawTxt() { return drawtxt; }
  174.     public Insets getInsets() { return new Insets(0,5,5,5); }
  175. }
  176.  
  177. class BtnPanel extends UIPanel implements SDKConsts
  178. {
  179.     private UIRadioButton uit, uig, uii, above, onleft;
  180.     private UICheckButton uigrp, uid, uis;
  181.  
  182.     private SDKCallbacks ctrl;
  183.     private UIRadioGroup statics, style;
  184.     private UIGroup align;
  185.     private UICheckGroup other;
  186.     private UIPanel panel;
  187.  
  188.     public BtnPanel(SDKCallbacks ctrl)
  189.     {
  190.         this.ctrl = ctrl;
  191.  
  192.         setLayout(new UIVerticalFlowLayout(UIVerticalFlowLayout.FILL, 0));
  193.         statics = new UIRadioGroup("Static Components");
  194.         style = new UIRadioGroup("Image Placement");
  195.         align = new UIGroup("Alignment");
  196.         other = new UICheckGroup("Other Statics");
  197.  
  198.         add(statics); add(style); add(align); add(other);
  199.  
  200.         uit = (UIRadioButton)statics.add("UIText");
  201.         uig = (UIRadioButton)statics.add("UIGraphic");
  202.         uii = (UIRadioButton)statics.add("UIItem"); uii.setChecked(true);
  203.  
  204.         above = (UIRadioButton)style.add("ABOVE"); above.setChecked(true);
  205.         onleft = (UIRadioButton)style.add("ONLEFT");
  206.  
  207.         align.setLayout(new UIBorderLayout(0,0));
  208.         align.add(new MyUIGraphic(SDKImages.get(COMPASS), ctrl), "Center");
  209.  
  210.         uigrp = (UICheckButton)other.add("UIGroup"); uigrp.setChecked(true);
  211.         uid = (UICheckButton)other.add("UIDrawText"); uid.setChecked(true);
  212.         uis = (UICheckButton)other.add("UIStatus");  uis.setChecked(true);
  213.     }
  214.  
  215.     public boolean handleEvent(Event e)
  216.     {
  217.         if ( e.id == e.LIST_SELECT ) {
  218.             // need to test radiobtn first since it extends checkbtn
  219.             if ( e.arg instanceof UIRadioButton ) {
  220.                 if ( e.arg == uit ) {
  221.                     above.setEnabled(false); onleft.setEnabled(false); 
  222.                     ctrl.setItemType(T_TEXT);
  223.                 }
  224.                 else if ( e.arg == uig ) {
  225.                     above.setEnabled(false); onleft.setEnabled(false); 
  226.                     ctrl.setItemType(T_GRAPHIC);
  227.                 }
  228.                 else if ( e.arg == uii ) {
  229.                     above.setEnabled(true); onleft.setEnabled(true); 
  230.                     ctrl.setItemType(T_ITEM);
  231.                 }
  232.                 else if ( e.arg == above )
  233.                     ctrl.setImagePos(UIItem.ABOVE);
  234.                 else if ( e.arg == onleft )
  235.                     ctrl.setImagePos(UIItem.ONLEFT);
  236.             }
  237.             else if ( e.arg instanceof UICheckButton ) {
  238.                 if ( e.arg == uigrp )
  239.                     ctrl.setDisplay(S_GRP);
  240.                 else if ( e.arg == uid )
  241.                     ctrl.setDisplay(S_DRAWTXT);
  242.                 else if ( e.arg == uis )
  243.                     ctrl.setDisplay(S_STATUS);
  244.             }
  245.         }
  246.         else if ( e.id == e.LIST_DESELECT ) {
  247.             if ( e.arg instanceof UICheckButton ) {
  248.                 if ( e.arg == uigrp )
  249.                     ctrl.setDisplay(S_NO_GRP);
  250.                 else if ( e.arg == uid )
  251.                     ctrl.setDisplay(S_NO_DRAWTXT);
  252.                 else if ( e.arg == uis )
  253.                     ctrl.setDisplay(S_NO_STATUS);
  254.             }
  255.         }
  256.         return(super.handleEvent(e));
  257.     }
  258. }
  259.  
  260. class MyUIGraphic extends UIGraphic
  261. {
  262.     private SDKCallbacks ctrl;
  263.  
  264.     public MyUIGraphic(Image img, SDKCallbacks ctrl)
  265.     {
  266.         super(img);
  267.         this.ctrl = ctrl;
  268.     }
  269.  
  270.     public boolean mouseDrag(Event evt, int x, int y)
  271.     {
  272.         setPos(x ,y);
  273.         return super.mouseDrag(evt, x, y);
  274.     }
  275.  
  276.     public boolean mouseDown(Event evt, int x, int y)
  277.     {
  278.         setPos(x ,y);
  279.         return super.mouseDown(evt, x, y);
  280.     }
  281.  
  282.     private void setPos(int x, int y)
  283.     {
  284.         if ( x < 40 ) {
  285.             if ( y < 25 ) ctrl.setItemPosition(UIItem.TOPLEFT);
  286.             else if ( y > 50) ctrl.setItemPosition(UIItem.BOTTOMLEFT);
  287.             else ctrl.setItemPosition(UIItem.LEFT);
  288.         }
  289.         else if ( x > 80 ) {
  290.             if ( y < 25 ) ctrl.setItemPosition(UIItem.TOPRIGHT);
  291.             else if ( y > 50) ctrl.setItemPosition(UIItem.BOTTOMRIGHT);
  292.             else ctrl.setItemPosition(UIItem.RIGHT);
  293.         }
  294.         else {
  295.             if ( y < 25 ) ctrl.setItemPosition(UIItem.TOP);
  296.             else if ( y > 50) ctrl.setItemPosition(UIItem.BOTTOM);
  297.             else ctrl.setItemPosition(UIItem.CENTERED);
  298.         }
  299.     }
  300. }
  301.  
  302. class ImagePanel extends UIPanel
  303. {
  304.     private SDKCallbacks ctrl;
  305.  
  306.     public ImagePanel(SDKCallbacks ctrl)
  307.     {
  308.         super();
  309.         this.ctrl = ctrl;
  310.     }
  311.  
  312.     public Insets getInsets()
  313.     {
  314.         if ( ctrl.isDrawGrp() ) {
  315.             if ( ctrl.isDrawTxt() )
  316.                 return new Insets(0,0,0,0);
  317.             else
  318.                 return new Insets(0,0,75,0);
  319.         }
  320.         else {
  321.             if ( ctrl.isDrawTxt() )
  322.                 return new Insets(18,6,6,6);
  323.             else
  324.                 return new Insets(18,6,81,6);
  325.         }
  326.     }
  327. }
  328.