001    /*
002     * Copyright (c) 2005 Einar Pehrson <einar@pehrson.nu>.
003     *
004     * This file is part of
005     * CleanSheets - a spreadsheet application for the Java platform.
006     *
007     * CleanSheets is free software; you can redistribute it and/or modify
008     * it under the terms of the GNU General Public License as published by
009     * the Free Software Foundation; either version 2 of the License, or
010     * (at your option) any later version.
011     *
012     * CleanSheets is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015     * GNU General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with CleanSheets; if not, write to the Free Software
019     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
020     */
021    package csheets.ui.ctrl;
022    
023    import java.awt.Color;
024    import java.awt.Font;
025    import java.awt.Frame;
026    import java.awt.event.ActionEvent;
027    import java.awt.event.KeyEvent;
028    import java.io.BufferedReader;
029    import java.io.IOException;
030    import java.io.InputStream;
031    import java.io.InputStreamReader;
032    
033    import javax.swing.Icon;
034    import javax.swing.ImageIcon;
035    import javax.swing.JOptionPane;
036    import javax.swing.JScrollPane;
037    import javax.swing.JTextArea;
038    
039    import csheets.CleanSheets;
040    
041    /**
042     * An action for displaying the license agreement.
043     * @author Einar Pehrson
044     */
045    @SuppressWarnings("serial")
046    public class LicenseAction extends BaseAction {
047    
048            /** The scroll pane in which the text is shown */
049            private JScrollPane licensePane;
050    
051            /**
052             * Creates a new help action.
053             */
054            public LicenseAction() {
055                    InputStream stream = CleanSheets.class.getResourceAsStream(
056                            "res/doc/license.txt");
057                    if (stream != null) {
058                            // Loads text from file
059                            String text;
060                            try {
061                                    BufferedReader br = new BufferedReader(new InputStreamReader(stream));
062                                    int availableBytes = stream.available();
063                                    char[] chars = new char[availableBytes];
064                                    br.read(chars, 0, availableBytes);
065                                    text = new String(chars);
066                            } catch (Exception ex) {
067                                    text = "An error occurred when loading the text.";
068                            } finally {
069                                    try {
070                                            stream.close();
071                                    } catch (IOException e) {}
072                            }
073    
074                            // Creates and configures text area
075                            JTextArea textArea = new JTextArea(text, 25, 80);
076                            textArea.setFont(new Font("Courier", Font.PLAIN, 12));
077                            textArea.setLineWrap(true);
078                            textArea.setWrapStyleWord(true);
079                            textArea.setTabSize(4);
080                            textArea.setDisabledTextColor(Color.black);
081                            textArea.setEnabled(false);
082                            licensePane = new JScrollPane(textArea);
083                    } else
084                            setEnabled(false);
085            }
086    
087            protected String getName() {
088                    return "License Agreement...";
089            }
090    
091            protected void defineProperties() {
092                    putValue(MNEMONIC_KEY, KeyEvent.VK_L);
093                    putValue(SMALL_ICON, new ImageIcon(CleanSheets.class.getResource("res/img/license.gif")));
094            }
095    
096            public void actionPerformed(ActionEvent e) {
097                    JOptionPane.showMessageDialog(
098                            (Frame)null,
099                            licensePane,
100                            "License Agreement",
101                            JOptionPane.PLAIN_MESSAGE,
102                            (Icon)null);
103            }
104    }