001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.text.java;
011:
012: import java.util.LinkedList;
013: import java.util.Map;
014:
015: import org.eclipse.text.edits.MalformedTreeException;
016: import org.eclipse.text.edits.TextEdit;
017:
018: import org.eclipse.jface.text.BadLocationException;
019: import org.eclipse.jface.text.IDocument;
020: import org.eclipse.jface.text.TextUtilities;
021: import org.eclipse.jface.text.TypedPosition;
022: import org.eclipse.jface.text.formatter.ContextBasedFormattingStrategy;
023: import org.eclipse.jface.text.formatter.FormattingContextProperties;
024: import org.eclipse.jface.text.formatter.IFormattingContext;
025:
026: import org.eclipse.jdt.core.formatter.CodeFormatter;
027:
028: import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil;
029: import org.eclipse.jdt.internal.ui.JavaPlugin;
030:
031: /**
032: * Formatting strategy for java source code.
033: *
034: * @since 3.0
035: */
036: public class JavaFormattingStrategy extends
037: ContextBasedFormattingStrategy {
038:
039: /** Documents to be formatted by this strategy */
040: private final LinkedList fDocuments = new LinkedList();
041: /** Partitions to be formatted by this strategy */
042: private final LinkedList fPartitions = new LinkedList();
043:
044: /**
045: * Creates a new java formatting strategy.
046: */
047: public JavaFormattingStrategy() {
048: super ();
049: }
050:
051: /*
052: * @see org.eclipse.jface.text.formatter.ContextBasedFormattingStrategy#format()
053: */
054: public void format() {
055: super .format();
056:
057: final IDocument document = (IDocument) fDocuments.removeFirst();
058: final TypedPosition partition = (TypedPosition) fPartitions
059: .removeFirst();
060:
061: if (document != null && partition != null) {
062: Map partitioners = null;
063: try {
064:
065: final TextEdit edit = CodeFormatterUtil.reformat(
066: CodeFormatter.K_COMPILATION_UNIT, document
067: .get(), partition.getOffset(),
068: partition.getLength(), 0, TextUtilities
069: .getDefaultLineDelimiter(document),
070: getPreferences());
071: if (edit != null) {
072: if (edit.getChildrenSize() > 20)
073: partitioners = TextUtilities
074: .removeDocumentPartitioners(document);
075:
076: edit.apply(document);
077: }
078:
079: } catch (MalformedTreeException exception) {
080: JavaPlugin.log(exception);
081: } catch (BadLocationException exception) {
082: // Can only happen on concurrent document modification - log and bail out
083: JavaPlugin.log(exception);
084: } finally {
085: if (partitioners != null)
086: TextUtilities.addDocumentPartitioners(document,
087: partitioners);
088: }
089: }
090: }
091:
092: /*
093: * @see org.eclipse.jface.text.formatter.ContextBasedFormattingStrategy#formatterStarts(org.eclipse.jface.text.formatter.IFormattingContext)
094: */
095: public void formatterStarts(final IFormattingContext context) {
096: super .formatterStarts(context);
097:
098: fPartitions
099: .addLast(context
100: .getProperty(FormattingContextProperties.CONTEXT_PARTITION));
101: fDocuments
102: .addLast(context
103: .getProperty(FormattingContextProperties.CONTEXT_MEDIUM));
104: }
105:
106: /*
107: * @see org.eclipse.jface.text.formatter.ContextBasedFormattingStrategy#formatterStops()
108: */
109: public void formatterStops() {
110: super.formatterStops();
111:
112: fPartitions.clear();
113: fDocuments.clear();
114: }
115: }
|