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@cib.de>
This commit is contained in:
Sainal Shah
2019-03-13 07:46:20 +01:00
committed by Samuel Mehrbrodt
parent 8402cd6974
commit f480c6cb7f
5 changed files with 51 additions and 3 deletions
@@ -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 {
@@ -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;
}
}
@@ -25,4 +25,5 @@ public interface ScriptSourceView {
String getText();
void undo();
void redo();
void addListener(UnsavedChangesListener toAdd);
}
@@ -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);
}