diff options
5 files changed, 51 insertions, 3 deletions
diff --git a/scripting/Jar_ScriptProviderForBeanShell.mk b/scripting/Jar_ScriptProviderForBeanShell.mk index 8bebc2eeb897..e0b4c3ab5776 100644 --- a/scripting/Jar_ScriptProviderForBeanShell.mk +++ b/scripting/Jar_ScriptProviderForBeanShell.mk @@ -33,6 +33,7 @@ $(eval $(call gb_Jar_add_sourcefiles,ScriptProviderForBeanShell,\ scripting/java/com/sun/star/script/framework/provider/beanshell/ScriptProviderForBeanShell \ scripting/java/com/sun/star/script/framework/provider/beanshell/ScriptSourceModel \ scripting/java/com/sun/star/script/framework/provider/beanshell/ScriptSourceView \ + scripting/java/com/sun/star/script/framework/provider/beanshell/UnsavedChangesListener \ )) $(eval $(call gb_Jar_add_packagefile,ScriptProviderForBeanShell,\ diff --git a/scripting/java/com/sun/star/script/framework/provider/beanshell/PlainSourceView.java b/scripting/java/com/sun/star/script/framework/provider/beanshell/PlainSourceView.java index 27a486dfcdbb..f75b1e75fb0e 100644 --- a/scripting/java/com/sun/star/script/framework/provider/beanshell/PlainSourceView.java +++ b/scripting/java/com/sun/star/script/framework/provider/beanshell/PlainSourceView.java @@ -42,6 +42,8 @@ import javax.swing.event.UndoableEditListener; import javax.swing.text.BadLocationException; import javax.swing.undo.CompoundEdit; import javax.swing.undo.UndoManager; +import java.util.List; +import java.util.ArrayList; public class PlainSourceView extends JScrollPane implements ScriptSourceView, DocumentListener { @@ -56,6 +58,7 @@ public class PlainSourceView extends JScrollPane implements private CompoundEdit compoundEdit = null; private static final int noLimit = -1; UndoManager undoManager; + private List<UnsavedChangesListener> unsavedListener = new ArrayList<UnsavedChangesListener>(); public PlainSourceView(ScriptSourceModel model) { this.model = model; @@ -72,6 +75,10 @@ public class PlainSourceView extends JScrollPane implements if(undoManager.canUndo()){ undoManager.undo(); } + // check if it's the last undoable change + if(undoManager.canUndo() == false){ + setModified(false); + } } public void redo(){ if(undoManager.canRedo()){ @@ -119,8 +126,17 @@ public class PlainSourceView extends JScrollPane implements return isModified; } + private void notifyListeners (boolean isUnsaved) { + for (UnsavedChangesListener listener : unsavedListener) { + listener.onUnsavedChanges(isUnsaved); + } + } + public void setModified(boolean value) { - isModified = value; + if(value != isModified) { + notifyListeners(value); + isModified = value; + } } private void initUI() { @@ -203,7 +219,7 @@ public class PlainSourceView extends JScrollPane implements /* If the number of lines in the JTextArea has changed then update the GlyphGutter */ private void doChanged() { - isModified = true; + setModified(true); if (linecount != ta.getLineCount()) { gg.update(); @@ -222,6 +238,10 @@ public class PlainSourceView extends JScrollPane implements public int getCurrentPosition() { return model.getCurrentPosition(); } + + public void addListener(UnsavedChangesListener toAdd) { + unsavedListener.add(toAdd); + } } class GlyphGutter extends JComponent { diff --git a/scripting/java/com/sun/star/script/framework/provider/beanshell/ScriptEditorForBeanShell.java b/scripting/java/com/sun/star/script/framework/provider/beanshell/ScriptEditorForBeanShell.java index 6ddb7e5deb51..ca00b6c7dd85 100644 --- a/scripting/java/com/sun/star/script/framework/provider/beanshell/ScriptEditorForBeanShell.java +++ b/scripting/java/com/sun/star/script/framework/provider/beanshell/ScriptEditorForBeanShell.java @@ -59,6 +59,7 @@ public class ScriptEditorForBeanShell implements ScriptEditor, ActionListener { private XScriptContext context; private URL scriptURL = null; private ClassLoader cl = null; + private JButton saveBtn; // global ScriptEditorForBeanShell returned for getEditor() calls private static ScriptEditorForBeanShell theScriptEditorForBeanShell; @@ -251,6 +252,15 @@ public class ScriptEditorForBeanShell implements ScriptEditor, ActionListener { this.model.setView(this.view); initUI(); + this.view.addListener(new UnsavedChangesListener() { + @Override + public void onUnsavedChanges(boolean isUnsaved) { + if(filename != null) { + // enable or disable save button depending on unsaved changes + saveBtn.setEnabled(isUnsaved); + } + } + }); frame.setVisible(true); } @@ -281,8 +291,11 @@ public class ScriptEditorForBeanShell implements ScriptEditor, ActionListener { b.addActionListener(this); toolbar.add(b); toolbar.addSeparator(); - if (label.equals("Save") && filename == null) { + + // disable save button on start + if (label.equals("Save")) { b.setEnabled(false); + saveBtn = b; } } diff --git a/scripting/java/com/sun/star/script/framework/provider/beanshell/ScriptSourceView.java b/scripting/java/com/sun/star/script/framework/provider/beanshell/ScriptSourceView.java index 6869fc39785b..e39511c24206 100644 --- a/scripting/java/com/sun/star/script/framework/provider/beanshell/ScriptSourceView.java +++ b/scripting/java/com/sun/star/script/framework/provider/beanshell/ScriptSourceView.java @@ -25,4 +25,5 @@ public interface ScriptSourceView { String getText(); void undo(); void redo(); + void addListener(UnsavedChangesListener toAdd); } diff --git a/scripting/java/com/sun/star/script/framework/provider/beanshell/UnsavedChangesListener.java b/scripting/java/com/sun/star/script/framework/provider/beanshell/UnsavedChangesListener.java new file mode 100644 index 000000000000..8efb2087c7a3 --- /dev/null +++ b/scripting/java/com/sun/star/script/framework/provider/beanshell/UnsavedChangesListener.java @@ -0,0 +1,13 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package com.sun.star.script.framework.provider.beanshell; + +public interface UnsavedChangesListener { + void onUnsavedChanges(boolean isModified); +} |