KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > ext > html > LineWrapFormatter


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * 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  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.editor.ext.html;
21
22 import javax.swing.text.Document JavaDoc;
23 import javax.swing.text.JTextComponent JavaDoc;
24 import javax.swing.text.BadLocationException JavaDoc;
25
26 import org.netbeans.editor.*;
27 import org.netbeans.editor.Settings;
28 import org.netbeans.editor.ext.*;
29
30 /**
31  * Simple formatter that will break the line on the nearest previous space
32  * after passing the text limit marker.
33  *
34  * @author Petr Nejedly
35  * @version 1.00
36  */

37 public class LineWrapFormatter extends ExtFormatter {
38
39     int textLimit;
40     Class JavaDoc kitClass;
41
42     public LineWrapFormatter(Class JavaDoc kitClass) {
43         super(kitClass);
44         this.kitClass = kitClass;
45     textLimit = getTextLimit();
46     }
47     
48     /** Gets text limit int value. If the value is not found in local map,
49      * then it is retrieving from Setting map. The default value is used if the
50      * value from settings is also null. */

51     private int getTextLimit(){
52         Object JavaDoc localValue = getSettingValue(SettingsNames.TEXT_LIMIT_WIDTH);
53         if (localValue != null && localValue instanceof Integer JavaDoc){
54             return ((Integer JavaDoc)localValue).intValue();
55         }else{
56             synchronized (Settings.class) {
57                 Object JavaDoc settingsValue = Settings.getValue(kitClass, SettingsNames.TEXT_LIMIT_WIDTH);
58                 if (settingsValue != null && settingsValue instanceof Integer JavaDoc)
59                     return ((Integer JavaDoc)settingsValue).intValue();
60             }
61         }
62         
63         return ((Integer JavaDoc)SettingsDefaults.defaultTextLimitWidth).intValue();
64     }
65     
66     public void settingsChange(SettingsChangeEvent evt) {
67     super.settingsChange(evt);
68     String JavaDoc name = (evt != null) ? evt.getSettingName() : null;
69     if (name == null || SettingsNames.TEXT_LIMIT_WIDTH.equals(name)) {
70         textLimit = getTextLimit();
71     }
72     }
73
74     protected boolean acceptSyntax(Syntax syntax) {
75     return (syntax instanceof HTMLSyntax);
76     }
77
78     public int[] getReformatBlock(JTextComponent JavaDoc target, String JavaDoc typedText) {
79         BaseDocument doc = Utilities.getDocument(target);
80         int dotPos = target.getCaret().getDot();
81
82     // don't reformat for DEL/BKSPC
83
if(typedText.length() == 0 || typedText.charAt(0) == 8 ||
84             typedText.charAt(0) == 127) return null;
85     
86         if (doc != null) {
87             try {
88                 int rstart = Utilities.getRowStart(doc, dotPos);
89                 if(dotPos - rstart > textLimit) {
90             String JavaDoc preText = doc.getText(rstart, dotPos - rstart);
91             int lastSpace = preText.lastIndexOf(' ');
92             if(lastSpace > 0) {
93             doc.remove(rstart+lastSpace, 1);
94             doc.insertString(rstart+lastSpace, "\n", null); // NOI18N
95
}
96                 }
97             }catch(BadLocationException JavaDoc e) {
98         e.printStackTrace();
99             }
100         }
101     
102         return null;
103     }
104     
105     /** Returns offset of EOL for the white line */
106     protected int getEOLOffset(BaseDocument bdoc, int offset) throws BadLocationException JavaDoc{
107         return offset;
108     }
109
110     protected void initFormatLayers() { /* No layers */ }
111
112 }
113
Popular Tags