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