Package jsynoptic.plugins.java3d.panels

Source Code of jsynoptic.plugins.java3d.panels.SceneGraphObjectDialog$RemoveListener

/* ========================
* 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: SceneGraphObjectDialog.java,v 1.7 2008/10/23 17:21:05 cazenave Exp $
*
* Changes
* -------
* 31 janv. 08  : Initial public release
*
*/
package jsynoptic.plugins.java3d.panels;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Frame;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.UndoableEditEvent;

import jsynoptic.plugins.java3d.SceneGraphObjectHolder;
import jsynoptic.plugins.java3d.edit.SceneGraphObjectEdit;

/**
*
*/
public class SceneGraphObjectDialog extends JFrame {

    SceneGraphObjectPanel<?> _panel;
    final SceneGraphObjectHolder _holder;

    public SceneGraphObjectDialog(SceneGraphObjectHolder holder, final RemoveListener remove) {
        super();
        _holder=holder;
        _panel = null;
        JButton b = new JButton("OK"); // TODO I18N
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (_panel != null && _panel._editor != null && _holder.getUndoableEditListener()!=null) {
                    _holder.getUndoableEditListener().undoableEditHappened(
                            new UndoableEditEvent(SceneGraphObjectDialog.this, _panel._editor));
                }
                close();
            }
        });
        JPanel p = new JPanel();
        p.add(b);
        if(remove!=null){
            JButton r = new JButton("REMOVE"); // TODO I18N
            r.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    remove.remove();
                    close();
                }
            });
            p.add(r);
        }
        JButton u = new JButton("CANCEL"); // TODO I18N
        u.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (_panel != null && _panel._editor != null) {
                    _panel._editor.undo();
                }
                close();
            }
        });
        p.add(u);
        getContentPane().add(BorderLayout.SOUTH, p);
    }

    public static interface RemoveListener {
        public void remove();
    }
   
    public SceneGraphObjectEdit<?> getEditor() {
        if (_panel != null) {
            return _panel._editor;
        }
        return null;
    }

    public void set(SceneGraphObjectPanel<?> panel) {
        _panel = panel;
        setTitle(_panel._editor.getPresentationName());
        getContentPane().add(BorderLayout.CENTER, _panel);
        pack();
        setVisible(true);
    }

    private void cleanComponent(Container p) {
        for (Component c : p.getComponents()) {
            if (c instanceof Container) {
                cleanComponent((Container) c);
            }
            if (c instanceof PropertiesPanel.Editor) {
                PropertiesPanel.Editor<?, ?> pe = (PropertiesPanel.Editor<?, ?>) c;
                pe.edit(null);
            }
        }
    }

    public void close() {
        if (_panel != null) {
            cleanComponent(_panel);
        }
        dispose();
    }

    static HashMap<Class<?>, String> _editClassNames = new HashMap<Class<?>, String>();
    static HashMap<Class<?>, String> _panelClassNames = new HashMap<Class<?>, String>();

    /**
     * Register edit class and panel class for a scene graph object not managed
     * by classes in jsynoptic.plugins.java3d.edit package and
     * jsynoptic.plugins.java3d.panels package
     *
     * @param c
     *            the scene graph object class name
     * @param editClassName
     *            the SceneGraphObjectEdit subclass name
     * @param panelClassName
     *            the SceneGraphObjectPanel subclass name
     */
    static void register(Class<?> c, String editClassName, String panelClassName) {
        _editClassNames.put(c, editClassName);
        _panelClassNames.put(c, panelClassName);
    }

    @SuppressWarnings("unchecked")
    private static <T> Class<T> getClass(Class<?> c, HashMap<Class<?>, String> map,
            String prefix, String suffix,Class<?>[] ct){
        String baseName = c.getName();
        int k = baseName.lastIndexOf('.');
        if (k > 0) {
            baseName = baseName.substring(k + 1);
        }
        String className=map.get(c);
        if(className==null){
            className=prefix+baseName+suffix;
        }
        try{
            Class<T> res = (Class<T>) Class.forName(className);
            ct[0]=c;
            return res;
        }
        catch (ClassNotFoundException e) {
            Class<?> up=c.getSuperclass();
            if(up==null || up==Object.class){
                return null;
            }
            return getClass(up, map, prefix, suffix,ct);
        }
    }
   
    public static SceneGraphObjectDialog createDialog(SceneGraphObjectHolder holder,
                                            Point location, RemoveListener remove) {
        SceneGraphObjectDialog res = new SceneGraphObjectDialog(holder, remove);
        if(location!=null){
            res.setLocation(location);
        }
        Class<?>[] ct=new Class<?>[1];
        Class<SceneGraphObjectEdit<?>> c =getClass(holder.getSceneGraphObject().getClass(),_editClassNames,
                "jsynoptic.plugins.java3d.edit.","Edit",ct);
        if(c==null){
            System.err.println("Can not get Edit class for object with class="+holder.getSceneGraphObject().getClass());
            res.close();
            return null;
        }
        Class<?>[] cp=new Class<?>[1];
        Class<SceneGraphObjectPanel<?>> p =getClass(holder.getSceneGraphObject().getClass(),_panelClassNames,
                "jsynoptic.plugins.java3d.panels.","Panel",cp);
        if(p==null){
            System.err.println("Can not get Panel class for object with class="+holder.getSceneGraphObject().getClass());
            res.close();
            return null;
        }
        SceneGraphObjectEdit<?> edit=null;
        try {
            Constructor<SceneGraphObjectEdit<?>> ctor = c.getConstructor(ct[0]);
            edit = ctor.newInstance(holder.getSceneGraphObject());
            edit.setHolder(holder);
            Constructor<SceneGraphObjectPanel<?>> ctorp;
            try{
                 ctorp= p.getConstructor(Frame.class, edit.getClass());
            }
            catch(NoSuchMethodException ne){
                ctorp=p.getConstructor(Frame.class, edit.getClass().getSuperclass());
            }
            SceneGraphObjectPanel<?> panel = ctorp.newInstance(res, edit);
            res.set(panel);
            return res;

        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        if(edit==null){
            System.err.println("Can not instantiate edit class="+c.getName()+" for object with class="+holder.getSceneGraphObject().getClass());
        }
        else{
            System.err.println("Can not instantiate panel class="+p.getName()+" for object with class="+holder.getSceneGraphObject().getClass());
        }
        res.close();
        return null;
    }
}
TOP

Related Classes of jsynoptic.plugins.java3d.panels.SceneGraphObjectDialog$RemoveListener

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.