2 Anagram- Find anagrams of a word
3 Copyright (C) 2013 Agnibho Mondal
5 This file is part of Anagram.
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.
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.
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/>.
20 package com
.agnibho
.code
.anagram
;
26 import java
.awt
.event
.ActionEvent
;
27 import java
.awt
.event
.ActionListener
;
28 import java
.util
.List
;
29 import java
.util
.concurrent
.ExecutionException
;
35 @SuppressWarnings("serial")
36 public class UI
extends JFrame
implements ActionListener
{
45 public JLabel heading
;
46 public JTextField input
;
47 public JLabel wordInfo
;
48 public JProgressBar progressBar
;
51 public JButton cancel
;
52 public JRadioButton single
, multi
;
53 public ButtonGroup buttons
;
54 public JEditorPane output
;
59 heading
=new JLabel("Anagram Finder", JLabel
.CENTER
);
60 heading
.setFont(new java
.awt
.Font(null, 0, 24));
62 input
=new JTextField(30);
63 input
.setActionCommand("entry");
65 wordInfo
=new JLabel();
67 progressBar
=new JProgressBar(0, 100);
68 progressBar
.setValue(0);
69 progressBar
.setStringPainted(true);
71 help
=new JButton("Help");
72 help
.setActionCommand("help");
74 find
=new JButton("Find");
75 find
.setActionCommand("entry");
77 cancel
=new JButton("Cancel");
78 cancel
.setActionCommand("cancel");
80 single
=new JRadioButton("Single Word");
81 multi
=new JRadioButton("Multi Word");
82 buttons
=new ButtonGroup();
85 single
.setSelected(true);
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));
96 input
.addActionListener(this);
97 find
.addActionListener(this);
98 cancel
.addActionListener(this);
99 help
.addActionListener(this);
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
)
114 .addGroup(layout
.createParallelGroup(GroupLayout
.Alignment
.CENTER
)
115 .addComponent(single
)
117 .addComponent(wordInfo
))
118 .addComponent(scroll
)
119 .addPreferredGap(LayoutStyle
.ComponentPlacement
.RELATED
, GroupLayout
.DEFAULT_SIZE
, 50)
121 layout
.setHorizontalGroup(
122 layout
.createParallelGroup()
123 .addComponent(heading
, 0, GroupLayout
.DEFAULT_SIZE
, Short
.MAX_VALUE
)
124 .addGroup(layout
.createSequentialGroup()
128 .addGroup(layout
.createSequentialGroup()
129 .addComponent(single
)
131 .addComponent(wordInfo
))
132 .addComponent(scroll
, 0, GroupLayout
.PREFERRED_SIZE
, Short
.MAX_VALUE
)
134 layout
.linkSize(SwingConstants
.HORIZONTAL
, find
, help
);
135 layout
.linkSize(SwingConstants
.HORIZONTAL
, single
, multi
);
137 setTitle("Anagram Finder");
139 setDefaultCloseOperation(WindowConstants
.EXIT_ON_CLOSE
);
141 setLocationRelativeTo(null);
146 public void actionPerformed(ActionEvent ae
){
147 if(ae
.getActionCommand().equals("entry")){
150 if(new IsValid().isValid(in
, multi
.isSelected())){
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()){
161 public void process(List
<Double
> progressValues
){
162 double progressValue
=progressValues
.get(progressValues
.size()-1);
163 if(progressValue
<100)
164 progressBar
.setValue((int)progressValue
);
166 progressBar
.setIndeterminate(true);
167 output
.setText("<html><p align=center><b>Search Complete</b></p><p><b>Formatting the Results...</b></p></html>");
175 formatted
=formatted
+"<html><p align=center><b>No match found</b></p></html>";
179 for(int i
=0; i
<out
.length();i
++){
180 if(out
.charAt(i
)=='/'){
181 formatted
=formatted
+"<br>";
185 formatted
=formatted
+out
.charAt(i
);
187 formatted
=formatted
+"<hr><p align=center>"+count
+" matches found for<br><b>"+in
+"</b></p></html>";
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>";
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
);
211 output
.setText("<html><p align=center><b>Invalid entry</b></p></html>");
213 output
.setText("<html><p align=center><b>Enter a word first</b></p></html>");
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>"
222 + "Developed by:<br>"
223 + "Agnibho Mondal<br>"
224 +"http://code.agnibho.com/"
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);
237 * @param args the command line arguments
239 public static void main(String
[] args
) {
240 SwingUtilities
.invokeLater(new Runnable(){
243 UIManager
.setLookAndFeel(UIManager
.getSystemLookAndFeelClassName());
245 catch (Exception ex
){
246 ex
.printStackTrace();
248 new UI().setVisible(true);