Package jsynoptic.builtin.ui

Source Code of jsynoptic.builtin.ui.TextArrayPropertiesPanel

/* ========================
* 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-2004, by :
*     Corporate:
*         EADS Astrium SAS
*         EADS CRC
*     Individual:
*         Nicolas Brodu
*        Jean-Baptiste Lièvremont
*
* $$Id: TextArrayPropertiesPanel.java,v 1.12 2008/07/22 09:25:17 ogor Exp $$
*
* Changes
* -------
*
*/
package jsynoptic.builtin.ui;

import java.awt.CardLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPanel;

import jsynoptic.builtin.Builtin;
import jsynoptic.builtin.TextArrayShape;
import simtools.ui.ActionCheckBox;
import simtools.ui.JPropertiesPanel;
import simtools.ui.MenuResourceBundle;
import simtools.ui.GridBagPanel;
import simtools.ui.ResourceFinder;

public class TextArrayPropertiesPanel extends JPropertiesPanel {
  
    protected JPanel cards;
    protected JComboBox cbx;
    protected ActionCheckBox cbSameFont;
    protected JButton bcelladd, bcelldelete;
    protected Vector textPanels;
    boolean comboIsVisible;

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

    /**
     * @param shapeName
     * @param displayCombo :
     *            if true,
     */
    public TextArrayPropertiesPanel(String shapeName, boolean comboIsVisible) {
        super(shapeName);
        textPanels = new Vector();
        this.comboIsVisible = comboIsVisible;
       
        addOnCurrentRow(createContents(), 2, true, true, true);
    }

    /**
     * By overwriting this method, it is possible to create different kind of text properties panel
     * @param name
     * @param width
     * @param height
     * @return a text s properties panel
     */
    protected TextPropertiesPanel createTextPropertiesPanel(boolean showTransform, String shapeName) {
        return new TextPropertiesPanel(showTransform, shapeName);
    }

