Package jsynoptic.ui

Source Code of jsynoptic.ui.TemplateChooser$TemplateListModel

/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info:  http://jsynoptic.sourceforge.net/index.html
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2005, by :
*     Corporate:
*         EADS Astrium SAS
*         EADS CRC
*     Individual:
*         Claude Cazenave
*
* $Id: TemplateChooser.java,v 1.6 2008/11/27 11:18:01 ogor Exp $
*
* Changes
* -------
* 25 february 2008 : Initial public release (CC);
*
*/
package jsynoptic.ui;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import jsynoptic.base.Template;

import simtools.ui.HeaderPanel;
import simtools.ui.MenuResourceBundle;
import simtools.ui.ResourceFinder;


/**
* Type
* <br><b>Summary:</b><br>
* Allow user to select a template and to customize its parameters.
*/
public class TemplateChooser extends JDialog implements ActionListener{

    public static MenuResourceBundle resources = ResourceFinder.getMenu(TemplateChooser.class);

    public static final int CANCEL_OPTION = 0;
    public static final int CREATE_OPTION = 1;

    protected JButton create, cancel, help;
    protected JPanel templatesPanels;
    protected JPanel inner;
    protected HeaderPanel headerPanel;

    protected JList tlist;
    protected int returnState;

    /**
     * @param parent - the parent frame that displays this dialog
     */
    public TemplateChooser(Frame parent){
        super(parent, resources.getString("selectATemplate"), true);
        createContent();
      
        if (JDialog.isDefaultLookAndFeelDecorated()) {
            boolean supportsWindowDecorations =
                UIManager.getLookAndFeel().getSupportsWindowDecorations();
            if (supportsWindowDecorations) {
                setUndecorated(true);
                getRootPane().setWindowDecorationStyle(JRootPane.QUESTION_DIALOG);
            }
        }
        pack();
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    }

    /**
     * Pops up a template chooser dialog.

     * @return the return state of the template chooser on popdown:
     * <ul>
     <li>TemplateChooser.CANCEL_OPTION
     *  <li>TemplateChooser.CREATE_OPTION
     * </ul>
     */
    public int showDialog(){
        returnState = CANCEL_OPTION;
       
        // update list and template panels
        ((TemplateListModel) tlist.getModel()).update();
        updateTemplatePanels();
       
        // Select current template
        tlist.setSelectedValue(Template.getCurrentTemplate(), true);
        displayTemplate( Template.getCurrentTemplate());
       
        // display dialog
        show();
        return returnState;
    }

   
    protected void updateTemplatePanels(){
        templatesPanels.removeAll()
        templatesPanels.add(new JPanel() , "emptyPanel");
        for (Iterator it = Template.getTemplates().iterator(); it.hasNext();) {
            Template t = (Template)(it.next());
            if t.getOptionPanelForTemplate() != null) {
                templatesPanels.add( t.getOptionPanelForTemplate(), t.getName());
            }
        }
    }
   
    protected void createContent(){
        Container content = getContentPane();
        content.setLayout(new BorderLayout());

        // Message panel
        headerPanel = new HeaderPanel(true);
        content.add(headerPanel, BorderLayout.NORTH);

        // Inner panel
        inner=  new JPanel(new BorderLayout());

        // Navigation bar
        JPanel navigationBarPanel = new JPanel(new BorderLayout());
        JPanel navigationBar = new JPanel();
        create = resources.getButton("create", this);
        cancel = resources.getButton("cancel", this);
        help = resources.getButton("help", this);
        navigationBar.add(create);
        navigationBar.add(cancel);
        navigationBar.add(help);
        navigationBarPanel.add(navigationBar, BorderLayout.EAST);
        inner.add(navigationBarPanel,BorderLayout.SOUTH);

        inner.setPreferredSize(new Dimension(350,400));//TODO

        content.add(inner, BorderLayout.CENTER);

        // template card panel
        templatesPanels = new JPanel();
        templatesPanels.setLayout(new CardLayout());

        inner.add(templatesPanels, BorderLayout.CENTER);
       
      
        tlist = new JList(new TemplateListModel());
        tlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        tlist.setVisibleRowCount(5);
        JScrollPane tlistScrollPane = new JScrollPane(tlist);
        content.add(tlistScrollPane, BorderLayout.WEST);

        tlist.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                create.setEnabled(tlist.getSelectedValue() != null);
            }
        });

        tlist.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                displayTemplate( (Template)tlist.getSelectedValue() );
            }
        });

        tlist.setSelectedValue( Template.getCurrentTemplate(), true);

        updateTemplatePanels();
       
       
        displayTemplate( (Template) tlist.getSelectedValue());
    }

   
   
    private void displayTemplate(Template t) {
        if (t != null){
            CardLayout cl = (CardLayout) (templatesPanels.getLayout());

            if ( t.getOptionPanelForTemplate() != null ){
                cl.show(templatesPanels, t.getName());
            } else {
                cl.show(templatesPanels,"emptyPanel");
            }

            headerPanel.setTitle(t.getName());
            headerPanel.displayInfo(t.getTemplateInformation());
        }
    }

   
   
    public void actionPerformed(ActionEvent e){
        if (e.getSource() == create){
            returnState = CREATE_OPTION;
            Template.setCurrentTemplate( (Template)tlist.getSelectedValue()  );
            dispose();

        } else if (e.getSource() == cancel){
            dispose();

        }else if (e.getSource() == help){
        }
    }

    protected static class TemplateListModel extends DefaultListModel {
        TemplateListModel() {
            fillFromMainList();
        }

        public void update() {
            this.clear();
            fillFromMainList();
        }

        protected void fillFromMainList() {
            for (Iterator it = Template.getTemplates().iterator(); it.hasNext();) {
                addElement(it.next());
            }
        }
    }

    public int getReturnState() {
        return returnState;
    }


}
TOP

Related Classes of jsynoptic.ui.TemplateChooser$TemplateListModel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.