messageformatdemo.java

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

JAVA
1,034
字号
        Panel allArgs = new Panel();

        addWithFont(allArgs,new Label(" "),Utility.labelFont);
        addWithFont(allArgs,new Label("Arguments", Label.LEFT),
                                      Utility.labelFont);
        addWithFont(allArgs,new Label("0",Label.RIGHT),Utility.labelFont);
        addWithFont(allArgs,arg1Text=new TextField(12),Utility.editFont);
        addWithFont(allArgs,new Label("1",Label.RIGHT),Utility.labelFont);
        addWithFont(allArgs,arg2Text=new TextField(12),Utility.editFont);
        addWithFont(allArgs,new Label("2",Label.RIGHT),Utility.labelFont);
        addWithFont(allArgs,arg3Text=new TextField(12),Utility.editFont);
        addWithFont(allArgs,new Label("3",Label.RIGHT),Utility.labelFont);
        addWithFont(allArgs,arg4Text=new TextField(12),Utility.editFont);

	arg1Text.addKeyListener(this);
	arg2Text.addKeyListener(this);
	arg3Text.addKeyListener(this);
	arg4Text.addKeyListener(this);

        Utility.fixGrid(allArgs,2);

        Panel formatPanel = new Panel();
        addWithFont(formatPanel,new Label(" "),Utility.labelFont);
        addWithFont(formatPanel,new Label("Formats", Label.LEFT),
                    Utility.labelFont);

        addWithFont(formatPanel,new Label("0",Label.RIGHT),Utility.labelFont);
        addWithFont(formatPanel,arg1Type,Utility.choiceFont);

        addWithFont(formatPanel,new Label("1",Label.RIGHT),Utility.labelFont);
        addWithFont(formatPanel,arg2Type = cloneChoice(arg1Type),
                    Utility.choiceFont);

        addWithFont(formatPanel,new Label("2",Label.RIGHT),Utility.labelFont);
        addWithFont(formatPanel,arg3Type = cloneChoice(arg1Type),
                    Utility.choiceFont);

        addWithFont(formatPanel,new Label("3",Label.RIGHT),Utility.labelFont);
        addWithFont(formatPanel,arg4Type = cloneChoice(arg1Type),
                    Utility.choiceFont);

	arg2Type.addItemListener(this);
	arg3Type.addItemListener(this);
	arg4Type.addItemListener(this);

        Utility.fixGrid(formatPanel,2);

        // Choices panel

        Panel choicesPanel = new Panel();
        addWithFont(choicesPanel,new Label(">=", Label.LEFT),
                    Utility.labelFont);
        addWithFont(choicesPanel,new Label("Choice Strings", Label.LEFT),
                    Utility.labelFont);

        addWithFont(choicesPanel,choice1Num=new TextField(4),
                    Utility.editFont);
        addWithFont(choicesPanel,choice1Text=new TextField(16),
                    Utility.editFont);
        addWithFont(choicesPanel,choice2Num=new TextField(4),
                    Utility.editFont);
        addWithFont(choicesPanel,choice2Text=new TextField(16),
                    Utility.editFont);
        addWithFont(choicesPanel,choice3Num=new TextField(4),
                    Utility.editFont);
        addWithFont(choicesPanel,choice3Text=new TextField(16),
                    Utility.editFont);
        addWithFont(choicesPanel,choice4Num=new TextField(4),
                    Utility.editFont);
        addWithFont(choicesPanel,choice4Text=new TextField(16),
                    Utility.editFont);

	choice1Text.addKeyListener(this);
	choice2Text.addKeyListener(this);
	choice3Text.addKeyListener(this);
	choice4Text.addKeyListener(this);
	choice1Num.addKeyListener(this);
	choice2Num.addKeyListener(this);
	choice3Num.addKeyListener(this);
	choice4Num.addKeyListener(this);

        Utility.fixGrid(choicesPanel,2);
        add(titleCreditPanel);
        add(patternResultPanel);
        add(localeCreditPanel);
        Panel bottomPanel = new Panel();
        bottomPanel.add(allArgs);

        XBorderPanel x = new XBorderPanel(); // MD 8/7
        x.setBackground(Color.lightGray);
        x.setLayout(null);
        x.setSize(8,130);
        bottomPanel.add(x);

        bottomPanel.add(formatPanel);

        XBorderPanel x1 = new XBorderPanel(); // MD 8/7
        x1.setBackground(Color.lightGray);
        x1.setLayout(null);
        x1.setSize(8,130);
        bottomPanel.add(x1);

        bottomPanel.add(choicesPanel);
        Utility.fixGrid(bottomPanel,5);
        // MD 8/7 only after fixGrid
        Utility.setInsets(bottomPanel,x,new Insets(20,20,2,2));
        Utility.setInsets(bottomPanel,x1,new Insets(20,20,2,20));

        add(bottomPanel);

        Panel copyrightPanel = new Panel();
        addWithFont (copyrightPanel,new Label(copyrightString, Label.LEFT),
                     Utility.creditFont);
        addWithFont (copyrightPanel,new Label(copyrightString2, Label.LEFT),
                     Utility.creditFont);
        Utility.fixGrid(copyrightPanel,1);
        add(copyrightPanel);

    }

    private Object createObject(Choice typeMenu, TextField textField )
    {
        int type = typeMenu.getSelectedIndex();
        String text = textField.getText();

        Object result = null;

        try {
            if (type == NUMBER || type == CHOICE)
            {
                result = new Double(text);
            }
            else if (type == DATE)
            {
	      // Still use the deprecated new Date(text) until 
	      // the DateFormat.parse(text) is working properly.
	      result = new Long( (new Date(text)).getTime() + 1);
	      // 1 millisecond was added to display the date correctly.
	      // This is done to fix the following scenario, eg,
	      // "27 Sept 96" ==> "26 Sept 96 12:00 AM PDT" which is
	      // equvalent to "27 Sept 96 00:00 AM PDT".  -- CLH 9/27/96
            }
            else if (type == NONE)
            {
                result = text;
            }
        }
        catch (RuntimeException e) {
        }
        return result;
    }
	
	/**
	 * Added for ibm.6983
	 * Reformat %n into {n} and setup format type ('|' overrides format type from that of another type)
	 * if a set of format types is provided then set up these
	 * otherwise add as strings (eg {0,number})
	 */
	private String convertPatternText(Format[] formats, String pattern)
	{
		StringBuffer result = new StringBuffer();
		int formatNumber = 0;
		
		for (int i = 0; i < pattern.length(); i++)
		{
			char ch = pattern.charAt(i);
			if (ch == '{')
				result.append("'{'");
			else if (ch == '\'')
				result.append("''");
			else if (ch == '%')
			{
				if (i + 1 < pattern.length())
				{
					i++;
					ch = pattern.charAt(i);
					if (ch == '%')
						result.append(ch);
					else if (Character.isDigit(ch))
					{
						int digit = Character.digit(ch, 10);
						result.append("{" + ch );
						if ((i + 2 < pattern.length()) && (pattern.charAt(i+1) == '|'))
						{
							i += 2;
							ch = pattern.charAt(i);
							if (Character.isDigit(ch))
							{
								digit = Character.digit(ch, 10);
							}
						}
						switch(digit) {
						case  0: if ((formats == null) && (arg1Type.getSelectedIndex() != NONE) && (arg1Type.getSelectedIndex() != CHOICE))
									 result.append("," + arg1Type.getSelectedItem()); 
								 else if (formats != null)
									 formats[formatNumber] = createFormat(arg1Type); 
								 break;
						case  1: if ((formats == null) && (arg2Type.getSelectedIndex() != NONE) && (arg2Type.getSelectedIndex() != CHOICE))
									 result.append("," + arg2Type.getSelectedItem()); 
								 else if (formats != null)
									 formats[formatNumber] = createFormat(arg2Type); 
								 break;
						case  2: if ((formats == null) && (arg3Type.getSelectedIndex() != NONE) && (arg3Type.getSelectedIndex() != CHOICE))
									 result.append("," + arg3Type.getSelectedItem()); 
								 else if (formats != null)
									 formats[formatNumber] = createFormat(arg3Type); 
								 break;
						case  3: if ((formats == null) && (arg4Type.getSelectedIndex() != NONE) && (arg4Type.getSelectedIndex() != CHOICE))
									 result.append("," + arg4Type.getSelectedItem()); 
								 else if (formats != null)
									 formats[formatNumber] = createFormat(arg4Type); 
								 break;
						default: break; // do nothing
						}
						result.append('}');
						formatNumber++;
					}
				}
			}
			else
				result.append(ch);
		}
		return result.toString();
	}

    private void errorText(String s)
    {
        if (DEBUG)
        {
           System.out.println(s);

        }
    }
    private static final String creditString =
        "v1.1a7, Demos";
    private static final String copyrightString =
        "";
    private static final String copyrightString2 =
        "";
    private static final int FIELD_COLUMNS = 60;

    static private final int NUMBER = 0;
    static private final int DATE = 1;
    static private final int CHOICE = 2;
    static private final int NONE = 3;

    private static final boolean DEBUG = false;

    private Locale[] locales;

    private DemoApplet applet;

    private ChoiceFormat choiceFormat;    // XXX
}

