KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > menu > ReloadWithEncodingProvider


1 /*
2  * ReloadWithEncodingProvider.java - Recent file list menu
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2006 Marcelo Vanzin
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.menu;
24
25 //{{{ Imports
26
import java.awt.event.ActionEvent JavaDoc;
27 import java.awt.event.ActionListener JavaDoc;
28
29 import java.nio.charset.Charset JavaDoc;
30 import java.nio.charset.UnsupportedCharsetException JavaDoc;
31
32 import java.util.Arrays JavaDoc;
33 import java.util.Hashtable JavaDoc;
34
35 import javax.swing.JMenu JavaDoc;
36 import javax.swing.JMenuItem JavaDoc;
37 import javax.swing.JOptionPane JavaDoc;
38
39 import org.gjt.sp.jedit.Buffer;
40 import org.gjt.sp.jedit.GUIUtilities;
41 import org.gjt.sp.jedit.jEdit;
42 import org.gjt.sp.jedit.MiscUtilities;
43 import org.gjt.sp.jedit.View;
44 //}}}
45

46 /**
47  * Menu provider for actions to reload the current buffer with a
48  * specific encoding.
49  *
50  * @version $Id$
51  */

52 public class ReloadWithEncodingProvider implements ActionListener JavaDoc, DynamicMenuProvider
53 {
54     private View view;
55
56     //{{{ updateEveryTime() method
57
public boolean updateEveryTime()
58     {
59         return false;
60     } //}}}
61

62     //{{{ update() method
63
public void update(JMenu JavaDoc menu)
64     {
65         view = GUIUtilities.getView(menu);
66
67         // auto detect
68
JMenuItem JavaDoc auto = new JMenuItem JavaDoc(
69             jEdit.getProperty("vfs.browser.commands.encoding.auto-detect"));
70         auto.setActionCommand("auto-detect");
71         auto.addActionListener(this);
72         menu.add(auto);
73         menu.addSeparator();
74
75         // all the enabled encodings + the system encoding
76
String JavaDoc[] encodings = MiscUtilities.getEncodings(true);
77         String JavaDoc systemEncoding = System.getProperty("file.encoding");
78
79         if (Arrays.binarySearch(encodings, systemEncoding) < 0) {
80             String JavaDoc[] tmp_a = new String JavaDoc[encodings.length + 1];
81             System.arraycopy(encodings, 0, tmp_a, 0, encodings.length);
82             tmp_a[encodings.length] = systemEncoding;
83             encodings = tmp_a;
84         }
85
86         Arrays.sort(encodings);
87
88         int maxItems = jEdit.getIntegerProperty("menu.spillover",20);
89         for (int i = 0; i < encodings.length; i++)
90         {
91             JMenuItem JavaDoc mi = new JMenuItem JavaDoc(encodings[i]);
92             mi.setActionCommand("encoding@" + encodings[i]);
93             mi.addActionListener(this);
94             if ((menu.getMenuComponentCount() >= maxItems) && (i < encodings.length))
95             {
96                 JMenu JavaDoc newMenu = new JMenu JavaDoc(jEdit.getProperty("common.more"));
97                 menu.add(newMenu);
98                 menu = newMenu;
99             }
100             menu.add(mi);
101         }
102
103         menu.addSeparator();
104
105         // option to prompt for the encoding
106
JMenuItem JavaDoc other = new JMenuItem JavaDoc(
107             jEdit.getProperty("vfs.browser.other-encoding.label"));
108         other.setActionCommand("other-encoding");
109         other.addActionListener(this);
110         menu.add(other);
111     } //}}}
112

113     //{{{ actionPerformed() method
114
public void actionPerformed(ActionEvent JavaDoc ae)
115     {
116         JMenuItem JavaDoc mi = (JMenuItem JavaDoc) ae.getSource();
117         String JavaDoc action = mi.getActionCommand();
118         String JavaDoc encoding = null;
119         Hashtable JavaDoc props = null;
120
121         if (action.startsWith("encoding@"))
122         {
123             encoding = action.substring(9);
124         }
125         else if (action.equals("other-encoding"))
126         {
127             encoding = JOptionPane.showInputDialog(view,
128                 jEdit.getProperty("encoding-prompt.message"),
129                 jEdit.getProperty("encoding-prompt.title"),
130                 JOptionPane.QUESTION_MESSAGE);
131             if (encoding == null)
132                 return;
133
134             try
135             {
136                 Charset.forName(encoding);
137             }
138             catch (UnsupportedCharsetException JavaDoc uce)
139             {
140                 String JavaDoc msg = jEdit.getProperty("reload-encoding.error",
141                         new Object JavaDoc[] { encoding });
142                 JOptionPane.showMessageDialog(view,
143                     msg,
144                     jEdit.getProperty("common.error"),
145                     JOptionPane.ERROR_MESSAGE);
146                 return;
147             }
148         }
149
150         if (encoding != null)
151         {
152             props = new Hashtable JavaDoc();
153             props.put(Buffer.ENCODING, encoding);
154         }
155
156         String JavaDoc path = view.getBuffer().getPath();
157         jEdit.closeBuffer(view, view.getBuffer());
158         jEdit.openFile(view,null,path,false,props);
159     } //}}}
160
}
161
Popular Tags