]> Softwares of Agnibho - anagram.git/blob - com/agnibho/code/anagram/UI.java
742c62338145c5a3800fe4affd4c8781b01fc6f5
[anagram.git] / com / agnibho / code / anagram / UI.java
1 /*
2 Anagram- Find anagrams of a word
3 Copyright (C) 2013 Agnibho Mondal
4
5 This file is part of Anagram.
6
7 Anagram is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 Anagram is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Anagram. If not, see <http://www.gnu.org/licenses/>.
19 */
20 package com.agnibho.code.anagram;
21
22 import java.awt.*;
23
24 import javax.swing.*;
25
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 import java.util.List;
29 import java.util.concurrent.ExecutionException;
30
31 /**
32 *
33 * @author SPARK
34 */
35 @SuppressWarnings("serial")
36 public class UI extends JFrame implements ActionListener {
37
38 String in;
39 String out;
40 String formatted;
41 int count;
42
43 CheckAnagram checker;
44
45 public JLabel heading;
46 public JTextField input;
47 public JLabel wordInfo;
48 public JProgressBar progressBar;
49 public JButton help;
50 public JButton find;
51 public JButton cancel;
52 public JRadioButton single, multi;
53 public ButtonGroup buttons;
54 public JEditorPane output;
55
56 GroupLayout layout;
57
58 public UI(){
59 heading=new JLabel("Anagram Finder", JLabel.CENTER);
60 heading.setFont(new java.awt.Font(null, 0, 24));
61
62 input=new JTextField(30);
63 input.setActionCommand("entry");
64
65 wordInfo=new JLabel();
66
67 progressBar=new JProgressBar(0, 100);
68 progressBar.setValue(0);
69 progressBar.setStringPainted(true);
70
71 help=new JButton("Help");
72 help.setActionCommand("help");
73
74 find=new JButton("Find");
75 find.setActionCommand("entry");
76
77 cancel=new JButton("Cancel");
78 cancel.setActionCommand("cancel");
79
80 single=new JRadioButton("Single Word");
81 multi=new JRadioButton("Multi Word");
82 buttons=new ButtonGroup();
83 buttons.add(single);
84 buttons.add(multi);
85 single.setSelected(true);
86
87 output=new JEditorPane();
88 output.setEditable(false);
89 output.setContentType("text/html");
90 JScrollPane scroll=new JScrollPane(output);
91 scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
92 scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
93 scroll.setPreferredSize(new Dimension(600, 200));
94 scroll.setMinimumSize(new Dimension(600, 200));
95
96 input.addActionListener(this);
97 find.addActionListener(this);
98 cancel.addActionListener(this);
99 help.addActionListener(this);
100
101 layout=new GroupLayout(getContentPane());
102 getContentPane().setLayout(layout);
103 layout.setAutoCreateGaps(true);
104 layout.setAutoCreateContainerGaps(true);
105 layout.setVerticalGroup(
106 layout.createSequentialGroup()
107 .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE)
108 .addComponent(heading, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
109 .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE)
110 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
111 .addComponent(input, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
112 .addComponent(find)
113 .addComponent(help))
114 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
115 .addComponent(single)
116 .addComponent(multi)
117 .addComponent(wordInfo))
118 .addComponent(scroll)
119 .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, 50)
120 );
121 layout.setHorizontalGroup(
122 layout.createParallelGroup()
123 .addComponent(heading, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
124 .addGroup(layout.createSequentialGroup()
125 .addComponent(input)
126 .addComponent(find)
127 .addComponent(help))
128 .addGroup(layout.createSequentialGroup()
129 .addComponent(single)
130 .addComponent(multi)
131 .addComponent(wordInfo))
132 .addComponent(scroll, 0, GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE)
133 );
134 layout.linkSize(SwingConstants.HORIZONTAL, find, help);
135 layout.linkSize(SwingConstants.HORIZONTAL, single, multi);
136
137 setTitle("Anagram Finder");
138 pack();
139 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
140 setSize(600, 400);
141 setLocationRelativeTo(null);
142 setResizable(false);
143 }
144
145 @Override
146 public void actionPerformed(ActionEvent ae){
147 if(ae.getActionCommand().equals("entry")){
148 in=input.getText();
149 if(!in.isEmpty()){
150 if(new IsValid().isValid(in, multi.isSelected())){
151 formatted="";
152 input.setEnabled(false);
153 find.setEnabled(false);
154 wordInfo.setText("");
155 layout.replace(find, cancel);
156 layout.replace(wordInfo, progressBar);
157 progressBar.setIndeterminate(false);
158 output.setText("<html><p align=center><b>Search in progress...</b></p><p align=center><b>Please wait...</b></p></html>");
159 checker=new CheckAnagram(in, multi.isSelected()){
160 @Override
161 public void process(List <Double> progressValues){
162 double progressValue=progressValues.get(progressValues.size()-1);
163 if(progressValue<100)
164 progressBar.setValue((int)progressValue);
165 else{
166 progressBar.setIndeterminate(true);
167 output.setText("<html><p align=center><b>Search Complete</b></p><p><b>Formatting the Results...</b></p></html>");
168 }
169 }
170 @Override
171 public void done(){
172 try {
173 out=get();
174 if(out==null)
175 formatted=formatted+"<html><p align=center><b>No match found</b></p></html>";
176 else{
177 formatted="<html>";
178 count=0;
179 for(int i=0; i<out.length();i++){
180 if(out.charAt(i)=='/'){
181 formatted=formatted+"<br>";
182 count++;
183 }
184 else
185 formatted=formatted+out.charAt(i);
186 }
187 formatted=formatted+"<hr><p align=center>"+count+" matches found for<br><b>"+in+"</b></p></html>";
188 }
189 if(new IsPalindrome().check(in))
190 JOptionPane.showMessageDialog(null, "The word you entered is a palindrome", "Palindrome", JOptionPane.PLAIN_MESSAGE);
191 if(single.isSelected()&&!checker.isWord())
192 wordInfo.setText("The word you entered does not exist in the Dictionary");
193 } catch (java.util.concurrent.CancellationException e) {
194 formatted="<html><p align=center><b>Search cancelled</b></p></html>";
195 } catch (InterruptedException e) {
196 formatted="<html><p align=center><b>"+e.toString()+"</b></p></html>";
197 } catch (ExecutionException e) {
198 formatted="<html><p align=center><b>"+e.toString()+"</b></p></html>";
199 }
200 output.setText(formatted);
201 output.setCaretPosition(0);
202 input.setEnabled(true);
203 find.setEnabled(true);
204 layout.replace(cancel, find);
205 layout.replace(progressBar, wordInfo);
206 }
207 };
208 checker.execute();
209 }
210 else
211 output.setText("<html><p align=center><b>Invalid entry</b></p></html>");
212 }else
213 output.setText("<html><p align=center><b>Enter a word first</b></p></html>");
214 }
215 if(ae.getActionCommand().equals("help"))
216 JOptionPane.showMessageDialog(getContentPane(), "<html>"
217 + "<p>Enter a word in the provided field, choose singleword or multiword mode and click the search button to find its anagrams.</p>"
218 + "<p>The search results are dependent on the attached dictionary. The software is using UKACD for the word list.</p>"
219 + "<p>Grammatical filtering of the multi-worded search results is not avaiable."
220 + "<p align=centre><b>Warning:</b> Multi-worded searches may take long time.</p>"
221 + "<hr>"
222 + "Developed by:<br>"
223 + "Agnibho Mondal<br>"
224 +"http://code.agnibho.com/"
225 +"<hr>"
226 +"Anagram Copyright (C) 2013 Agnibho Mondal<br>"
227 +"This program comes with ABSOLUTELY NO WARRANTY<br>"
228 +"This is free software, and you are welcome to redistribute it under certain conditions"
229 +"</html>", "Anagram Finder Help", JOptionPane.PLAIN_MESSAGE);
230 if(ae.getActionCommand().equals("cancel"))
231 checker.cancel(true);
232 }
233
234
235
236 /**
237 * @param args the command line arguments
238 */
239 public static void main(String[] args) {
240 SwingUtilities.invokeLater(new Runnable(){
241 public void run(){
242 try{
243 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
244 }
245 catch (Exception ex){
246 ex.printStackTrace();
247 }
248 new UI().setVisible(true);
249 }
250 });
251 }
252 }