KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > google > spellcheck > SuggestionForm


1 package sample.google.spellcheck;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.Container JavaDoc;
5 import java.awt.Dimension JavaDoc;
6 import java.awt.GridLayout JavaDoc;
7 import java.awt.HeadlessException JavaDoc;
8 import java.awt.Toolkit JavaDoc;
9 import java.awt.event.ActionEvent JavaDoc;
10 import java.awt.event.ActionListener JavaDoc;
11 import java.awt.event.KeyEvent JavaDoc;
12 import java.io.File JavaDoc;
13 import java.io.IOException JavaDoc;
14
15 import javax.swing.JEditorPane JavaDoc;
16 import javax.swing.JFrame JavaDoc;
17 import javax.swing.JMenu JavaDoc;
18 import javax.swing.JMenuBar JavaDoc;
19 import javax.swing.JMenuItem JavaDoc;
20 import javax.swing.JOptionPane JavaDoc;
21 import javax.swing.JScrollPane JavaDoc;
22 import javax.swing.KeyStroke JavaDoc;
23 import javax.swing.WindowConstants JavaDoc;
24 import javax.swing.event.HyperlinkEvent JavaDoc;
25 import javax.swing.event.HyperlinkListener JavaDoc;
26
27 import sample.google.common.util.PropertyLoader;
28
29 /**
30  * class sample.google.spellcheck.SuggestionForm
31  * This is the implementation of the GUI
32  *
33  * @author Nadana Gunarathna
34  */

35 public class SuggestionForm extends javax.swing.JFrame JavaDoc implements HyperlinkListener JavaDoc {
36     private AsyncPanel asyncPanel;
37     private SyncPanel syncPanel;
38     private JEditorPane JavaDoc helpDisplayPane;
39
40     private JMenuItem JavaDoc syncMenuItem;
41     private JMenuItem JavaDoc asyncMenuItem;
42     private static final String JavaDoc HELP_FILE_NAME = "/docs/GoogleSpellCheck.html";
43
44
45     public SuggestionForm() throws HeadlessException JavaDoc {
46         asyncPanel = new AsyncPanel();
47         syncPanel = new SyncPanel();
48
49         JMenuBar JavaDoc menuBar;
50         //Create the menu bar.
51
menuBar = new JMenuBar JavaDoc();
52
53         JMenu JavaDoc modeMenu = new JMenu JavaDoc("Mode");
54         modeMenu.setMnemonic(KeyEvent.VK_M);
55         syncMenuItem = new JMenuItem JavaDoc("Sync Mode", KeyEvent.VK_S);
56         syncMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
57         syncMenuItem.addActionListener(new ActionListener JavaDoc() {
58             public void actionPerformed(ActionEvent JavaDoc e) {
59                 setSyncPanel();
60             }
61         });
62         asyncMenuItem = new JMenuItem JavaDoc("ASync Mode", KeyEvent.VK_A);
63         asyncMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
64         asyncMenuItem.addActionListener(new ActionListener JavaDoc() {
65             public void actionPerformed(ActionEvent JavaDoc e) {
66                 setAsyncPanel();
67             }
68         });
69         modeMenu.add(syncMenuItem);
70         modeMenu.add(asyncMenuItem);
71
72         JMenu JavaDoc settingsMenu = new JMenu JavaDoc("Settings");
73         settingsMenu.setMnemonic(KeyEvent.VK_S);
74         JMenuItem JavaDoc googleKeyMenu = new JMenuItem JavaDoc("Set Google Key", KeyEvent.VK_G);
75         googleKeyMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, ActionEvent.CTRL_MASK));
76         googleKeyMenu.addActionListener(new ActionListener JavaDoc() {
77             public void actionPerformed(ActionEvent JavaDoc e) {
78                 setKey();
79             }
80         });
81         settingsMenu.add(googleKeyMenu);
82
83         JMenu JavaDoc clearMenu = new JMenu JavaDoc("Clear");
84         clearMenu.setMnemonic(KeyEvent.VK_C);
85         JMenuItem JavaDoc clearMenuItem = new JMenuItem JavaDoc("Clear text boxes");
86         clearMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
87         clearMenuItem.addActionListener(new ActionListener JavaDoc() {
88             public void actionPerformed(ActionEvent JavaDoc e) {
89                 asyncPanel.clear();
90                 syncPanel.clear();
91             }
92         });
93         clearMenu.add(clearMenuItem);
94
95         JMenu JavaDoc helpMenu = new JMenu JavaDoc("Help");
96         JMenuItem JavaDoc mnuItemHelp = new JMenuItem JavaDoc("Show Help");
97         helpMenu.add(mnuItemHelp);
98
99         mnuItemHelp.addActionListener(new ActionListener JavaDoc() {
100             public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc(1, 1));
113         setAsyncPanel();
114
115
116     }
117
118     public static void main(String JavaDoc[] args) {
119         SuggestionForm form = new SuggestionForm();
120         Dimension JavaDoc 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.setResizable(false);
126
//form.pack();
127
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 JavaDoc 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     /**
160      * method showHelp
161      */

162     private void showHelp() {
163
164         JFrame JavaDoc frame = new JFrame JavaDoc();
165         Dimension JavaDoc 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 JavaDoc layout = new BorderLayout JavaDoc();
171
172         JScrollPane JavaDoc jsp;
173
174
175         helpDisplayPane = new JEditorPane JavaDoc();
176         helpDisplayPane.addHyperlinkListener(this);
177         helpDisplayPane.setEditable(false);
178         helpDisplayPane.setContentType("text/html");
179
180         jsp = new JScrollPane JavaDoc(helpDisplayPane);
181
182         Container JavaDoc contentPane = frame.getContentPane();
183         contentPane.setLayout(layout);
184         contentPane.add(jsp, BorderLayout.CENTER);
185         String JavaDoc helpDoc = System.getProperty("user.dir") + HELP_FILE_NAME;
186
187         try {
188             helpDisplayPane.setPage(new File JavaDoc(helpDoc).toURL());
189         } catch (IOException JavaDoc 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 JavaDoc e) {
199         if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
200             try {
201                 String JavaDoc url = e.getURL().toString();
202                 helpDisplayPane.setPage(url);
203 //
204
} catch (Exception JavaDoc 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