Package jsynoptic.base

Source Code of jsynoptic.base.GridShapesTemplate

/* ========================
* 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: GridShapesTemplate.java,v 1.1 2008/02/25 13:02:46 ogor Exp $
*
* Changes
* -------
* 25 february 2008 : Initial public release (CC);
*
*/
package jsynoptic.base;

import java.awt.Font;


import javax.swing.JLabel;
import javax.swing.JSpinner;

import javax.swing.SpinnerNumberModel;

import simtools.diagram.Resizable;
import simtools.shapes.AbstractShape;
import simtools.ui.MenuResourceBundle;
import simtools.ui.ResourceFinder;
import simtools.ui.UserProperties;

import jsynoptic.ui.Run;
import jsynoptic.ui.ShapesContainer;

/**
* An abstract template dedicated to create grid of shapes
* @author zxpletran007
*
*/
public abstract class GridShapesTemplate extends Template{

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

    protected static final int DEFAULT_COLUMN_NUMBER = 1;
    protected static final int DEFAULT_ROW_NUMBER = 3;

    protected static final int DEFAULT_COLUMN_MARGIN = 50;
    protected static final int DEFAULT_ROW_MARGIN = 30;

    protected static final int BORDER_X_MARGIN = 50;
    protected static final int BORDER_Y_MARGIN = 50;
   
    protected int nbRows;
    protected int nbColumns;

    protected int rowMargin;
    protected int colMargin;

    protected GridShapesTemplatePanel gridShapePanel;

    public GridShapesTemplate(){
        nbRows = DEFAULT_ROW_NUMBER;
        nbColumns = DEFAULT_COLUMN_NUMBER;
        rowMargin = DEFAULT_ROW_MARGIN;
        colMargin = DEFAULT_COLUMN_MARGIN;
       
        // get properties from configuration file
        UserProperties prop = Run.getProperties();
        if (prop!=null){
            nbRows = prop.getInt("jsynoptic.base.GridShapesTemplate.nbRows", DEFAULT_ROW_NUMBER);
            nbColumns = prop.getInt("jsynoptic.base.GridShapesTemplate.nbColumns", DEFAULT_COLUMN_NUMBER);
        }
      
        gridShapePanel = new GridShapesTemplatePanel();
    }


    public void setShapeContainer(ShapesContainer sc){
       
        // Cell size
        int height = sc.getComponent().getDiagramHeight() -BORDER_Y_MARGIN;
        int width =sc.getComponent().getDiagramWidth() - BORDER_X_MARGIN;
       
        int cellHeight = (height - (nbRows+1) * rowMargin) / nbRows;
        int cellWidth = (width - (nbColumns+1) * colMargin) / nbColumns;

        // cell index
        for(int cellRow=0; cellRow < nbRows; cellRow++){
            for(int cellColumn=0; cellColumn < nbColumns; cellColumn++) {
                int cellOx =  cellColumn*(cellWidth + colMargin) + BORDER_X_MARGIN;
                int cellOy =  cellRow*(cellHeight + rowMargin) + cellHeight + BORDER_Y_MARGIN;

                AbstractShape newShape =  createShape(cellOx, cellOy, width, height);

                // Resize the shape size
                if (newShape instanceof Resizable){
                    //the current size is withdrawn and the new size is added
                    ((Resizable)newShape).resize(((-newShape.getBounds().width)+ cellWidth),((-newShape.getBounds().height)+ cellHeight));
                }
                sc.addElement(newShape);
            }
        }
    }



    /**
     * Shall be overwriten in subclasses
     * Create and set a shape to be added into the grid
     * @param ox
     * @param oy
     * @param width
     * @param height
     * @return a new shape to be added into the grid
     */
    protected abstract AbstractShape createShape(int ox, int oy, int width, int height);


    protected class GridShapesTemplatePanel extends TemplatePanel {

        protected JSpinner nbRowInput, nbColInput;
        protected JLabel nbRowLabel, nbColLabel;

        public GridShapesTemplatePanel(){
            super(resources.getString("gridProperties"));

            nbColInput = new JSpinner(new SpinnerNumberModel(nbColumns,1,20,1));
            nbRowInput = new JSpinner(new SpinnerNumberModel(nbRows,1,20,1));

            nbRowLabel = new JLabel(resources.getString("nbRows"));
            nbColLabel = new JLabel(resources.getString("nbCols"));
           
            nbRowLabel.setFont(new Font("Dialog", 0, 12));
            nbColLabel.setFont(new Font("Dialog", 0, 12));

            addOnCurrentRow(nbColLabel);
            addOnCurrentRow(nbColInput);
            carriageReturn();
           
            addOnCurrentRow(nbRowLabel);
            addOnCurrentRow(nbRowInput);

            nbColInput.setValue(new Integer(nbColumns));
            nbRowInput.setValue(new Integer(nbRows));
        }

        public void getProperties(){
            nbColumns = ((Integer)nbColInput.getValue()).intValue();
            nbRows = ((Integer)nbRowInput.getValue()).intValue();
           
            // save properties
            UserProperties prop = Run.getProperties();
            if (prop!=null){
                prop.setInt("jsynoptic.base.GridShapesTemplate.nbColumns", nbColumns);
                prop.setInt("jsynoptic.base.GridShapesTemplate.nbRows", nbRows);
            }
        }
    }

}
TOP

Related Classes of jsynoptic.base.GridShapesTemplate

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.