The enter and tab keys were causing edits regardless of setEditable(false);
this is now fixed. Minor clean-up resulting from my aborted refactoring of the keyboard event handling code.
This commit is contained in:
parent
6a8753df3b
commit
4eda412cb5
2 changed files with 99 additions and 60 deletions
|
@ -212,32 +212,56 @@ public Clipboard rtfBoard;
|
||||||
public DataFlavor rtfFlavor;
|
public DataFlavor rtfFlavor;
|
||||||
public RTFEditorKit rtfEd = null;
|
public RTFEditorKit rtfEd = null;
|
||||||
|
|
||||||
|
/** Initializes this object. All constructors should call
|
||||||
|
this. */
|
||||||
|
private void initialize(StatusBar sb,
|
||||||
|
TibetanKeyboard keyboard,
|
||||||
|
java.net.URL keyboardURL)
|
||||||
|
{
|
||||||
|
if (null != keyboard)
|
||||||
|
TibetanMachineWeb.setKeyboard(keyboard);
|
||||||
|
if (null != keyboardURL)
|
||||||
|
TibetanMachineWeb.setKeyboard(keyboardURL);
|
||||||
|
setupKeyboard();
|
||||||
|
setupEditor();
|
||||||
|
if (null != sb)
|
||||||
|
setStatusBar(sb);
|
||||||
|
// this(new StyledEditorKit(), keyboardURL);
|
||||||
|
}
|
||||||
|
|
||||||
|
// DLC
|
||||||
|
private Action getActionByName(String name) {
|
||||||
|
Action[] actions = this.getActions();
|
||||||
|
for (int i = 0; i < actions.length; i++) {
|
||||||
|
if (actions[i].getValue(Action.NAME).equals(name)) {
|
||||||
|
return actions[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Creates a new DuffPane that updates sb, if sb is not null,
|
/** Creates a new DuffPane that updates sb, if sb is not null,
|
||||||
with messages about how the users' keypresses are being
|
with messages about how the users' keypresses are being
|
||||||
interpreted. */
|
interpreted. */
|
||||||
public DuffPane(StatusBar sb) {
|
public DuffPane(StatusBar sb) {
|
||||||
this();
|
super();
|
||||||
setStatusBar(sb);
|
initialize(sb, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public DuffPane() {
|
public DuffPane() {
|
||||||
setupKeyboard();
|
super();
|
||||||
setupEditor();
|
initialize(null, null, null);
|
||||||
// this(new StyledEditorKit());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public DuffPane(TibetanKeyboard keyboard) {
|
public DuffPane(TibetanKeyboard keyboard) {
|
||||||
TibetanMachineWeb.setKeyboard(keyboard);
|
super();
|
||||||
setupKeyboard();
|
initialize(null, keyboard, null);
|
||||||
setupEditor();
|
|
||||||
// this(new StyledEditorKit(), keyboard);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public DuffPane(java.net.URL keyboardURL) {
|
public DuffPane(java.net.URL keyboardURL) {
|
||||||
TibetanMachineWeb.setKeyboard(keyboardURL);
|
super();
|
||||||
setupKeyboard();
|
initialize(null, null, keyboardURL);
|
||||||
setupEditor();
|
|
||||||
// this(new StyledEditorKit(), keyboardURL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Sets the status bar to update with mode information. If sb is
|
/** Sets the status bar to update with mode information. If sb is
|
||||||
|
@ -247,7 +271,7 @@ public RTFEditorKit rtfEd = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** If we have a status bar, update it. */
|
/** If we have a status bar, update it. */
|
||||||
private void updateStatus(String newStatus) {
|
protected void updateStatus(String newStatus) {
|
||||||
if (statBar != null) {
|
if (statBar != null) {
|
||||||
/* If we've seen this message just before, append " x2" to
|
/* If we've seen this message just before, append " x2" to
|
||||||
the end of it. The third message will cause a toggle,
|
the end of it. The third message will cause a toggle,
|
||||||
|
@ -260,31 +284,12 @@ public RTFEditorKit rtfEd = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** If we have a status bar, append msg to its current status. */
|
/** If we have a status bar, append msg to its current status. */
|
||||||
private void appendStatus(String msg) {
|
protected void appendStatus(String msg) {
|
||||||
if (statBar != null) {
|
if (statBar != null) {
|
||||||
statBar.replaceStatus(statBar.currentStatus() + msg);
|
statBar.replaceStatus(statBar.currentStatus() + msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
public DuffPane() {
|
|
||||||
setupKeyboard();
|
|
||||||
setupEditor(styledEditorKit);
|
|
||||||
}
|
|
||||||
|
|
||||||
public DuffPane(TibetanKeyboard keyboard) {
|
|
||||||
TibetanMachineWeb.setKeyboard(keyboard);
|
|
||||||
setupKeyboard();
|
|
||||||
setupEditor();
|
|
||||||
}
|
|
||||||
|
|
||||||
public DuffPane(java.net.URL keyboardURL) {
|
|
||||||
TibetanMachineWeb.setKeyboard(keyboardURL);
|
|
||||||
setupKeyboard();
|
|
||||||
setupEditor();
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
private static int defaultTibFontSize() {
|
private static int defaultTibFontSize() {
|
||||||
// FIXME: at program exit, or when the user selects "Save
|
// FIXME: at program exit, or when the user selects "Save
|
||||||
// preferences", or somehow, save the value the users chooses:
|
// preferences", or somehow, save the value the users chooses:
|
||||||
|
@ -305,7 +310,7 @@ public RTFEditorKit rtfEd = null;
|
||||||
*
|
*
|
||||||
* @param sek the StyledEditorKit for the editing window
|
* @param sek the StyledEditorKit for the editing window
|
||||||
*/
|
*/
|
||||||
public void setupEditor() {
|
private void setupEditor() {
|
||||||
rtfBoard = getToolkit().getSystemClipboard();
|
rtfBoard = getToolkit().getSystemClipboard();
|
||||||
rtfFlavor = new DataFlavor("text/rtf", "Rich Text Format");
|
rtfFlavor = new DataFlavor("text/rtf", "Rich Text Format");
|
||||||
rtfEd = new RTFEditorKit();
|
rtfEd = new RTFEditorKit();
|
||||||
|
@ -384,8 +389,7 @@ public RTFEditorKit rtfEd = null;
|
||||||
* but don't register it here.
|
* but don't register it here.
|
||||||
*/
|
*/
|
||||||
public void registerKeyboard() {
|
public void registerKeyboard() {
|
||||||
TibetanKeyboard tk = null;
|
registerKeyboard((TibetanKeyboard)null);
|
||||||
registerKeyboard(tk);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -796,12 +800,13 @@ public RTFEditorKit rtfEd = null;
|
||||||
/**
|
/**
|
||||||
* Required by implementations of the
|
* Required by implementations of the
|
||||||
* {@link java.awt.event.FocusListener FocusListener} interface,
|
* {@link java.awt.event.FocusListener FocusListener} interface,
|
||||||
* this method does nothing.
|
* this method resets the keyboard.
|
||||||
*
|
*
|
||||||
* @param e a FocusEvent
|
* @param e a FocusEvent
|
||||||
*/
|
*/
|
||||||
public void focusLost(FocusEvent e) {
|
public void focusLost(FocusEvent e) {
|
||||||
initKeyboard();
|
initKeyboard();
|
||||||
|
appendStatus(" (because the window focus was lost)");
|
||||||
}
|
}
|
||||||
|
|
||||||
class RTFSelection implements ClipboardOwner, Transferable {
|
class RTFSelection implements ClipboardOwner, Transferable {
|
||||||
|
@ -858,6 +863,18 @@ class RTFSelection implements ClipboardOwner, Transferable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Copies the current selection to the system clipboard, unless
|
||||||
|
cut-and-paste operations are disabled. */
|
||||||
|
public void copyCurrentSelection() {
|
||||||
|
copy(getSelectionStart(), getSelectionEnd(), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** If this.isEditable(), then this copies the current selection
|
||||||
|
to the system clipboard and then deletes it. */
|
||||||
|
public void cutCurrentSelection() {
|
||||||
|
copy(getSelectionStart(), getSelectionEnd(), true);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cuts or copies the specified portion of this object's document
|
* Cuts or copies the specified portion of this object's document
|
||||||
* to the system clipboard. What is cut/copied is Wylie text -
|
* to the system clipboard. What is cut/copied is Wylie text -
|
||||||
|
@ -870,7 +887,8 @@ class RTFSelection implements ClipboardOwner, Transferable {
|
||||||
* @param remove this should be true if the operation is 'cut',
|
* @param remove this should be true if the operation is 'cut',
|
||||||
* false if it is 'copy'
|
* false if it is 'copy'
|
||||||
*/
|
*/
|
||||||
public void copy(int start, int end, boolean remove) {
|
private void copy(int start, int end, boolean remove) {
|
||||||
|
updateStatus("Copied to clipboard");
|
||||||
int p1 = start;
|
int p1 = start;
|
||||||
int p2 = end;
|
int p2 = end;
|
||||||
if (p1 != p2) {
|
if (p1 != p2) {
|
||||||
|
@ -918,6 +936,11 @@ public void paste(int offset) {
|
||||||
|
|
||||||
//construct new document that contains only portion of text you want to paste
|
//construct new document that contains only portion of text you want to paste
|
||||||
StyledDocument sd = new DefaultStyledDocument();
|
StyledDocument sd = new DefaultStyledDocument();
|
||||||
|
|
||||||
|
// I swear this happened once when I pasted in some
|
||||||
|
// random junk just after Jskad started up.
|
||||||
|
ThdlDebug.verify(null != in);
|
||||||
|
|
||||||
rtfEd.read(in, sd, 0);
|
rtfEd.read(in, sd, 0);
|
||||||
for (int i=0; i<sd.getLength()-1; i++) { //getLength()-1 so that final newline is not included in paste
|
for (int i=0; i<sd.getLength()-1; i++) { //getLength()-1 so that final newline is not included in paste
|
||||||
try {
|
try {
|
||||||
|
@ -955,6 +978,11 @@ public void paste(int offset) {
|
||||||
isCutAndPasteEnabled = true;
|
isCutAndPasteEnabled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns true iff cut-and-paste operations are enabled. */
|
||||||
|
public boolean isCutAndPasteOn() {
|
||||||
|
return isCutAndPasteEnabled;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disables cutting and pasting of Tibetan text.
|
* Disables cutting and pasting of Tibetan text.
|
||||||
* Cut and paste must be disabled if Jskad's
|
* Cut and paste must be disabled if Jskad's
|
||||||
|
@ -983,7 +1011,6 @@ public void paste(int offset) {
|
||||||
case KeyEvent.VK_ESCAPE:
|
case KeyEvent.VK_ESCAPE:
|
||||||
e.consume();
|
e.consume();
|
||||||
initKeyboard();
|
initKeyboard();
|
||||||
// toggleLanguage();
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case KeyEvent.VK_A:
|
case KeyEvent.VK_A:
|
||||||
|
@ -1017,20 +1044,24 @@ public void paste(int offset) {
|
||||||
|
|
||||||
case KeyEvent.VK_TAB:
|
case KeyEvent.VK_TAB:
|
||||||
e.consume();
|
e.consume();
|
||||||
initKeyboard();
|
if (this.isEditable()) {
|
||||||
if (isTibetan)
|
initKeyboard();
|
||||||
doc.appendDuff(caret.getDot()," ",TibetanMachineWeb.getAttributeSet(1));
|
if (isTibetan)
|
||||||
else
|
doc.appendDuff(caret.getDot()," ",TibetanMachineWeb.getAttributeSet(1));
|
||||||
append(" ", romanAttributeSet);
|
else
|
||||||
|
append(" ", romanAttributeSet);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case KeyEvent.VK_ENTER:
|
case KeyEvent.VK_ENTER:
|
||||||
e.consume();
|
e.consume();
|
||||||
initKeyboard();
|
if (this.isEditable()) {
|
||||||
if (isTibetan)
|
initKeyboard();
|
||||||
doc.appendDuff(caret.getDot(),"\n",TibetanMachineWeb.getAttributeSet(1)); // FIXME does this work on all platforms?
|
if (isTibetan)
|
||||||
else
|
doc.appendDuff(caret.getDot(),"\n",TibetanMachineWeb.getAttributeSet(1)); // FIXME does this work on all platforms?
|
||||||
append("\n", romanAttributeSet); // FIXME does this work on all platforms?
|
else
|
||||||
|
append("\n", romanAttributeSet); // FIXME does this work on all platforms?
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1109,13 +1140,14 @@ public void paste(int offset) {
|
||||||
*
|
*
|
||||||
* @param e a KeyEvent */
|
* @param e a KeyEvent */
|
||||||
public void processTibetan(KeyEvent e) {
|
public void processTibetan(KeyEvent e) {
|
||||||
boolean changedStatus = false;
|
|
||||||
char c = e.getKeyChar();
|
char c = e.getKeyChar();
|
||||||
|
|
||||||
int start = getSelectionStart();
|
int start = getSelectionStart();
|
||||||
int end = getSelectionEnd();
|
int end = getSelectionEnd();
|
||||||
boolean shouldIBackSpace = true;
|
boolean shouldIBackSpace = true;
|
||||||
|
|
||||||
|
// We don't handle just any old keypress. We handle only the
|
||||||
|
// ones that enter text.
|
||||||
if (e.isControlDown() || e.isAltDown())
|
if (e.isControlDown() || e.isAltDown())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -1131,6 +1163,9 @@ public void paste(int offset) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Have we modified the status bar?
|
||||||
|
boolean changedStatus = false;
|
||||||
|
|
||||||
key_block:
|
key_block:
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
|
@ -142,7 +142,7 @@ public class Jskad extends JPanel implements DocumentListener {
|
||||||
JMenu fileMenu = new JMenu("File");
|
JMenu fileMenu = new JMenu("File");
|
||||||
|
|
||||||
JMenuItem newItem = new JMenuItem("New");
|
JMenuItem newItem = new JMenuItem("New");
|
||||||
// newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,2)); //Ctrl-n
|
// newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,java.awt.Event.CTRL_MASK)); //Ctrl-n
|
||||||
newItem.addActionListener(new ThdlActionListener() {
|
newItem.addActionListener(new ThdlActionListener() {
|
||||||
public void theRealActionPerformed(ActionEvent e) {
|
public void theRealActionPerformed(ActionEvent e) {
|
||||||
newFile();
|
newFile();
|
||||||
|
@ -151,7 +151,7 @@ public class Jskad extends JPanel implements DocumentListener {
|
||||||
fileMenu.add(newItem);
|
fileMenu.add(newItem);
|
||||||
|
|
||||||
JMenuItem openItem = new JMenuItem("Open");
|
JMenuItem openItem = new JMenuItem("Open");
|
||||||
// openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,2)); //Ctrl-o
|
// openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,java.awt.Event.CTRL_MASK)); //Ctrl-o
|
||||||
openItem.addActionListener(new ThdlActionListener() {
|
openItem.addActionListener(new ThdlActionListener() {
|
||||||
public void theRealActionPerformed(ActionEvent e) {
|
public void theRealActionPerformed(ActionEvent e) {
|
||||||
openFile();
|
openFile();
|
||||||
|
@ -179,7 +179,7 @@ public class Jskad extends JPanel implements DocumentListener {
|
||||||
}
|
}
|
||||||
|
|
||||||
JMenuItem saveItem = new JMenuItem("Save");
|
JMenuItem saveItem = new JMenuItem("Save");
|
||||||
// saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,2)); //Ctrl-s
|
// saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,java.awt.Event.CTRL_MASK)); //Ctrl-s
|
||||||
saveItem.addActionListener(new ThdlActionListener() {
|
saveItem.addActionListener(new ThdlActionListener() {
|
||||||
public void theRealActionPerformed(ActionEvent e) {
|
public void theRealActionPerformed(ActionEvent e) {
|
||||||
if (fileName == null)
|
if (fileName == null)
|
||||||
|
@ -207,7 +207,8 @@ public class Jskad extends JPanel implements DocumentListener {
|
||||||
|
|
||||||
if (parentObject instanceof JFrame || parentObject instanceof JInternalFrame) {
|
if (parentObject instanceof JFrame || parentObject instanceof JInternalFrame) {
|
||||||
JMenuItem cutItem = new JMenuItem("Cut");
|
JMenuItem cutItem = new JMenuItem("Cut");
|
||||||
cutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,2)); //Ctrl-x
|
cutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
|
||||||
|
java.awt.Event.CTRL_MASK)); //Ctrl-x
|
||||||
cutItem.addActionListener(new ThdlActionListener() {
|
cutItem.addActionListener(new ThdlActionListener() {
|
||||||
public void theRealActionPerformed(ActionEvent e) {
|
public void theRealActionPerformed(ActionEvent e) {
|
||||||
cutSelection();
|
cutSelection();
|
||||||
|
@ -216,7 +217,8 @@ public class Jskad extends JPanel implements DocumentListener {
|
||||||
editMenu.add(cutItem);
|
editMenu.add(cutItem);
|
||||||
|
|
||||||
JMenuItem copyItem = new JMenuItem("Copy");
|
JMenuItem copyItem = new JMenuItem("Copy");
|
||||||
copyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,2)); //Ctrl-c
|
copyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
|
||||||
|
java.awt.Event.CTRL_MASK)); //Ctrl-c
|
||||||
copyItem.addActionListener(new ThdlActionListener() {
|
copyItem.addActionListener(new ThdlActionListener() {
|
||||||
public void theRealActionPerformed(ActionEvent e) {
|
public void theRealActionPerformed(ActionEvent e) {
|
||||||
copySelection();
|
copySelection();
|
||||||
|
@ -225,7 +227,8 @@ public class Jskad extends JPanel implements DocumentListener {
|
||||||
editMenu.add(copyItem);
|
editMenu.add(copyItem);
|
||||||
|
|
||||||
JMenuItem pasteItem = new JMenuItem("Paste");
|
JMenuItem pasteItem = new JMenuItem("Paste");
|
||||||
pasteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,2)); //Ctrl-v
|
pasteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
|
||||||
|
java.awt.Event.CTRL_MASK)); //Ctrl-v
|
||||||
pasteItem.addActionListener(new ThdlActionListener() {
|
pasteItem.addActionListener(new ThdlActionListener() {
|
||||||
public void theRealActionPerformed(ActionEvent e) {
|
public void theRealActionPerformed(ActionEvent e) {
|
||||||
pasteSelection();
|
pasteSelection();
|
||||||
|
@ -235,7 +238,8 @@ public class Jskad extends JPanel implements DocumentListener {
|
||||||
editMenu.addSeparator();
|
editMenu.addSeparator();
|
||||||
|
|
||||||
JMenuItem selectallItem = new JMenuItem("Select All");
|
JMenuItem selectallItem = new JMenuItem("Select All");
|
||||||
selectallItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,2)); //Ctrl-a
|
selectallItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,
|
||||||
|
java.awt.Event.CTRL_MASK)); //Ctrl-a
|
||||||
selectallItem.addActionListener(new ThdlActionListener() {
|
selectallItem.addActionListener(new ThdlActionListener() {
|
||||||
public void theRealActionPerformed(ActionEvent e) {
|
public void theRealActionPerformed(ActionEvent e) {
|
||||||
dp.setSelectionStart(0);
|
dp.setSelectionStart(0);
|
||||||
|
@ -706,11 +710,11 @@ public class Jskad extends JPanel implements DocumentListener {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void cutSelection() {
|
private void cutSelection() {
|
||||||
dp.copy(dp.getSelectionStart(), dp.getSelectionEnd(), true);
|
dp.cutCurrentSelection();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void copySelection() {
|
private void copySelection() {
|
||||||
dp.copy(dp.getSelectionStart(), dp.getSelectionEnd(), false);
|
dp.copyCurrentSelection();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void pasteSelection() {
|
private void pasteSelection() {
|
||||||
|
|
Loading…
Reference in a new issue