KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > charset > CharacterEncodingSubMenu


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.undation, Inc., 59 Temple Place - Suite 330, Boston, MA
18
// 02111-1307, USA.
19
package org.columba.mail.gui.charset;
20
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23 import java.nio.charset.Charset JavaDoc;
24 import java.nio.charset.UnsupportedCharsetException JavaDoc;
25 import java.util.Hashtable JavaDoc;
26
27 import javax.swing.ButtonGroup JavaDoc;
28 import javax.swing.JMenuItem JavaDoc;
29 import javax.swing.JRadioButtonMenuItem JavaDoc;
30
31 import org.columba.api.gui.frame.IFrameMediator;
32 import org.columba.core.charset.CharsetEvent;
33 import org.columba.core.charset.CharsetListener;
34 import org.columba.core.charset.CharsetOwnerInterface;
35 import org.columba.core.gui.base.CMenu;
36 import org.columba.core.gui.menu.IMenu;
37 import org.columba.core.logging.Logging;
38 import org.columba.core.resourceloader.ImageLoader;
39 import org.columba.mail.util.MailResourceLoader;
40
41 /**
42  * Creates a menu containing submenus with different charsets from which the
43  * user can choose.
44  */

45
46 public class CharacterEncodingSubMenu extends IMenu implements ActionListener JavaDoc,
47         CharsetListener {
48
49     // String definitions for the charsetnames
50
// NOTE: these are also used to look up the
51
// menuentries from the resourceloader
52
private static final String JavaDoc[] CHARSET_STRINGS = {
53
54     // Global # 1
55
"UTF-8", "UTF-16", "US-ASCII",
56
57             // West Europe # 4
58
"windows-1252", "ISO-8859-1", "ISO-8859-15", "ISO-8859-7",
59             "windows-1253", "ISO-8859-3",
60
61             // East Europe # 10
62
"ISO-8859-4", "ISO-8859-13", "windows-1257", "ISO-8859-2",
63             "ISO-8859-5", "KOI8-R", "windows-1251",
64
65             // East Asian # 17
66
"GB2312", "GBK", "GB18030", "Big5", "Big5-HKSCS", "EUC-TW",
67             "EUC-JP", "Shift_JIS", "ISO-2022-JP", "MS932", "EUC-KR", "JOHAB",
68             "ISO-2022-KR",
69
70             // West Asian # 30
71
"TIS620", "ISO-8859-9", "windows-1254", "windows-1258" };
72
73     private static final Charset JavaDoc[] CHARSETS = createCharsetArray();
74
75     private static final String JavaDoc[] groups = { "global", "westeurope",
76             "easteurope", "eastasian", "seswasian" };
77
78     private static final int[] groupOffset = { 0, 3, 9, 16, 29, 33 };
79
80     private ButtonGroup JavaDoc group;
81
82     private Hashtable JavaDoc hashtable;
83
84     /**
85      * The menu item showing the currently selected charset.
86      */

87     // protected CharsetMenuItem selectedMenuItem = new CharsetMenuItem(null);
88
/**
89      * Creates a new menu for choosing charsets. The passed IFrameMediator
90      * instance needs to implement CharsetOwnerInterface.
91      */

92     public CharacterEncodingSubMenu(IFrameMediator mediator) {
93         super(mediator, MailResourceLoader.getString("menu", "mainframe",
94                 "menu_view_charset"), "menu_view_charset");
95
96         //setIcon(ImageLoader.getImageIcon("stock_font_16.png"));
97

98         group = new ButtonGroup JavaDoc();
99
100         hashtable = new Hashtable JavaDoc();
101
102         add(createMenuItem(null));
103
104         // Automatic Generation of Groups
105
CMenu subsubMenu;
106
107         for (int i = 0; i < groups.length; i++) {
108             subsubMenu = new CMenu(MailResourceLoader.getString("menu",
109                     "mainframe", "menu_view_charset_" + groups[i]),
110                     "menu_view_charset_" + groups[i]);
111             add(subsubMenu);
112
113             for (int j = groupOffset[i]; j < groupOffset[i + 1]; j++) {
114                 if (CHARSETS[j] != null) {
115                     JMenuItem JavaDoc item = createMenuItem(CHARSETS[j]);
116                     subsubMenu.add(item);
117                 }
118             }
119         }
120
121         ((CharsetOwnerInterface) controller).addCharsetListener(this);
122
123         // simulate charset changed to initialize selectedMenuItem
124
charsetChanged(new CharsetEvent(this,
125                 ((CharsetOwnerInterface) controller).getCharset()));
126
127     }
128
129     private static Charset JavaDoc[] createCharsetArray() {
130         Charset JavaDoc[] result = new Charset JavaDoc[CHARSET_STRINGS.length];
131         for (int i = 0; i < CHARSET_STRINGS.length; i++) {
132             try {
133                 result[i] = Charset.forName(CHARSET_STRINGS[i]);
134             } catch (UnsupportedCharsetException JavaDoc e) {
135                 if (Logging.DEBUG)
136                     e.printStackTrace();
137
138                 result[i] = null;
139             }
140         }
141         return result;
142     }
143
144     /**
145      * Creates a menu item and registers action and mouse listeners.
146      */

147     protected CharsetMenuItem createMenuItem(Charset JavaDoc charset) {
148         CharsetMenuItem menuItem = new CharsetMenuItem(charset);
149         group.add(menuItem);
150         menuItem.addActionListener(this);
151         if (charset != null)
152             hashtable.put(charset, menuItem);
153
154         return menuItem;
155     }
156
157     /**
158      * Called when a charset is chosen from the menu.
159      */

160     public void actionPerformed(ActionEvent JavaDoc e) {
161         ((CharsetOwnerInterface) controller).setCharset(((CharsetMenuItem) e
162                 .getSource()).getCharset());
163     }
164
165     /**
166      * Updates the selectedMenuItem according to the chosen charset.
167      */

168     public void charsetChanged(CharsetEvent e) {
169         // selectedMenuItem.setCharset(e.getCharset());
170
if (e.getCharset() == null) {
171             JRadioButtonMenuItem JavaDoc item = (JRadioButtonMenuItem JavaDoc) getMenuComponent(0);
172             item.setSelected(true);
173
174         } else {
175             if (hashtable.containsKey(e.getCharset())) {
176                 CharsetMenuItem menuItem = (CharsetMenuItem) hashtable.get(e
177                         .getCharset());
178                 menuItem.setSelected(true);
179             }
180         }
181     }
182 }
Popular Tags