[Lutinutil-commits] r926 - trunk/commandline/commandline-ui-action/src/main/java/org/codelutin/option/ui/actions
Author: tchemit Date: 2008-07-27 10:02:12 +0000 (Sun, 27 Jul 2008) New Revision: 926 Added: trunk/commandline/commandline-ui-action/src/main/java/org/codelutin/option/ui/actions/ActionHelper.java Log: introduce ActionHelper class to manage action enable state + logic of refreshing i18n actions from an ui Added: trunk/commandline/commandline-ui-action/src/main/java/org/codelutin/option/ui/actions/ActionHelper.java =================================================================== --- trunk/commandline/commandline-ui-action/src/main/java/org/codelutin/option/ui/actions/ActionHelper.java (rev 0) +++ trunk/commandline/commandline-ui-action/src/main/java/org/codelutin/option/ui/actions/ActionHelper.java 2008-07-27 10:02:12 UTC (rev 926) @@ -0,0 +1,132 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * 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 Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * # #% + */ +package org.codelutin.option.ui.actions; + +import jaxx.runtime.JAXXObject; +import static org.codelutin.i18n.I18n._; +import org.codelutin.jaxx.action.MyAbstractAction; +import org.codelutin.jaxx.action.factory.ActionFactory; +import org.codelutin.option.Context; + +import javax.swing.AbstractButton; +import javax.swing.JComboBox; +import java.util.Locale; +import java.util.regex.Matcher; + +/** + * Usefull methods for action on ui + * + * @author chemit + */ +public class ActionHelper { + + /** + * Disable the given buttons + * + * @param buttons the buttons to disable + */ + public static void DisableButton(AbstractButton... buttons) { + for (AbstractButton button : buttons) { + UpdateButton(button, false); + } + } + + /** + * Enable an action and add to it a libelle + * + * @param button the button to enable + * @param params extra params to compute action libelle + */ + public static void EnableButton(AbstractButton button, Object... params) { + UpdateButton(button, true, params); + } + + /** + * Update the enabled state of a button action. + * <p/> + * If action is to be enabled, compute also a libelle on it, otherwise remove libelle. + * + * @param button the button to treate + * @param enabled <code>true</code> if to enable, <code>false</code> otherwise + * @param params extra params to compute action libelle (only use when action is to be enabled) + */ + public static void UpdateButton(AbstractButton button, boolean enabled, Object... params) { + button.setEnabled(enabled); + if (!enabled) { + button.setToolTipText(null); + } else { + MyAbstractAction action = (MyAbstractAction) button.getAction(); + String t = org.codelutin.i18n.I18n._(action.getI18nToolTipText(), params); + button.setToolTipText(t); + } + } + + /** + * Update the enabled state of a combo box action. + * <p/> + * If action is to be enabled, compute also a libelle on it, otherwise remove libelle. + * + * @param comboBox the button to treate + * @param enabled <code>true</code> if to enable, <code>false</code> otherwise + * @param params extra params to compute action libelle (only use when action is to be enabled) + */ + public static void UpdateCombo(JComboBox comboBox, boolean enabled, Object... params) { + comboBox.setEnabled(enabled); + if (!enabled) { + comboBox.setToolTipText(null); + } else { + MyAbstractAction action = (MyAbstractAction) comboBox.getAction(); + if (action != null) { + String t = _(action.getI18nToolTipText(), params); + comboBox.setToolTipText(t); + } + } + } + + /** + * Update the i18n actions found in <code>factory</code> (diable the i18n action of the current locale found in + * context, and enabled all the others). + * + * @param context application context + * @param actionFactory factory of actions + * @param mainUI main ui + */ + public static void refreshI18nActions(Context context, ActionFactory actionFactory, JAXXObject mainUI) { + + Locale locale = (Locale) context.getMainConfig().getProperty("locale"); + String[] actions = actionFactory.getActionNames(); + for (String actionName : actions) { + Matcher matcher = ChangeLocaleAction.PATTERN_NAME.matcher(actionName); + if (!matcher.matches()) { + continue; + } + AbstractButton button = (AbstractButton) mainUI.getObjectById(actionName); + MyAbstractAction myAbstractAction = (MyAbstractAction) button.getAction(); + ChangeLocaleAction action; + if (myAbstractAction.hasDelegate()) { + action = (ChangeLocaleAction) myAbstractAction.getDelegate(); + } else { + action = (ChangeLocaleAction) myAbstractAction; + } + boolean enable = !action.equalsLocale(locale); + UpdateButton(button, enable, enable ? action.getLocale() : null); + button.setEnabled(enable); + } + } + + protected ActionHelper() { + // no instance + } +}
participants (1)
-
tchemit@users.labs.libre-entreprise.org