01: /*
02: * The contents of this file are subject to the terms of the Common Development
03: * and Distribution License (the License). You may not use this file except in
04: * compliance with the License.
05: *
06: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
07: * or http://www.netbeans.org/cddl.txt.
08: *
09: * When distributing Covered Code, include this CDDL Header Notice in each file
10: * and include the License file at http://www.netbeans.org/cddl.txt.
11: * If applicable, add the following below the CDDL Header, with the fields
12: * enclosed by brackets [] replaced by your own identifying information:
13: * "Portions Copyrighted [year] [name of copyright owner]"
14: *
15: * Portions Copyrighted 2007 Sun Microsystems, Inc.
16: */
17: package org.netbeans.modules.php.editor;
18:
19: import java.util.logging.Level;
20: import java.util.logging.Logger;
21: import javax.swing.text.BadLocationException;
22: import org.netbeans.editor.BaseDocument;
23: import org.netbeans.modules.editor.indent.api.Indent;
24: import org.netbeans.modules.editor.indent.api.Reformat;
25:
26: /**
27: *
28: * @author Tomasz.Slota@Sun.COM
29: */
30: public class FormattingUtils {
31: private static final Logger logger = Logger
32: .getLogger(FormattingUtils.class.getName());
33:
34: public static void reformat(BaseDocument doc, int startPos,
35: int endPos) {
36: Reformat reformat = Reformat.get(doc);
37: reformat.lock();
38:
39: try {
40: doc.atomicLock();
41: try {
42: reformat.reformat(startPos, endPos);
43: } catch (BadLocationException e) {
44: logger.log(Level.WARNING, e.getMessage(), e);
45: } finally {
46: doc.atomicUnlock();
47: }
48: } finally {
49: reformat.unlock();
50: }
51: }
52:
53: public static void indentNewLine(BaseDocument doc, int caretPos) {
54: Indent indent = Indent.get(doc);
55: indent.lock();
56:
57: try {
58: doc.atomicLock();
59: try {
60: indent.reindent(caretPos);
61: } catch (BadLocationException e) {
62: logger.log(Level.WARNING, e.getMessage(), e);
63: } finally {
64: doc.atomicUnlock();
65: }
66: } finally {
67: indent.unlock();
68: }
69: }
70: }
|