Package jsynoptic.plugins.java3d.panels

Source Code of jsynoptic.plugins.java3d.panels.PrimitiveDialog

/* ========================
* 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-2008, by :
*     Corporate:
*         EADS Astrium
*     Individual:
*         Claude Cazenave
*
* $Id: PrimitiveDialog.java,v 1.5 2008/12/17 22:37:52 cazenave Exp $
*
* Changes
* -------
* 31 janv. 08  : Initial public release
*
*/
package jsynoptic.plugins.java3d.panels;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import simtools.ui.GridBagPanel;

import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.Cone;
import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.geometry.Sphere;

/**
*
*/
public class PrimitiveDialog extends JDialog {
    final Class<? extends Primitive> _primitiveClass;
   
    Primitive _result;
   
    float  _radius=1.f;
    float _height=1.f;
    float _depth=1.f;
    int _divisions=1;
    int _heightDivisions=1;
   
    JTextField _tfX;
    JTextField _tfY;
    JTextField _tfZ;
    JTextField _tfXDiv;
    JTextField _tfYDiv;
   
    GridBagPanel _panel;

    public PrimitiveDialog(Window windowAncestor, Point location, Class<? extends Primitive> primitiveClass, String title) {
        super(windowAncestor,title,Dialog.ModalityType.APPLICATION_MODAL);
        if(location!=null){
            setLocation(location);
        }
        _primitiveClass=primitiveClass;
        _result=null;
       
        JButton b = new JButton("OK"); // TODO I18N
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                getValues();
                createPrimitive();
                dispose();
            }
        });
        JPanel p = new JPanel();
        p.add(b);
        JButton u = new JButton("CANCEL"); // TODO I18N
        u.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        p.add(u);
        getContentPane().add(BorderLayout.SOUTH, p);
       
        createPanel();
        getContentPane().add(BorderLayout.CENTER, _panel);
        pack();
        setVisible(true);
    }

    public Primitive getResult(){
        return _result;
    }
   
    private void createPanel(){
        _panel=new GridBagPanel();
        if(_primitiveClass==Sphere.class){
            _divisions=16;
            _panel.addOnCurrentRow(new JLabel("Radius")); // TODO i18N
            _panel.addOnCurrentRow(_tfX=new JTextField(""+_radius,PointsDialog.NDIGIT));
            _panel.carriageReturn();
            _panel.addOnCurrentRow(new JLabel("Divisions")); // TODO i18N
            _panel.addOnCurrentRow(_tfXDiv=new JTextField(""+_divisions,3));
            _panel.carriageReturn();
        }
        else if(_primitiveClass==Cylinder.class || _primitiveClass==Cone.class){
            _height=2.f;
            _divisions=15;
            _panel.addOnCurrentRow(new JLabel("Radius")); // TODO i18N
            _panel.addOnCurrentRow(_tfX=new JTextField(""+_radius,PointsDialog.NDIGIT));
            _panel.addOnCurrentRow(new JLabel("Height")); // TODO i18N
            _panel.addOnCurrentRow(_tfY=new JTextField(""+_height,PointsDialog.NDIGIT));
            _panel.carriageReturn();
            _panel.addOnCurrentRow(new JLabel("Divisions")); // TODO i18N
            _panel.addOnCurrentRow(_tfXDiv=new JTextField(""+_divisions,3));
            _panel.addOnCurrentRow(new JLabel("Height divisions")); // TODO i18N
            _panel.addOnCurrentRow(_tfYDiv=new JTextField(""+_heightDivisions,3));
            _panel.carriageReturn();
        }
        else if(_primitiveClass==Box.class){
            _panel.addOnCurrentRow(new JLabel("X")); // TODO i18N
            _panel.addOnCurrentRow(_tfX=new JTextField(""+_radius,PointsDialog.NDIGIT));
            _panel.addOnCurrentRow(new JLabel("Y")); // TODO i18N
            _panel.addOnCurrentRow(_tfY=new JTextField(""+_height,PointsDialog.NDIGIT));
            _panel.addOnCurrentRow(new JLabel("Z")); // TODO i18N
            _panel.addOnCurrentRow(_tfZ=new JTextField(""+_depth,PointsDialog.NDIGIT));
            _panel.carriageReturn();
        }     
    }
   
    private void getValues(){
        if(_tfX!=null){
            try{
                _radius=Float.parseFloat(_tfX.getText());
            }
            catch(NumberFormatException nfe){
                _tfX.setText(""+_radius);
            }
        }
        if(_tfY!=null){
            try{
                _height=Float.parseFloat(_tfY.getText());
            }
            catch(NumberFormatException nfe){
                _tfY.setText(""+_height);
            }
        }
        if(_tfZ!=null){
            try{
                _depth=Float.parseFloat(_tfZ.getText());
            }
            catch(NumberFormatException nfe){
                _tfZ.setText(""+_depth);
            }
        }
        if(_tfXDiv!=null){
            try{
                _divisions=Integer.parseInt(_tfXDiv.getText());
            }
            catch(NumberFormatException nfe){
                _tfXDiv.setText(""+_divisions);
            }
        }
        if(_tfYDiv!=null){
            try{
                _heightDivisions=Integer.parseInt(_tfYDiv.getText());
            }
            catch(NumberFormatException nfe){
                _tfYDiv.setText(""+_heightDivisions);
            }
        }
   }
   
    private void createPrimitive(){
        int genFlag=Primitive.GENERATE_NORMALS|Primitive.GENERATE_TEXTURE_COORDS|Primitive.GEOMETRY_NOT_SHARED|Primitive.ENABLE_GEOMETRY_PICKING;
        if(_primitiveClass==Sphere.class){
            _result=new Sphere(_radius, genFlag , _divisions);
        }
        else if(_primitiveClass==Cylinder.class){
            _result=new Cylinder(_radius, _height, genFlag,
                    _divisions, _heightDivisions, null);
        }
        else if(_primitiveClass==Cone.class){
            _result=new Cone(_radius, _height, genFlag,
                    _divisions, _heightDivisions, null);
        }
        else if(_primitiveClass==Box.class){
            _result=new Box(_radius, _height, _depth, genFlag, null, 6);
        }
        else {
            try {
                _result=_primitiveClass.newInstance();
            } catch (InstantiationException e1) {
                throw new RuntimeException("Invalid Primitive class : "+_primitiveClass.getName(),e1);
            } catch (IllegalAccessException e1) {
                throw new RuntimeException("Invalid Primitive class : "+_primitiveClass.getName(),e1);
            }
        }
    }

}
TOP

Related Classes of jsynoptic.plugins.java3d.panels.PrimitiveDialog

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.