TField cut/paste working
authorKevin Lamonte <kevin.lamonte@gmail.com>
Sat, 2 Nov 2019 04:46:12 +0000 (23:46 -0500)
committerKevin Lamonte <kevin.lamonte@gmail.com>
Sat, 2 Nov 2019 04:46:12 +0000 (23:46 -0500)
src/jexer/EditMenuUser.java
src/jexer/TApplication.java
src/jexer/TField.java
src/jexer/TTableWindow.java
src/jexer/TWidget.java

index b6e7845065c482f27322f8d2a1973fa695499654..52dc33e4db97a806ccc059ec94ec9483f5f1a152 100644 (file)
@@ -36,30 +36,30 @@ package jexer;
 public interface EditMenuUser {
 
     /**
-     * Check if cut to clipboard is supported.
+     * Check if the cut menu item should be enabled.
      *
-     * @return true if cut to clipboard is supported
+     * @return true if the cut menu item should be enabled
      */
     public boolean isEditMenuCut();
 
     /**
-     * Check if copy to clipboard is supported.
+     * Check if the copy menu item should be enabled.
      *
-     * @return true if copy to clipboard is supported
+     * @return true if the copy menu item should be enabled
      */
     public boolean isEditMenuCopy();
 
     /**
-     * Check if paste from clipboard is supported.
+     * Check if the paste menu item should be enabled.
      *
-     * @return true if paste from clipboard is supported
+     * @return true if the paste menu item should be enabled
      */
     public boolean isEditMenuPaste();
 
     /**
-     * Check if clear selection is supported.
+     * Check if the clear menu item should be enabled.
      *
-     * @return true if clear selection is supported
+     * @return true if the clear menu item should be enabled
      */
     public boolean isEditMenuClear();
 
index a38b2daba2a95fbe382de7ffc383520f14d5ad68..13cd07958ee3a61c9b36e2a76cf0f2560e48920a 100644 (file)
@@ -1025,6 +1025,24 @@ public class TApplication implements Runnable {
             new TFontChooserWindow(this);
             return true;
         }
+
+        if (menu.getId() == TMenu.MID_CUT) {
+            postMenuEvent(new TCommandEvent(cmCut));
+            return true;
+        }
+        if (menu.getId() == TMenu.MID_COPY) {
+            postMenuEvent(new TCommandEvent(cmCopy));
+            return true;
+        }
+        if (menu.getId() == TMenu.MID_PASTE) {
+            postMenuEvent(new TCommandEvent(cmPaste));
+            return true;
+        }
+        if (menu.getId() == TMenu.MID_CLEAR) {
+            postMenuEvent(new TCommandEvent(cmClear));
+            return true;
+        }
+
         return false;
     }
 
@@ -1071,6 +1089,47 @@ public class TApplication implements Runnable {
                 Thread.currentThread() + " finishEventProcessing()\n");
         }
 
+        // See if we need to enable/disable the edit menu.
+        EditMenuUser widget = null;
+        if (activeMenu == null) {
+            if (activeWindow != null) {
+                if (activeWindow.getActiveChild() instanceof EditMenuUser) {
+                    widget = (EditMenuUser) activeWindow.getActiveChild();
+                }
+            } else if (desktop != null) {
+                if (desktop.getActiveChild() instanceof EditMenuUser) {
+                    widget = (EditMenuUser) desktop.getActiveChild();
+                }
+            }
+            if (widget == null) {
+                disableMenuItem(TMenu.MID_CUT);
+                disableMenuItem(TMenu.MID_COPY);
+                disableMenuItem(TMenu.MID_PASTE);
+                disableMenuItem(TMenu.MID_CLEAR);
+            } else {
+                if (widget.isEditMenuCut()) {
+                    enableMenuItem(TMenu.MID_CUT);
+                } else {
+                    disableMenuItem(TMenu.MID_CUT);
+                }
+                if (widget.isEditMenuCopy()) {
+                    enableMenuItem(TMenu.MID_COPY);
+                } else {
+                    disableMenuItem(TMenu.MID_COPY);
+                }
+                if (widget.isEditMenuPaste()) {
+                    enableMenuItem(TMenu.MID_PASTE);
+                } else {
+                    disableMenuItem(TMenu.MID_PASTE);
+                }
+                if (widget.isEditMenuClear()) {
+                    enableMenuItem(TMenu.MID_CLEAR);
+                } else {
+                    disableMenuItem(TMenu.MID_CLEAR);
+                }
+            }
+        }
+
         // Process timers and call doIdle()'s
         doIdle();
 
