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:
Chirag Manwani
2016-02-22 15:22:30 +05:30
committed by Noel Grandin
parent b9c71ed6de
commit 1b287f14b2
3 changed files with 37 additions and 22 deletions

View File

@@ -52,10 +52,11 @@ public class PlainSourceView extends JScrollPane implements
private GlyphGutter gg; private GlyphGutter gg;
private int linecount; private int linecount;
private boolean isModified = false; private boolean isModified = false;
private UndoManager undoManager;
private static final String undoKey = "Undo"; private static final String undoKey = "Undo";
private static final String redoKey = "Redo"; private static final String redoKey = "Redo";
private CompoundEdit compoundEdit = null; private CompoundEdit compoundEdit = null;
private final int noLimit = -1;
UndoManager undoManager;
public PlainSourceView(ScriptSourceModel model) { public PlainSourceView(ScriptSourceModel model) {
this.model = model; this.model = model;
@@ -63,6 +64,21 @@ public class PlainSourceView extends JScrollPane implements
model.setView(this); 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() { public void clear() {
ta.setText(""); ta.setText("");
} }
@@ -123,7 +139,7 @@ public class PlainSourceView extends JScrollPane implements
ta.insert(model.getText(), 0); ta.insert(model.getText(), 0);
ta.setFont(new Font(Font.MONOSPACED, ta.getFont().getStyle(), ta.getFont().getSize())); ta.setFont(new Font(Font.MONOSPACED, ta.getFont().getStyle(), ta.getFont().getSize()));
undoManager = new UndoManager(); undoManager = new UndoManager();
undoManager.setLimit(noLimit);
ta.getDocument().addUndoableEditListener(new UndoableEditListener(){ ta.getDocument().addUndoableEditListener(new UndoableEditListener(){
@Override @Override
public void undoableEditHappened(UndoableEditEvent editEvent) { public void undoableEditHappened(UndoableEditEvent editEvent) {
@@ -151,23 +167,14 @@ public class PlainSourceView extends JScrollPane implements
ta.getActionMap().put(undoKey, new AbstractAction(undoKey){ ta.getActionMap().put(undoKey, new AbstractAction(undoKey){
@Override @Override
public void actionPerformed(ActionEvent event) { public void actionPerformed(ActionEvent event) {
if(compoundEdit!=null){ undo();
compoundEdit.end();
undoManager.addEdit(compoundEdit);
compoundEdit = null;
}
if(undoManager.canUndo()){
undoManager.undo();
}
} }
}); });
ta.getActionMap().put(redoKey, new AbstractAction(redoKey){ ta.getActionMap().put(redoKey, new AbstractAction(redoKey){
@Override @Override
public void actionPerformed(ActionEvent event) { public void actionPerformed(ActionEvent event) {
if(undoManager.canRedo()){ redo();
undoManager.redo();
}
} }
}); });

View File

@@ -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.framework.provider.SwingInvocation;
import com.sun.star.script.provider.XScriptContext; import com.sun.star.script.provider.XScriptContext;
import java.awt.BorderLayout;
import java.awt.FlowLayout; import java.awt.FlowLayout;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; 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(); JPanel p = new JPanel();
p.setLayout(new FlowLayout()); p.setLayout(new FlowLayout());
@@ -281,8 +282,8 @@ public class ScriptEditorForBeanShell implements ScriptEditor, ActionListener {
} }
} }
frame.getContentPane().add((JComponent)view, "Center"); frame.getContentPane().add((JComponent)view, BorderLayout.CENTER);
frame.getContentPane().add(p, "South"); frame.add(p, BorderLayout.NORTH);
frame.pack(); frame.pack();
frame.setSize(590, 480); frame.setSize(590, 480);
frame.setLocation(300, 200); frame.setLocation(300, 200);
@@ -358,18 +359,23 @@ public class ScriptEditorForBeanShell implements ScriptEditor, ActionListener {
} }
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Run")) { String actionCommand = e.getActionCommand();
if (actionCommand.equals("Run")) {
try { try {
execute(); execute();
} catch (Exception invokeException) { } catch (Exception invokeException) {
showErrorMessage(invokeException.getMessage()); showErrorMessage(invokeException.getMessage());
} }
} else if (e.getActionCommand().equals("Close")) { } else if (actionCommand.equals("Close")) {
doClose(); doClose();
} else if (e.getActionCommand().equals("Save")) { } else if (actionCommand.equals("Save")) {
saveTextArea(); saveTextArea();
} else if (e.getActionCommand().equals("Clear")) { } else if (actionCommand.equals("Clear")) {
view.clear(); view.clear();
} else if(actionCommand.equals("Undo")){
view.undo();
} else if(actionCommand.equals("Redo")){
view.redo();
} }
} }
} }

View File

@@ -23,4 +23,6 @@ public interface ScriptSourceView {
boolean isModified(); boolean isModified();
void setModified(boolean value); void setModified(boolean value);
String getText(); String getText();
} void undo();
void redo();
}