Example usage for org.eclipse.jdt.internal.formatter DefaultCodeFormatterOptions getMap

List of usage examples for org.eclipse.jdt.internal.formatter DefaultCodeFormatterOptions getMap

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.formatter DefaultCodeFormatterOptions getMap.

Prototype

public Map<String, String> getMap() 

Source Link

Usage

From source file:com.iw.plugins.spindle.ui.wizards.factories.ClassFactory.java

License:Mozilla Public License

/**
 * Format an entire Java source file//from   w  w w  .  ja v  a2 s .  c  o  m
 * 
 * @param sourceString
 *          entire file as one unformatted String
 * @return formatted String
 */
public static String formatJava(String sourceString, int initialIndentationLevel, String lineDelim) {
    String ret = sourceString;
    DefaultCodeFormatterOptions dcfo = DefaultCodeFormatterOptions.getJavaConventionsSettings();
    dcfo.line_separator = lineDelim;
    CodeFormatter cformatter = ToolFactory.createCodeFormatter(dcfo.getMap());
    TextEdit te = cformatter.format(CodeFormatter.K_COMPILATION_UNIT, sourceString, 0, sourceString.length(),
            initialIndentationLevel, lineDelim);
    IDocument l_doc = new Document(sourceString);
    try {
        te.apply(l_doc);
    } catch (MalformedTreeException e) {
        // not fatal, since we still have our original contents
        UIPlugin.warn(e);
    } catch (BadLocationException e) {
        // not fatal, since we still have our original contents
        UIPlugin.warn(e);
    }
    String maybe = l_doc.get();
    // in case formatting fails, we'll just return what we got
    if (!didFormatChoke(maybe)) {
        ret = maybe;
    } else {
        UIPlugin.warn("Java formatter choked"); //TODO choked
    }
    return ret + lineDelim; // dunno why we add the extra, but we did
}

From source file:org.nabucco.framework.mda.model.java.file.JavaCodeFormatter.java

License:Open Source License

/**
 * Creates a code formatter with default formatter properties.
 * /*from   w w w . j  a  va  2 s .  c  om*/
 * <ul>
 * <li>Source : 1.5</li>
 * <li>Compliance : 1.5</li>
 * <li>Target Platform : 1.5</li>
 * </ul>
 * 
 * @return the default code formatter
 */
private CodeFormatter createDefaultCodeFormatter() {

    DefaultCodeFormatterOptions formatterOptions = DefaultCodeFormatterOptions.getEclipseDefaultSettings();

    formatterOptions.page_width = MAX_LINE_WIDTH;

    @SuppressWarnings("unchecked")
    Map<String, Object> options = formatterOptions.getMap();
    options.put("org.eclipse.jdt.core.compiler.compliance", 1.5);
    options.put("org.eclipse.jdt.core.compiler.codegen.targetPlatform", 1.5);
    options.put("org.eclipse.jdt.core.compiler.source", 1.5);

    return new DefaultCodeFormatter(formatterOptions, options);
}