Package jsynoptic.plugins.java3d.panels

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

/* ========================
* 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-2007, by :
*     Corporate:
*         EADS Astrium
*     Individual:
*         Claude Cazenave
*
* $Id: ColorButton.java,v 1.1 2008/02/11 14:09:28 cazenave Exp $
*
* Changes
* -------
* 21 janv. 08  : Initial public release
*
*/
package jsynoptic.plugins.java3d.panels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.media.j3d.SceneGraphObject;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.vecmath.Color3f;

import jsynoptic.plugins.java3d.edit.PropertyEdit;
import simtools.ui.ColorChooserProvider;

public class ColorButton <T extends SceneGraphObject> extends JButton implements
                    PropertyEdit.UndoRedoListener,
                    PropertiesPanel.Editor<T, Color3f>,
                    PropertiesPanel.NeedsDialog {
    static final Color3f DEFAULT_COLOR = new Color3f(0.f, 0.f, 0.f);

    Color _color;
   
    PropertyEdit<T, Color3f> _editor;

    String _title;
   
    Frame _owner;
   
    public ColorButton() {
        super("    ");
        _color = DEFAULT_COLOR.get();
        setBackground(_color);
        _editor=null;
        _title=null;
        _owner=null;
        setFocusPainted(false);
        addActionListener(new ActionListener() {
            boolean _dialogOk;

            public void actionPerformed(ActionEvent e) {
                final JDialog dialog = new JDialog(_owner, true);
                dialog.setTitle(_title);
                JColorChooser chooser = ColorChooserProvider.colorChooserProvider.getColorChooser();
                dialog.getContentPane().add(chooser);
                JButton cancel, ok;
                JPanel p = new JPanel();
                // TODO i18n
                p.add(ok = new JButton("OK"));
                p.add(cancel = new JButton("Cancel"));
                _dialogOk = false;
                ok.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        _dialogOk = true;
                        dialog.dispose();
                    }
                });
                cancel.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        dialog.dispose();
                    }
                });
                dialog.getContentPane().add(p, BorderLayout.SOUTH);
                dialog.pack();
                dialog.setVisible(true);
                if (_dialogOk) {
                    set(chooser.getColor());
                }
            }
        });
    }

    void set(Color c) {
        _color = c;
        setBackground(_color);
        if(_editor!=null){
            _editor.setNewValue(new Color3f(_color));
        }
    }

    void set(Color3f c) {
        set(c.get());
    }

    Color3f get() {
        return new Color3f(_color);
    }
   
    @Override
    public void edit(PropertyEdit<T,Color3f> editor){
        if(_editor!=null){
            _editor.removeListener(this);
        }
        _editor=editor;
        if(_editor!=null){
            _editor.addListener(this);
            _color = _editor.getPropertyValue().get();
            setBackground(_color);
        }
    }

    @Override
    public void undoRedoPerfomed(boolean isUndo) {
        _color = _editor.getPropertyValue().get();
        setBackground(_color);
    }

    @Override
    public void set(String title, Frame owner) {
        _title=title;
        _owner=owner;
    }   
}
TOP

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

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.