NewFileOne.java :  » ERP-CRM-Financial » jGnash-2 » jgnash » ui » wizards » file » Java Open Source

Java Open Source » ERP CRM Financial » jGnash 2 
jGnash 2 » jgnash » ui » wizards » file » NewFileOne.java
/*
 * jGnash, a personal finance application
 * Copyright (C) 2001-2008 Craig Cavanaugh
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package jgnash.ui.wizards.file;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.StyledEditorKit;

import jgnash.engine.EngineFactory;
import jgnash.ui.actions.DatabasePathAction;
import jgnash.ui.components.wizard.WizardPage;
import jgnash.ui.util.TextResource;
import jgnash.util.Resource;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;

/** New file wizard panel
 * 
 * @author  Craig Cavanaugh
 * 
 * $Id: NewFileOne.java 197 2008-01-01 08:13:35Z ccavanaugh $
 */
public class NewFileOne extends JPanel implements WizardPage, ActionListener {

    protected final Resource rb = Resource.get();

    private JTextField dbNameField = new JTextField();

    private JButton dbNameButton;

    private JLabel overwriteLabel;

    private JEditorPane helpPane;

    public NewFileOne() {

        layoutMainPanel();
    }

    private void initComponents() {
        helpPane = new JEditorPane();
        helpPane.setEditable(false);
        helpPane.setEditorKit(new StyledEditorKit());
        helpPane.setBackground(getBackground());
        helpPane.setText(TextResource.getString("NewFileOne.txt"));
        dbNameButton = new JButton("...");
        dbNameButton.addActionListener(this);

        overwriteLabel = new JLabel(rb.getString("Message.Overwritedb"));
        overwriteLabel.setIcon(UIManager.getIcon("OptionPane.warningIcon"));
        overwriteLabel.setFont(overwriteLabel.getFont().deriveFont(Font.ITALIC));

        dbNameField.addCaretListener(new CaretListener() {
            public void caretUpdate(CaretEvent e) {
                checkForOverwrite();
            }
        });
    }

    private void layoutMainPanel() {
        initComponents();
        FormLayout layout = new FormLayout("p, 8dlu, f:d:g, 4dlu, p", "");
        DefaultFormBuilder builder = new DefaultFormBuilder(layout, this);

        builder.appendSeparator(rb.getString("Title.DatabaseCfg"));
        builder.nextLine();
        builder.appendRelatedComponentsGapRow();
        builder.nextLine();
        builder.append(helpPane, 3);
        builder.nextLine();
        builder.appendRelatedComponentsGapRow();
        builder.nextLine();
        builder.append(rb.getString("Label.DatabaseName"), dbNameField, dbNameButton);
        builder.nextLine();
        builder.append(overwriteLabel, 5);
        builder.nextLine();
        builder.appendRelatedComponentsGapRow();
        builder.nextLine();
    }

    public boolean isPageValid() {
        return dbNameField.getText().length() > 0;
    }

    /**
     * toString must return a valid description for this page that will
     * appear in the task list of the WizardDialog
     * @return page description
     */
    @Override
    public String toString() {
        return "1. " + rb.getString("Title.DatabaseCfg");
    }

    public void getSettings(Map<Enum<?>, Object> map) {
        dbNameField.setText((String) map.get(NewFileDialog.Settings.DATABASENAME));

        checkForOverwrite();
    }

    public void putSettings(Map<Enum<?>, Object> map) {
        map.put(NewFileDialog.Settings.DATABASENAME, dbNameField.getText());
    }

    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == dbNameButton) {
            databaseNameAction();
        }
    }

    private void databaseNameAction() {
        String result = DatabasePathAction.databaseNameAction(this);

        if (result.length() > 0) {
            dbNameField.setText(result);
            checkForOverwrite();
        }
    }

    private void checkForOverwrite() {
        String database = dbNameField.getText();
        overwriteLabel.setVisible(EngineFactory.doesDatabaseExist(database));
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.