1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package org.openoffice.netbeans.editor;
24 
25 import javax.swing.*;
26 import javax.swing.text.Document;
27 import javax.swing.event.DocumentListener;
28 import javax.swing.event.DocumentEvent;
29 
30 import java.io.*;
31 import java.util.ResourceBundle;
32 
33 import javax.swing.text.Caret;
34 import org.netbeans.editor.*;
35 import org.netbeans.editor.ext.*;
36 
37 import com.sun.star.script.framework.provider.beanshell.ScriptSourceView;
38 import com.sun.star.script.framework.provider.beanshell.ScriptSourceModel;
39 
40 public class NetBeansSourceView extends JPanel
41     implements ScriptSourceView, DocumentListener {
42 
43     private ScriptSourceModel model;
44     private JEditorPane pane;
45     private boolean isModified = false;
46 
47     static {
48         // Feed our kits with their default Settings
Settings.addInitializer( new BaseSettingsInitializer(), Settings.CORE_LEVEL)49         Settings.addInitializer(
50             new BaseSettingsInitializer(), Settings.CORE_LEVEL);
Settings.addInitializer( new ExtSettingsInitializer(), Settings.CORE_LEVEL)51         Settings.addInitializer(
52             new ExtSettingsInitializer(), Settings.CORE_LEVEL);
Settings.reset()53         Settings.reset();
54 
55         try {
56             Class kitClass = Class.forName(
57                 NetBeansSourceView.class.getPackage().getName() + ".JavaKit");
58 
59             JEditorPane.registerEditorKitForContentType(
60                 "text/x-java", kitClass.getName(), kitClass.getClassLoader());
61         }
62         catch( ClassNotFoundException exc ) {
63         }
64     }
65 
66     private class MyLocalizer implements LocaleSupport.Localizer {
67         private ResourceBundle bundle;
68 
MyLocalizer( String bundleName )69         public MyLocalizer( String bundleName ) {
70             bundle = ResourceBundle.getBundle( bundleName );
71         }
72 
73         // Localizer
getString( String key )74         public String getString( String key ) {
75             return bundle.getString( key );
76         }
77     }
78 
NetBeansSourceView(ScriptSourceModel model)79     public NetBeansSourceView(ScriptSourceModel model) {
80         this.model = model;
81 
82         LocaleSupport.addLocalizer(
83             new MyLocalizer("org.netbeans.editor.Bundle"));
84 
85         pane = new JEditorPane("text/x-java", "");
86         pane.setText(model.getText());
87 
88         JScrollPane spane = new JScrollPane();
89         spane.setViewportView(pane);
90         setLayout(new java.awt.GridLayout(1, 1));
91         add(spane);
92 
93         pane.getDocument().addDocumentListener(this);
94     }
95 
main(String[] args)96     public static void main(String[] args) {
97         if (args.length < 1) {
98             System.err.println("No file specified");
99             System.exit(-1);
100         }
101 
102         File f = new File(args[0]);
103 
104         if (!f.exists() || !f.isFile()) {
105             System.err.println("Invalid file");
106             System.exit(-1);
107         }
108 
109         java.net.URL url = null;
110         try {
111             url = f.toURL();
112         }
113         catch (java.net.MalformedURLException mue) {
114             System.err.println("Invalid file");
115             System.exit(-1);
116         }
117 
118         NetBeansSourceView view =
119             new NetBeansSourceView(new ScriptSourceModel(url));
120 
121         JFrame frame = new JFrame();
122         frame.getContentPane().add(view);
123         frame.setSize(640, 480);
124         frame.show();
125     }
126 
127     // Code grabbed from NetBeans editor module
scrollToLine(int line)128     public void scrollToLine(int line)
129     {
130         BaseDocument doc = Utilities.getDocument(pane);
131 
132         int pos = -1;
133         if (doc != null) {
134             // Obtain the offset where to jump
135             pos = Utilities.getRowStartFromLineOffset(doc, line);
136         }
137 
138         if (pos != -1) {
139             Caret caret = pane.getCaret();
140             if (caret instanceof BaseCaret) { // support extended scroll mode
141                 BaseCaret bCaret = (BaseCaret)caret;
142                 bCaret.setDot(pos, bCaret, EditorUI.SCROLL_FIND);
143             }
144             else {
145                 caret.setDot(pos);
146             }
147         }
148     }
149 
clear()150     public void clear() {
151         pane.setText("");
152     }
153 
update()154     public void update() {
155         /* Remove ourselves as a DocumentListener while loading the source
156            so we don't get a storm of DocumentEvents during loading */
157         pane.getDocument().removeDocumentListener(this);
158 
159         if (isModified == false)
160         {
161             pane.setText(model.getText());
162         }
163 
164         // scroll to current position of the model
165         try {
166             scrollToLine(model.getCurrentPosition());
167         }
168         catch (Exception e) {
169             // couldn't scroll to line, do nothing
170         }
171 
172         // Add back the listener
173         pane.getDocument().addDocumentListener(this);
174     }
175 
isModified()176     public boolean isModified() {
177         return isModified;
178     }
179 
setModified(boolean value)180     public void setModified(boolean value) {
181         isModified = value;
182     }
183 
getText()184     public String getText() {
185         return pane.getText();
186     }
187 
188     /* Implementation of DocumentListener interface */
insertUpdate(DocumentEvent e)189     public void insertUpdate(DocumentEvent e) {
190         doChanged(e);
191     }
192 
removeUpdate(DocumentEvent e)193     public void removeUpdate(DocumentEvent e) {
194         doChanged(e);
195     }
196 
changedUpdate(DocumentEvent e)197     public void changedUpdate(DocumentEvent e) {
198         doChanged(e);
199     }
200 
doChanged(DocumentEvent e)201     public void doChanged(DocumentEvent e) {
202         isModified = true;
203     }
204 
205 }
206