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