|
The previous section presented the entire listing for FancySrcCanvas, which is the longest of the project's three source files. You already saw ColorChoice in the "Specifying Colors" Section. That leaves only FancySrcFrame, which follows. You have seen its important pieces, but they were in pieces. Here it is, all in one place:
package fancysrc; import java.awt.*; import java.awt.event.*; public class FancySrcFrame extends Frame implements ActionListener, ItemListener { private MenuItem openMI, exitMI; private String fileName; private Checkbox showLinesBox; private ColorChoice keywordChoice, commentChoice; private FancySrcCanvas srcCanvas; private FileDialog dialog; FancySrcFrame() { // Build menu. MenuBar mbar = new MenuBar(); Menu fileMenu = new Menu("File"); openMI = new MenuItem("Open…"); openMI.addActionListener(this); fileMenu.add(openMI); exitMI = new MenuItem("Exit"); exitMI.addActionListener(this); fileMenu.add(exitMI); mbar.add(fileMenu); setMenuBar(mbar); // Build control panel. Panel panel = new Panel(); // Uses flow layout showLinesBox = new Checkbox("Show lines"); showLinesBox.addItemListener(this); panel.add(showLinesBox); keywordChoice = new ColorChoice(); keywordChoice.addItemListener(this); keywordChoice.select(1); panel.add(new Label("Keyword color")); panel.add(keywordChoice); commentChoice = new ColorChoice(); commentChoice.addItemListener(this); commentChoice.select(2); panel.add(new Label("Comment color")); panel.add(commentChoice); add(panel, BorderLayout.NORTH); // Build text display panel. srcCanvas = new FancySrcCanvas(); add(srcCanvas, BorderLayout.CENTER); // Set to a reasonable size. setSize(720, 550); } public void actionPerformed(ActionEvent e) { if (e.getSource() == openMI) { if (dialog == null) dialog = new FileDialog(this, "Source File", FileDialog.LOAD); dialog.setVisible(true); // Modal if (dialog.getFile() == null) return; // Canceled fileName = dialog.getDirectory() + dialog.getFile(); boolean underline = showLinesBox.getState(); Color keywordColor = keywordChoice.getSelectedColor(); Color commentColor = commentChoice.getSelectedColor(); srcCanvas.reconfigure(fileName, underline, keywordColor, commentColor); } else // Must be "Exit" menu item System.exit(0); } // Called if user activity in checkbox or // either choice. public void itemStateChanged(ItemEvent e) { boolean underline = showLinesBox.getState(); Color keywordColor = keywordChoice.getSelectedColor(); Color commentColor = commentChoice.getSelectedColor(); srcCanvas.reconfigure(fileName, underline, keywordColor, commentColor); } public static void main(String[] args) { (new FancySrcFrame()).setVisible(true); } }
The only part of this code that has not been explained already is the itemStateChanged() method. This is called when the user checks the Show lines check box or either color choice. There is no need to call getSource() and figure out which component caused the method call, because the response is the same in any case: A call is made to the reconfigure() method of the FancySrcCanvas.
Note | The complete source to this project is on your CD-ROM, in the FinalProjectSource directory. |
|