001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.undation, Inc., 59 Temple Place - Suite 330, Boston, MA
018: // 02111-1307, USA.
019: package org.columba.mail.gui.charset;
020:
021: import java.awt.event.ActionEvent;
022: import java.awt.event.ActionListener;
023: import java.nio.charset.Charset;
024: import java.nio.charset.UnsupportedCharsetException;
025: import java.util.Hashtable;
026:
027: import javax.swing.ButtonGroup;
028: import javax.swing.JMenuItem;
029: import javax.swing.JRadioButtonMenuItem;
030:
031: import org.columba.api.gui.frame.IFrameMediator;
032: import org.columba.core.charset.CharsetEvent;
033: import org.columba.core.charset.CharsetListener;
034: import org.columba.core.charset.CharsetOwnerInterface;
035: import org.columba.core.gui.base.CMenu;
036: import org.columba.core.gui.menu.IMenu;
037: import org.columba.core.logging.Logging;
038: import org.columba.core.resourceloader.ImageLoader;
039: import org.columba.mail.util.MailResourceLoader;
040:
041: /**
042: * Creates a menu containing submenus with different charsets from which the
043: * user can choose.
044: */
045:
046: public class CharacterEncodingSubMenu extends IMenu implements
047: ActionListener, CharsetListener {
048:
049: // String definitions for the charsetnames
050: // NOTE: these are also used to look up the
051: // menuentries from the resourceloader
052: private static final String[] CHARSET_STRINGS = {
053:
054: // Global # 1
055: "UTF-8",
056: "UTF-16",
057: "US-ASCII",
058:
059: // West Europe # 4
060: "windows-1252", "ISO-8859-1", "ISO-8859-15",
061: "ISO-8859-7",
062: "windows-1253",
063: "ISO-8859-3",
064:
065: // East Europe # 10
066: "ISO-8859-4", "ISO-8859-13", "windows-1257", "ISO-8859-2",
067: "ISO-8859-5", "KOI8-R",
068: "windows-1251",
069:
070: // East Asian # 17
071: "GB2312", "GBK", "GB18030", "Big5", "Big5-HKSCS", "EUC-TW",
072: "EUC-JP", "Shift_JIS", "ISO-2022-JP", "MS932", "EUC-KR",
073: "JOHAB", "ISO-2022-KR",
074:
075: // West Asian # 30
076: "TIS620", "ISO-8859-9", "windows-1254", "windows-1258" };
077:
078: private static final Charset[] CHARSETS = createCharsetArray();
079:
080: private static final String[] groups = { "global", "westeurope",
081: "easteurope", "eastasian", "seswasian" };
082:
083: private static final int[] groupOffset = { 0, 3, 9, 16, 29, 33 };
084:
085: private ButtonGroup group;
086:
087: private Hashtable hashtable;
088:
089: /**
090: * The menu item showing the currently selected charset.
091: */
092: // protected CharsetMenuItem selectedMenuItem = new CharsetMenuItem(null);
093: /**
094: * Creates a new menu for choosing charsets. The passed IFrameMediator
095: * instance needs to implement CharsetOwnerInterface.
096: */
097: public CharacterEncodingSubMenu(IFrameMediator mediator) {
098: super (mediator, MailResourceLoader.getString("menu",
099: "mainframe", "menu_view_charset"), "menu_view_charset");
100:
101: //setIcon(ImageLoader.getImageIcon("stock_font_16.png"));
102:
103: group = new ButtonGroup();
104:
105: hashtable = new Hashtable();
106:
107: add(createMenuItem(null));
108:
109: // Automatic Generation of Groups
110: CMenu subsubMenu;
111:
112: for (int i = 0; i < groups.length; i++) {
113: subsubMenu = new CMenu(MailResourceLoader.getString("menu",
114: "mainframe", "menu_view_charset_" + groups[i]),
115: "menu_view_charset_" + groups[i]);
116: add(subsubMenu);
117:
118: for (int j = groupOffset[i]; j < groupOffset[i + 1]; j++) {
119: if (CHARSETS[j] != null) {
120: JMenuItem item = createMenuItem(CHARSETS[j]);
121: subsubMenu.add(item);
122: }
123: }
124: }
125:
126: ((CharsetOwnerInterface) controller).addCharsetListener(this );
127:
128: // simulate charset changed to initialize selectedMenuItem
129: charsetChanged(new CharsetEvent(this ,
130: ((CharsetOwnerInterface) controller).getCharset()));
131:
132: }
133:
134: private static Charset[] createCharsetArray() {
135: Charset[] result = new Charset[CHARSET_STRINGS.length];
136: for (int i = 0; i < CHARSET_STRINGS.length; i++) {
137: try {
138: result[i] = Charset.forName(CHARSET_STRINGS[i]);
139: } catch (UnsupportedCharsetException e) {
140: if (Logging.DEBUG)
141: e.printStackTrace();
142:
143: result[i] = null;
144: }
145: }
146: return result;
147: }
148:
149: /**
150: * Creates a menu item and registers action and mouse listeners.
151: */
152: protected CharsetMenuItem createMenuItem(Charset charset) {
153: CharsetMenuItem menuItem = new CharsetMenuItem(charset);
154: group.add(menuItem);
155: menuItem.addActionListener(this );
156: if (charset != null)
157: hashtable.put(charset, menuItem);
158:
159: return menuItem;
160: }
161:
162: /**
163: * Called when a charset is chosen from the menu.
164: */
165: public void actionPerformed(ActionEvent e) {
166: ((CharsetOwnerInterface) controller)
167: .setCharset(((CharsetMenuItem) e.getSource())
168: .getCharset());
169: }
170:
171: /**
172: * Updates the selectedMenuItem according to the chosen charset.
173: */
174: public void charsetChanged(CharsetEvent e) {
175: // selectedMenuItem.setCharset(e.getCharset());
176: if (e.getCharset() == null) {
177: JRadioButtonMenuItem item = (JRadioButtonMenuItem) getMenuComponent(0);
178: item.setSelected(true);
179:
180: } else {
181: if (hashtable.containsKey(e.getCharset())) {
182: CharsetMenuItem menuItem = (CharsetMenuItem) hashtable
183: .get(e.getCharset());
184: menuItem.setSelected(true);
185: }
186: }
187: }
188: }
|