Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| org.openide.src 1.8.22
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.openide.src
Class ElementFormat

    java.lang.Object
      java.text.Format
          org.openide.src.ElementFormat

All Implemented Interfaces:
  Serializable, Cloneable

    public final class ElementFormat

extends Format

A format used to print members of the source hierarchy. It is sometimes used for code generation of elements, and also for formatting the display names of the nodes representing the hierarchy.

This format is similar to `MessageFormat`. It also uses special characters in the pattern and replaces them with strings, depending on the code.

For example:

``

     ElementFormat fmt = new ElementFormat ("{m} {r} {n} ({p})");
     MethodElement method = getMethodSomewhere ();
     System.out.println (fmt.format (method));

...should print something like this: `"public int method(int,char)"`

The substitution codes are:

- `{m}` Modifiers
- `{n}` Name
- `{C}` Name of class (with all outerclasses)
- `{f}` Full name of element with package
- `{t}` Type
- `{r}` Return type
- `{s}` Superclass
- `{c}` Static (for initializers)
- `{p}` Parameters with types but not variable names (e.g. `"int,char"`).
- `{a}` Parameters with types and names (e.g. `"int x,char c"`).
- `{i}` Interfaces
- `{e}` Exceptions

The following table shows which codes may be used to format which kinds of element. An asterisk means the code may be used, a hyphen means it cannot:

``

     character   | m  n  f  C  t  r  s  c  p  a  i  e
     ----------------------------------------------------
     Initializer | -  -  -  -  -  -  -  *  -  -  -  -
     Field       | *  *  *  -  *  -  -  -  -  -  -  -
     Constructor | *  *  *  -  -  -  -  -  *  *  -  *
     Method      | *  *  *  -  -  *  -  -  *  *  -  *
     Class       | *  *  *  *  -  -  *  -  -  -  *  -
     Interface   | *  *  *  *  -  -  -  -  -  -  *  -

The grammar for expressions:

``

     messageFormatPattern := string ( "{" messageFormatElement "}" string )*

     messageFormatElement := simple_argument { "," prefix "," suffix }

     messageFormatElement := array_argument { "," prefix "," suffix { "," delim } }

     simple_argument := "m" | "n" | "f" | "C" | "t" | "r" | "s" | "c"

     array_argument := "p" | "a" | "i" | "e"

     prefix := string

     suffix := string

     delim  := string

Comments on the previous grammar:

- `simple_argument` - arguments which are replaced by a single string
- `array_argument` - arguments for arrays (parameters, ...)
- `prefix` - prefix before the format element if nonempty
- `suffix` - suffix after the format element if nonempty
- `delim` - delimiter between the members of the array
- `string` - a bare string, or enclosed in double quotes if necessary (e.g. if it contains a comma)

Example formats:

- For a method which doesn't throw any exceptions: `{e,throws ,}` => `""`
- For a method which throws `IOException`: `{e,throws ,`} => `"throws IOException"`
- Method parameters #1: `{p,,,-}` => `"int-int-int"`
- Method parameters #2: `{p,(,),", "}` => `"(int, int, int)"`

The default delimiter is a comma.

This class currently has a default property editor in the property editor search path for the IDE.

See Also:
  Serialized Form

Nested Class Summary

Nested classes/interfaces inherited from class java.text.Format
`Format.Field`

Constructor Summary
`ElementFormat(String pattern)` Create a new format.

Method Summary
`boolean` | `dependsOnProperty(String prop)` Test whether a property could affect the formatting.
`String` | `format(Element element)` Formats an element.
`StringBuffer` | `format(Object o, StringBuffer toAppendTo, FieldPosition pos)` Format an object.
`String` | `getPattern()` Get the pattern.
`boolean` | `isSourceFormat()` Test if this format generate strings in source format or fully qualified format.
`Object` | `parseObject(String source, ParsePosition status)` Don't parse objects.
`void` | `setSourceFormat(boolean source)` Set whether the formating is used for code generation.

Methods inherited from class java.text.Format
`clone, format, formatToCharacterIterator, parseObject`

Methods inherited from class java.lang.Object
`equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### ElementFormat

    public ElementFormat(String pattern)

  Create a new format. See documentation for the class for the syntax of the format argument.

  Parameters:
    `pattern` - the pattern describing the format

Method Detail

### setSourceFormat

    public void setSourceFormat(boolean source)

  Set whether the formating is used for code generation. Default value is `true`.

  Parameters:
    `source` - `true` means that all Identifier and Type objects used in formating are evaluated by `getSourceName` method. Otherwise (`false`) the getFullName() is called.

### isSourceFormat

    public boolean isSourceFormat()

  Test if this format generate strings in source format or fully qualified format.

  Returns:
    the source flag

### getPattern

    public String getPattern()

  Get the pattern.

  Returns:
    the current pattern

### format

    public StringBuffer format(Object o,
                               StringBuffer toAppendTo,
                               FieldPosition pos)

  Format an object.

  Specified by:
    `format` in class `Format`
  Parameters:
    `o` - should be an `Element`
    `toAppendTo` - the string buffer to format to
    `pos` - currently ignored
  Returns:
    the same string buffer it was passed (for convenient chaining)
  Throws:
    `IllegalArgumentException` - if the object was not really an `Element`

### format

    public String format(Element element)

  Formats an element.

  Parameters:
    `element` - the element to be printed
  Returns:
    the formatted string using the pattern

### dependsOnProperty

    public boolean dependsOnProperty(String prop)

  Test whether a property could affect the formatting. I.e., if that property would be read due to one of the control codes in the pattern.

  Parameters:
    `prop` - the property name from `ElementProperties`
  Returns:
    `true` if so

### parseObject

    public Object parseObject(String source,
                              ParsePosition status)

  Don't parse objects.

  Specified by:
    `parseObject` in class `Format`
  Parameters:
    `source` - ignored
    `status` - ignored
  Returns:
    `null` in the default implementation

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| org.openide.src 1.8.22
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Built on December 7 2006. | Portions Copyright 1997-2005 Sun Microsystems, Inc. All rights reserved.