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 */
021package csheets.ui.ctrl;
022
023import java.awt.Color;
024import java.awt.Font;
025import java.awt.Frame;
026import java.awt.event.ActionEvent;
027import java.awt.event.KeyEvent;
028import java.io.BufferedReader;
029import java.io.IOException;
030import java.io.InputStream;
031import java.io.InputStreamReader;
032
033import javax.swing.Icon;
034import javax.swing.ImageIcon;
035import javax.swing.JOptionPane;
036import javax.swing.JScrollPane;
037import javax.swing.JTextArea;
038
039import csheets.CleanSheets;
040
041/**
042 * An action for displaying the license agreement.
043 * @author Einar Pehrson
044 */
045@SuppressWarnings("serial")
046public 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}