1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 package org.openoffice.netbeans.editor; 28 29 import java.io.*; 30 import java.awt.event.KeyEvent; 31 import java.awt.event.InputEvent; 32 import java.awt.event.ActionEvent; 33 import java.net.URL; 34 import java.text.MessageFormat; 35 36 import java.util.Map; 37 import java.util.List; 38 import java.util.ResourceBundle; 39 import java.util.MissingResourceException; 40 import javax.swing.KeyStroke; 41 import javax.swing.JEditorPane; 42 import javax.swing.JMenuItem; 43 import javax.swing.Action; 44 import javax.swing.text.Document; 45 import javax.swing.text.JTextComponent; 46 import javax.swing.text.TextAction; 47 import javax.swing.text.BadLocationException; 48 import org.netbeans.editor.*; 49 import org.netbeans.editor.ext.*; 50 import org.netbeans.editor.ext.java.*; 51 52 /** 53 * Java editor kit with appropriate document 54 * 55 * @author Miloslav Metelka 56 * @version 1.00 57 */ 58 59 /* This class is based on the JavaKit class in the demosrc directory of 60 * the editor module of the NetBeans project: http://www.netbeans.org 61 * 62 * The class sets up an EditorKit for syntax highlighting and code completion 63 * of Java syntax 64 */ 65 66 public class JavaKit extends ExtKit { 67 68 public static final String JAVA_MIME_TYPE = "text/x-java"; // NOI18N 69 70 static final long serialVersionUID =-5445829962533684922L; 71 72 static { 73 Settings.addInitializer( new JavaSettingsInitializer( JavaKit.class ) ); 74 Settings.reset(); 75 76 URL skeleton = null, body = null; 77 skeleton = JavaKit.class.getResource("OOo.jcs"); 78 body = JavaKit.class.getResource("OOo.jcb"); 79 80 if (skeleton != null && body != null) { 81 DAFileProvider provider = new DAFileProvider( 82 new URLAccessor(skeleton), 83 new URLAccessor(body)); 84 85 JCBaseFinder finder = new JCBaseFinder(); 86 finder.append( provider ); 87 JavaCompletion.setFinder( finder ); 88 } 89 } 90 91 public String getContentType() { 92 return JAVA_MIME_TYPE; 93 } 94 95 /** Create new instance of syntax coloring scanner 96 * @param doc document to operate on. It can be null in the cases the syntax 97 * creation is not related to the particular document 98 */ 99 public Syntax createSyntax(Document doc) { 100 return new JavaSyntax(); 101 } 102 103 /** Create syntax support */ 104 public SyntaxSupport createSyntaxSupport(BaseDocument doc) { 105 return new JavaSyntaxSupport(doc); 106 } 107 108 public Completion createCompletion(ExtEditorUI extEditorUI) { 109 return new JavaCompletion(extEditorUI); 110 } 111 112 /** Create the formatter appropriate for this kit */ 113 public Formatter createFormatter() { 114 return new JavaFormatter(this.getClass()); 115 } 116 117 protected EditorUI createEditorUI() { 118 return new ExtEditorUI(); 119 } 120 121 protected void initDocument(BaseDocument doc) { 122 doc.addLayer(new JavaDrawLayerFactory.JavaLayer(), 123 JavaDrawLayerFactory.JAVA_LAYER_VISIBILITY); 124 doc.addDocumentListener(new JavaDrawLayerFactory.LParenWatcher()); 125 } 126 127 /** 128 * DataAccessor for parser DB files via URL streams 129 * 130 * @author Petr Nejedly 131 */ 132 public static class URLAccessor implements DataAccessor { 133 134 URL url; 135 InputStream stream; 136 int streamOff; 137 int actOff; 138 139 public URLAccessor(URL url) { 140 this.url = url; 141 } 142 143 /** Not implemented 144 */ 145 public void append(byte[] buffer, int off, int len) 146 throws IOException 147 { 148 throw new IllegalArgumentException("read only!"); 149 } 150 151 /** 152 * Reads exactly <code>len</code> bytes from this file resource 153 * into the byte array, starting at the current file pointer. 154 * This method reads repeatedly from the file until the requested 155 * number of bytes are read. This method blocks until the requested 156 * number of bytes are read, the end of the inputStream is detected, 157 * or an exception is thrown. 158 * 159 * @param buffer the buffer into which the data is read. 160 * @param off the start offset of the data. 161 * @param len the number of bytes to read. 162 */ 163 public void read(byte[] buffer, int off, int len) throws IOException { 164 InputStream str = getStream(actOff); 165 while (len > 0) { 166 int count = str.read(buffer, off, len); 167 streamOff += count; 168 off += count; 169 len -= count; 170 } 171 } 172 173 /** Opens DataAccessor file resource 174 * @param requestWrite if true, file is opened for read/write 175 */ 176 public void open(boolean requestWrite) throws IOException { 177 if(requestWrite) 178 throw new IllegalArgumentException("read only!"); 179 } 180 181 /** Closes DataAccessor file resource */ 182 public void close() throws IOException { 183 if (stream != null) { 184 stream.close(); 185 stream = null; 186 } 187 } 188 189 /** 190 * Returns the current offset in this file. 191 * 192 * @return the offset from the beginning of the file, in bytes, 193 * at which the next read or write occurs. 194 */ 195 public long getFilePointer() throws IOException { 196 return actOff; 197 } 198 199 /** Clears the file and sets the offset to 0 */ 200 public void resetFile() throws IOException { 201 throw new IllegalArgumentException("read only!"); 202 } 203 204 /** 205 * Sets the file-pointer offset, measured from the beginning of this 206 * file, at which the next read or write occurs. 207 */ 208 public void seek(long pos) throws IOException { 209 actOff = (int)pos; 210 } 211 212 /** Gets InputStream prepared for reading from <code>off</code> 213 * offset position 214 */ 215 private InputStream getStream(int off) throws IOException { 216 if (streamOff > off && stream != null) { 217 stream.close(); 218 stream = null; 219 } 220 221 if(stream == null) { 222 stream = url.openStream(); 223 streamOff = 0; 224 } 225 226 while (streamOff < off) { 227 long len = stream.skip(off - streamOff); 228 streamOff += (int)len; 229 if (len == 0) throw new IOException("EOF"); 230 } 231 232 return stream; 233 } 234 235 public int getFileLength() { 236 try { 237 int l = url.openConnection().getContentLength(); 238 return l; 239 } catch (IOException e) { 240 return 0; 241 } 242 } 243 244 public String toString() { 245 return url.toString(); 246 } 247 } 248 } 249