// MD 8/7 whole class, from Ralf. Use different name!
class XBorderPanel extends Panel
{
   /**
    * Panel shadow border width
    */
   protected int shadow = 4;

   /**
    * Panel raised vs depressed look
    */
   protected boolean raised = true;

    public XBorderPanel() {
        this.raised=true;
    }

    public XBorderPanel(boolean raised) {
        this.raised=raised;
    }


   /**
    * Re-layout parent. Called when a panel changes
    * size etc.
    */
   protected void layoutParent() {
      Container parent = getParent();
      if (parent != null) {
     parent.doLayout();
      }
   }

   public void paint(Graphics g) {
        super.paint(g);
        Dimension size = getSize();
        paintBorder(g, size);
    }

   protected void paintBorder(Graphics g, Dimension size) {
      Color c = getBackground();
      g.setColor(c);
      g.fillRect(0, 0, size.width, size.height);
      draw3DRect(g, 0, 0, size.width, size.height, raised);
   }

   /**
    * Draw a 3D Rectangle.
    * @param g the specified Graphics window
    * @param x, y, width, height
    * @param raised - true if border should be painted as raised.
    * @see #paint
    */
   public void draw3DRect(Graphics g, int x, int y, int width, int height,
              boolean raised) {
      Color c = g.getColor();
      Color brighter = avgColor(c,Color.white);
      Color darker = avgColor(c,Color.black);


      // upper left corner
      g.setColor(raised ? brighter : darker);
      for (int i=0; i<shadow; i++) {
      g.drawLine(x+i, y+i, x+width-1-i, y+i);
      g.drawLine(x+i, y+i, x+i, y+height-1-i);
      }
      // lower right corner
      g.setColor(raised ? darker : brighter);
      for (int i=0; i<shadow; i++) {
      g.drawLine(x+i, y+height-1-i, x+width-1-i, y+height-1-i);
      g.drawLine(x+width-1-i, y+height-1-i, x+width-1-i, y+i);
      }
      g.setColor(c);
      // added by rip.
      g.setColor(Color.black);
      g.drawRect(x,y,width+2,height+2);

   }

   public static Color avgColor(Color c1, Color c2) {
    return new Color(
        (c1.getRed()+c2.getRed())/2,
        (c1.getGreen()+c2.getGreen())/2,
        (c1.getBlue()+c2.getBlue())/2
        );
   }

}

⌨️ 快捷键说明

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