1 package sample.google.spellcheck; 2 3 import java.awt.BorderLayout ; 4 import java.awt.Container ; 5 import java.awt.Dimension ; 6 import java.awt.GridLayout ; 7 import java.awt.HeadlessException ; 8 import java.awt.Toolkit ; 9 import java.awt.event.ActionEvent ; 10 import java.awt.event.ActionListener ; 11 import java.awt.event.KeyEvent ; 12 import java.io.File ; 13 import java.io.IOException ; 14 15 import javax.swing.JEditorPane ; 16 import javax.swing.JFrame ; 17 import javax.swing.JMenu ; 18 import javax.swing.JMenuBar ; 19 import javax.swing.JMenuItem ; 20 import javax.swing.JOptionPane ; 21 import javax.swing.JScrollPane ; 22 import javax.swing.KeyStroke ; 23 import javax.swing.WindowConstants ; 24 import javax.swing.event.HyperlinkEvent ; 25 import javax.swing.event.HyperlinkListener ; 26 27 import sample.google.common.util.PropertyLoader; 28 29 35 public class SuggestionForm extends javax.swing.JFrame implements HyperlinkListener { 36 private AsyncPanel asyncPanel; 37 private SyncPanel syncPanel; 38 private JEditorPane helpDisplayPane; 39 40 private JMenuItem syncMenuItem; 41 private JMenuItem asyncMenuItem; 42 private static final String HELP_FILE_NAME = "/docs/GoogleSpellCheck.html"; 43 44 45 public SuggestionForm() throws HeadlessException { 46 asyncPanel = new AsyncPanel(); 47 syncPanel = new SyncPanel(); 48 49 JMenuBar menuBar; 50 menuBar = new JMenuBar (); 52 53 JMenu modeMenu = new JMenu ("Mode"); 54 modeMenu.setMnemonic(KeyEvent.VK_M); 55 syncMenuItem = new JMenuItem ("Sync Mode", KeyEvent.VK_S); 56 syncMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK)); 57 syncMenuItem.addActionListener(new ActionListener () { 58 public void actionPerformed(ActionEvent e) { 59 setSyncPanel(); 60 } 61 }); 62 asyncMenuItem = new JMenuItem ("ASync Mode", KeyEvent.VK_A); 63 asyncMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK)); 64 asyncMenuItem.addActionListener(new ActionListener () { 65 public void actionPerformed(ActionEvent e) { 66 setAsyncPanel(); 67 } 68 }); 69 modeMenu.add(syncMenuItem); 70 modeMenu.add(asyncMenuItem); 71 72 JMenu settingsMenu = new JMenu ("Settings"); 73 settingsMenu.setMnemonic(KeyEvent.VK_S); 74 JMenuItem googleKeyMenu = new JMenuItem ("Set Google Key", KeyEvent.VK_G); 75 googleKeyMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, ActionEvent.CTRL_MASK)); 76 googleKeyMenu.addActionListener(new ActionListener () { 77 public void actionPerformed(ActionEvent e) { 78 setKey(); 79 } 80 }); 81 settingsMenu.add(googleKeyMenu); 82 83 JMenu clearMenu = new JMenu ("Clear"); 84 clearMenu.setMnemonic(KeyEvent.VK_C); 85 JMenuItem clearMenuItem = new JMenuItem ("Clear text boxes"); 86 clearMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK)); 87 clearMenuItem.addActionListener(new ActionListener () { 88 public void actionPerformed(ActionEvent e) { 89 asyncPanel.clear(); 90 syncPanel.clear(); 91 } 92 }); 93 clearMenu.add(clearMenuItem); 94 95 JMenu helpMenu = new JMenu ("Help"); 96 JMenuItem mnuItemHelp = new JMenuItem ("Show Help"); 97 helpMenu.add(mnuItemHelp); 98 99 mnuItemHelp.addActionListener(new ActionListener () { 100 public void actionPerformed(ActionEvent e) { 101 showHelp(); 102 } 103 }); 104 105 menuBar.add(modeMenu); 106 menuBar.add(settingsMenu); 107 menuBar.add(clearMenu); 108 menuBar.add(helpMenu); 109 110 this.setJMenuBar(menuBar); 111 112 this.getContentPane().setLayout(new GridLayout (1, 1)); 113 setAsyncPanel(); 114 115 116 } 117 118 public static void main(String [] args) { 119 SuggestionForm form = new SuggestionForm(); 120 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 121 form.setLocation(screenSize.width / 4, 122 screenSize.height / 4); 123 form.setSize(screenSize.width / 2, screenSize.height / 2); 124 form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 125 form.show(); 128 } 129 130 private void setAsyncPanel() { 131 this.getContentPane().removeAll(); 132 this.getContentPane().add(asyncPanel); 133 this.syncMenuItem.setEnabled(true); 134 this.asyncMenuItem.setEnabled(false); 135 this.getContentPane().repaint(); 136 this.setTitle("Google Spell checker - Async Mode"); 137 this.show(); 138 139 } 140 141 private void setSyncPanel() { 142 this.getContentPane().removeAll(); 143 this.getContentPane().add(syncPanel); 144 this.syncMenuItem.setEnabled(false); 145 this.asyncMenuItem.setEnabled(true); 146 this.getContentPane().repaint(); 147 this.setTitle("Google Spell checker - Sync Mode"); 148 this.show(); 149 } 150 151 private void setKey() { 152 String key = JOptionPane.showInputDialog(this, "Set the Google Key", 153 PropertyLoader.getGoogleKey()); 154 if (key != null && !key.trim().equals("")) { 155 PropertyLoader.setGoogleKey(key); 156 } 157 } 158 159 162 private void showHelp() { 163 164 JFrame frame = new JFrame (); 165 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 166 frame.setLocation(screenSize.width / 5, 167 screenSize.height / 5); 168 frame.setSize(screenSize.width / 2, screenSize.height / 2); 169 170 BorderLayout layout = new BorderLayout (); 171 172 JScrollPane jsp; 173 174 175 helpDisplayPane = new JEditorPane (); 176 helpDisplayPane.addHyperlinkListener(this); 177 helpDisplayPane.setEditable(false); 178 helpDisplayPane.setContentType("text/html"); 179 180 jsp = new JScrollPane (helpDisplayPane); 181 182 Container contentPane = frame.getContentPane(); 183 contentPane.setLayout(layout); 184 contentPane.add(jsp, BorderLayout.CENTER); 185 String helpDoc = System.getProperty("user.dir") + HELP_FILE_NAME; 186 187 try { 188 helpDisplayPane.setPage(new File (helpDoc).toURL()); 189 } catch (IOException e) { 190 JOptionPane.showMessageDialog(this, "Help file not detected", "Help file error", 191 JOptionPane.ERROR_MESSAGE); 192 return; 193 } 194 frame.setVisible(true); 195 } 196 197 198 public void hyperlinkUpdate(HyperlinkEvent e) { 199 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { 200 try { 201 String url = e.getURL().toString(); 202 helpDisplayPane.setPage(url); 203 } catch (Exception err) { 205 JOptionPane.showMessageDialog(this, "Help file not detected", err.getMessage(), 206 JOptionPane.ERROR_MESSAGE); 207 return; 208 } 209 210 } 211 } 212 } 213 | Popular Tags |