tdf#98004 Added toolbar to beanshell editor with undo/redo buttons
Change-Id: I1d553473f34622e1cb1dab3ffe74ec0c5fa05605 Reviewed-on: https://gerrit.libreoffice.org/22612 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Noel Grandin <noelgrandin@gmail.com>
This commit is contained in:
committed by
Noel Grandin
parent
b9c71ed6de
commit
1b287f14b2
@@ -52,10 +52,11 @@ public class PlainSourceView extends JScrollPane implements
|
||||
private GlyphGutter gg;
|
||||
private int linecount;
|
||||
private boolean isModified = false;
|
||||
private UndoManager undoManager;
|
||||
private static final String undoKey = "Undo";
|
||||
private static final String redoKey = "Redo";
|
||||
private CompoundEdit compoundEdit = null;
|
||||
private final int noLimit = -1;
|
||||
UndoManager undoManager;
|
||||
|
||||
public PlainSourceView(ScriptSourceModel model) {
|
||||
this.model = model;
|
||||
@@ -63,6 +64,21 @@ public class PlainSourceView extends JScrollPane implements
|
||||
model.setView(this);
|
||||
}
|
||||
|
||||
public void undo(){
|
||||
if(compoundEdit!=null){
|
||||
compoundEdit.end();
|
||||
undoManager.addEdit(compoundEdit);
|
||||
compoundEdit = null;
|
||||
}
|
||||
if(undoManager.canUndo()){
|
||||
undoManager.undo();
|
||||
}
|
||||
}
|
||||
public void redo(){
|
||||
if(undoManager.canRedo()){
|
||||
undoManager.redo();
|
||||
}
|
||||
}
|
||||
public void clear() {
|
||||
ta.setText("");
|
||||
}
|
||||
@@ -123,7 +139,7 @@ public class PlainSourceView extends JScrollPane implements
|
||||
ta.insert(model.getText(), 0);
|
||||
ta.setFont(new Font(Font.MONOSPACED, ta.getFont().getStyle(), ta.getFont().getSize()));
|
||||
undoManager = new UndoManager();
|
||||
|
||||
undoManager.setLimit(noLimit);
|
||||
ta.getDocument().addUndoableEditListener(new UndoableEditListener(){
|
||||
@Override
|
||||
public void undoableEditHappened(UndoableEditEvent editEvent) {
|
||||
@@ -151,23 +167,14 @@ public class PlainSourceView extends JScrollPane implements
|
||||
ta.getActionMap().put(undoKey, new AbstractAction(undoKey){
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
if(compoundEdit!=null){
|
||||
compoundEdit.end();
|
||||
undoManager.addEdit(compoundEdit);
|
||||
compoundEdit = null;
|
||||
}
|
||||
if(undoManager.canUndo()){
|
||||
undoManager.undo();
|
||||
}
|
||||
undo();
|
||||
}
|
||||
});
|
||||
|
||||
ta.getActionMap().put(redoKey, new AbstractAction(redoKey){
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
if(undoManager.canRedo()){
|
||||
undoManager.redo();
|
||||
}
|
||||
redo();
|
||||
}
|
||||
});
|
||||
|
||||
|
@@ -23,6 +23,7 @@ import com.sun.star.script.framework.provider.ScriptEditor;
|
||||
import com.sun.star.script.framework.provider.SwingInvocation;
|
||||
import com.sun.star.script.provider.XScriptContext;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.FlowLayout;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
@@ -267,7 +268,7 @@ public class ScriptEditorForBeanShell implements ScriptEditor, ActionListener {
|
||||
}
|
||||
);
|
||||
|
||||
String[] labels = {"Run", "Clear", "Save", "Close"};
|
||||
String[] labels = {"Run", "Clear", "Save", "Close","Undo","Redo"};
|
||||
JPanel p = new JPanel();
|
||||
p.setLayout(new FlowLayout());
|
||||
|
||||
@@ -281,8 +282,8 @@ public class ScriptEditorForBeanShell implements ScriptEditor, ActionListener {
|
||||
}
|
||||
}
|
||||
|
||||
frame.getContentPane().add((JComponent)view, "Center");
|
||||
frame.getContentPane().add(p, "South");
|
||||
frame.getContentPane().add((JComponent)view, BorderLayout.CENTER);
|
||||
frame.add(p, BorderLayout.NORTH);
|
||||
frame.pack();
|
||||
frame.setSize(590, 480);
|
||||
frame.setLocation(300, 200);
|
||||
@@ -358,18 +359,23 @@ public class ScriptEditorForBeanShell implements ScriptEditor, ActionListener {
|
||||
}
|
||||
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (e.getActionCommand().equals("Run")) {
|
||||
String actionCommand = e.getActionCommand();
|
||||
if (actionCommand.equals("Run")) {
|
||||
try {
|
||||
execute();
|
||||
} catch (Exception invokeException) {
|
||||
showErrorMessage(invokeException.getMessage());
|
||||
}
|
||||
} else if (e.getActionCommand().equals("Close")) {
|
||||
} else if (actionCommand.equals("Close")) {
|
||||
doClose();
|
||||
} else if (e.getActionCommand().equals("Save")) {
|
||||
} else if (actionCommand.equals("Save")) {
|
||||
saveTextArea();
|
||||
} else if (e.getActionCommand().equals("Clear")) {
|
||||
} else if (actionCommand.equals("Clear")) {
|
||||
view.clear();
|
||||
} else if(actionCommand.equals("Undo")){
|
||||
view.undo();
|
||||
} else if(actionCommand.equals("Redo")){
|
||||
view.redo();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -23,4 +23,6 @@ public interface ScriptSourceView {
|
||||
boolean isModified();
|
||||
void setModified(boolean value);
|
||||
String getText();
|
||||
}
|
||||
void undo();
|
||||
void redo();
|
||||
}
|
||||
|
Reference in New Issue
Block a user