messageformatdemo.java

来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 1,034 行 · 第 1/3 页

JAVA
1,034
字号
        arg3Text.setText("3 Mar 96");
        arg3Type.select(DATE);

        arg4Text.setText("");
        arg4Type.select(NUMBER);

        patternText.setText("The disk '%1' contained %0 on %2.");

        // Set up the choice format controls too
        choice1Num.setText("0");
        choice2Num.setText("1");
        choice3Num.setText("2");
        resetFormat();
        doFormatting();

    }

    /**
     * Reset to the default message format using the ResourceBundle mechanism.
     * @see java.util.ResourceBundle
     * @see java.util.ListResourceBundle
     */
    public void resetFormat() {
        Locale locale = locales[localeMenu.getSelectedIndex()];

	ClassLoader classLoader = this.getClass().getClassLoader();

        choice4Num.setText("");
        choice4Text.setText("");

        ResourceBundle choiceResources =
             ResourceBundle.getBundle("ChoiceResource", locale);

	patternText.setText(choiceResources.getString("patternText"));
        choice1Text.setText(choiceResources.getString("choice1"));
        choice2Text.setText(choiceResources.getString("choice2"));
        choice3Text.setText(choiceResources.getString("choice3"));
    }

    /**
     * Create a new format based on the selected type.  For example, a new
     * format needs to be created if a different locale or format type is
     * selected.
     */
    public Format createFormat(Choice typeMenu)
    {
        int type = typeMenu.getSelectedIndex();
        Locale locale = locales[localeMenu.getSelectedIndex()];

        Format result = null;
        if (type == NUMBER) {
            result = NumberFormat.getInstance(locale);
        }
        else if (type == DATE) {
            result = DateFormat.getDateInstance(DateFormat.DEFAULT, locale);
        }
        else if (type == CHOICE) {
            result = choiceFormat;    // XXX
        }
        return result;
    }

    /**
     * Create a new format based on the selection changes and update the
     * output text area.
     */
    public void doFormatting() {

        // Create a ChoiceFormat based on the settings in the choice field
        double[] limits = new double[4];
        limits[0] = doubleValue(choice1Num.getText());
        limits[1] = doubleValue(choice2Num.getText());
        limits[2] = doubleValue(choice3Num.getText());
        limits[3] = doubleValue(choice4Num.getText());

        String[] choices = new String[4];
        choices[0] = choice1Text.getText();
        choices[1] = choice2Text.getText();
        choices[2] = choice3Text.getText();
        choices[3] = choice4Text.getText();


        choiceFormat = new ChoiceFormat(limits, choices);    // XXX

        // Create the individual formatters for the items in the pattern....
        Format[] formats = new Format[10];								/*ibm.6983*/
		String convertedPattern;										/*ibm.6983*/
		String pattern = new String(patternText.getText());				/*ibm.6983*/
 		int[] formatTypes = new int[4];									/*ibm.6983*/
		convertedPattern = convertPatternText(formats, pattern);		/*ibm.6983*/
		//convert choice strings										/*ibm.6983*/
		for (int j = 0; j < 4; j++)										/*ibm.6983*/
			choices[j] = convertPatternText(null, choices[j]);			/*ibm.6983*/
        // Now create the Message Formatter itself						
        MessageFormat format = new MessageFormat(convertedPattern);		
		format.setFormats(formats);										/*ibm.6983*/
//      Format[] formats = new Format[4];								/*ibm.6983*/
//      formats[0] = createFormat(arg1Type);							/*ibm.6983*/
//      formats[1] = createFormat(arg2Type);							/*ibm.6983*/
//      formats[2] = createFormat(arg3Type);							/*ibm.6983*/
//      formats[3] = createFormat(arg4Type);							/*ibm.6983*/

        // Create the array of objects to format....
        Object[] objects = new Object[4];
        objects[0] = createObject(arg1Type, arg1Text);
        objects[1] = createObject(arg2Type, arg2Text);
        objects[2] = createObject(arg3Type, arg3Text);
        objects[3] = createObject(arg4Type, arg4Text);

        String result = null;
        try {
            result = format.format(objects);
        }
        catch (Exception e)
        {
            errorText("format threw an exception: " + e.toString());
            result = "ERROR";
        }
        resultText.setText(result);
    }

  /* ItemListener method */
  public void itemStateChanged(ItemEvent e) {
    if((e.getSource() == arg1Type) || (e.getSource() == arg2Type) || (e.getSource() == arg3Type)
       || (e.getSource() == arg4Type)) {
      doFormatting();
    } else if (e.getSource() == localeMenu) {
      resetFormat();
      doFormatting();
    }
  }
  
  /* KeyListener methods */
  public void keyPressed(KeyEvent e) {
  }

  public void keyReleased(KeyEvent e) {
    if ((e.getSource() == choice1Text) || (e.getSource() == choice2Text) || 
	(e.getSource() == choice3Text) || (e.getSource() == choice4Text)) {
      e.consume();
      doFormatting();
    } else if ((e.getSource() == choice1Num) ||(e.getSource() == choice2Num) ||
	       (e.getSource() == choice3Num) ||(e.getSource() == choice4Num)) {
      e.consume();
      doFormatting();
    } else if ((e.getSource() == arg1Text) ||(e.getSource() == arg2Text) ||
	       (e.getSource() == arg3Text) ||(e.getSource() == arg4Text)) {
      e.consume();
      doFormatting();
    } else if (e.getSource() == patternText) {
      e.consume();
      doFormatting();
    }
      
  }

  public void keyTyped(KeyEvent e) {
  }
  
  /* Window Listener methods */
  public void windowClosed(WindowEvent e) {
  }

  public void windowDeiconified(WindowEvent e) {
  }

  public void windowIconified(WindowEvent e) {
  }

  public void windowActivated(WindowEvent e) {
  }

  public void windowDeactivated(WindowEvent e) {
  }

  public void windowOpened(WindowEvent e) {
  }

  public void windowClosing(WindowEvent e) {
    setVisible(false);
    dispose();

    if (applet != null) {
      applet.demoClosed();
    } else System.exit(0);

  }

    //------------------------------------------------------------
    // package private
    //------------------------------------------------------------

    double doubleValue(String s) {
        try {
            return Double.valueOf(s).doubleValue();
        } catch (Exception foo) {
            return Double.POSITIVE_INFINITY;
        }
    }

    void constrainedAdd(GridBagConstraints c,
                        Container container,
                        Component foo,
                        Font font) {
        GridBagLayout gridbag = (GridBagLayout)container.getLayout();
        if (font != null)
            foo.setFont(font);
        gridbag.setConstraints(foo, c);
        container.add(foo);
    }

    Choice cloneChoice (Choice source) {
        Choice result = new Choice();
        for (int i = 0; i < source.getItemCount(); ++i)
            result.addItem(source.getItem(i));
        result.setFont(source.getFont());
        return result;
    }


    void addWithFont(Container container, Component foo, Font font) {
        if (font != null)
            foo.setFont(font);
        container.add(foo);
    }

    //{{DECLARE_CONTROLS
    Label label1;
    Label label2;
    TextField patternText;
    Label label3;
    Label label4;
    Label label5;
    Label labelArg;
    Label labelForm;
    TextField arg1Text;
    TextField arg2Text;
    TextField arg3Text;
    Choice arg1Type;
    Choice arg2Type;
    Choice arg3Type;
    Label label6;
    Choice localeMenu;
    Label localeLabel;
    Label label13;
    Label label14;
    TextField resultText;
    Label label7;
    Label label8;
    Label label9;
    TextField choice1Num;
    TextField choice2Num;
    TextField choice3Num;
    TextField choice4Num;
    TextField choice1Text;
    TextField choice2Text;
    TextField choice3Text;
    TextField choice4Text;
    Label label10;
    Label label11;
    Label label12;
    TextField arg4Text;
    Choice arg4Type;
    //}}

    //------------------------------------------------------------
    // private
    //------------------------------------------------------------
    private void buildGUI() {

        //{{INIT_CONTROLS
        setLayout(new FlowLayout(FlowLayout.CENTER,2,2));   // MD 8/7
        setBackground(Color.white); // MD 8/7

        // Main Title

        Panel titleCreditPanel = new Panel();
        label6=new Label("Message Format Demo", Label.CENTER);
        label6.setFont(Utility.titleFont);
        titleCreditPanel.add(label6);

        Panel creditPanel = new Panel();
        label13=new Label(creditString);
        label13.setFont(Utility.creditFont);
        creditPanel.add(label13);

        titleCreditPanel.add(creditPanel);

        Utility.fixGrid(titleCreditPanel,1);

        // result text

        Panel patternResultPanel = new Panel();

        addWithFont(patternResultPanel,new
            Label("Result", Label.RIGHT),Utility.labelFont);
        addWithFont(patternResultPanel,resultText= new
            TextField(FIELD_COLUMNS),Utility.editFont);

        addWithFont(patternResultPanel,new
            Label("Pattern", Label.RIGHT),Utility.labelFont);
        addWithFont(patternResultPanel,patternText=new
            TextField(FIELD_COLUMNS),Utility.editFont);

	patternText.addKeyListener(this);
        Utility.fixGrid(patternResultPanel,2);

        // Locale and credits

        Panel localeCreditPanel = new Panel();
        // localeCreditPanel.setLayout(new GridBagLayout());

        localeLabel=new Label("Locale:",Label.LEFT);
        localeLabel.setFont(Utility.labelFont);
        //localeCreditPanel.add("loc",localeLabel);

        // LOCALE
        localeMenu= new Choice();
	localeMenu.addItemListener(this);
        // Stick the names of the locales into the locale popup menu
        Locale displayLocale = Locale.getDefault();
        for (int i = 0; i < locales.length; i++) {
            if (locales[i].getCountry().length() > 0) {
                localeMenu.addItem( locales[i].getDisplayName() );
                if (locales[i].equals(Locale.getDefault())) {
                    localeMenu.select(i);
                }
            }
        }
           localeMenu.setFont(Utility.choiceFont);

        Panel localePanel=new Panel();
        localePanel.add(localeLabel);
        localePanel.add(localeMenu);
        localeCreditPanel.add(localePanel);

        arg1Type= new Choice();
           arg1Type.setFont(Utility.choiceFont);
        arg1Type.addItem("Number");
        arg1Type.addItem("Date");
        arg1Type.addItem("Choice");
        arg1Type.addItem("None");
	arg1Type.addItemListener(this);

        // PUT THE ARGUMENTS/ FORMATS into GRID

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?