1 19 package org.openide.text; 20 21 import org.openide.ServiceType; 22 import org.openide.util.HelpCtx; 23 import org.openide.util.Lookup; 24 25 import java.io.Writer ; 26 27 import java.util.Collections ; 28 import java.util.Enumeration ; 29 import java.util.HashMap ; 30 import java.util.Map ; 31 32 import javax.swing.text.*; 33 34 35 41 public abstract class IndentEngine extends ServiceType { 42 static final long serialVersionUID = -8548906260608507035L; 43 44 45 private static Map <String ,IndentEngine> map = new HashMap <String ,IndentEngine>(7); 46 private static IndentEngine INSTANCE = null; 47 48 public HelpCtx getHelpCtx() { 49 return HelpCtx.DEFAULT_HELP; 50 } 51 52 58 public abstract int indentLine(Document doc, int offset); 59 60 67 public abstract int indentNewLine(Document doc, int offset); 68 69 86 public abstract Writer createWriter(Document doc, int offset, Writer writer); 87 88 93 protected boolean acceptMimeType(String mime) { 94 return false; 95 } 96 97 100 @Deprecated 101 public synchronized static void register(String mime, IndentEngine eng) { 102 map.put(mime, eng); 103 } 104 105 108 public static Enumeration <? extends IndentEngine> indentEngines() { 109 return Collections.enumeration(Lookup.getDefault().lookupAll(IndentEngine.class)); 110 } 111 112 115 public synchronized static IndentEngine find(String mime) { 116 Enumeration <? extends IndentEngine> en = indentEngines(); 117 118 while (en.hasMoreElements()) { 119 IndentEngine eng = en.nextElement(); 120 121 if (eng.acceptMimeType(mime)) { 122 return eng; 123 } 124 } 125 126 IndentEngine eng = map.get(mime); 127 128 if (eng != null) { 129 return eng; 130 } 131 132 return getDefault(); 133 } 134 135 138 public static IndentEngine find(Document doc) { 139 Object o = doc.getProperty("indentEngine"); 141 if (o instanceof IndentEngine) { 142 return (IndentEngine) o; 143 } else { 144 o = doc.getProperty("mimeType"); 146 String s = (o instanceof String ) ? (String ) o : "text/plain"; 148 return find(s); 149 } 150 } 151 152 153 public static synchronized IndentEngine getDefault() { 154 if (INSTANCE == null) { 155 INSTANCE = new Default(); 156 } 157 158 return INSTANCE; 159 } 160 161 163 private static final class Default extends IndentEngine { 164 private static final long serialVersionUID = 4493180326470838469L; 165 166 Default() { 167 } 168 169 public int indentLine(Document doc, int offset) { 170 return offset; 171 } 172 173 public int indentNewLine(Document doc, int offset) { 174 try { 175 doc.insertString(offset, "\n", null); } catch (BadLocationException ex) { 177 } 179 180 return offset + 1; 181 } 182 183 public Writer createWriter(Document doc, int offset, Writer writer) { 184 return writer; 185 } 186 187 protected boolean acceptMimeType(String mime) { 188 return true; 189 } 190 191 public HelpCtx getHelpCtx() { 192 return new HelpCtx(Default.class); 193 } 194 } 195 } 196 | Popular Tags |