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

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