[Libreoffice-commits] core.git: scripting/Jar_ScriptProviderForBeanShell.mk scripting/java
Libreoffice Gerrit user
logerrit at kemper.freedesktop.org
Wed Mar 13 06:46:45 UTC 2019
scripting/Jar_ScriptProviderForBeanShell.mk | 1
scripting/java/com/sun/star/script/framework/provider/beanshell/PlainSourceView.java | 24 +++++++++-
scripting/java/com/sun/star/script/framework/provider/beanshell/ScriptEditorForBeanShell.java | 15 +++++-
scripting/java/com/sun/star/script/framework/provider/beanshell/ScriptSourceView.java | 1
scripting/java/com/sun/star/script/framework/provider/beanshell/UnsavedChangesListener.java | 13 +++++
5 files changed, 51 insertions(+), 3 deletions(-)
New commits:
commit f480c6cb7f41bf38a2259dface77b8cca87ce6ec
Author: Sainal Shah <sainaledava at gmail.com>
AuthorDate: Sun Mar 10 10:16:59 2019 +0800
Commit: Samuel Mehrbrodt <Samuel.Mehrbrodt at cib.de>
CommitDate: Wed Mar 13 07:46:20 2019 +0100
tdf#123588 Beanshell editor to indicate whether script is saved
Beanshell to enable save button only upon modification of script.
Save button disables after script is successfully saved. Also, save
button will be disabled when there are no undoable changes.
ScriptEditorForBeanShell registers listener for unsaved changes.
PlainSourceView triggers listener calls upon modification of script or
after a successful save, which lets to enable/ disable the save button
accordingly.
Change-Id: I32a2fc473924a7c85cdd6004637ab6a0b60acf38
Reviewed-on: https://gerrit.libreoffice.org/69046
Tested-by: Jenkins
Reviewed-by: Samuel Mehrbrodt <Samuel.Mehrbrodt at cib.de>
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);
+}
More information about the Libreoffice-commits
mailing list