Skip to content

Commit

Permalink
Added Copy/Paste operations
Browse files Browse the repository at this point in the history
Also no longer discarding all key events corresponding to buttons. Only
'Enter' is discarded because in can lead to a focused button being pressed.
  • Loading branch information
M4444 committed Jun 1, 2018
1 parent 72c2b47 commit a7fb5a4
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
Binary file modified dist/C41C.jar
Binary file not shown.
27 changes: 27 additions & 0 deletions src/c41c/Window.form
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,35 @@
</Menu>
<Menu class="javax.swing.JMenu" name="MENU_Edit">
<Properties>
<Property name="mnemonic" type="int" value="69"/>
<Property name="text" type="java.lang.String" value="Edit"/>
</Properties>
<SubComponents>
<MenuItem class="javax.swing.JMenuItem" name="MENU_ITEM_Copy">
<Properties>
<Property name="accelerator" type="javax.swing.KeyStroke" editor="org.netbeans.modules.form.editors.KeyStrokeEditor">
<KeyStroke key="Ctrl+C"/>
</Property>
<Property name="mnemonic" type="int" value="67"/>
<Property name="text" type="java.lang.String" value="Copy"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="MENU_ITEM_CopyActionPerformed"/>
</Events>
</MenuItem>
<MenuItem class="javax.swing.JMenuItem" name="MENU_ITEM_Paste">
<Properties>
<Property name="accelerator" type="javax.swing.KeyStroke" editor="org.netbeans.modules.form.editors.KeyStrokeEditor">
<KeyStroke key="Ctrl+V"/>
</Property>
<Property name="mnemonic" type="int" value="80"/>
<Property name="text" type="java.lang.String" value="Paste"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="MENU_ITEM_PasteActionPerformed"/>
</Events>
</MenuItem>
</SubComponents>
</Menu>
</SubComponents>
</Menu>
Expand Down
70 changes: 67 additions & 3 deletions src/c41c/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
package c41c;

import java.awt.KeyboardFocusManager;
import java.awt.Toolkit;
import java.awt.datatransfer.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractButton;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.*;
Expand Down Expand Up @@ -49,6 +54,8 @@ public Window() {

initComponents();
myInitComponets();
// Set clipboard
Clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
// Set text pane atrributes
SimpleAttributeSet attribs = new SimpleAttributeSet();
StyleConstants.setAlignment(attribs , StyleConstants.ALIGN_RIGHT);
Expand Down Expand Up @@ -173,6 +180,8 @@ private void initComponents() {
MenuBar = new javax.swing.JMenuBar();
MENU_View = new javax.swing.JMenu();
MENU_Edit = new javax.swing.JMenu();
MENU_ITEM_Copy = new javax.swing.JMenuItem();
MENU_ITEM_Paste = new javax.swing.JMenuItem();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("C41C");
Expand Down Expand Up @@ -1030,7 +1039,29 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
MENU_View.setText("View");
MenuBar.add(MENU_View);

MENU_Edit.setMnemonic('E');
MENU_Edit.setText("Edit");

MENU_ITEM_Copy.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
MENU_ITEM_Copy.setMnemonic('C');
MENU_ITEM_Copy.setText("Copy");
MENU_ITEM_Copy.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MENU_ITEM_CopyActionPerformed(evt);
}
});
MENU_Edit.add(MENU_ITEM_Copy);

MENU_ITEM_Paste.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
MENU_ITEM_Paste.setMnemonic('P');
MENU_ITEM_Paste.setText("Paste");
MENU_ITEM_Paste.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MENU_ITEM_PasteActionPerformed(evt);
}
});
MENU_Edit.add(MENU_ITEM_Paste);

MenuBar.add(MENU_Edit);

setJMenuBar(MenuBar);
Expand Down Expand Up @@ -1311,6 +1342,9 @@ private void addKeyDispatcher() {
if (getFocusOwner().getClass().getSimpleName().equals("BorderlessTextField") &&
e.getKeyCode() != 27)
return false;
// Disable entry when Ctrl or Alt is being held down
if (e.isControlDown() || e.isAltDown())
return false;
System.out.println("Key Code: " + e.getKeyCode());
switch(e.getKeyCode()) {
default:
Expand All @@ -1325,7 +1359,8 @@ private void addKeyDispatcher() {
case 10:
case 61:
BUTTON_equal.doClick();
break;
// Discarded the event to avoid inadvertatnly pressing a focused button.
return true;
case 107:
BUTTON_plus.doClick();
break;
Expand Down Expand Up @@ -1398,8 +1433,7 @@ private void addKeyDispatcher() {
BUTTON_F.doClick();
break;
}
//Return 'true' if you want to discard the event.
return true;
// Return 'true' if the event should be discarded.
}
return false;
});
Expand Down Expand Up @@ -1598,6 +1632,33 @@ private void BUTTON_ClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-
refreshTextArea();
}//GEN-LAST:event_BUTTON_ClearActionPerformed

private void MENU_ITEM_CopyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MENU_ITEM_CopyActionPerformed
StringSelection stringSelection = new StringSelection(Operands[0].toString(Base));
Clipboard.setContents(stringSelection, null);
}//GEN-LAST:event_MENU_ITEM_CopyActionPerformed

private void MENU_ITEM_PasteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MENU_ITEM_PasteActionPerformed
Transferable t = Clipboard.getContents(this);
if (t == null)
// The clipboard is currently unavailable.
return;
try {
String number = (String) t.getTransferData(DataFlavor.stringFlavor);
Operands[Active] = new BigInteger(number, Base);
if (Active == 1)
SecondOperandEntered = true;
changeAllBits(Operands[Active]);

refreshTextArea();
} catch (NumberFormatException e) {
System.out.println("Bad number format");
} catch (UnsupportedFlavorException e) {
System.out.println("Clipboard doesn't contain a string");
} catch (IOException e) {
System.out.println("Clipboard doesn't contain a string anymore");
}
}//GEN-LAST:event_MENU_ITEM_PasteActionPerformed

private void testTextArea() {
long start=System.currentTimeMillis();

Expand Down Expand Up @@ -2006,6 +2067,8 @@ public void run() {
private javax.swing.JLabel LABEL_zero_bit;
private javax.swing.JLabel LABEL_zero_flag;
private javax.swing.JMenu MENU_Edit;
private javax.swing.JMenuItem MENU_ITEM_Copy;
private javax.swing.JMenuItem MENU_ITEM_Paste;
private javax.swing.JMenu MENU_View;
private javax.swing.JMenuBar MenuBar;
private javax.swing.JPanel PANEL_Bits;
Expand All @@ -2019,6 +2082,7 @@ public void run() {
private javax.swing.JTextPane TextPane;
// End of variables declaration//GEN-END:variables
// My variables
private final Clipboard Clipboard;
private final BigInteger[] Operands;
private static final int MEMORY = 2;
private int Active;
Expand Down

0 comments on commit a7fb5a4

Please sign in to comment.