home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VCafe / WDESAMPL.BIN / MessageFormatFrame.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-03-17  |  10.7 KB  |  417 lines

  1. import java.awt.Choice;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Container;
  5. import java.awt.FlowLayout;
  6. import java.awt.Font;
  7. import java.awt.Frame;
  8. import java.awt.GridBagConstraints;
  9. import java.awt.GridBagLayout;
  10. import java.awt.Insets;
  11. import java.awt.Label;
  12. import java.awt.LayoutManager;
  13. import java.awt.Panel;
  14. import java.awt.TextComponent;
  15. import java.awt.TextField;
  16. import java.awt.Window;
  17. import java.awt.event.InputEvent;
  18. import java.awt.event.ItemEvent;
  19. import java.awt.event.ItemListener;
  20. import java.awt.event.KeyEvent;
  21. import java.awt.event.KeyListener;
  22. import java.awt.event.WindowEvent;
  23. import java.awt.event.WindowListener;
  24. import java.text.ChoiceFormat;
  25. import java.text.DateFormat;
  26. import java.text.Format;
  27. import java.text.MessageFormat;
  28. import java.text.NumberFormat;
  29. import java.util.Date;
  30. import java.util.EventObject;
  31. import java.util.Locale;
  32. import java.util.ResourceBundle;
  33.  
  34. class MessageFormatFrame extends Frame implements WindowListener, ItemListener, KeyListener {
  35.    Label label1;
  36.    Label label2;
  37.    TextField patternText;
  38.    Label label3;
  39.    Label label4;
  40.    Label label5;
  41.    Label labelArg;
  42.    Label labelForm;
  43.    TextField arg1Text;
  44.    TextField arg2Text;
  45.    TextField arg3Text;
  46.    Choice arg1Type;
  47.    Choice arg2Type;
  48.    Choice arg3Type;
  49.    Label label6;
  50.    Choice localeMenu;
  51.    Label localeLabel;
  52.    Label label13;
  53.    Label label14;
  54.    TextField resultText;
  55.    Label label7;
  56.    Label label8;
  57.    Label label9;
  58.    TextField choice1Num;
  59.    TextField choice2Num;
  60.    TextField choice3Num;
  61.    TextField choice4Num;
  62.    TextField choice1Text;
  63.    TextField choice2Text;
  64.    TextField choice3Text;
  65.    TextField choice4Text;
  66.    Label label10;
  67.    Label label11;
  68.    Label label12;
  69.    TextField arg4Text;
  70.    Choice arg4Type;
  71.    private static final String creditString = "v1.1a7, Demos";
  72.    private static final String copyrightString = "";
  73.    private static final String copyrightString2 = "";
  74.    private static final int FIELD_COLUMNS = 60;
  75.    private static final int NUMBER = 0;
  76.    private static final int DATE = 1;
  77.    private static final int CHOICE = 2;
  78.    private static final int NONE = 3;
  79.    private static final boolean DEBUG = false;
  80.    private Locale[] locales;
  81.    private DemoApplet applet;
  82.    private ChoiceFormat choiceFormat;
  83.  
  84.    public MessageFormatFrame(DemoApplet var1) {
  85.       super("Message Formatting Demo");
  86.       this.applet = var1;
  87.       ((Window)this).addWindowListener(this);
  88.       this.init();
  89.       this.start();
  90.    }
  91.  
  92.    public void init() {
  93.       this.locales = Utility.getG7Locales();
  94.       this.buildGUI();
  95.    }
  96.  
  97.    public void start() {
  98.       this.arg1Text.setText("3");
  99.       this.arg1Type.select(2);
  100.       this.arg2Text.setText("MyDisk");
  101.       this.arg2Type.select(3);
  102.       this.arg3Text.setText("3 Mar 96");
  103.       this.arg3Type.select(1);
  104.       this.arg4Text.setText("");
  105.       this.arg4Type.select(0);
  106.       this.patternText.setText("The disk '%1' contained %0 on %2.");
  107.       this.choice1Num.setText("0");
  108.       this.choice2Num.setText("1");
  109.       this.choice3Num.setText("2");
  110.       this.resetFormat();
  111.       this.doFormatting();
  112.    }
  113.  
  114.    public void resetFormat() {
  115.       Locale var1 = this.locales[this.localeMenu.getSelectedIndex()];
  116.       this.getClass().getClassLoader();
  117.       this.choice4Num.setText("");
  118.       this.choice4Text.setText("");
  119.       ResourceBundle var2 = ResourceBundle.getBundle("ChoiceResource", var1);
  120.       this.patternText.setText(var2.getString("patternText"));
  121.       this.choice1Text.setText(var2.getString("choice1"));
  122.       this.choice2Text.setText(var2.getString("choice2"));
  123.       this.choice3Text.setText(var2.getString("choice3"));
  124.    }
  125.  
  126.    public Format createFormat(Choice var1) {
  127.       int var2 = var1.getSelectedIndex();
  128.       Locale var3 = this.locales[this.localeMenu.getSelectedIndex()];
  129.       Object var4 = null;
  130.       if (var2 == 0) {
  131.          var4 = NumberFormat.getInstance(var3);
  132.       } else if (var2 == 1) {
  133.          var4 = DateFormat.getDateInstance(2, var3);
  134.       } else if (var2 == 2) {
  135.          var4 = this.choiceFormat;
  136.       }
  137.  
  138.       return (Format)var4;
  139.    }
  140.  
  141.    public void doFormatting() {
  142.       double[] var1 = new double[]{this.doubleValue(this.choice1Num.getText()), this.doubleValue(this.choice2Num.getText()), this.doubleValue(this.choice3Num.getText()), this.doubleValue(this.choice4Num.getText())};
  143.       String[] var2 = new String[]{this.choice1Text.getText(), this.choice2Text.getText(), this.choice3Text.getText(), this.choice4Text.getText()};
  144.       this.choiceFormat = new ChoiceFormat(var1, var2);
  145.       Format[] var3 = new Format[]{this.createFormat(this.arg1Type), this.createFormat(this.arg2Type), this.createFormat(this.arg3Type), this.createFormat(this.arg4Type)};
  146.       MessageFormat var4 = new MessageFormat(this.patternText.getText());
  147.       Object[] var5 = new Object[]{this.createObject(this.arg1Type, this.arg1Text), this.createObject(this.arg2Type, this.arg2Text), this.createObject(this.arg3Type, this.arg3Text), this.createObject(this.arg4Type, this.arg4Text)};
  148.       Object var6 = null;
  149.  
  150.       try {
  151.          var9 = ((Format)var4).format(var5);
  152.       } catch (Exception var8) {
  153.          this.errorText("format threw an exception: " + ((Throwable)var8).toString());
  154.          var9 = "ERROR";
  155.       }
  156.  
  157.       this.resultText.setText(var9);
  158.    }
  159.  
  160.    public void itemStateChanged(ItemEvent var1) {
  161.       if (((EventObject)var1).getSource() != this.arg1Type && ((EventObject)var1).getSource() != this.arg2Type && ((EventObject)var1).getSource() != this.arg3Type && ((EventObject)var1).getSource() != this.arg4Type) {
  162.          if (((EventObject)var1).getSource() == this.localeMenu) {
  163.             this.resetFormat();
  164.             this.doFormatting();
  165.          }
  166.  
  167.       } else {
  168.          this.doFormatting();
  169.       }
  170.    }
  171.  
  172.    public void keyPressed(KeyEvent var1) {
  173.    }
  174.  
  175.    public void keyReleased(KeyEvent var1) {
  176.       if (((EventObject)var1).getSource() != this.choice1Text && ((EventObject)var1).getSource() != this.choice2Text && ((EventObject)var1).getSource() != this.choice3Text && ((EventObject)var1).getSource() != this.choice4Text) {
  177.          if (((EventObject)var1).getSource() != this.choice1Num && ((EventObject)var1).getSource() != this.choice2Num && ((EventObject)var1).getSource() != this.choice3Num && ((EventObject)var1).getSource() != this.choice4Num) {
  178.             if (((EventObject)var1).getSource() != this.arg1Text && ((EventObject)var1).getSource() != this.arg2Text && ((EventObject)var1).getSource() != this.arg3Text && ((EventObject)var1).getSource() != this.arg4Text) {
  179.                if (((EventObject)var1).getSource() == this.patternText) {
  180.                   ((InputEvent)var1).consume();
  181.                   this.doFormatting();
  182.                }
  183.  
  184.             } else {
  185.                ((InputEvent)var1).consume();
  186.                this.doFormatting();
  187.             }
  188.          } else {
  189.             ((InputEvent)var1).consume();
  190.             this.doFormatting();
  191.          }
  192.       } else {
  193.          ((InputEvent)var1).consume();
  194.          this.doFormatting();
  195.       }
  196.    }
  197.  
  198.    public void keyTyped(KeyEvent var1) {
  199.    }
  200.  
  201.    public void windowClosed(WindowEvent var1) {
  202.    }
  203.  
  204.    public void windowDeiconified(WindowEvent var1) {
  205.    }
  206.  
  207.    public void windowIconified(WindowEvent var1) {
  208.    }
  209.  
  210.    public void windowActivated(WindowEvent var1) {
  211.    }
  212.  
  213.    public void windowDeactivated(WindowEvent var1) {
  214.    }
  215.  
  216.    public void windowOpened(WindowEvent var1) {
  217.    }
  218.  
  219.    public void windowClosing(WindowEvent var1) {
  220.       ((Component)this).setVisible(false);
  221.       ((Frame)this).dispose();
  222.       if (this.applet != null) {
  223.          this.applet.demoClosed();
  224.       } else {
  225.          System.exit(0);
  226.       }
  227.    }
  228.  
  229.    double doubleValue(String var1) {
  230.       try {
  231.          return Double.valueOf(var1);
  232.       } catch (Exception var2) {
  233.          return Double.POSITIVE_INFINITY;
  234.       }
  235.    }
  236.  
  237.    void constrainedAdd(GridBagConstraints var1, Container var2, Component var3, Font var4) {
  238.       GridBagLayout var5 = (GridBagLayout)var2.getLayout();
  239.       if (var4 != null) {
  240.          var3.setFont(var4);
  241.       }
  242.  
  243.       var5.setConstraints(var3, var1);
  244.       var2.add(var3);
  245.    }
  246.  
  247.    Choice cloneChoice(Choice var1) {
  248.       Choice var2 = new Choice();
  249.  
  250.       for(int var3 = 0; var3 < var1.getItemCount(); ++var3) {
  251.          var2.addItem(var1.getItem(var3));
  252.       }
  253.  
  254.       ((Component)var2).setFont(((Component)var1).getFont());
  255.       return var2;
  256.    }
  257.  
  258.    void addWithFont(Container var1, Component var2, Font var3) {
  259.       if (var3 != null) {
  260.          var2.setFont(var3);
  261.       }
  262.  
  263.       var1.add(var2);
  264.    }
  265.  
  266.    private void buildGUI() {
  267.       ((Container)this).setLayout(new FlowLayout(1, 2, 2));
  268.       ((Component)this).setBackground(Color.white);
  269.       Panel var1 = new Panel();
  270.       this.label6 = new Label("Message Format Demo", 1);
  271.       this.label6.setFont(Utility.titleFont);
  272.       ((Container)var1).add(this.label6);
  273.       Panel var2 = new Panel();
  274.       this.label13 = new Label("v1.1a7, Demos");
  275.       this.label13.setFont(Utility.creditFont);
  276.       ((Container)var2).add(this.label13);
  277.       ((Container)var1).add(var2);
  278.       Utility.fixGrid(var1, 1);
  279.       Panel var3 = new Panel();
  280.       this.addWithFont(var3, new Label("Result", 2), Utility.labelFont);
  281.       this.addWithFont(var3, this.resultText = new TextField(60), Utility.editFont);
  282.       this.addWithFont(var3, new Label("Pattern", 2), Utility.labelFont);
  283.       this.addWithFont(var3, this.patternText = new TextField(60), Utility.editFont);
  284.       this.patternText.addKeyListener(this);
  285.       Utility.fixGrid(var3, 2);
  286.       Panel var4 = new Panel();
  287.       this.localeLabel = new Label("Locale:", 0);
  288.       this.localeLabel.setFont(Utility.labelFont);
  289.       this.localeMenu = new Choice();
  290.       this.localeMenu.addItemListener(this);
  291.       Locale.getDefault();
  292.  
  293.       for(int var5 = 0; var5 < this.locales.length; ++var5) {
  294.          if (this.locales[var5].getCountry().length() > 0) {
  295.             this.localeMenu.addItem(this.locales[var5].getDisplayName());
  296.             if (this.locales[var5].equals(Locale.getDefault())) {
  297.                this.localeMenu.select(var5);
  298.             }
  299.          }
  300.       }
  301.  
  302.       this.localeMenu.setFont(Utility.choiceFont);
  303.       Panel var6 = new Panel();
  304.       ((Container)var6).add(this.localeLabel);
  305.       ((Container)var6).add(this.localeMenu);
  306.       ((Container)var4).add(var6);
  307.       this.arg1Type = new Choice();
  308.       this.arg1Type.setFont(Utility.choiceFont);
  309.       this.arg1Type.addItem("Number");
  310.       this.arg1Type.addItem("Date");
  311.       this.arg1Type.addItem("Choice");
  312.       this.arg1Type.addItem("None");
  313.       this.arg1Type.addItemListener(this);
  314.       Panel var7 = new Panel();
  315.       this.addWithFont(var7, new Label(" "), Utility.labelFont);
  316.       this.addWithFont(var7, new Label("Arguments", 0), Utility.labelFont);
  317.       this.addWithFont(var7, new Label("0", 2), Utility.labelFont);
  318.       this.addWithFont(var7, this.arg1Text = new TextField(12), Utility.editFont);
  319.       this.addWithFont(var7, new Label("1", 2), Utility.labelFont);
  320.       this.addWithFont(var7, this.arg2Text = new TextField(12), Utility.editFont);
  321.       this.addWithFont(var7, new Label("2", 2), Utility.labelFont);
  322.       this.addWithFont(var7, this.arg3Text = new TextField(12), Utility.editFont);
  323.       this.addWithFont(var7, new Label("3", 2), Utility.labelFont);
  324.       this.addWithFont(var7, this.arg4Text = new TextField(12), Utility.editFont);
  325.       this.arg1Text.addKeyListener(this);
  326.       this.arg2Text.addKeyListener(this);
  327.       this.arg3Text.addKeyListener(this);
  328.       this.arg4Text.addKeyListener(this);
  329.       Utility.fixGrid(var7, 2);
  330.       Panel var8 = new Panel();
  331.       this.addWithFont(var8, new Label(" "), Utility.labelFont);
  332.       this.addWithFont(var8, new Label("Formats", 0), Utility.labelFont);
  333.       this.addWithFont(var8, new Label("0", 2), Utility.labelFont);
  334.       this.addWithFont(var8, this.arg1Type, Utility.choiceFont);
  335.       this.addWithFont(var8, new Label("1", 2), Utility.labelFont);
  336.       this.addWithFont(var8, this.arg2Type = this.cloneChoice(this.arg1Type), Utility.choiceFont);
  337.       this.addWithFont(var8, new Label("2", 2), Utility.labelFont);
  338.       this.addWithFont(var8, this.arg3Type = this.cloneChoice(this.arg1Type), Utility.choiceFont);
  339.       this.addWithFont(var8, new Label("3", 2), Utility.labelFont);
  340.       this.addWithFont(var8, this.arg4Type = this.cloneChoice(this.arg1Type), Utility.choiceFont);
  341.       this.arg2Type.addItemListener(this);
  342.       this.arg3Type.addItemListener(this);
  343.       this.arg4Type.addItemListener(this);
  344.       Utility.fixGrid(var8, 2);
  345.       Panel var9 = new Panel();
  346.       this.addWithFont(var9, new Label(">=", 0), Utility.labelFont);
  347.       this.addWithFont(var9, new Label("Choice Strings", 0), Utility.labelFont);
  348.       this.addWithFont(var9, this.choice1Num = new TextField(4), Utility.editFont);
  349.       this.addWithFont(var9, this.choice1Text = new TextField(16), Utility.editFont);
  350.       this.addWithFont(var9, this.choice2Num = new TextField(4), Utility.editFont);
  351.       this.addWithFont(var9, this.choice2Text = new TextField(16), Utility.editFont);
  352.       this.addWithFont(var9, this.choice3Num = new TextField(4), Utility.editFont);
  353.       this.addWithFont(var9, this.choice3Text = new TextField(16), Utility.editFont);
  354.       this.addWithFont(var9, this.choice4Num = new TextField(4), Utility.editFont);
  355.       this.addWithFont(var9, this.choice4Text = new TextField(16), Utility.editFont);
  356.       this.choice1Text.addKeyListener(this);
  357.       this.choice2Text.addKeyListener(this);
  358.       this.choice3Text.addKeyListener(this);
  359.       this.choice4Text.addKeyListener(this);
  360.       this.choice1Num.addKeyListener(this);
  361.       this.choice2Num.addKeyListener(this);
  362.       this.choice3Num.addKeyListener(this);
  363.       this.choice4Num.addKeyListener(this);
  364.       Utility.fixGrid(var9, 2);
  365.       ((Container)this).add(var1);
  366.       ((Container)this).add(var3);
  367.       ((Container)this).add(var4);
  368.       Panel var10 = new Panel();
  369.       ((Container)var10).add(var7);
  370.       XBorderPanel var11 = new XBorderPanel();
  371.       ((Component)var11).setBackground(Color.lightGray);
  372.       ((Container)var11).setLayout((LayoutManager)null);
  373.       ((Component)var11).setSize(8, 130);
  374.       ((Container)var10).add(var11);
  375.       ((Container)var10).add(var8);
  376.       XBorderPanel var12 = new XBorderPanel();
  377.       ((Component)var12).setBackground(Color.lightGray);
  378.       ((Container)var12).setLayout((LayoutManager)null);
  379.       ((Component)var12).setSize(8, 130);
  380.       ((Container)var10).add(var12);
  381.       ((Container)var10).add(var9);
  382.       Utility.fixGrid(var10, 5);
  383.       Utility.setInsets(var10, var11, new Insets(20, 20, 2, 2));
  384.       Utility.setInsets(var10, var12, new Insets(20, 20, 2, 20));
  385.       ((Container)this).add(var10);
  386.       Panel var13 = new Panel();
  387.       this.addWithFont(var13, new Label("", 0), Utility.creditFont);
  388.       this.addWithFont(var13, new Label("", 0), Utility.creditFont);
  389.       Utility.fixGrid(var13, 1);
  390.       ((Container)this).add(var13);
  391.    }
  392.  
  393.    private Object createObject(Choice var1, TextField var2) {
  394.       int var3 = var1.getSelectedIndex();
  395.       String var4 = ((TextComponent)var2).getText();
  396.       Object var5 = null;
  397.  
  398.       try {
  399.          if (var3 != 0 && var3 != 2) {
  400.             if (var3 == 1) {
  401.                var5 = new Long((new Date(var4)).getTime() + 1L);
  402.             } else if (var3 == 3) {
  403.                var5 = var4;
  404.             }
  405.          } else {
  406.             var5 = new Double(var4);
  407.          }
  408.       } catch (RuntimeException var6) {
  409.       }
  410.  
  411.       return var5;
  412.    }
  413.  
  414.    private void errorText(String var1) {
  415.    }
  416. }
  417.