jformattedtextfield.java

来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 649 行 · 第 1/2 页

JAVA
649
字号
   * be created for the specified Format.   *    * @param format the Format that this JFormattedTextField should be able   * to handle   */  public JFormattedTextField (Format format)  {    this ();    setFormatterFactory(getAppropriateFormatterFactory(format));  }  /**   * Creates a JFormattedTextField with the specified formatter.  This will    * create a {@link DefaultFormatterFactory} with this formatter as the default   * formatter.   *    * @param formatter the formatter to use for this JFormattedTextField   */  public JFormattedTextField (AbstractFormatter formatter)  {    this(new DefaultFormatterFactory (formatter));  }  /**   * Creates a JFormattedTextField with the specified formatter factory.   *    * @param factory the formatter factory to use for this JFormattedTextField   */  public JFormattedTextField (AbstractFormatterFactory factory)  {    setFormatterFactory(factory);  }  /**   * Creates a JFormattedTextField with the specified formatter factory and   * initial value.   *    * @param factory the initial formatter factory for this JFormattedTextField   * @param value the initial value for the text field   */  public JFormattedTextField (AbstractFormatterFactory factory, Object value)  {        setFormatterFactory(factory);    setValue(value);  }  /**   * Creates a JFormattedTextField with the specified value.  This creates a   * formatter and formatterFactory that are appropriate for the value.   *    * @param value the initial value for this JFormattedTextField   */  public JFormattedTextField (Object value)  {    setValue(value);  }    /**   * Returns an AbstractFormatterFactory that will give an appropriate   * AbstractFormatter for the given Format.   * @param format the Format to match with an AbstractFormatter.   * @return a DefaultFormatterFactory whose defaultFormatter is appropriate   * for the given Format.   */  private AbstractFormatterFactory getAppropriateFormatterFactory (Format format)  {    AbstractFormatter newFormatter;    if (format instanceof DateFormat)      newFormatter = new DateFormatter((DateFormat)format);    else if (format instanceof NumberFormat)      newFormatter = new NumberFormatter ((NumberFormat)format);    else      newFormatter = new InternationalFormatter(format);        return new DefaultFormatterFactory(newFormatter);  }  /**   * Forces the current value from the editor to be set as the current   * value.  If there is no current formatted this has no effect.   *    * @throws ParseException if the formatter cannot format the current value   */  public void commitEdit ()    throws ParseException  {    if (formatter == null)      return;    // Note: this code is a lot like setValue except that we don't want    // to create a new formatter.    Object oldValue = this.value;        this.value = formatter.stringToValue(getText());;    editValid = true;        firePropertyChange("value", oldValue, this.value);   }  /**   * Gets the command list supplied by the UI augmented by the specific   * Actions for JFormattedTextField.   *    * @return an array of Actions that this text field supports   */  public Action[] getActions ()  {    // FIXME: Add JFormattedTextField specific actions    // These are related to committing or cancelling edits.    return super.getActions();  }  /**   * Returns the behaviour of this JFormattedTextField upon losing focus.  This   * is one of <code>COMMIT</code>, <code>COMMIT_OR_REVERT</code>,    * <code>PERSIST</code>, or <code>REVERT</code>.     * @return the behaviour upon losing focus   */  public int getFocusLostBehavior()  {    return focusLostBehavior;  }  /**   * Returns the current formatter used for this JFormattedTextField.   * @return the current formatter used for this JFormattedTextField   */  public AbstractFormatter getFormatter ()  {    return formatter;  }    /**   * Returns the factory currently used to generate formatters for this   * JFormattedTextField.   * @return the factory currently used to generate formatters   */  public AbstractFormatterFactory getFormatterFactory ()  {    return formatterFactory;  }  public String getUIClassID ()  {    return "FormattedTextFieldUI";  }  /**   * Returns the last valid value.  This may not be the value currently shown    * in the text field depending on whether or not the formatter commits on    * valid edits and allows invalid input to be temporarily displayed.     * @return the last committed valid value   */  public Object getValue ()  {    return value;  }  /**   * This method is used to provide feedback to the user when an invalid value   * is input during editing.      */  protected void invalidEdit ()  {    UIManager.getLookAndFeel().provideErrorFeedback(this);  }  /**   * Returns true if the current value being edited is valid.  This property is   * managed by the current formatted.   * @return true if the value being edited is valid.   */  public boolean isEditValid ()  {    return editValid;  }  /**   * Processes focus events.  This is overridden because we may want to    * change the formatted depending on whether or not this field has    * focus.   *    * @param evt the FocusEvent   */  protected void processFocusEvent (FocusEvent evt)  {    super.processFocusEvent(evt);    // Let the formatterFactory change the formatter for this text field    // based on whether or not it has focus.    setFormatter (formatterFactory.getFormatter(this));  }    /**   * Associates this JFormattedTextField with a Document and propagates   * a PropertyChange event to each listener.   *    * @param newDocument the Document to associate with this text field   */  public void setDocument(Document newDocument)  {    // FIXME: This method should do more than this.  Must do some handling    // of the DocumentListeners.    Document oldDocument = getDocument();    if (oldDocument == newDocument)      return;        super.setDocument(newDocument);  }  /**   * Sets the behaviour of this JFormattedTextField upon losing focus.   * This must be <code>COMMIT</code>, <code>COMMIT_OR_REVERT</code>,    * <code>PERSIST</code>, or <code>REVERT</code> or an    * IllegalArgumentException will be thrown.   *    * @param behavior   * @throws IllegalArgumentException if <code>behaviour</code> is not    * one of the above   */  public void setFocusLostBehavior(int behavior)  {    if (behavior != COMMIT	&& behavior != COMMIT_OR_REVERT	&& behavior != PERSIST	&& behavior != REVERT)      throw new IllegalArgumentException("invalid behavior");    this.focusLostBehavior = behavior;  }  /**   * Sets the formatter for this JFormattedTextField.  Normally the formatter   * factory will take care of this, or calls to setValue will also make sure   * that the formatter is set appropriately.     *    * @param formatter the AbstractFormatter to use for formatting the value for   * this JFormattedTextField   */  protected void setFormatter (AbstractFormatter formatter)  {    AbstractFormatter oldFormatter = null;        oldFormatter = this.formatter;    if (oldFormatter != null)      oldFormatter.uninstall();        this.formatter = formatter;        if (formatter != null)      formatter.install(this);    firePropertyChange("formatter", oldFormatter, formatter);  }  /**   * Sets the factory from which this JFormattedTextField should obtain    * its formatters.     *    * @param factory the AbstractFormatterFactory that will be used to generate   * formatters for this JFormattedTextField   */  public void setFormatterFactory (AbstractFormatterFactory factory)  {    if (formatterFactory == factory)      return;        AbstractFormatterFactory oldFactory = formatterFactory;    formatterFactory = factory;    firePropertyChange("formatterFactory", oldFactory, factory);        // Now set the formatter according to our new factory.    if (formatterFactory != null)      setFormatter(formatterFactory.getFormatter(this));    else      setFormatter(null);  }  /**   * Sets the value that will be formatted and displayed.   *      * @param newValue the value to be formatted and displayed   */  public void setValue (Object newValue)  {    if (value == newValue)      return;    Object oldValue = value;    value = newValue;        // If there is no formatterFactory then make one.    if (formatterFactory == null)      setFormatterFactory(createFormatterFactory(newValue));        // Set the formatter appropriately.  This is because there may be a new    // formatterFactory from the line above, or we may want a new formatter    // depending on the type of newValue (or if newValue is null).    setFormatter (formatterFactory.getFormatter(this));    firePropertyChange("value", oldValue, newValue);  }  /**   * A helper method that attempts to create a formatter factory that is    * suitable to format objects of the type like <code>value</code>.   *   * @param value an object which should be formatted by the formatter factory.   *   * @return a formatter factory able to format objects of the class of   *     <code>value</code>   */  AbstractFormatterFactory createFormatterFactory(Object value)  {    AbstractFormatter formatter = null;    if (value instanceof Date)      formatter = new DateFormatter();    else if (value instanceof Number)      formatter = new NumberFormatter();    else      formatter = new DefaultFormatter();            return new DefaultFormatterFactory(formatter);  }}

⌨️ 快捷键说明

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