1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
/** Copyright 2003 by Jean-Hugues de Raigniac <jhraigniac@workingfrog.org>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
package org.workingfrog.i18n.swing;
import java.awt.Component;
import java.beans.PropertyChangeListener;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import org.workingfrog.i18n.util.LocaleEvent;
import org.workingfrog.i18n.util.LocaleListener;
import org.workingfrog.i18n.util.Translator;
/**
* i18n version of javax.swing.JMenu
*
* @author Jean-Hugues de Raigniac
*/
public class I18NJMenu extends JMenu implements LocaleListener {
/** bundle */
private String bundle = "menu";
/** i18n key */
private String i18nKey = "menu.error";
/**
* Override javax.swing.JMenu.JMenu (java.lang.String).
*
* @param i18nKey i18n bundle key
*/
public I18NJMenu (String i18nKey) {
super(i18nKey);
if (bundle != null && i18nKey != null) {
setText(Translator.checkValue(i18nKey, this));
}
Translator.checkKey(i18nKey, this);
this.i18nKey = i18nKey;
}
/**
* Creates a new menu item attached to the specified
* <code>Action</code> object and appends it to the end of this menu.
* As of JDK 1.3, this is no longer the preferred method for adding
* <code>Actions</code> to
* a container. Instead it is recommended to configure a control with
* an action using <code>setAction</code>,
* and then add that control directly
* to the <code>Container</code>.
*
* @param a the <code>Action</code> for the menu item to be added
* @see Action
* @return The new JMenuItem
*/
public JMenuItem add(Action a) {
JMenuItem mi = createActionComponent(a);
mi.setAction(a);
if (a instanceof LocaleListener) {
mi.setText(Translator.checkValue(((I18NAction) a).getKey(), this));
}
add(mi);
return mi;
}
/**
* Factory method which creates the <code>JMenuItem</code> for
* <code>Action</code>s added to the <code>JMenu</code>.
* As of JDK 1.3, this is no
* longer the preferred method. Instead it is recommended to configure
* a control with an action using <code>setAction</code>,
* and then adding that
* control directly to the <code>Container</code>.
*
* @param a the <code>Action</code> for the menu item to be added
* @return the new menu item
* @see Action
*/
protected JMenuItem createActionComponent(Action a) {
JMenuItem mi = (a instanceof I18NAction
|| a instanceof I18NActionWrapper)
? new I18NJMenuItem((a instanceof I18NAction)
? ((I18NAction) a).getKey()
: ((I18NActionWrapper) a).getKey())
: new I18NJMenuItem((String) a.getValue(Action.NAME),
(Icon) a.getValue(Action.SMALL_ICON)){
protected PropertyChangeListener
createActionPropertyChangeListener (Action a) {
PropertyChangeListener pcl = createActionChangeListener(this);
if (pcl == null) {
pcl = super.createActionPropertyChangeListener(a);
}
return pcl;
}
};
mi.setHorizontalTextPosition(JButton.RIGHT);
mi.setVerticalTextPosition(JButton.CENTER);
mi.setEnabled(a.isEnabled());
return mi;
}
/**
* Allow the use of a customized i18n resource bundle.
*
* @param bundle bundle binding
*/
public void setBundle (String bundle) {
this.bundle = bundle;
if (i18nKey != null) {
setText(Translator.checkValue(i18nKey, this));
}
}
/**
* Propagate the LocaleEvent to the components of this JMenu.
*
* @param event contains the new Locale
*/
public void localeChanged (LocaleEvent event) {
setText(Translator.checkValue(i18nKey, this));
int componentNumber = getMenuComponentCount();
//System.out.println("componentNumber (JMenu) = " + componentNumber);
for (int i = 0; i < componentNumber; i++) {
Component component = getMenuComponent(i);
if (component instanceof LocaleListener) {
((LocaleListener) component).localeChanged(event);
} else {
if (!(component instanceof JPopupMenu.Separator)) {
// Translator.log(Translator.I18N,
// "JMenu, N : " + component.toString());
}
}
}
}
}
|