xref: /AOO41X/main/scripting/java/org/openoffice/netbeans/editor/NetBeansSourceView.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 javax.swing.*;
26cdf0e10cSrcweir import javax.swing.text.Document;
27cdf0e10cSrcweir import javax.swing.event.DocumentListener;
28cdf0e10cSrcweir import javax.swing.event.DocumentEvent;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir import java.io.*;
31cdf0e10cSrcweir import java.util.ResourceBundle;
32cdf0e10cSrcweir 
33cdf0e10cSrcweir import javax.swing.text.Caret;
34cdf0e10cSrcweir import org.netbeans.editor.*;
35cdf0e10cSrcweir import org.netbeans.editor.ext.*;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir import com.sun.star.script.framework.provider.beanshell.ScriptSourceView;
38cdf0e10cSrcweir import com.sun.star.script.framework.provider.beanshell.ScriptSourceModel;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir public class NetBeansSourceView extends JPanel
41cdf0e10cSrcweir     implements ScriptSourceView, DocumentListener {
42cdf0e10cSrcweir 
43cdf0e10cSrcweir     private ScriptSourceModel model;
44cdf0e10cSrcweir     private JEditorPane pane;
45cdf0e10cSrcweir     private boolean isModified = false;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir     static {
48cdf0e10cSrcweir         // Feed our kits with their default Settings
Settings.addInitializer( new BaseSettingsInitializer(), Settings.CORE_LEVEL)49cdf0e10cSrcweir         Settings.addInitializer(
50cdf0e10cSrcweir             new BaseSettingsInitializer(), Settings.CORE_LEVEL);
Settings.addInitializer( new ExtSettingsInitializer(), Settings.CORE_LEVEL)51cdf0e10cSrcweir         Settings.addInitializer(
52cdf0e10cSrcweir             new ExtSettingsInitializer(), Settings.CORE_LEVEL);
Settings.reset()53cdf0e10cSrcweir         Settings.reset();
54cdf0e10cSrcweir 
55cdf0e10cSrcweir         try {
56cdf0e10cSrcweir             Class kitClass = Class.forName(
57cdf0e10cSrcweir                 NetBeansSourceView.class.getPackage().getName() + ".JavaKit");
58cdf0e10cSrcweir 
59cdf0e10cSrcweir             JEditorPane.registerEditorKitForContentType(
60cdf0e10cSrcweir                 "text/x-java", kitClass.getName(), kitClass.getClassLoader());
61cdf0e10cSrcweir         }
62cdf0e10cSrcweir         catch( ClassNotFoundException exc ) {
63cdf0e10cSrcweir         }
64cdf0e10cSrcweir     }
65cdf0e10cSrcweir 
66cdf0e10cSrcweir     private class MyLocalizer implements LocaleSupport.Localizer {
67cdf0e10cSrcweir         private ResourceBundle bundle;
68cdf0e10cSrcweir 
MyLocalizer( String bundleName )69cdf0e10cSrcweir         public MyLocalizer( String bundleName ) {
70cdf0e10cSrcweir             bundle = ResourceBundle.getBundle( bundleName );
71cdf0e10cSrcweir         }
72cdf0e10cSrcweir 
73cdf0e10cSrcweir         // Localizer
getString( String key )74cdf0e10cSrcweir         public String getString( String key ) {
75cdf0e10cSrcweir             return bundle.getString( key );
76cdf0e10cSrcweir         }
77cdf0e10cSrcweir     }
78cdf0e10cSrcweir 
NetBeansSourceView(ScriptSourceModel model)79cdf0e10cSrcweir     public NetBeansSourceView(ScriptSourceModel model) {
80cdf0e10cSrcweir         this.model = model;
81cdf0e10cSrcweir 
82cdf0e10cSrcweir         LocaleSupport.addLocalizer(
83cdf0e10cSrcweir             new MyLocalizer("org.netbeans.editor.Bundle"));
84cdf0e10cSrcweir 
85cdf0e10cSrcweir         pane = new JEditorPane("text/x-java", "");
86cdf0e10cSrcweir         pane.setText(model.getText());
87cdf0e10cSrcweir 
88cdf0e10cSrcweir         JScrollPane spane = new JScrollPane();
89cdf0e10cSrcweir         spane.setViewportView(pane);
90cdf0e10cSrcweir         setLayout(new java.awt.GridLayout(1, 1));
91cdf0e10cSrcweir         add(spane);
92cdf0e10cSrcweir 
93cdf0e10cSrcweir         pane.getDocument().addDocumentListener(this);
94cdf0e10cSrcweir     }
95cdf0e10cSrcweir 
main(String[] args)96cdf0e10cSrcweir     public static void main(String[] args) {
97cdf0e10cSrcweir         if (args.length < 1) {
98cdf0e10cSrcweir             System.err.println("No file specified");
99cdf0e10cSrcweir             System.exit(-1);
100cdf0e10cSrcweir         }
101cdf0e10cSrcweir 
102cdf0e10cSrcweir         File f = new File(args[0]);
103cdf0e10cSrcweir 
104cdf0e10cSrcweir         if (!f.exists() || !f.isFile()) {
105cdf0e10cSrcweir             System.err.println("Invalid file");
106cdf0e10cSrcweir             System.exit(-1);
107cdf0e10cSrcweir         }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         java.net.URL url = null;
110cdf0e10cSrcweir         try {
111cdf0e10cSrcweir             url = f.toURL();
112cdf0e10cSrcweir         }
113cdf0e10cSrcweir         catch (java.net.MalformedURLException mue) {
114cdf0e10cSrcweir             System.err.println("Invalid file");
115cdf0e10cSrcweir             System.exit(-1);
116cdf0e10cSrcweir         }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir         NetBeansSourceView view =
119cdf0e10cSrcweir             new NetBeansSourceView(new ScriptSourceModel(url));
120cdf0e10cSrcweir 
121cdf0e10cSrcweir         JFrame frame = new JFrame();
122cdf0e10cSrcweir         frame.getContentPane().add(view);
123cdf0e10cSrcweir         frame.setSize(640, 480);
124cdf0e10cSrcweir         frame.show();
125cdf0e10cSrcweir     }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir     // Code grabbed from NetBeans editor module
scrollToLine(int line)128cdf0e10cSrcweir     public void scrollToLine(int line)
129cdf0e10cSrcweir     {
130cdf0e10cSrcweir         BaseDocument doc = Utilities.getDocument(pane);
131cdf0e10cSrcweir 
132cdf0e10cSrcweir         int pos = -1;
133cdf0e10cSrcweir         if (doc != null) {
134cdf0e10cSrcweir             // Obtain the offset where to jump
135cdf0e10cSrcweir             pos = Utilities.getRowStartFromLineOffset(doc, line);
136cdf0e10cSrcweir         }
137cdf0e10cSrcweir 
138cdf0e10cSrcweir         if (pos != -1) {
139cdf0e10cSrcweir             Caret caret = pane.getCaret();
140cdf0e10cSrcweir             if (caret instanceof BaseCaret) { // support extended scroll mode
141cdf0e10cSrcweir                 BaseCaret bCaret = (BaseCaret)caret;
142cdf0e10cSrcweir                 bCaret.setDot(pos, bCaret, EditorUI.SCROLL_FIND);
143cdf0e10cSrcweir             }
144cdf0e10cSrcweir             else {
145cdf0e10cSrcweir                 caret.setDot(pos);
146cdf0e10cSrcweir             }
147cdf0e10cSrcweir         }
148cdf0e10cSrcweir     }
149cdf0e10cSrcweir 
clear()150cdf0e10cSrcweir     public void clear() {
151cdf0e10cSrcweir         pane.setText("");
152cdf0e10cSrcweir     }
153cdf0e10cSrcweir 
update()154cdf0e10cSrcweir     public void update() {
155cdf0e10cSrcweir         /* Remove ourselves as a DocumentListener while loading the source
156cdf0e10cSrcweir            so we don't get a storm of DocumentEvents during loading */
157cdf0e10cSrcweir         pane.getDocument().removeDocumentListener(this);
158cdf0e10cSrcweir 
159cdf0e10cSrcweir         if (isModified == false)
160cdf0e10cSrcweir         {
161cdf0e10cSrcweir             pane.setText(model.getText());
162cdf0e10cSrcweir         }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir         // scroll to current position of the model
165cdf0e10cSrcweir         try {
166cdf0e10cSrcweir             scrollToLine(model.getCurrentPosition());
167cdf0e10cSrcweir         }
168cdf0e10cSrcweir         catch (Exception e) {
169cdf0e10cSrcweir             // couldn't scroll to line, do nothing
170cdf0e10cSrcweir         }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir         // Add back the listener
173cdf0e10cSrcweir         pane.getDocument().addDocumentListener(this);
174cdf0e10cSrcweir     }
175cdf0e10cSrcweir 
isModified()176cdf0e10cSrcweir     public boolean isModified() {
177cdf0e10cSrcweir         return isModified;
178cdf0e10cSrcweir     }
179cdf0e10cSrcweir 
setModified(boolean value)180cdf0e10cSrcweir     public void setModified(boolean value) {
181cdf0e10cSrcweir         isModified = value;
182cdf0e10cSrcweir     }
183cdf0e10cSrcweir 
getText()184cdf0e10cSrcweir     public String getText() {
185cdf0e10cSrcweir         return pane.getText();
186cdf0e10cSrcweir     }
187cdf0e10cSrcweir 
188cdf0e10cSrcweir     /* Implementation of DocumentListener interface */
insertUpdate(DocumentEvent e)189cdf0e10cSrcweir     public void insertUpdate(DocumentEvent e) {
190cdf0e10cSrcweir         doChanged(e);
191cdf0e10cSrcweir     }
192cdf0e10cSrcweir 
removeUpdate(DocumentEvent e)193cdf0e10cSrcweir     public void removeUpdate(DocumentEvent e) {
194cdf0e10cSrcweir         doChanged(e);
195cdf0e10cSrcweir     }
196cdf0e10cSrcweir 
changedUpdate(DocumentEvent e)197cdf0e10cSrcweir     public void changedUpdate(DocumentEvent e) {
198cdf0e10cSrcweir         doChanged(e);
199cdf0e10cSrcweir     }
200cdf0e10cSrcweir 
doChanged(DocumentEvent e)201cdf0e10cSrcweir     public void doChanged(DocumentEvent e) {
202cdf0e10cSrcweir         isModified = true;
203cdf0e10cSrcweir     }
204cdf0e10cSrcweir 
205cdf0e10cSrcweir }
206