Kaydet (Commit) f480c6cb authored tarafından Sainal Shah's avatar Sainal Shah Kaydeden (comit) Samuel Mehrbrodt

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: 's avatarSamuel Mehrbrodt <Samuel.Mehrbrodt@cib.de>
üst 8402cd69
......@@ -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,\
......
......@@ -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);
}
/* -*- 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);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment