Skip to content

Commit

Permalink
Added basic keyboard shortcut infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
fletchthefletch committed Aug 4, 2021
1 parent 7a86699 commit 2f2b275
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 11 deletions.
11 changes: 0 additions & 11 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,5 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
115 changes: 115 additions & 0 deletions src/main/java/nz/ac/massey/cs/flackpad/KeyBinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package nz.ac.massey.cs.flackpad;

import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.Toolkit;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.KeyStroke;

public class KeyBinder {
private Window windowInstance;
KeyBinder(Window windowInstance) {
this.windowInstance = windowInstance;
addKeyBindings();
}

private void addKeyBindings() {

TextArea textArea = windowInstance.getTextArea();

// Common key shortcuts
KeyStroke ctrl_s = KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK);
KeyStroke ctrl_c = KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK);
KeyStroke ctrl_v = KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_DOWN_MASK);
KeyStroke ctrl_x = KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_DOWN_MASK);

/*** Actions ***/

// Save action
Action save = new AbstractAction() {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
FileIO.save(windowInstance);
}
};

// Copy action
Action copy = new AbstractAction() {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
try {
StringSelection stringSelection = new StringSelection(textArea.getSelectedText());
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);
}
catch (Exception err) {
Dialogs.error("Could not copy selected text to clipboard", windowInstance);
}
}
};

// Cut action
Action cut = new AbstractAction() {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
try {
StringSelection stringSelection = new StringSelection(textArea.getSelectedText());
textArea.replaceSelection("");
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);
}
catch (Exception err) {
Dialogs.error("Could not cut selected", windowInstance);
}
}
};

// Paste action
Action paste = new AbstractAction() {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
try {
/*
* Handle 2 cases:
* a. Paste with text selected
* b. Paste with no text selected
*/
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable data = clipboard.getContents(null);

String plaintext = (String) data.getTransferData(DataFlavor.stringFlavor);

if (textArea.getSelectedText() != null) {
// .a
textArea.replaceSelection(plaintext);
} else {
// .b
textArea.insert(plaintext, textArea.getCaretPosition());
}
}
catch (Exception err) {
Dialogs.error("Could not paste from clipboard", windowInstance);
}
}
};

// Adding key bindings
makeBinding(textArea, ctrl_s, "save_msg", save);
makeBinding(textArea, ctrl_c, "copy_msg", copy);
makeBinding(textArea, ctrl_v, "paste_msg", paste);
makeBinding(textArea, ctrl_x, "cut_msg", cut);

}

private void makeBinding(TextArea area, KeyStroke key, String msg, Action act) {
area.getInputMap().put(key, msg);
area.getActionMap().put(msg, act);
}
}

3 changes: 3 additions & 0 deletions src/main/java/nz/ac/massey/cs/flackpad/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class Window extends JFrame {
textArea = new TextArea(this);
frame.add(new JScrollPane(textArea));

// Add key bindings to instance
new KeyBinder(this);

frame.setSize(1000, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
Expand Down

0 comments on commit 2f2b275

Please sign in to comment.