@Override public synchronized JMenu getJMenu(final String menuName, final Icon icon, final String clearName, final String clearTooltip) { if (menu == null) { createJMenu(menuName, icon, clearName, clearTooltip); restoreMenu(); } return menu; }
@Override @SuppressWarnings("unchecked") public void clear() { ArrayList<StringMenuItem<T>> keyClone = (ArrayList<StringMenuItem<T>>) itemStrings .clone(); for (StringMenuItem<T> item : keyClone) { removeItem(item); } }
@SuppressWarnings("unchecked") private synchronized void restoreMenu() { try { log.trace("Trying to deserialize key list from preferences"); byte[] data = preferences.getBytes(preferencesKey); if (data != null) { ObjectInputStream ins = new ObjectInputStream( new ByteArrayInputStream(data)); // Generates unchecked warnings but we know ArrayList<StringMenuItem<T>> restoredKeys = (ArrayList<StringMenuItem<T>>) ins .readObject(); createJMenu(); for (final StringMenuItem<T> item : restoredKeys) { addItemInternal(item); } } } catch (InvalidDataTypeException e) { log.warn(e); } catch (BackendException e) { log.warn(e); } catch (IOException e) { log.warn(e); } catch (ClassNotFoundException e) { log.warn(e); } }
private synchronized void addItemInternal( final StringMenuItem<T> stringMenuItem) { log.trace("Creating JMenu item"); JMenuItem jMenuItem = createJMenuItem(stringMenuItem); log.trace("Adding cache entry for item: " + stringMenuItem); itemStringToJMenuItem.put(stringMenuItem, jMenuItem); log.trace("Adding key to key list"); itemStrings.add(stringMenuItem); log.trace("Addin JMenuItem: " + jMenuItem + " to JMenu"); getJMenu().insert(jMenuItem, 2); }
@Override public synchronized void addItem(final StringMenuItem<T> menuItem) { if (menuItem == null) { log.fatal("Item may not be null"); throw new IllegalArgumentException("Item may not be null"); } if (!EventQueue.isDispatchThread()) { log.fatal("Must be called on the EventQueue dispatch thread"); throw new IllegalStateException( "Must be called on the EventQueue dispatch thread"); } if (!itemStringToJMenuItem.containsKey(menuItem)) { log.trace("Adding item to menu"); addItemInternal(menuItem); } }
@SuppressWarnings("unchecked") public <T extends Comparable<T> & Serializable> StringMenu<T> getMenu(Class<T> clazz, String preferenceKey, String menuPresentationName, Icon menuIcon) { if (!menuCache.containsKey(preferenceKey)) { log.trace("Creating new string menu"); StringMenu menu = new StringMenuImpl(preferences, workbench, messages, preferenceKey, menuPresentationName, menuIcon); menu = WorkerFactory.decorate(menu, StringMenu.class); menuCache.put(preferenceKey, menu); } return (StringMenu<T>)menuCache.get(preferenceKey); }
private synchronized void addItemInternal( final StringMenuItem<T> stringMenuItem) { log.trace("Creating JMenu item"); JMenuItem jMenuItem = createJMenuItem(stringMenuItem); log.trace("Adding cache entry for item: " + stringMenuItem); itemStringToJMenuItem.put(stringMenuItem, jMenuItem); log.trace("Adding key to key list"); itemStrings.add(stringMenuItem); log.trace("Addin JMenuItem: " + jMenuItem + " to JMenu"); this.menu.insert(jMenuItem, 2); }
/** * @see nu.zoom.swing.desktop.component.stringmenu.StringMenu#removeItem(java.lang.String) */ public synchronized void removeItem(StringMenuItem<T> menuItem) { if (!EventQueue.isDispatchThread()) { log.fatal("Must be called on the EventQueue dispatch thread"); throw new IllegalStateException( "Must be called on the EventQueue dispatch thread"); } if (menuItem != null) { log.trace("Looking for menu item to remove"); final JMenuItem cachedJMenuItem = itemStringToJMenuItem .get(menuItem); log.trace("Cache returned item: " + cachedJMenuItem); if (cachedJMenuItem != null) { log.trace("Removing item from JMenu"); getJMenu().remove(cachedJMenuItem); log.trace("Removing item from key list"); itemStrings.remove(menuItem); log.trace("Removing item from cache"); itemStringToJMenuItem.remove(menuItem); log.trace("Removing ALL action listeners from JMenuItem"); ActionListener[] listeners = cachedJMenuItem .getActionListeners(); for (ActionListener listener : listeners) { cachedJMenuItem.removeActionListener(listener); } } } }
/** * @see nu.zoom.swing.desktop.component.stringmenu.StringMenu#addItem(java.lang.String) */ public synchronized void addItem(final StringMenuItem<T> menuItem) { if (menuItem == null) { log.fatal("Item may not be null"); throw new IllegalArgumentException("Item may not be null"); } if (!EventQueue.isDispatchThread()) { log.fatal("Must be called on the EventQueue dispatch thread"); throw new IllegalStateException( "Must be called on the EventQueue dispatch thread"); } if (!itemStringToJMenuItem.containsKey(menuItem)) { log.trace("Adding item to menu"); addItemInternal(menuItem); } }
@Override @SuppressWarnings("unchecked") public <T extends Comparable<T> & Serializable> StringMenu<T> getMenu( final Class<T> clazz, final String preferenceKey) { if (!menuCache.containsKey(preferenceKey)) { log.trace("Creating new string menu"); StringMenu<T> menu = new StringMenuImpl<T>(preferences, workbench, preferenceKey); menu = WorkerFactory.decorate(menu, StringMenu.class); menuCache.put(preferenceKey, menu); } return (StringMenu<T>) menuCache.get(preferenceKey); }
/** * @see nu.zoom.swing.desktop.component.stringmenu.StringMenu#getJMenu() */ public synchronized JMenu getJMenu() { if (menu == null) { restoreMenu(); if (menu == null) { log.trace("Menu not restored from preferences, creating new."); createJMenu(); } } return menu; }
@SuppressWarnings("unchecked") private synchronized void restoreMenu() { try { log.trace("Trying to deserialize key list from preferences"); byte[] data = preferences.getBytes(preferencesKey); if (data != null) { ObjectInputStream ins = new ObjectInputStream( new ByteArrayInputStream(data)); // Generates unchecked warnings but we know ArrayList<StringMenuItem<T>> restoredKeys = (ArrayList<StringMenuItem<T>>) ins .readObject(); for (final StringMenuItem<T> item : restoredKeys) { addItemInternal(item); } } } catch (InvalidDataTypeException e) { log.warn(e); } catch (BackendException e) { log.warn(e); } catch (IOException e) { log.warn(e); } catch (ClassNotFoundException e) { log.warn(e); } }
@SuppressWarnings("unchecked") public void clear() { ArrayList<StringMenuItem<T>> keyClone = (ArrayList<StringMenuItem<T>>) itemStrings .clone(); for (StringMenuItem<T> item : keyClone) { removeItem(item); } }