home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / Windows / Win32ResSupport / GuiApp.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  11.5 KB  |  554 lines

  1. //
  2. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  3. //
  4. //
  5.  
  6. // For UI
  7. import java.applet.*;
  8. import java.awt.*;
  9. import com.ms.ui.*;
  10. import com.ms.fx.*;
  11.  
  12. // For events.
  13. import java.awt.Event;
  14.  
  15. // For resources
  16. import com.ms.ui.resource.*;
  17. import java.io.InputStream;
  18. import java.util.Date;
  19.  
  20. //
  21. // For the file
  22. //
  23. import java.io.File;
  24.  
  25. public class GuiApp extends UIApplet implements Runnable
  26. {
  27.     //
  28.     // For more UI - these are used.
  29.     //
  30.     private GuiAppFrame m_frame;
  31.     private    Thread        m_GuiApp = null;
  32.  
  33.     public static void main(String args[])
  34.     {
  35.         GuiApp App=new GuiApp();
  36.         App.init();
  37.         App.start();
  38.     }
  39.  
  40.     public GuiApp()
  41.     {
  42.     }
  43.  
  44.     public String getAppletInfo()
  45.     {
  46.         return "Name: GuiApp\r\n" +
  47.                "Created with Microsoft Visual J++ Version 1.0";
  48.     }
  49.  
  50.     public void init()
  51.     {
  52.         m_frame=new GuiAppFrame("SomeTitle",this);
  53.         doProperties();
  54.         System.exit(1);
  55.     }
  56.  
  57.     public void destroy()
  58.     {
  59.     }
  60.  
  61.     public void start()
  62.     {
  63.         if (m_GuiApp == null)
  64.         {
  65.             m_GuiApp = new Thread(this);
  66.             m_GuiApp.start();
  67.         }
  68.     }
  69.     
  70.     public void stop()
  71.     {
  72.         if (m_GuiApp != null)
  73.         {
  74.             m_GuiApp.stop();
  75.             m_GuiApp = null;
  76.         }
  77.     }
  78.  
  79.     //
  80.     // My app doesn't do anything.
  81.     //
  82.     public void run()
  83.     {
  84.         try
  85.         {
  86.             while (true)
  87.             {
  88.                 Thread.sleep(300);
  89.             }
  90.         }
  91.         catch (InterruptedException e)
  92.         {
  93.             stop();
  94.         }
  95.     }
  96.  
  97.     private ExamplePropertyDialog sheet;
  98.  
  99.     private void doProperties()
  100.     {
  101.         // Explicitly set the font so that all systems
  102.         // will realize what font is used in the
  103.         // upcoming property sheet
  104.         Win32ResourceDecoder res = ExamplePropertyDialog.getResources();
  105.         ResourcePropertyPage template = new ResourcePropertyPage(res,
  106.             ExamplePropertyDialog.IDD_BUTTONS);
  107.         template.addContent();
  108.  
  109.         Font oldFont = m_frame.getFont();
  110.         m_frame.setFont(template.getFont());
  111.         sheet=new ExamplePropertyDialog(m_frame,"Resource Tester",true,res);
  112.         m_frame.setFont(oldFont);
  113.  
  114.         sheet.display();
  115.     }
  116. }
  117.  
  118. //
  119. // This is a property Dialog - holds property pages.
  120. //
  121. class ExamplePropertyDialog extends UIPropertyDialog
  122. {
  123.     public static int IDD_BUTTONS    =102;
  124.     public static int IDD_STATICS    =103;
  125.     public static int IDD_GROUPS    =104;
  126.     public static int IDD_BOXES        =105;
  127.     public static int IDD_EDIT        =106;
  128.     public static int IDD_PROGRESS    =107;
  129.     public static int IDD_MULTILINE    =108;
  130.     public static int IDD_COMBOS    =109;
  131.     public static int IDD_USERCLASS    =110;
  132.  
  133.     //
  134.     // The resources needed
  135.     //
  136.     private static Win32ResourceDecoder resources;
  137.  
  138.     //
  139.     //
  140.     //
  141.     public static  int IDS_FILETYPE=18;
  142.     public static  int IDS_BUTTONS=19;
  143.     public static  int IDS_EDIT=20;
  144.     public static  int IDS_MULTILINE=21;
  145.     public static  int IDS_COMBOS=22;
  146.     public static  int IDS_BOXES=23;
  147.     public static  int IDS_GROUPS=24;
  148.     public static  int IDS_STATICS=25;
  149.     public static  int IDS_USER=26;
  150.     public static  int IDS_PROGRESS=27;
  151.  
  152.     private Dimension m_size;
  153.  
  154.     int getCharWidth(IUIContainer parent)
  155.     {
  156.         Font f = parent.getFont();
  157.         if(f == null)
  158.             f = new FxFont("Dialog", Font.PLAIN, 8);
  159.  
  160.         FontMetrics m = parent.getFontMetrics(f);
  161.         String s     = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  162.         int    width = m.stringWidth(s) / s.length();
  163.  
  164.         if (width <= 0)
  165.             width = 1;
  166.         return width;
  167.     }
  168.  
  169.     int getCharHeight(IUIContainer parent)
  170.     {
  171.         FontMetrics m = parent.getFontMetrics(parent.getFont());
  172.         int height = m.getHeight();
  173.         return height;
  174.     }
  175.  
  176.     void mapDimension(Dimension dim, int charWidth, int charHeight)
  177.     {
  178.         dim.width  = (dim.width  * 4 + charWidth  - 1) / charWidth;
  179.         dim.height = (dim.height * 8 + charHeight - 1) / charHeight;
  180.     }
  181.  
  182.     ExamplePropertyDialog(UIFrame frame, String title, boolean modal,
  183.             Win32ResourceDecoder res)
  184.     {
  185.         super(frame,title,modal);
  186.  
  187.         m_size = new Dimension(100, 100);
  188.  
  189.         // addPage( getString(IDS_FILETYPE),    new FileGeneral(res, "c:\\win\\win.ini") );
  190.         addPage( getString(IDS_BUTTONS),    new ResourcePropertyPage( res, IDD_BUTTONS));
  191.         addPage( getString(IDS_EDIT),        new ResourcePropertyPage( res, IDD_EDIT));
  192.         addPage( getString(IDS_MULTILINE),    new ResourcePropertyPage( res, IDD_MULTILINE));
  193.         addPage( getString(IDS_COMBOS),        new ComboTestPage( res, IDD_COMBOS));
  194.         addPage( getString(IDS_BOXES),        new ResourcePropertyPage( res, IDD_BOXES));
  195.         // addPage( getString(IDS_GROUPS),        new ResourcePropertyPage( res, IDD_GROUPS));
  196.         addPage( getString(IDS_STATICS),    new ResourcePropertyPage( res, IDD_STATICS));
  197.         // addPage( getString(IDS_USER),        new ResourcePropertyPage( res, IDD_USERCLASS));
  198.         // addPage( getString(IDS_PROGRESS),    new ResourcePropertyPage( res, IDD_PROGRESS));
  199.  
  200.         mapDimension(m_size, getCharWidth(frame), getCharHeight(frame));
  201.     }
  202.  
  203.     public void addPage(String name, IUIComponent comp)
  204.     {
  205.         super.addPage(name, comp);
  206.  
  207.         if (comp instanceof UIPropertyPage)
  208.         {
  209.             ((UIPropertyPage)comp).addContent();
  210.         }
  211.  
  212.         Dimension size = comp.getPreferredSize();
  213.         m_size.width  = Math.max(m_size.width , size.width );
  214.         m_size.height = Math.max(m_size.height, size.height);
  215.     }
  216.  
  217.     //
  218.     // Returns a valid resource stream
  219.     //
  220.     public static Win32ResourceDecoder getResources()
  221.     {
  222.         if(resources==null)
  223.         {
  224.             Class cl=ExamplePropertyDialog.class;
  225.             InputStream is=cl.getResourceAsStream("strtable.res");
  226.             try
  227.             {
  228.                 resources=new Win32ResourceDecoder( is );
  229.             }
  230.             catch (Exception e)
  231.             {
  232.                 e.printStackTrace();
  233.             }
  234.         }
  235.         return resources;
  236.     }
  237.  
  238.     //
  239.     // Adds a page - rather lame really
  240.     //
  241.     private void addDialog(int id)
  242.     {
  243.         UIPanel p=new UIPanel();
  244.         populate(p, id);
  245.         addPage( p );
  246.     }
  247.  
  248.     //
  249.     // Given a Panel (UIPropertyPage) and an ID, it will populates it for you.
  250.     //
  251.     public void populate(UIPanel uip, int id)
  252.     {
  253.         Win32ResourceDecoder res=getResources();
  254.         try
  255.         {
  256.             res.populateDialog(uip, id);
  257.         }
  258.         catch (Exception e)
  259.         {
  260.             System.out.println("Problem "+e);
  261.         }
  262.     }
  263.  
  264.     //
  265.     // This property dialog has a Custom Size
  266.     //
  267.     public Dimension getPageDimension()
  268.     {
  269.         return new Dimension(m_size);
  270.     }
  271.  
  272.     public boolean doCancelAction(Event e, Object o)
  273.     {
  274.         System.out.println("Do Cancel");
  275.         dispose();
  276.         return false;
  277.     }
  278.  
  279.     private String getString(int id)
  280.     {
  281.         Win32ResourceDecoder res=getResources();
  282.         String st="";
  283.         
  284.         try{
  285.             st=res.getString(id); 
  286.         }
  287.         catch (Exception e)
  288.         {
  289.         }
  290.         return st;
  291.     }
  292. }
  293.  
  294. //
  295. // A PropertyPage that understands it comes from a resource file
  296. //
  297. class ResourcePropertyPage extends UIPropertyPage
  298. {
  299.     //
  300.     //
  301.     //
  302.     private Win32ResourceDecoder res;
  303.     private int id;
  304.     private boolean contentAdded = false;
  305.  
  306.     ResourcePropertyPage( Win32ResourceDecoder res, int id)
  307.     {
  308.         this.res = res;
  309.         this.id  = id;
  310.     }
  311.  
  312.     public boolean addContent()
  313.     {
  314.         if (contentAdded)
  315.         {
  316.             return(true);
  317.         }
  318.  
  319.         try
  320.         {
  321.             res.populateDialog(this, id);
  322.         }
  323.         catch ( Exception e)
  324.         {
  325.         }
  326.  
  327.         contentAdded = true;
  328.         return true;
  329.     }
  330.  
  331.     public void setText(int id, String text)
  332.     {
  333.         IUIComponent comp=getComponentFromID(id);
  334.         if(comp!=null)
  335.         {
  336.             if(comp instanceof UIScrollViewer)
  337.                 comp = ((UIScrollViewer)comp).getContent();
  338.  
  339.             if(comp instanceof IUIContainer)
  340.             {
  341.                 IUIContainer uic=(IUIContainer)comp;
  342.                 int i=0;
  343.                 try
  344.                 {
  345.                     while(i>=0)
  346.                     {
  347.                         comp = uic.getChild(i);
  348.                         if(comp instanceof UIDrawText)
  349.                         {
  350.                             ((UIDrawText)comp).setValueText(text);
  351.                             return;
  352.                         }
  353.                         i++;
  354.                     }
  355.                 }
  356.                 catch(Exception e)
  357.                 {
  358.                 }
  359.             }
  360.             if(comp instanceof UIEdit)
  361.                 ((UIEdit)comp).setValueText(text);
  362.         }
  363.     }
  364. }
  365.  
  366. //
  367. // General file properties tab.
  368. //
  369. class FileGeneral extends ResourcePropertyPage
  370. {
  371.     public static int IDD_FILEINFO    =101;
  372.     static final int ID_NAME=101;
  373.     static final int ID_PATH=102;
  374.     static final int ID_LOCATION=103;
  375.     static final int ID_SIZE=104;
  376.     static final int ID_MODIFIED=108;
  377.     static final int ID_READABLE=1000;
  378.     static final int ID_WRITEABLE=1001;
  379.  
  380.     File file;    // the file we are looking at.
  381.     FileGeneral(Win32ResourceDecoder res, String fileName)
  382.     {
  383.         super(res, IDD_FILEINFO );
  384.         this.file=new File(fileName);
  385.     }
  386.  
  387.     FileGeneral(Win32ResourceDecoder res, File Name)
  388.     {
  389.         super(res, IDD_FILEINFO );
  390.         this.file=Name;
  391.     }
  392.  
  393.     //
  394.     // We actually populate the content for this dialog
  395.     //
  396.     public boolean addContent()
  397.     {
  398.         if(super.addContent() == false)
  399.             return false;
  400.  
  401.         try
  402.         {
  403.             setText( ID_NAME, file.getName() );
  404.             setText( ID_PATH,file.getPath() );
  405.             setText( ID_LOCATION,file.getParent() );
  406.             setText( ID_SIZE,""+file.length());
  407.             Date  date=new Date( file.lastModified() );
  408.             setText( ID_MODIFIED,date.toString());
  409.             getComponentFromID( ID_READABLE).setChecked( file.canRead() );
  410.             getComponentFromID( ID_WRITEABLE).setChecked( file.canWrite() );
  411.         }
  412.         catch (Exception e)
  413.         {
  414.             System.out.println("Problem with controls "+e);
  415.         }
  416.         return true;
  417.     }
  418. }
  419.  
  420.  
  421. //
  422. //
  423. //
  424. class ComboTestPage extends ResourcePropertyPage
  425. {
  426.     static final int ID_DROPDOWN=1000;
  427.     static final int ID_SIMPLE=1002;
  428.     static final int ID_DROPLIST=1003;
  429.  
  430.  
  431.     static final int ID_LISTSINGLE=1001;
  432.     static final int ID_LISTMULTIPLE=1004;
  433.     static final int ID_LISTEXTENDED=1005;
  434.     static final int ID_LISTTABBED=1006;
  435.     static final int ID_LISTNOSEL=1008;
  436.  
  437.     File file;    // the file we are looking at.
  438.     ComboTestPage(Win32ResourceDecoder res, int id)
  439.     {
  440.         super(res, id );
  441.     }
  442.  
  443.     //
  444.     // We actually populate the content for this dialog
  445.     //
  446.     public boolean addContent()
  447.     {
  448.         if(super.addContent() == false)
  449.             return false;
  450.  
  451.         try
  452.         {
  453.             fillCombo(ID_DROPDOWN);
  454.             fillCombo(ID_SIMPLE);
  455.             fillCombo(ID_DROPLIST);
  456.  
  457.             fillList(ID_LISTSINGLE);
  458.             fillList(ID_LISTMULTIPLE);
  459.             fillList(ID_LISTEXTENDED);
  460.             fillList(ID_LISTTABBED,true);
  461.             fillList(ID_LISTNOSEL);
  462.         }
  463.         catch (Exception e)
  464.         {
  465.             System.out.println("Problem with controls "+e);
  466.         }
  467.         return true;
  468.     }
  469.  
  470.     private void fillCombo(int id)
  471.     {
  472.         fillCombo(id, false);
  473.     }
  474.  
  475.     private void fillList(int id)
  476.     {
  477.         fillList(id, false);
  478.     }
  479.  
  480.     private void fillCombo(int id, boolean tabs)
  481.     {
  482.         try
  483.         {
  484.             UIChoice uic=(UIChoice)getComponentFromID(id);
  485.             for(int i=0;i<10;i++)
  486.             {
  487.                 if(tabs)
  488.                     uic.addString("Item\t"+i+"\t Tabbed");
  489.                 else
  490.                     uic.addString("Item "+i);
  491.             }
  492.         }
  493.         catch ( Exception e)
  494.         {
  495.             System.out.println(e);
  496.         }
  497.     }
  498.  
  499.     private void fillList(int id, boolean tabs)
  500.     {
  501.         try
  502.         {
  503.             UIScrollViewer uisc=(UIScrollViewer)getComponentFromID(id);
  504.             UIList uic=(UIList)uisc.getContent();
  505.             for(int i=0;i<10;i++)
  506.             {
  507.                 if(tabs)
  508.                     uic.add("Item\t"+i+"\t Tabbed");
  509.                 else
  510.                     uic.add("Item "+i);
  511.             }
  512.         }
  513.         catch ( Exception e)
  514.         {
  515.             System.out.println(e);
  516.         }
  517.     }
  518.  
  519. }
  520.  
  521. //==============================================================================
  522. // STANDALONE APPLICATION SUPPORT
  523. //     This frame class acts as a top-level window in which the applet appears
  524. // when it's run as a standalone application.
  525. //==============================================================================
  526. class GuiAppFrame extends UIFrame
  527. {
  528.     IUIComponent parent;
  529.     public boolean action(Event e, Object o)
  530.     {
  531.         return parent.action(e,o);
  532.     }
  533.  
  534.     public GuiAppFrame(String str, IUIComponent c)
  535.     {
  536.         super (str);
  537.         parent=c;
  538.     }
  539.  
  540.     public boolean handleEvent(Event evt)
  541.     {
  542.         switch (evt.id)
  543.         {
  544.             case Event.WINDOW_DESTROY:
  545.                 hide();
  546.                 System.exit(0);
  547.                 return true;
  548.  
  549.             default:
  550.                 return super.handleEvent(evt);
  551.         }             
  552.     }
  553. }
  554.