    protected JComponent createContents() {
        bcelladd = resources.getButton("addCellButton", null);
        bcelldelete = resources.getButton("deleteCellButton", null);
        cbx = new JComboBox();
        cbx.setEditable(false);
        cbx.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CardLayout cl = (CardLayout) (cards.getLayout());
                int index = cbx.getSelectedIndex();
                if (index >= 0) {
                    TextPropertiesPanel tpp = (TextPropertiesPanel) TextArrayPropertiesPanel.this.textPanels.get(index);
                    tpp.updateSourceTree();
                    cl.show(cards, Integer.toString(index));
                }
            }
        });
        bcelldelete.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int index = cbx.getSelectedIndex();
                textPanels.remove(index);
                updateCells();
                // Disable delete button in case there is only one cell.
                bcelldelete.setEnabled(textPanels.size() > 1);
                if (index > (textPanels.size() - 1)) {
                    index = textPanels.size() - 1;
                }
                TextPropertiesPanel tpp = (TextPropertiesPanel) TextArrayPropertiesPanel.this.textPanels.get(index);
                tpp.updateSourceTree();
                ((CardLayout) (cards.getLayout())).show(cards, Integer.toString(index));
            }
        });
        bcelladd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JPropertiesPanel textPanel = createTextPropertiesPanel(false,Builtin.resources.getString("TextArray") );
                // New cell has same properties as the last cell created
                JPropertiesPanel textPanelModel = (JPropertiesPanel) textPanels.get(textPanels.size() - 1);
                String[] props = textPanelModel.getPropertyNames();
                if (props != null) {
                    for (int j = 0; j < props.length; j++) {
                        String pname = props[j];
                        textPanel.setPropertyValue(pname, textPanelModel.getPropertyValue(pname));
                    }
                }
                // fin test
                // Set new panel owner
                textPanel.setOwner(TextArrayPropertiesPanel.this.getOwner());
                // Add to the list of text cells panels
                textPanels.add(textPanel);
                // Disable delete button in case there is only one cell.
                bcelldelete.setEnabled(textPanels.size() > 1);
                updateCells();
                int index = textPanels.size() - 1;
                cbx.setSelectedIndex(index);
                TextPropertiesPanel tpp = (TextPropertiesPanel) TextArrayPropertiesPanel.this.textPanels.get(index);
                tpp.updateSourceTree();
                ((CardLayout) (cards.getLayout())).show(cards, Integer.toString(index));
                enableFontChoosers(!cbSameFont.isSelected());
            }
        });
        cards = new JPanel();
        cards.setLayout(new CardLayout());
        cbSameFont = new ActionCheckBox(resources.getString("UseAUniqueFontForTheWholeArray"), true) {
            public void actionPerformed(ActionEvent e) {
                if (cbSameFont.isSelected()) {
                    JPropertiesPanel jp = (JPropertiesPanel) TextArrayPropertiesPanel.this.textPanels.get(cbx
                            .getSelectedIndex());
                    for (int i = 0; i < TextArrayPropertiesPanel.this.textPanels.size(); i++) {
                        JPropertiesPanel jpi = (JPropertiesPanel) TextArrayPropertiesPanel.this.textPanels.get(i);
                        if (jpi != jp) {
                            jpi.setPropertyValue("FONT", jp.getPropertyValue("FONT"));
                        }
                    }
                }
                enableFontChoosers(!cbSameFont.isSelected());
            }
        };
        // Add panel
        GridBagPanel textArrayPanel = new GridBagPanel();
        textArrayPanel.addOnCurrentRow(cbx, 2);
        textArrayPanel.addOnCurrentRow(bcelladd);
        textArrayPanel.addOnCurrentRow(bcelldelete);
        textArrayPanel.carriageReturn();
        textArrayPanel.addOnCurrentRow(cards, 1, true, true, true);
        textArrayPanel.addOnCurrentRow(cbSameFont);
        textArrayPanel.carriageReturn();
        return textArrayPanel;
    }

    protected void enableFontChoosers(boolean enabled) {
        for (int i = 0; i < textPanels.size(); i++) {
            TextPropertiesPanel tpp = (TextPropertiesPanel) textPanels.get(i);
            tpp.fontChooserPanel.setEnabled(enabled);
            tpp.cbFrameLocked.setEnabled(enabled);
            tpp.cbFontLocked.setEnabled(enabled);
            tpp.cbMargin.setEnabled(enabled);
            tpp.nfCharNumber.setEnabled(enabled && tpp.cbFrameLocked.isSelected());
            tpp.lcharNumber.setEnabled(enabled && tpp.cbFrameLocked.isSelected());
        }
    }

    public void select(int index) {
        cbx.setSelectedIndex(index);
    }

    public String[] getPropertyNames() {
        if (_propertyNames == null) {
            _propertyNames = new TextArrayShape.TextArrayShapePropertiesNames().getPropertyNames();
        }
        return _propertyNames;
    }

    /*
     * (non-Javadoc)
     *
     * @see simtools.ui.JPropertiesPanel#getPropertyValue(java.lang.String)
     */
    public Object getPropertyValue(String name) {
        Object res = null;
        if (name.equalsIgnoreCase("TEXT_ARRAY_CELL_LIST")) {
            // Get cells properties from panels
            Vector cellsProperties = new Vector();
            for (int i = 0; i < textPanels.size(); i++) {
                Vector cellProperties = new Vector();
                TextPropertiesPanel textPropertiesPanel = (TextPropertiesPanel) textPanels.get(i);
                String[] props = textPropertiesPanel.getPropertyNames();
                if (props != null) {
                    for (int j = 0; j < props.length; j++) {
                        // Each property has a name and a value
                        Object[] property = new Object[2];
                        String pname = props[j];
                        Object value = textPropertiesPanel.getPropertyValue(pname);
                        property[0] = pname;
                        property[1] = value;
                        cellProperties.add(property);
                    }
                }
                cellsProperties.add(cellProperties);
            }
            res = cellsProperties;
        } else if (name.equalsIgnoreCase("SAME_FONT")) {
            if (cbSameFont != null) {
                res = new Boolean(cbSameFont.isSelected());
            } else {
                res = Boolean.FALSE;
            }
        }
        return res;
    }

    /*
     * (non-Javadoc)
     *
     * @see simtools.ui.JPropertiesPanel#setPropertyValue(java.lang.String,
     *      java.lang.Object)
     */
    public void setPropertyValue(String name, Object value) {
        if (name.equalsIgnoreCase("TEXT_ARRAY_CELL_LIST")) {
            if (value instanceof Vector) {
                textPanels.clear();
                // Build textPanels with cells properties
                Vector cellsProperties = (Vector) value;
                for (int i = 0; i < cellsProperties.size(); i++) {
                    Vector cellProperties = (Vector) cellsProperties.get(i);
                    JPropertiesPanel textPanel = createTextPropertiesPanel(false, Builtin.resources.getString("TextArray") );
                    for (int j = 0; j < cellProperties.size(); j++) {
                        Object[] property = (Object[]) cellProperties.get(j);
                        String pname = (String) property[0];
                        Object pvalue = property[1];
                        textPanel.setPropertyValue(pname, pvalue);
                    }
                    textPanels.add(textPanel);
                }
                if (comboIsVisible) {
                    updateCells();
                    bcelldelete.setEnabled(textPanels.size() > 1); // at least
                    // one cell
                    // ...
                    cbx.setSelectedIndex(0);
                    ((CardLayout) (cards.getLayout())).show(cards, Integer.toString(0));
                }
            }
        }
        if (name.equalsIgnoreCase("SAME_FONT")) {
            if (value instanceof Boolean) {
                if (cbSameFont != null) {
                    cbSameFont.setSelected(((Boolean) value).booleanValue());
                    enableFontChoosers(!cbSameFont.isSelected());
                }
            }
        }
    }

    /**
     * When textPanels vector has changed : - update textPanels name - update
     * Cards panel
     */
    protected void updateCells() {
        String t = resources.getString("Cell");
        cards.removeAll();
        cbx.removeAllItems();
        for (int i = 0; i < textPanels.size(); i++) {
            TextPropertiesPanel tpp = (TextPropertiesPanel) textPanels.get(i);
            cbx.addItem(t + " " + (i + 1));
            cards.add(tpp, Integer.toString(i));
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see simtools.ui.JPropertiesPanel#setOwner(javax.swing.JDialog)
     */
    public void setOwner(JDialog owner) {
        super.setOwner(owner);
        for (int i = 0; i < textPanels.size(); i++) {
            ((TextPropertiesPanel) textPanels.get(i)).setOwner(owner);
        }
    }
}
TOP

Related Classes of jsynoptic.builtin.ui.TextArrayPropertiesPanel

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.