1 5 6 13 package org.lucane.client.widgets.jazzy; 14 15 import com.swabunga.spell.engine.Word; 16 import com.swabunga.spell.event.SpellCheckEvent; 17 18 import javax.swing.*; 19 import javax.swing.event.EventListenerList ; 20 import javax.swing.event.ListSelectionEvent ; 21 import javax.swing.event.ListSelectionListener ; 22 23 import org.lucane.client.util.Translation; 24 25 import java.awt.*; 26 import java.awt.event.ActionEvent ; 27 import java.awt.event.ActionListener ; 28 29 30 35 public class JSpellForm extends JPanel implements ActionListener , ListSelectionListener { 36 37 public static final String IGNORE_CMD = "IGNORE"; 38 39 public static final String IGNOREALL_CMD = "IGNOREALL"; 40 41 public static final String ADD_CMD = "ADD"; 42 43 public static final String REPLACE_CMD = "REPLACE"; 44 45 public static final String REPLACEALL_CMD = "REPLACEALL"; 46 47 public static final String CANCEL_CMD = "CANCEL"; 48 49 private static final String SUGGESTIONS_RES = "SUGGESTIONS"; 50 private static final String INVALIDWORD_RES = "INVALIDWORD"; 51 52 public static final String ADDWORD_1 = "ADDWORD_1"; 53 public static final String ADDWORD_2 = "ADDWORD_2"; 54 public static final String ADDWORD_3 = "ADDWORD_3"; 55 56 private JLabel wrongWordLabel; 57 58 59 protected JList suggestList; 60 protected JTextField checkText; 61 62 protected SpellCheckEvent spellEvent; 63 64 protected EventListenerList listenerList = new EventListenerList (); 65 67 68 public JSpellForm() { 69 initialiseGUI(); 71 } 72 73 74 private static final JButton createButton(String command, String text, ActionListener listener) { 75 JButton btn = new JButton(text); 76 btn.setActionCommand(command); 77 btn.addActionListener(listener); 78 return btn; 79 } 80 81 82 protected JPanel makeEastPanel() { 83 JPanel jPanel1 = new JPanel(); 84 BoxLayout layout = new BoxLayout(jPanel1, BoxLayout.Y_AXIS); 85 jPanel1.setLayout(layout); 86 87 JButton ignoreBtn = createButton(IGNORE_CMD, Translation.tr("jazzy."+IGNORE_CMD), this); 88 ignoreBtn.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE)); 89 jPanel1.add(ignoreBtn); 90 91 JButton ignoreAllBtn = createButton(IGNOREALL_CMD, Translation.tr("jazzy."+IGNOREALL_CMD), this); 92 ignoreAllBtn.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE)); 93 jPanel1.add(ignoreAllBtn); 94 95 JButton addBtn = createButton(ADD_CMD, Translation.tr("jazzy."+ADD_CMD), this); 96 addBtn.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE)); 97 jPanel1.add(addBtn); 98 99 JButton changeBtn = createButton(REPLACE_CMD, Translation.tr("jazzy."+REPLACE_CMD), this); 100 changeBtn.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE)); 101 jPanel1.add(changeBtn); 102 103 JButton changeAllBtn = createButton(REPLACEALL_CMD, Translation.tr("jazzy."+REPLACEALL_CMD), this); 104 changeAllBtn.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE)); 105 jPanel1.add(changeAllBtn); 106 107 JButton cancelBtn = createButton(CANCEL_CMD, Translation.tr("jazzy."+CANCEL_CMD), this); 108 cancelBtn.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE)); 109 jPanel1.add(cancelBtn); 110 111 return jPanel1; 112 } 113 114 protected JPanel makeCentrePanel() { 115 JPanel jPanel2 = new JPanel(); 116 jPanel2.setLayout(new BoxLayout(jPanel2, BoxLayout.Y_AXIS)); 117 JPanel jPanel3 = new JPanel(); 118 JLabel lbl1 = new JLabel(Translation.tr("jazzy."+INVALIDWORD_RES)); 119 wrongWordLabel = new JLabel(""); 120 wrongWordLabel.setForeground(Color.red); jPanel3.add(lbl1); 122 jPanel3.add(wrongWordLabel); 123 jPanel2.add(jPanel3); 124 checkText = new JTextField(); 125 jPanel2.add(checkText); 126 JLabel lbl2 = new JLabel(Translation.tr("jazzy."+SUGGESTIONS_RES)); 127 jPanel2.add(lbl2); 128 suggestList = new JList(); 129 suggestList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 130 jPanel2.add(new JScrollPane(suggestList)); 131 suggestList.addListSelectionListener(this); 132 return jPanel2; 133 } 134 135 136 protected void initialiseGUI() { 137 setLayout(new BorderLayout()); 138 this.add(makeEastPanel(), BorderLayout.EAST); 139 this.add(makeCentrePanel(), BorderLayout.CENTER); 140 } 141 142 143 public void addActionListener(ActionListener l) { 144 listenerList.add(ActionListener .class, l); 145 } 146 147 148 public void removeActionListener(ActionListener l) { 149 listenerList.remove(ActionListener .class, l); 150 } 151 152 protected void fireActionEvent(ActionEvent e) { 153 Object [] listeners = listenerList.getListenerList(); 155 for (int i = listeners.length - 2; i >= 0; i -= 2) { 158 if (listeners[i] == ActionListener .class) { 159 ((ActionListener ) listeners[i + 1]).actionPerformed(e); 160 } 161 } 162 } 163 164 165 public void setSpellEvent(SpellCheckEvent event) { 166 spellEvent = event; 167 DefaultListModel m = new DefaultListModel(); 168 java.util.List suggestions = event.getSuggestions(); 169 for (int i = 0; i < suggestions.size(); i++) { 170 m.addElement(suggestions.get(i)); 171 } 172 suggestList.setModel(m); 173 wrongWordLabel.setText(event.getInvalidWord()); 174 if (m.size() > 0) { 175 suggestList.setSelectedIndex(0); 176 checkText.setText(((Word) m.get(0)).getWord()); 177 } else { 178 checkText.setText(event.getInvalidWord()); 179 } 180 } 181 182 183 public void valueChanged(ListSelectionEvent e) { 184 if (!e.getValueIsAdjusting()) { 185 Object selectedValue = suggestList.getSelectedValue(); 186 if (selectedValue != null) 187 checkText.setText(selectedValue.toString()); 188 } 189 } 190 191 192 public void actionPerformed(ActionEvent e) { 193 if (IGNORE_CMD.equals(e.getActionCommand())) { 194 spellEvent.ignoreWord(false); 195 } else if (IGNOREALL_CMD.equals(e.getActionCommand())) { 196 spellEvent.ignoreWord(true); 197 } else if (REPLACE_CMD.equals(e.getActionCommand())) { 198 spellEvent.replaceWord(checkText.getText(), false); 199 } else if (REPLACEALL_CMD.equals(e.getActionCommand())) { 200 spellEvent.replaceWord(checkText.getText(), true); 201 } else if (ADD_CMD.equals(e.getActionCommand())) { 202 String inField = checkText.getText(); 203 Object selObj = suggestList.getSelectedValue(); 204 String selected = (selObj == null ? "" : selObj.toString()); 205 String addString = (inField.equals(selected) ? spellEvent.getInvalidWord() : inField); 206 207 int n = JOptionPane.showConfirmDialog(this, Translation.tr("jazzy."+ADDWORD_1) + " '" + addString + "' " + 208 Translation.tr("jazzy."+ADDWORD_2), Translation.tr("jazzy."+ADDWORD_3), JOptionPane.YES_NO_OPTION); 209 210 if (n == JOptionPane.YES_OPTION) { 211 spellEvent.addToDictionary(addString); 212 } else { 213 return; 214 } 215 } else if (CANCEL_CMD.equals(e.getActionCommand())) { 216 spellEvent.cancel(); 217 } 218 fireActionEvent(e); 219 } 220 221 public static void main(String [] args) { 222 try { 223 JSpellForm pane = new JSpellForm(); 224 JFrame frm = new JFrame(Translation.tr("jazzy.title")); 225 frm.getContentPane().add(pane); 226 frm.setSize(300, 300); 227 frm.setVisible(true); 228 frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 229 } catch (Exception ex) { 230 ex.printStackTrace(); 231 } 232 } 233 } 234 235 236 237 | Popular Tags |