@@ -1642,6 +1701,15 @@ public class TApplication implements Runnable {
         return theme;
     }
 
+    /**
+     * Get the clipboard.
+     *
+     * @return the clipboard
+     */
+    public final Clipboard getClipboard() {
+        return clipboard;
+    }
+
     /**
      * Repaint the screen on the next update.
      */
@@ -3301,10 +3369,10 @@ public class TApplication implements Runnable {
      */
     public final TMenu addEditMenu() {
         TMenu editMenu = addMenu(i18n.getString("editMenuTitle"));
-        editMenu.addDefaultItem(TMenu.MID_CUT);
-        editMenu.addDefaultItem(TMenu.MID_COPY);
-        editMenu.addDefaultItem(TMenu.MID_PASTE);
-        editMenu.addDefaultItem(TMenu.MID_CLEAR);
+        editMenu.addDefaultItem(TMenu.MID_CUT, false);
+        editMenu.addDefaultItem(TMenu.MID_COPY, false);
+        editMenu.addDefaultItem(TMenu.MID_PASTE, false);
+        editMenu.addDefaultItem(TMenu.MID_CLEAR, false);
         TStatusBar statusBar = editMenu.newStatusBar(i18n.
             getString("editMenuStatus"));
         statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help"));
index 7c8b5bc415e62882a24941734da6ac213c706b75..29632ca5ba026533c9f4487702f983eccfd19f27 100644 (file)
@@ -31,14 +31,16 @@ package jexer;
 import jexer.bits.CellAttributes;
 import jexer.bits.GraphicsChars;
 import jexer.bits.StringUtils;
+import jexer.event.TCommandEvent;
 import jexer.event.TKeypressEvent;
 import jexer.event.TMouseEvent;
+import static jexer.TCommand.*;
 import static jexer.TKeypress.*;
 
 /**
  * TField implements an editable text field.
  */
-public class TField extends TWidget {
+public class TField extends TWidget implements EditMenuUser {
 
     // ------------------------------------------------------------------------
     // Variables --------------------------------------------------------------
@@ -234,12 +236,13 @@ public class TField extends TWidget {
 
         if (keypress.equals(kbRight)) {
             if (position < text.length()) {
+                int lastPosition = position;
                 screenPosition += StringUtils.width(text.codePointAt(position));
                 position += Character.charCount(text.codePointAt(position));
                 if (fixed == true) {
                     if (screenPosition == getWidth()) {
                         screenPosition--;
-                        position -= Character.charCount(text.codePointAt(position));
+                        position -= Character.charCount(text.codePointAt(lastPosition));
                     }
                 } else {
                     while ((screenPosition - windowStart +
@@ -373,6 +376,42 @@ public class TField extends TWidget {
         // Pass to parent for the things we don't care about.
         super.onKeypress(keypress);
     }
+    /**
+     * Handle posted command events.
+     *
+     * @param command command event
+     */
+    @Override
+    public void onCommand(final TCommandEvent command) {
+        if (command.equals(cmCut)) {
+            // Copy text to clipboard, and then remove it.
+            getClipboard().copyText(text);
+            setText("");
+            return;
+        }
+
+        if (command.equals(cmCopy)) {
+            // Copy text to clipboard.
+            getClipboard().copyText(text);
+            return;
+        }
+
+        if (command.equals(cmPaste)) {
+            // Paste text from clipboard.
+            String newText = getClipboard().pasteText();
+            if (newText != null) {
+                setText(newText);
+            }
+            return;
+        }
+
+        if (command.equals(cmClear)) {
+            // Remove text.
+            setText("");
+            return;
+        }
+
+    }
 
     // ------------------------------------------------------------------------
     // TWidget ----------------------------------------------------------------
@@ -467,7 +506,11 @@ public class TField extends TWidget {
         assert (text != null);
         this.text = text;
         position = 0;
+        screenPosition = 0;
         windowStart = 0;
+        if ((fixed == true) && (this.text.length() > getWidth())) {
+            this.text = this.text.substring(0, getWidth());
+        }
     }
 
     /**
@@ -668,4 +711,44 @@ public class TField extends TWidget {
         updateAction = action;
     }
 
+    // ------------------------------------------------------------------------
+    // EditMenuUser -----------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * Check if the cut menu item should be enabled.
+     *
+     * @return true if the cut menu item should be enabled
+     */
+    public boolean isEditMenuCut() {
+        return true;
+    }
+
+    /**
+     * Check if the copy menu item should be enabled.
+     *
+     * @return true if the copy menu item should be enabled
+     */
+    public boolean isEditMenuCopy() {
+        return true;
+    }
+
+    /**
+     * Check if the paste menu item should be enabled.
+     *
+     * @return true if the paste menu item should be enabled
+     */
+    public boolean isEditMenuPaste() {
+        return true;
+    }
+
+    /**
+     * Check if the clear menu item should be enabled.
+     *
+     * @return true if the clear menu item should be enabled
+     */
+    public boolean isEditMenuClear() {
+        return true;
+    }
+
 }
index 44ff7b48949749c1590bcead79b57f3b55d727fc..766ceafdd901a0a4044105aba5114093cf7ef7a6 100644 (file)
@@ -112,7 +112,6 @@ public class TTableWindow extends TScrollableWindow {
      */
     public void onFocus() {
         // Enable the table menu items.
-        getApplication().enableMenuItem(TMenu.MID_CUT);
         getApplication().enableMenuItem(TMenu.MID_TABLE_RENAME_COLUMN);
         getApplication().enableMenuItem(TMenu.MID_TABLE_RENAME_ROW);
         getApplication().enableMenuItem(TMenu.MID_TABLE_VIEW_ROW_LABELS);
@@ -171,7 +170,6 @@ public class TTableWindow extends TScrollableWindow {
      */
     public void onUnfocus() {
         // Disable the table menu items.
-        getApplication().disableMenuItem(TMenu.MID_CUT);
         getApplication().disableMenuItem(TMenu.MID_TABLE_RENAME_COLUMN);
         getApplication().disableMenuItem(TMenu.MID_TABLE_RENAME_ROW);
         getApplication().disableMenuItem(TMenu.MID_TABLE_VIEW_ROW_LABELS);
index e94fed25077c68ca763b8dc90e76b98d36898469..012ed756c3a60adf4f40bf72e9c54954a3c28e98 100644 (file)
@@ -36,6 +36,7 @@ import java.util.ArrayList;
 import jexer.backend.Screen;
 import jexer.bits.Cell;
 import jexer.bits.CellAttributes;
+import jexer.bits.Clipboard;
 import jexer.bits.ColorTheme;
 import jexer.event.TCommandEvent;
 import jexer.event.TInputEvent;
@@ -598,9 +599,8 @@ public abstract class TWidget implements Comparable<TWidget> {
      * @param command command event
      */
     public void onCommand(final TCommandEvent command) {
-        // Default: do nothing, pass to children instead
-        for (TWidget widget: children) {
-            widget.onCommand(command);
+        if (activeChild != null) {
+            activeChild.onCommand(command);
         }
     }
 
@@ -1133,6 +1133,18 @@ public abstract class TWidget implements Comparable<TWidget> {
         return null;
     }
 
+    /**
+     * Get the Clipboard.
+     *
+     * @return the Clipboard, or null if not assigned
+     */
+    public Clipboard getClipboard() {
+        if (window != null) {
+            return window.getApplication().getClipboard();
+        }
+        return null;
+    }
+
     /**
      * Comparison operator.  For various subclasses it sorts on:
      * <ul>