[Japi-cvs] SF.net SVN: japi: [569] libs/swing-action/trunk/src/net/sf/japi/swing/ ActionFactory.jav
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2007-08-11 18:42:08
|
Revision: 569 http://japi.svn.sourceforge.net/japi/?rev=569&view=rev Author: christianhujer Date: 2007-08-11 11:42:06 -0700 (Sat, 11 Aug 2007) Log Message: ----------- Improved handling of null values in factory methods. Modified Paths: -------------- libs/swing-action/trunk/src/net/sf/japi/swing/ActionFactory.java Modified: libs/swing-action/trunk/src/net/sf/japi/swing/ActionFactory.java =================================================================== --- libs/swing-action/trunk/src/net/sf/japi/swing/ActionFactory.java 2007-08-11 17:36:04 UTC (rev 568) +++ libs/swing-action/trunk/src/net/sf/japi/swing/ActionFactory.java 2007-08-11 18:42:06 UTC (rev 569) @@ -426,7 +426,6 @@ */ @SuppressWarnings({"NestedAssignment"}) @NotNull public Action initAction(final boolean store, @NotNull final Action action, @NotNull final String key) throws NullPointerException { - if (key == null) { throw new NullPointerException("null key for Action initialization not allowed."); } action.putValue(ACTION_ID, key); String value; final String text = getString(key + ".text"); @@ -502,9 +501,6 @@ * @throws NullPointerException if <var>key</var> is <code>null</code> */ @Nullable public String getStringFromPrefs(@NotNull final String key) throws NullPointerException { - if (key == null) { - throw new NullPointerException("null key not allowed"); - } String value = null; for (final Preferences pref : prefs) { value = pref.get(key, value); @@ -608,7 +604,7 @@ } else { final Action action = getAction(key); if (action == null) { - throw new Error("No Action for key " + key); + throw new MissingResourceException(this + " has no definitin for action " + key, getClass().getName(), key); } if (action instanceof ToggleAction) { menu.add(((ToggleAction) action).createCheckBoxMenuItem()); @@ -668,15 +664,19 @@ } /** Method for creating a popup menu. - * @param store whether to store the initialized Actions in the ActionMap of this ActionFactory - * @param popupKey Action key of popup menu to create + * @param store Whether to store the initialized Actions in the ActionMap of this ActionFactory. + * @param popupKey Action key of popup menu to create. * @return JPopupMenu created for <var>popupKey</var> - * @throws NullPointerException if no menubar definition was found + * @throws MissingResourceException if no menubar definition was found. * @todo make error handling consistent (see createMenu and others) */ - @NotNull public JPopupMenu createPopupMenu(final boolean store, final String popupKey) throws NullPointerException { + @NotNull public JPopupMenu createPopupMenu(final boolean store, final String popupKey) throws MissingResourceException { final JPopupMenu menu = new JPopupMenu(); - for (final String key : getString(popupKey + ".menu").split("\\s+")) { + final String popupDefinition = getString(popupKey + ".menu"); + if (popupDefinition == null) { + throw new MissingResourceException(this + " has no definition for popup " + popupKey, getClass().getName(), popupKey + ".menu"); + } + for (final String key : popupDefinition.split("\\s+")) { if (key != null && key.length() == 0) { /* ignore this for empty menus */ } else if (key == null || "-".equals(key) || "|".equals(key)) { @@ -686,7 +686,7 @@ } else { final Action action = getAction(key); if (action == null) { - throw new Error("No Action for key " + key); + throw new MissingResourceException(this + " has no definitin for action " + key, getClass().getName(), key); } if (action instanceof ToggleAction) { menu.add(((ToggleAction) action).createCheckBoxMenuItem()); @@ -701,14 +701,18 @@ /** Method for creating a Menu. * This method assumes that the underlying properties contain an entry like <code>key + ".menu"</code> which lists the menu element's keys. * Submenus are build recursively. - * @param store whether to store the initialized Action in the ActionMap of this ActionFactory - * @param menuKey action key for menu - * @return menu created from the menu definition found - * @throws Error in case a menu definition for <var>menuKey</var> wasn't found + * @param store Whether to store the initialized Action in the ActionMap of this ActionFactory. + * @param menuKey Action key for menu. + * @return menu created from the menu definition found. + * @throws MissingResourceException In case a menu definition for <var>menuKey</var> wasn't found */ - public JMenu createMenu(final boolean store, final String menuKey) throws Error { + public JMenu createMenu(final boolean store, final String menuKey) throws MissingResourceException { final JMenu menu = new JMenu(createAction(store, menuKey)); - for (final String key : getString(menuKey + ".menu").split("\\s+")) { + final String menuDefinition = getString(menuKey + ".menu"); + if (menuDefinition == null) { + throw new MissingResourceException(this + " has no definition for menu " + menuKey, getClass().getName(), menuKey + ".menu"); + } + for (final String key : menuDefinition.split("\\s+")) { if (key != null && key.length() == 0) { /* ignore this for empty menus */ } else if (key == null || "-".equals(key) || "|".equals(key)) { @@ -718,7 +722,7 @@ } else { final Action action = getAction(key); if (action == null) { - throw new Error("No Action for key " + key); + throw new MissingResourceException(this + " has no definitin for action " + key, getClass().getName(), key); } if (action instanceof ToggleAction) { menu.add(((ToggleAction) action).createCheckBoxMenuItem()); @@ -735,14 +739,15 @@ * @param barKey Action key of menu to create * @param target Target object * @return JMenuBar created for <var>barKey</var> + * @throws MissingResourceException In case a menu or menubar definition was not found. */ - public JMenuBar createMenuBar(final boolean store, final String barKey, final Object target) { + public JMenuBar createMenuBar(final boolean store, final String barKey, final Object target) throws MissingResourceException { final JMenuBar menuBar = new JMenuBar(); - final String menuBarSpec = getString(barKey + ".menubar"); - if (menuBarSpec == null) { - throw new RuntimeException("Missing Resource for " + barKey + ".menubar"); + final String menuBarDefinition = getString(barKey + ".menubar"); + if (menuBarDefinition == null) { + throw new MissingResourceException(this + " has no definitin for menubar " + barKey, getClass().getName(), barKey); } - for (final String key : menuBarSpec.split("\\s+")) { + for (final String key : menuBarDefinition.split("\\s+")) { menuBar.add(createMenu(store, key, target)); } return menuBar; @@ -753,10 +758,15 @@ * @param popupKey Action key of popup menu to create * @param target Target object * @return JPopupMenu created for <var>barKey</var> + * @throws MissingResourceException In case a menu definition was not found. */ - public JPopupMenu createPopupMenu(final boolean store, final String popupKey, final Object target) { + public JPopupMenu createPopupMenu(final boolean store, final String popupKey, final Object target) throws MissingResourceException { final JPopupMenu menu = new JPopupMenu(); - for (final String key : getString(popupKey + ".menu").split("\\s+")) { + final String popupDefinition = getString(popupKey + ".menu"); + if (popupDefinition == null) { + throw new MissingResourceException(this + " has no definition for popup " + popupKey, getClass().getName(), popupKey + ".menu"); + } + for (final String key : popupDefinition.split("\\s+")) { if (key != null && key.length() == 0) { /* ignore this for empty menus */ } else if (key == null || "-".equals(key) || "|".equals(key)) { @@ -772,7 +782,7 @@ action = createAction(store, key, target); } if (action == null) { - throw new Error("No Action for key " + key); + throw new MissingResourceException(this + " has no definitin for action " + key, getClass().getName(), key); } if (action instanceof ToggleAction) { menu.add(((ToggleAction) action).createCheckBoxMenuItem()); @@ -795,7 +805,11 @@ */ public JMenu createMenu(final boolean store, final String menuKey, final Object target) throws Error { final JMenu menu = new JMenu(createAction(store, menuKey)); - for (final String key : getString(menuKey + ".menu").split("\\s+")) { + final String menuDefinition = getString(menuKey + ".menu"); + if (menuDefinition == null) { + throw new MissingResourceException(this + " has no definition for menu " + menuKey, getClass().getName(), menuKey + ".menu"); + } + for (final String key : menuDefinition.split("\\s+")) { if (key != null && key.length() == 0) { /* ignore this for empty menus */ } else if (key == null || "-".equals(key) || "|".equals(key)) { @@ -811,7 +825,7 @@ action = createAction(store, key, target); } if (action == null) { - throw new Error("No Action for key " + key); + throw new MissingResourceException(this + " has no definitin for action " + key, getClass().getName(), key); } if (action instanceof ToggleAction) { menu.add(((ToggleAction) action).createCheckBoxMenuItem()); @@ -878,7 +892,7 @@ } else { final Action action = getAction(key); if (action == null) { - throw new Error("No Action for key " + key); + throw new MissingResourceException(this + " has no definitin for action " + key, getClass().getName(), key); } toolBar.add(action); } @@ -889,9 +903,14 @@ /** Method for creating a toolbar. * @param barKey Action keys of toolbar to create * @return JToolBar created for <var>barKey</var> + * @throws MissingResourceException In case there is no definition for the requested toolbar. */ - public JToolBar createToolBar(final String barKey) { - return createToolBar(getString(barKey + ".toolbar").split("\\s+")); + public JToolBar createToolBar(final String barKey) throws MissingResourceException { + final String toolbarDefinition = getString(barKey + ".toolbar"); + if (toolbarDefinition == null) { + throw new MissingResourceException(this + " has no definition for toolbar " + barKey, getClass().getName(), barKey); + } + return createToolBar(toolbarDefinition.split("\\s+")); } /** Method for creating a toolbar. @@ -919,9 +938,14 @@ * @param object Instance to invoke method on if the Action was activated ({@link Action#actionPerformed(ActionEvent)}) * @param barKey Action keys of toolbar to create * @return JToolBar created for <var>barKey</var> + * @throws MissingResourceException In case there is no definition for the requested toolbar. */ - public JToolBar createToolBar(final Object object, final String barKey) { - return createToolBar(object, getString(barKey + ".toolbar").split("\\s+")); + public JToolBar createToolBar(final Object object, final String barKey) throws MissingResourceException { + final String toolbarDefinition = getString(barKey + ".toolbar"); + if (toolbarDefinition == null) { + throw new MissingResourceException(this + " has no definition for toolbar " + barKey, getClass().getName(), barKey); + } + return createToolBar(object, toolbarDefinition.split("\\s+")); } /** Method to find the JMenuItem for a specific Action key. @@ -1116,6 +1140,7 @@ * @note the label text will be the key if no String for the key was found */ public JLabel createLabel(final String key, final Object... args) { + //noinspection RedundantCast return createLabel((Component) null, key, args); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |