X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTApplication.java;h=d06d0d659b0ed7dfd474743e0a43bb55edc9b8fe;hb=329fd62e4acdaa8e9f4cccd518d47c0b07e79f51;hp=1b928dd8442dae3e851a64e91bc8bbd8ee115b4f;hpb=a043164fd1cc1b38f03bb104f8b5240cdf5705c6;p=fanfix.git diff --git a/src/jexer/TApplication.java b/src/jexer/TApplication.java index 1b928dd..d06d0d6 100644 --- a/src/jexer/TApplication.java +++ b/src/jexer/TApplication.java @@ -1,4 +1,4 @@ -/** +/* * Jexer - Java Text User Interface * * License: LGPLv3 or later @@ -89,7 +89,7 @@ public class TApplication implements Runnable { ECMA48, /** - * Synonym for ECMA48 + * Synonym for ECMA48. */ XTERM } @@ -430,6 +430,11 @@ public class TApplication implements Runnable { */ private Map accelerators; + /** + * All menu items. + */ + private List menuItems; + /** * Windows and widgets pull colors from this ColorTheme. */ @@ -513,6 +518,10 @@ public class TApplication implements Runnable { // Fall through... case ECMA48: backend = new ECMA48Backend(this, null, null); + break; + default: + throw new IllegalArgumentException("Invalid backend type: " + + backendType); } TApplicationImpl(); } @@ -561,6 +570,7 @@ public class TApplication implements Runnable { subMenus = new LinkedList(); timers = new LinkedList(); accelerators = new HashMap(); + menuItems = new ArrayList(); // Setup the main consumer thread primaryEventHandler = new WidgetEventHandler(this, true); @@ -997,7 +1007,7 @@ public class TApplication implements Runnable { // secondary thread locks again. When it gives up, we have the // single lock back. boolean oldLock = unlockHandleEvent(); - assert (oldLock == true); + assert (oldLock); while (secondaryEventReceiver != null) { synchronized (primaryEventHandler) { @@ -1075,6 +1085,7 @@ public class TApplication implements Runnable { synchronized (windows) { int z = window.getZ(); window.setZ(-1); + window.onUnfocus(); Collections.sort(windows); windows.remove(0); TWindow activeWindow = null; @@ -1083,10 +1094,14 @@ public class TApplication implements Runnable { w.setZ(w.getZ() - 1); if (w.getZ() == 0) { w.setActive(true); + w.onFocus(); assert (activeWindow == null); activeWindow = w; } else { - w.setActive(false); + if (w.isActive()) { + w.setActive(false); + w.onUnfocus(); + } } } } @@ -1152,8 +1167,10 @@ public class TApplication implements Runnable { } windows.get(activeWindowI).setActive(false); windows.get(activeWindowI).setZ(windows.get(nextWindowI).getZ()); + windows.get(activeWindowI).onUnfocus(); windows.get(nextWindowI).setZ(0); windows.get(nextWindowI).setActive(true); + windows.get(nextWindowI).onFocus(); } // synchronized (windows) @@ -1171,12 +1188,16 @@ public class TApplication implements Runnable { assert (window.isModal()); } for (TWindow w: windows) { - w.setActive(false); + if (w.isActive()) { + w.setActive(false); + w.onUnfocus(); + } w.setZ(w.getZ() + 1); } windows.add(window); - window.setActive(true); window.setZ(0); + window.setActive(true); + window.onFocus(); } } @@ -1320,10 +1341,12 @@ public class TApplication implements Runnable { // We will be switching to another window assert (windows.get(0).isActive()); assert (!window.isActive()); + windows.get(0).onUnfocus(); windows.get(0).setActive(false); windows.get(0).setZ(window.getZ()); window.setZ(0); window.setActive(true); + window.onFocus(); return; } } @@ -1500,17 +1523,76 @@ public class TApplication implements Runnable { } /** - * Add a keyboard accelerator to the global hash. + * Add a menu item to the global list. If it has a keyboard accelerator, + * that will be added the global hash. * - * @param item menu item this accelerator relates to - * @param keypress keypress that will dispatch a TMenuEvent + * @param item the menu item */ - public final void addAccelerator(final TMenuItem item, - final TKeypress keypress) { + public final void addMenuItem(final TMenuItem item) { + menuItems.add(item); + + TKeypress key = item.getKey(); + if (key != null) { + synchronized (accelerators) { + assert (accelerators.get(key) == null); + accelerators.put(key.toLowerCase(), item); + } + } + } - synchronized (accelerators) { - assert (accelerators.get(keypress) == null); - accelerators.put(keypress, item); + /** + * Disable one menu item. + * + * @param id the menu item ID + */ + public final void disableMenuItem(final int id) { + for (TMenuItem item: menuItems) { + if (item.getId() == id) { + item.setEnabled(false); + } + } + } + + /** + * Disable the range of menu items with ID's between lower and upper, + * inclusive. + * + * @param lower the lowest menu item ID + * @param upper the highest menu item ID + */ + public final void disableMenuItems(final int lower, final int upper) { + for (TMenuItem item: menuItems) { + if ((item.getId() >= lower) && (item.getId() <= upper)) { + item.setEnabled(false); + } + } + } + + /** + * Enable one menu item. + * + * @param id the menu item ID + */ + public final void enableMenuItem(final int id) { + for (TMenuItem item: menuItems) { + if (item.getId() == id) { + item.setEnabled(true); + } + } + } + + /** + * Enable the range of menu items with ID's between lower and upper, + * inclusive. + * + * @param lower the lowest menu item ID + * @param upper the highest menu item ID + */ + public final void enableMenuItems(final int lower, final int upper) { + for (TMenuItem item: menuItems) { + if ((item.getId() >= lower) && (item.getId() <= upper)) { + item.setEnabled(true); + } } } @@ -1827,6 +1909,7 @@ public class TApplication implements Runnable { * * @param path path of selected file * @return the result of the new file open box + * @throws IOException if java.io operation throws */ public final String fileOpenBox(final String path) throws IOException { @@ -1840,6 +1923,7 @@ public class TApplication implements Runnable { * @param path path of selected file * @param type one of the Type constants * @return the result of the new file open box + * @throws IOException if java.io operation throws */ public final String fileOpenBox(final String path, final TFileOpenBox.Type type) throws IOException {