/* * Created on Apr 5, 2005 */ package WordPro2Controller; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.net.URL; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.border.Border; import WordPro2Document.IDocument; import WordPro2Document.MyDocument; import WordPro2Speller.ISpellChecker; import WordPro2Speller.MySpellChecker; /** A window and controller for the Word Processor. * Don't change this file -- you won't be able to hand it in, but * your files will need to compile and run with it. * A new, improved version of it might be released before the project is due. * You can browse the code to learn something about constructing a GUI, * but it doesn't contain too many clues about the homework itself. */ public class WordProFrame extends JFrame { private int FRAMEWIDTH = 700; private int FRAMEHEIGHT = 500; public static final String frameTitle = "The Magnificent CSE373 Word Processor and Spell Checker"; public static final Color docColor = Color.WHITE; public static final Color spellColor = Color.YELLOW; public static final Color controlColor = Color.PINK; public static final Color background = Color.BLACK; /** Create a window for viewing the Word Processor. * There are panels for the document, the spelling dictionary, * and some buttons. * @param defaultDictionary An initial spelling dictionary, which * may be empty but should not be null. * @param defaultDocument An intial document, which may be empty * but should not be null. * @param pictureLocation URL to a picture to be displayed on the * control panel. May be null. If a picture cannot be loaded from * the URL, an error message is printed to the console and the program * continues. */ public WordProFrame(final String[] defaultDictionary, final String defaultDocument, final String pictureLocation) { super(frameTitle); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(new Dimension(FRAMEWIDTH, FRAMEHEIGHT)); Container content = this.getContentPane(); content.setLayout(new FlowLayout()); content.setBackground(background); DocumentViewer dviewer = new DocumentViewer(); dviewer.setPreferredSize(new Dimension( this.FRAMEWIDTH/2, this.FRAMEHEIGHT-20)); dviewer.setBackground(docColor); JPanel dviewerPanel = new JPanel(); dviewerPanel.add(new JScrollPane(dviewer)); Border dvborder = BorderFactory.createTitledBorder("Current document"); dviewerPanel.setBorder(dvborder); content.add(dviewerPanel); SpellerViewer scviewer = new SpellerViewer(); scviewer.setPreferredSize(new Dimension( this.FRAMEWIDTH/5, this.FRAMEHEIGHT-20)); scviewer.setBackground(spellColor); JPanel scviewerPanel = new JPanel(); scviewerPanel.add(new JScrollPane(scviewer)); Border scborder = BorderFactory.createTitledBorder("Dictionary"); scviewerPanel.setBorder(scborder); content.add(scviewerPanel); ISpellChecker spellChecker = new MySpellChecker( defaultDictionary, scviewer); IDocument doc = new MyDocument(spellChecker, dviewer); doc.open(defaultDocument); JComponent controlpanel = new ControlPanel(doc, spellChecker, pictureLocation); controlpanel.setPreferredSize(new Dimension( this.FRAMEWIDTH/5, this.FRAMEHEIGHT-20)); content.add(new JScrollPane(controlpanel)); this.pack(); this.setVisible(true); } /** Buttons and an image. * */private class ControlPanel extends Box { ControlPanel(final IDocument doc, final ISpellChecker checker, String pictureLocation) { super(BoxLayout.Y_AXIS); this.setBackground(controlColor); URL classPicture; try { classPicture = new URL(pictureLocation); } catch (IOException e) { System.out.println("Unable to load picture " + pictureLocation); classPicture = null; } ImageIcon picture = new ImageIcon(classPicture); JLabel label = new JLabel(); Dimension picsize = new Dimension( FRAMEWIDTH/4, FRAMEHEIGHT/4); label.setPreferredSize(picsize); ImageIcon newpic = new ImageIcon(picture.getImage().getScaledInstance( picsize.width, picsize.height, 0)); label.setIcon(newpic); this.add(new JScrollPane(label)); JButton openFileButton = new JButton("Open document"); openFileButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { doc.open(); } }); this.add(openFileButton); JButton openDictionaryButton = new JButton("Choose dictionary"); openDictionaryButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { checker.open(); } }); this.add(openDictionaryButton); JButton checkWordButton = new JButton("Check a word..."); checkWordButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String wordToCheck = JOptionPane.showInputDialog(WordProFrame.this, "Type a word and press OK", "Check spelling of one word", JOptionPane.PLAIN_MESSAGE); String resultMessage; if (wordToCheck == null || wordToCheck.trim().length() == 0) { resultMessage = "No word entered."; } else { boolean result = checker.isCorrectlySpelled(wordToCheck); if (result) { resultMessage = wordToCheck + " is correctly spelled"; } else { resultMessage = wordToCheck + " is not in the dictionary"; } } JOptionPane.showMessageDialog(WordProFrame.this, resultMessage, "Speller Checker result", JOptionPane.INFORMATION_MESSAGE); } }); this.add(checkWordButton); } } }