Author: tchemit Date: 2013-05-08 12:52:36 +0200 (Wed, 08 May 2013) New Revision: 2665 Url: http://nuiton.org/projects/jaxx/repository/revisions/2665 Log: fixes #2688: Add a JaxxRuntimeException to catch some special errors Added: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/JAXXRuntimeException.java Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/BeanComboBoxHandler.java trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/BeanDoubleListHandler.java trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/BeanFilterableComboBoxHandler.java trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/BeanUIUtil.java Added: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/JAXXRuntimeException.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/JAXXRuntimeException.java (rev 0) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/JAXXRuntimeException.java 2013-05-08 10:52:36 UTC (rev 2665) @@ -0,0 +1,54 @@ +package jaxx.runtime.swing; + +/* + * #%L + * JAXX :: Widgets + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2008 - 2013 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +/** + * A special runtime exception to catch special errors fro + * + * @author tchemit <chemit@codelutin.com> + * @since 2.5.19 + */ +public class JAXXRuntimeException extends RuntimeException { + private static final long serialVersionUID = -2963740979386945340L; + + public JAXXRuntimeException() { + } + + public JAXXRuntimeException(String message) { + super(message); + } + + public JAXXRuntimeException(String message, Throwable cause) { + super(message, cause); + } + + public JAXXRuntimeException(Throwable cause) { + super(cause); + } + + public JAXXRuntimeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} Property changes on: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/JAXXRuntimeException.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/BeanComboBoxHandler.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/BeanComboBoxHandler.java 2013-04-28 21:13:44 UTC (rev 2664) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/BeanComboBoxHandler.java 2013-05-08 10:52:36 UTC (rev 2665) @@ -39,6 +39,8 @@ import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JPopupMenu; +import javax.swing.event.PopupMenuEvent; +import javax.swing.event.PopupMenuListener; import javax.swing.text.Document; import javax.swing.text.JTextComponent; import java.awt.event.FocusEvent; @@ -47,8 +49,6 @@ import java.beans.PropertyChangeListener; import java.lang.reflect.Method; import java.util.List; -import javax.swing.event.PopupMenuEvent; -import javax.swing.event.PopupMenuListener; /** * Le handler d'un {@link BeanComboBox}. @@ -140,9 +140,9 @@ combobox.addPopupMenuListener(new PopupMenuListener() { private O selectedItem; - + boolean canceled = false; - + public void popupMenuWillBecomeVisible(PopupMenuEvent e) { selectedItem = (O) combobox.getSelectedItem(); } @@ -151,10 +151,10 @@ O newSelectedItem = (O) combobox.getSelectedItem(); if (canceled) { ui.setSelectedItem(null); - - } else if ((newSelectedItem == null ^ selectedItem == null) - || (newSelectedItem != null - && !selectedItem.equals(newSelectedItem))) { + + } else if ((newSelectedItem == null ^ selectedItem == null) + || (newSelectedItem != null + && !selectedItem.equals(newSelectedItem))) { ui.setSelectedItem(newSelectedItem); } selectedItem = null; @@ -185,7 +185,7 @@ this.decorator); ui.autoComplete = true; - + ui.addPropertyChangeListener(this); // set datas @@ -409,14 +409,7 @@ log.debug(ui.getProperty() + " on " + getBeanType() + " :: " + oldValue + " to " + newValue); } - try { - Method mut = getMutator(); - if (mut != null) { - mut.invoke(ui.getBean(), newValue); - } - } catch (Exception e) { - throw new RuntimeException(e); - } + BeanUIUtil.invokeMethod(getMutator(), ui.getBean(), (O) newValue); } /** @return le document de l'éditeur avant complétion. */ Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/BeanDoubleListHandler.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/BeanDoubleListHandler.java 2013-04-28 21:13:44 UTC (rev 2664) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/BeanDoubleListHandler.java 2013-05-08 10:52:36 UTC (rev 2665) @@ -25,22 +25,6 @@ */ import com.google.common.base.Predicate; -import java.awt.event.MouseEvent; -import java.beans.PropertyChangeEvent; -import java.beans.PropertyChangeListener; -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.List; -import javax.swing.JComponent; -import javax.swing.JList; -import javax.swing.JPopupMenu; -import javax.swing.ListSelectionModel; -import javax.swing.event.DocumentEvent; -import javax.swing.event.DocumentListener; -import javax.swing.event.ListDataEvent; -import javax.swing.event.ListDataListener; -import javax.swing.event.ListSelectionEvent; -import javax.swing.event.ListSelectionListener; import jaxx.runtime.SwingUtil; import jaxx.runtime.swing.JAXXButtonGroup; import jaxx.runtime.swing.model.JaxxDefaultListModel; @@ -53,6 +37,23 @@ import org.nuiton.util.decorator.JXPathDecorator; import org.nuiton.util.decorator.MultiJXPathDecorator; +import javax.swing.JComponent; +import javax.swing.JList; +import javax.swing.JPopupMenu; +import javax.swing.ListSelectionModel; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; +import javax.swing.event.ListDataEvent; +import javax.swing.event.ListDataListener; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; +import java.awt.event.MouseEvent; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.List; + /** * The handler of a {@link BeanDoubleList}. * <p/> @@ -267,7 +268,7 @@ // change decorator context decorator.setContextIndex(index); - List<O> data = (List<O>) Arrays.asList(((JaxxDefaultListModel<O>)ui.getModel().getUniverseModel()).toArray()); + List<O> data = (List<O>) Arrays.asList(((JaxxDefaultListModel<O>) ui.getModel().getUniverseModel()).toArray()); try { // Sort data with the decorator jxpath tokens. DecoratorUtil.sort(decorator, @@ -335,12 +336,10 @@ } protected void fireSelectionUpdate() { - if (ui.getBean() != null && getMutator() != null) { - try { - getMutator().invoke(ui.getBean(), ui.getModel().getSelected()); - } catch (Exception e) { - throw new RuntimeException(e); - } + if (ui.getBean() != null) { + BeanUIUtil.invokeMethod(getMutator(), + ui.getBean(), + ui.getModel().getSelected()); } } Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/BeanFilterableComboBoxHandler.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/BeanFilterableComboBoxHandler.java 2013-04-28 21:13:44 UTC (rev 2664) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/BeanFilterableComboBoxHandler.java 2013-05-08 10:52:36 UTC (rev 2665) @@ -25,9 +25,10 @@ package jaxx.runtime.swing.editor.bean; import com.google.common.base.Predicate; -import java.awt.event.ActionListener; +import jaxx.runtime.JAXXUtil; import jaxx.runtime.SwingUtil; import jaxx.runtime.swing.JAXXButtonGroup; +import jaxx.runtime.swing.model.JaxxFilterableComboBoxModel; import jaxx.runtime.swing.renderer.DecoratorListCellRenderer; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.logging.Log; @@ -36,10 +37,14 @@ import org.nuiton.util.decorator.JXPathDecorator; import org.nuiton.util.decorator.MultiJXPathDecorator; +import javax.swing.ComboBoxEditor; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JPopupMenu; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; import javax.swing.text.JTextComponent; +import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.event.ItemEvent; @@ -52,12 +57,6 @@ import java.beans.PropertyChangeListener; import java.lang.reflect.Method; import java.util.List; -import javax.swing.ComboBoxEditor; -import javax.swing.event.DocumentEvent; -import javax.swing.event.DocumentListener; -import jaxx.runtime.JAXXUtil; -import jaxx.runtime.swing.model.JaxxFilterableComboBoxModel; -import jaxx.runtime.swing.model.JaxxFilterableListModel; /** * Le handler d'un {@link BeanFilterableComboBox}. @@ -67,25 +66,22 @@ * * @param <O> le type des objet contenus dans le modèle du composant. * @author kmorin <kmorin@codelutin.com> - * @since 2.5.12 * @see BeanFilterableComboBox + * @since 2.5.12 */ public class BeanFilterableComboBoxHandler<O> implements PropertyChangeListener { public static final Log log = LogFactory.getLog(BeanFilterableComboBoxHandler.class); - /** - * ui if the handler - */ + + /** ui if the handler */ protected BeanFilterableComboBox<O> ui; - /** - * the mutator method on the property of boxed bean in the ui - */ + + /** the mutator method on the property of boxed bean in the ui */ protected Method mutator; - /** - * the decorator of data - */ + + /** the decorator of data */ protected MultiJXPathDecorator<O> decorator; - + protected boolean init; public BeanFilterableComboBoxHandler(BeanFilterableComboBox<O> ui) { @@ -142,7 +138,7 @@ } }; - + private final BeanUIUtil.PopupHandler popupHandler = new BeanUIUtil.PopupHandler() { @Override public JPopupMenu getPopup() { @@ -159,7 +155,7 @@ * Initialise le handler de l'ui * * @param decorator le decorateur a utiliser - * @param data la liste des données a gérer + * @param data la liste des données a gérer */ public void init(JXPathDecorator<O> decorator, List<O> data) { @@ -197,7 +193,7 @@ combobox.showPopup(); } } - + @Override public void keyReleased(KeyEvent e) { // if the typed text does not match the selected item, @@ -229,29 +225,29 @@ if (log.isDebugEnabled()) { log.debug("itemStateChanged selected " + item + " - " + (item != null ? item.getClass() : null)); } - ((JTextComponent) combobox.getEditor().getEditorComponent()).setForeground(null); + combobox.getEditor().getEditorComponent().setForeground(null); ui.setSelectedItem(item); - + } else { if (log.isDebugEnabled()) { log.debug("itemStateChanged deselected " + item + " - " + (item != null ? item.getClass() : null)); } - ((JTextComponent) combobox.getEditor().getEditorComponent()).setForeground(ui.getInvalidComboEditorTextColor()); + combobox.getEditor().getEditorComponent().setForeground(ui.getInvalidComboEditorTextColor()); } } }); // build popup popupHandler.preparePopup(ui.getSelectedToolTipText(), - ui.getNotSelectedToolTipText(), - ui.getI18nPrefix(), - ui.getPopupTitleText(), - indexes, - ui.getPopupSeparator(), - ui.getPopupLabel(), - ui.getSortUp(), - ui.getSortDown(), - this.decorator); + ui.getNotSelectedToolTipText(), + ui.getI18nPrefix(), + ui.getPopupTitleText(), + indexes, + ui.getPopupSeparator(), + ui.getPopupLabel(), + ui.getSortUp(), + ui.getSortDown(), + this.decorator); setFilterable(false, ui.getFilterable()); @@ -264,16 +260,14 @@ indexes.setSelectedButton(ui.getIndex()); } - /** - * Toggle the popup visible state. - */ + /** Toggle the popup visible state. */ public void togglePopup() { popupHandler.togglePopup(); } /** * @return {@code true} if there is no data in comboBox, {@code false} - * otherwise. + * otherwise. */ public boolean isEmpty() { boolean result = CollectionUtils.isEmpty(ui.getData()); @@ -329,9 +323,7 @@ } } - /** - * Sort data of the model. - */ + /** Sort data of the model. */ public void sortData() { // just update UI should do the math of this @@ -433,9 +425,9 @@ try { // Sort data with the decorator jxpath tokens. DecoratorUtil.sort(decorator, - data, - index, - reversesort); + data, + index, + reversesort); } catch (Exception eee) { log.warn(eee.getMessage(), eee); @@ -455,16 +447,11 @@ if (ui.selectedItem == null) { return; } - + ui.selectedItem = null; - try { - Method mut = getMutator(); - if (mut != null) { - mut.invoke(ui.getBean(), (O)null); - } - } catch (Exception e) { - throw new RuntimeException(e); - } + BeanUIUtil.invokeMethod(getMutator(), + ui.getBean(), + (O) null); } /** @@ -477,35 +464,28 @@ if (oldValue == null && newValue == null) { return; } - + if (!getBeanType().isInstance(newValue)) { newValue = null; } JTextComponent editorComponent = (JTextComponent) ui.getCombobox().getEditor().getEditorComponent(); editorComponent.setText(""); - + if (log.isDebugEnabled()) { log.debug(ui.getProperty() + " on " + getBeanType() + " :: " + oldValue + " to " + newValue); } - try { - Method mut = getMutator(); - if (mut != null) { - mut.invoke(ui.getBean(), newValue); - } - } catch (Exception e) { - throw new RuntimeException(e); - } + BeanUIUtil.invokeMethod(getMutator(), + ui.getBean(), + newValue); } public MultiJXPathDecorator<O> getDecorator() { return decorator; } - /** - * @return get the type of objects contained in the comboBox model. - */ + /** @return get the type of objects contained in the comboBox model. */ public Class<O> getBeanType() { Class<O> result = ui.getBeanType(); if (result == null) { @@ -514,9 +494,7 @@ return result; } - /** - * @return le mutateur a utiliser pour modifier le bean associé. - */ + /** @return le mutateur a utiliser pour modifier le bean associé. */ protected Method getMutator() { if (mutator == null && ui.getBean() != null && ui.getProperty() != null) { mutator = BeanUIUtil.getMutator(ui.getBean(), ui.getProperty()); @@ -536,7 +514,7 @@ if (BeanFilterableComboBox.PROPERTY_FILTERABLE.equals(propertyName)) { setFilterable((Boolean) evt.getOldValue(), - (Boolean) evt.getNewValue()); + (Boolean) evt.getNewValue()); return; } @@ -544,7 +522,7 @@ // decorator index has changed, force reload of data in ui setIndex((Integer) evt.getOldValue(), - (Integer) evt.getNewValue()); + (Integer) evt.getNewValue()); return; } @@ -552,7 +530,7 @@ // sort order has changed, force reload of data in ui setSortOrder((Boolean) evt.getOldValue(), - (Boolean) evt.getNewValue()); + (Boolean) evt.getNewValue()); return; } @@ -569,15 +547,14 @@ protected void fireEmpty(boolean wasEmpty) { ui.firePropertyChange(BeanComboBox.PROPERTY_EMPTY, wasEmpty, - isEmpty()); + isEmpty()); } - /** - * Editor for the Combobox of the UI - uses the decorator - */ + /** Editor for the Combobox of the UI - uses the decorator */ class JAXXFilterableComboBoxEditor implements ComboBoxEditor { Object oldItem; + ComboBoxEditor wrapped; public JAXXFilterableComboBoxEditor(ComboBoxEditor wrapped) { @@ -617,10 +594,10 @@ log.debug("getItem " + newValue + " - " + (newValue != null ? newValue.getClass() : null)); } - if (oldItem != null && getBeanType().isInstance(oldItem)) { + if (oldItem != null && getBeanType().isInstance(oldItem)) { // The original value is not a string. Should return the value in it's // original type. - if (newValue.equals(decorator.toString(oldItem))) { + if (newValue.equals(decorator.toString(oldItem))) { newValue = oldItem; } } @@ -644,7 +621,7 @@ public void removeActionListener(ActionListener l) { wrapped.removeActionListener(l); } - }; + } public void addFilter(Predicate<O> filter) { ((JaxxFilterableComboBoxModel) ui.getCombobox().getModel()).addFilter(filter); Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/BeanUIUtil.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/BeanUIUtil.java 2013-04-28 21:13:44 UTC (rev 2664) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/BeanUIUtil.java 2013-05-08 10:52:36 UTC (rev 2665) @@ -24,15 +24,10 @@ */ package jaxx.runtime.swing.editor.bean; -import org.apache.commons.beanutils.PropertyUtils; -import java.beans.PropertyDescriptor; -import java.lang.reflect.Method; -import org.nuiton.util.decorator.Decorator; -import org.nuiton.util.decorator.DecoratorUtil; -import org.nuiton.util.decorator.JXPathDecorator; -import org.nuiton.util.decorator.MultiJXPathDecorator; import jaxx.runtime.swing.JAXXButtonGroup; +import jaxx.runtime.swing.JAXXRuntimeException; import org.apache.commons.beanutils.MethodUtils; +import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.jdesktop.swingx.autocomplete.AutoCompleteComboBoxEditor; @@ -40,6 +35,10 @@ import org.jdesktop.swingx.autocomplete.AutoCompleteDocument; import org.jdesktop.swingx.autocomplete.ComboBoxAdaptor; import org.jdesktop.swingx.autocomplete.ObjectToStringConverter; +import org.nuiton.util.decorator.Decorator; +import org.nuiton.util.decorator.DecoratorUtil; +import org.nuiton.util.decorator.JXPathDecorator; +import org.nuiton.util.decorator.MultiJXPathDecorator; import javax.swing.AbstractButton; import javax.swing.ActionMap; @@ -62,6 +61,9 @@ import java.awt.event.KeyListener; import java.beans.Introspector; import java.beans.PropertyChangeListener; +import java.beans.PropertyDescriptor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.Date; import java.util.List; @@ -86,6 +88,19 @@ public static final Object[] EMPTY_CLASS_ARRAY = new Object[0]; + public static void invokeMethod(Method mut, Object source, Object... params) { + + if (mut != null) { + try { + mut.invoke(source, params); + } catch (IllegalAccessException e) { + throw new JAXXRuntimeException(e); + } catch (InvocationTargetException e) { + throw new JAXXRuntimeException(e.getCause()); + } + } + } + /** * Encapsule un {@link Decorator} dans un {@link ObjectToStringConverter}. * @@ -255,7 +270,8 @@ ); return (MultiJXPathDecorator<O>) clone; } catch (Exception e) { - throw new IllegalStateException("Could not clone decorator "+decorator, e); } + throw new IllegalStateException("Could not clone decorator " + decorator, e); + } } if (decorator instanceof MultiJXPathDecorator<?>) { @@ -276,7 +292,7 @@ separatorReplacement ); } - + public static Method getMutator(Object bean, String property) { Method mutator = null; if (bean == null) { @@ -294,7 +310,7 @@ } catch (Exception e) { throw new RuntimeException(e); } - + return mutator; } @@ -378,7 +394,7 @@ //Container container = ui.getIndexesContainer(); int nbContext = decorator.getNbContext(); - if (nbContext>1) { + if (nbContext > 1) { for (int i = 0; i < nbContext; i++) { String property = i18nPrefix + decorator.getProperty(i); String propertyI18n = _(property);