1 19 20 package org.netbeans.editor.ext.html; 21 22 import javax.swing.text.Document ; 23 import javax.swing.text.JTextComponent ; 24 import javax.swing.text.BadLocationException ; 25 26 import org.netbeans.editor.*; 27 import org.netbeans.editor.Settings; 28 import org.netbeans.editor.ext.*; 29 30 37 public class LineWrapFormatter extends ExtFormatter { 38 39 int textLimit; 40 Class kitClass; 41 42 public LineWrapFormatter(Class kitClass) { 43 super(kitClass); 44 this.kitClass = kitClass; 45 textLimit = getTextLimit(); 46 } 47 48 51 private int getTextLimit(){ 52 Object localValue = getSettingValue(SettingsNames.TEXT_LIMIT_WIDTH); 53 if (localValue != null && localValue instanceof Integer ){ 54 return ((Integer )localValue).intValue(); 55 }else{ 56 synchronized (Settings.class) { 57 Object settingsValue = Settings.getValue(kitClass, SettingsNames.TEXT_LIMIT_WIDTH); 58 if (settingsValue != null && settingsValue instanceof Integer ) 59 return ((Integer )settingsValue).intValue(); 60 } 61 } 62 63 return ((Integer )SettingsDefaults.defaultTextLimitWidth).intValue(); 64 } 65 66 public void settingsChange(SettingsChangeEvent evt) { 67 super.settingsChange(evt); 68 String 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 target, String typedText) { 79 BaseDocument doc = Utilities.getDocument(target); 80 int dotPos = target.getCaret().getDot(); 81 82 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 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); } 96 } 97 }catch(BadLocationException e) { 98 e.printStackTrace(); 99 } 100 } 101 102 return null; 103 } 104 105 106 protected int getEOLOffset(BaseDocument bdoc, int offset) throws BadLocationException { 107 return offset; 108 } 109 110 protected void initFormatLayers() { } 111 112 } 113 | Popular Tags |