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();
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;
}
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();
return theme;
}
+ /**
+ * Get the clipboard.
+ *
+ * @return the clipboard
+ */
+ public final Clipboard getClipboard() {
+ return clipboard;
+ }
+
/**
* Repaint the screen on the next update.
*/
*/
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"));
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 --------------------------------------------------------------
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 +
// 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 ----------------------------------------------------------------
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());
+ }
}
/**
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;
+ }
+
}
*/
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);
*/
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);
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;
* @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);
}
}
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>