1 22 23 package org.gjt.sp.jedit.menu; 24 25 import java.awt.event.ActionEvent ; 27 import java.awt.event.ActionListener ; 28 29 import java.nio.charset.Charset ; 30 import java.nio.charset.UnsupportedCharsetException ; 31 32 import java.util.Arrays ; 33 import java.util.Hashtable ; 34 35 import javax.swing.JMenu ; 36 import javax.swing.JMenuItem ; 37 import javax.swing.JOptionPane ; 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 46 52 public class ReloadWithEncodingProvider implements ActionListener , DynamicMenuProvider 53 { 54 private View view; 55 56 public boolean updateEveryTime() 58 { 59 return false; 60 } 62 public void update(JMenu menu) 64 { 65 view = GUIUtilities.getView(menu); 66 67 JMenuItem auto = new JMenuItem ( 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 String [] encodings = MiscUtilities.getEncodings(true); 77 String systemEncoding = System.getProperty("file.encoding"); 78 79 if (Arrays.binarySearch(encodings, systemEncoding) < 0) { 80 String [] tmp_a = new String [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 mi = new JMenuItem (encodings[i]); 92 mi.setActionCommand("encoding@" + encodings[i]); 93 mi.addActionListener(this); 94 if ((menu.getMenuComponentCount() >= maxItems) && (i < encodings.length)) 95 { 96 JMenu newMenu = new JMenu (jEdit.getProperty("common.more")); 97 menu.add(newMenu); 98 menu = newMenu; 99 } 100 menu.add(mi); 101 } 102 103 menu.addSeparator(); 104 105 JMenuItem other = new JMenuItem ( 107 jEdit.getProperty("vfs.browser.other-encoding.label")); 108 other.setActionCommand("other-encoding"); 109 other.addActionListener(this); 110 menu.add(other); 111 } 113 public void actionPerformed(ActionEvent ae) 115 { 116 JMenuItem mi = (JMenuItem ) ae.getSource(); 117 String action = mi.getActionCommand(); 118 String encoding = null; 119 Hashtable 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 uce) 139 { 140 String msg = jEdit.getProperty("reload-encoding.error", 141 new Object [] { 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 (); 153 props.put(Buffer.ENCODING, encoding); 154 } 155 156 String path = view.getBuffer().getPath(); 157 jEdit.closeBuffer(view, view.getBuffer()); 158 jEdit.openFile(view,null,path,false,props); 159 } } 161 | Popular Tags |