001: /*
002: * ReloadWithEncodingProvider.java - Recent file list menu
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2006 Marcelo Vanzin
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.menu;
024:
025: //{{{ Imports
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028:
029: import java.util.Arrays;
030: import java.util.Hashtable;
031:
032: import javax.swing.JMenu;
033: import javax.swing.JMenuItem;
034: import javax.swing.JOptionPane;
035:
036: import org.gjt.sp.jedit.Buffer;
037: import org.gjt.sp.jedit.GUIUtilities;
038: import org.gjt.sp.jedit.jEdit;
039: import org.gjt.sp.jedit.MiscUtilities;
040: import org.gjt.sp.jedit.View;
041: import org.gjt.sp.jedit.io.EncodingServer;
042:
043: //}}}
044:
045: /**
046: * Menu provider for actions to reload the current buffer with a
047: * specific encoding.
048: *
049: * @version $Id: ReloadWithEncodingProvider.java 10573 2007-09-14 02:04:59Z ezust $
050: */
051: public class ReloadWithEncodingProvider implements ActionListener,
052: DynamicMenuProvider {
053: private View view;
054:
055: //{{{ updateEveryTime() method
056: public boolean updateEveryTime() {
057: return false;
058: } //}}}
059:
060: //{{{ update() method
061: public void update(JMenu menu) {
062: view = GUIUtilities.getView(menu);
063:
064: // auto detect
065: JMenuItem auto = new JMenuItem(
066: jEdit
067: .getProperty("vfs.browser.commands.encoding.auto-detect"));
068: auto.setActionCommand("auto-detect");
069: auto.addActionListener(this );
070: menu.add(auto);
071: menu.addSeparator();
072: // all the enabled encodings + the system encoding
073: String[] encodings = MiscUtilities.getEncodings(true);
074: String systemEncoding = System.getProperty("file.encoding");
075:
076: if (Arrays.binarySearch(encodings, systemEncoding) < 0) {
077: String[] tmp_a = new String[encodings.length + 1];
078: System.arraycopy(encodings, 0, tmp_a, 0, encodings.length);
079: tmp_a[encodings.length] = systemEncoding;
080: encodings = tmp_a;
081: }
082:
083: Arrays.sort(encodings);
084:
085: int maxItems = jEdit.getIntegerProperty("menu.spillover", 20);
086: for (int i = 0; i < encodings.length; i++) {
087: JMenuItem mi = new JMenuItem(encodings[i]);
088: mi.setActionCommand("encoding@" + encodings[i]);
089: mi.addActionListener(this );
090: if ((menu.getMenuComponentCount() >= maxItems)
091: && (i < encodings.length)) {
092: JMenu newMenu = new JMenu(jEdit
093: .getProperty("common.more"));
094: menu.add(newMenu);
095: menu = newMenu;
096: }
097: menu.add(mi);
098: }
099:
100: menu.addSeparator();
101:
102: // option to prompt for the encoding
103: JMenuItem other = new JMenuItem(jEdit
104: .getProperty("vfs.browser.other-encoding.label"));
105: other.setActionCommand("other-encoding");
106: other.addActionListener(this );
107: menu.add(other);
108: } //}}}
109:
110: //{{{ actionPerformed() method
111: public void actionPerformed(ActionEvent ae) {
112: JMenuItem mi = (JMenuItem) ae.getSource();
113: String action = mi.getActionCommand();
114: String encoding = null;
115: Hashtable props = null;
116:
117: if (action.startsWith("encoding@")) {
118: encoding = action.substring(9);
119: } else if (action.equals("other-encoding")) {
120: encoding = JOptionPane.showInputDialog(view, jEdit
121: .getProperty("encoding-prompt.message"), jEdit
122: .getProperty("encoding-prompt.title"),
123: JOptionPane.QUESTION_MESSAGE);
124: if (encoding == null)
125: return;
126:
127: if (!EncodingServer.hasEncoding(encoding)) {
128: String msg = jEdit.getProperty("reload-encoding.error",
129: new Object[] { encoding });
130: JOptionPane.showMessageDialog(view, msg, jEdit
131: .getProperty("common.error"),
132: JOptionPane.ERROR_MESSAGE);
133: return;
134: }
135: }
136:
137: if (encoding != null) {
138: props = new Hashtable();
139: props.put(Buffer.ENCODING, encoding);
140: // Disable auto-detect because user explicitly
141: // specify an encoding.
142: props.put(Buffer.ENCODING_AUTODETECT, false);
143: }
144:
145: String path = view.getBuffer().getPath();
146: jEdit.closeBuffer(view, view.getBuffer());
147: jEdit.openFile(view, null, path, false, props);
148: } //}}}
149: }
|