X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTApplication.java;h=51566287286b7ef5af238a4ec1f22dae5df08a51;hb=4b257bd88a40bb1b7ad6b14c3ecaf6a3d5d10a9b;hp=d0c34bf0303adbe99e917f6afbcd278032915af6;hpb=0d47c5460c8e9d1198928308767a63ad35f46eb8;p=fanfix.git diff --git a/src/jexer/TApplication.java b/src/jexer/TApplication.java index d0c34bf..5156628 100644 --- a/src/jexer/TApplication.java +++ b/src/jexer/TApplication.java @@ -1,4 +1,4 @@ -/** +/* * Jexer - Java Text User Interface * * License: LGPLv3 or later @@ -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. */ @@ -561,6 +566,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); @@ -980,7 +986,8 @@ public class TApplication implements Runnable { public final void enableSecondaryEventReceiver(final TWidget widget) { assert (secondaryEventReceiver == null); assert (secondaryEventHandler == null); - assert (widget instanceof TMessageBox); + assert ((widget instanceof TMessageBox) + || (widget instanceof TFileOpenBox)); secondaryEventReceiver = widget; secondaryEventHandler = new WidgetEventHandler(this, false); (new Thread(secondaryEventHandler)).start(); @@ -1074,6 +1081,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; @@ -1082,10 +1090,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(); + } } } } @@ -1151,8 +1163,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) @@ -1170,12 +1184,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(); } } @@ -1319,10 +1337,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; } } @@ -1499,17 +1519,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 the menu item + */ + 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); + } + } + } + + /** + * 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 item menu item this accelerator relates to - * @param keypress keypress that will dispatch a TMenuEvent + * @param lower the lowest menu item ID + * @param upper the highest menu item ID */ - public final void addAccelerator(final TMenuItem item, - final TKeypress keypress) { + public final void disableMenuItems(final int lower, final int upper) { + for (TMenuItem item: menuItems) { + if ((item.getId() >= lower) && (item.getId() <= upper)) { + item.setEnabled(false); + } + } + } - synchronized (accelerators) { - assert (accelerators.get(keypress) == null); - accelerators.put(keypress, item); + /** + * 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); + } } }