Package jsynoptic.ui

Source Code of jsynoptic.ui.Panes

/* ========================
* 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-2003, by :
*     Corporate:
*         Astrium SAS
*         EADS CRC
*     Individual:
*         Nicolas Brodu
*
* $Id: Panes.java,v 1.2 2009/01/19 17:44:58 ogor Exp $
*
* Changes
* -------
* 25-Sep-2003 : Initial public release (NB);
*
*/
package jsynoptic.ui;

import java.awt.Frame;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JLabel;

import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;

import jsynoptic.base.Plugin;
import jsynoptic.ui.ShapeListModel.ShapePageModel;
import simtools.data.DataInfo;
import simtools.data.DataSource;
import simtools.data.DataSourcePool;
import simtools.ui.GridBagPanel;
import simtools.ui.FilteredSourceTree;
import simtools.ui.MenuResourceBundle;
import simtools.ui.ResourceFinder;
import simtools.ui.SplitTabPane;
import simtools.ui.StackPanel;


/**
*  Tab pane that gathers all JSynoptic panes, such as source panel, shape panel.
*  Plug-ins can provide additional panes.
*
* @author Nicolas Brodu
*
* @version 1.0 2001
*/
public class Panes extends SplitTabPane{

    protected FilteredSourceTree filteredSourceTree;
    protected JTextField tfAlias;
    protected JLabel lAlias;

    public static MenuResourceBundle resources = (MenuResourceBundle)ResourceFinder.get(Panes.class);

    protected ShapeListModel shapesListModel;
  
    public Panes(Frame owner, DataSourcePool sourcePool) {
        super(owner, JSplitPane.VERTICAL_SPLIT, true, true);
     
       
        // SOURCE PANE
        filteredSourceTree = new FilteredSourceTree(new SourceTree(sourcePool), resources.getString("KnownSources"));
        for (Iterator it = Run.plugins.iterator(); it.hasNext();) {
            Plugin p = (Plugin) it.next();
            Object[][] tab = p.getDataSourceIcons();
            if (tab != null) {
                SourceTree.addSourceIcons(tab);
            }
        }
        lAlias = new JLabel(resources.getStringValue("Alias"));
        tfAlias = new JTextField();
       
       
        GridBagPanel sourcePane = new GridBagPanel();
        sourcePane.addOnCurrentRow(filteredSourceTree, 5, true, true, true);
        sourcePane.addOnCurrentRow(lAlias);
        sourcePane.addOnCurrentRow(tfAlias, 4, true, false, true);
        sourcePane.addOnCurrentRow(new ExpressionPanel(filteredSourceTree.getSourceTree()), 5, true, false, true);

    
        tfAlias.getDocument().addDocumentListener(new DocumentListener() {
            public void changedUpdate(DocumentEvent e) {
                updateAlias();
            }

            public void insertUpdate(DocumentEvent e) {
                updateAlias();
            }

            public void removeUpdate(DocumentEvent e) {
                updateAlias();
            }

            private void updateAlias() {
                if (!tfAlias.isEnabled()) {
                    return;
                }
                Object o = filteredSourceTree.getSelectedSourceOrCollection();
                if (o instanceof DataSource) {
                    DataInfo di = ((DataSource) o).getInformation();
                    String alias = tfAlias.getText();
                    if (alias.equals("")) {
                        alias = null;
                    }
                    if (di != null) {
                        String oldAlias = di.alias;
                        if (((oldAlias == null) && (alias != null)) || ((oldAlias != null) && (alias == null))
                                || ((oldAlias != null) && (!oldAlias.equals(alias)))) {
                            di.alias = alias;
                            ((DataSource) o).notifyListenersForInfoChange(di);
                            tfAlias.getDocument().removeDocumentListener(this);
                            TreeNode tn = (TreeNode) ( filteredSourceTree.getSourceTree().getSelectionPath().getLastPathComponent());
                            ((DefaultTreeModel) ( filteredSourceTree.getSourceTree().getModel())).nodeChanged(tn);
                            tfAlias.getDocument().addDocumentListener(this);
                            tfAlias.requestFocus();
                        }
                    }
                }
            }
        });
       
        filteredSourceTree.getSourceTree().addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent e) {
                Object o =  filteredSourceTree.getSelectedSourceOrCollection();
                filteredSourceTree.getSourceTree().removeTreeSelectionListener(this);
                if ((o instanceof DataSource) && e.isAddedPath()) {
                    tfAlias.setEnabled(true);
                    lAlias.setEnabled(true);
                    tfAlias.setEditable(true);
                    String alias = DataInfo.getAlias(o);
                    tfAlias.setText(alias == null ? "" : alias);
                    tfAlias.requestFocus();
                } else {
                    tfAlias.setEnabled(false);
                    lAlias.setEnabled(false);
                    tfAlias.setEditable(false);
                    tfAlias.setText("");
                }
                filteredSourceTree.getSourceTree().addTreeSelectionListener(this);
            }
        });
       
        // wait for a selection
        tfAlias.setEnabled(false);
        tfAlias.setEditable(false);
        lAlias.setEnabled(false);

        addElement(sourcePane, resources.getStringValue("sources"));
       
       

        // SHAPE PANE
        shapesListModel = new ShapeListModel();

        // Add shape creators
        for (Iterator it = Run.plugins.iterator(); it.hasNext();) {
            Plugin p = (Plugin) it.next();
            List shapeCreators = p.getShapeCreators();
            if (shapeCreators != null){
                for (int i=0;i<shapeCreators.size();i++){
                    shapesListModel.addShapeCreator( (ShapeCreator)shapeCreators.get(i));
                }
            }
        }

        // Create the stack panel that displays the shape list model
        StackPanel shapePanel = new StackPanel(shapesListModel);
        addElement(shapePanel, resources.getStringValue("shapes"));
    }

    public SourceTree getSourceTree() {
        return  (SourceTree)filteredSourceTree.getSourceTree();
    }

    /**
     * Returns the relevant object for the selection, that is: - A DataSource or
     * DataSourceCollection when using the source tree - A Shape otherwise
     *
     * @return null if nothing selected or a list of selected objects
     */
    public AbstractList getSelectionList() {
        AbstractList res = null;
        if (isVisible(resources.getStringValue("shapes"))) {
           
            ShapePageModel pm = (ShapePageModel)shapesListModel.getSelectedPage();
            if (pm!= null){
                ShapeCreator sc = pm.getSelectedShapeCreator();
                if (sc != null){
                   
                    if (res == null) {
                        res = new ArrayList();
                    }
                   
                    res.add(sc);
               
            }
 
        }else if (isVisible(resources.getStringValue("sources"))) {
            Object o = filteredSourceTree.getSelectedSourceOrCollection();
            if (o != null) {
                if (res == null) {
                    res = new ArrayList();
                }
                res.add(o);
            }
        }
        return res;
    }



   
}
TOP

Related Classes of jsynoptic.ui.Panes

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.