    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/tools/app3d/Export3DFile.java $
    002    /*----------------------------------------------------------------------------
    003     This file is part of deegree, http://deegree.org/
    004     Copyright (C) 2001-2009 by:
    005       Department of Geography, University of Bonn
    006     and
    007       lat/lon GmbH
    008
    009     This library is free software; you can redistribute it and/or modify it under
    010     the terms of the GNU Lesser General Public License as published by the Free
    011     Software Foundation; either version 2.1 of the License, or (at your option)
    012     any later version.
    013     This library is distributed in the hope that it will be useful, but WITHOUT
    014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
    016     details.
    017     You should have received a copy of the GNU Lesser General Public License
    018     along with this library; if not, write to the Free Software Foundation, Inc.,
    019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    020
    021     Contact information:
    022
    023     lat/lon GmbH
    024     Aennchenstr. 19, 53177 Bonn
    025     Germany
    026     http://lat-lon.de/
    027
    028     Department of Geography, University of Bonn
    029     Prof. Dr. Klaus Greve
    030     Postfach 1147, 53001 Bonn
    031     Germany
    032     http://www.geographie.uni-bonn.de/deegree/
    033
    034     e-mail: info@deegree.org
    035    ----------------------------------------------------------------------------*/
    036
    037    package org.deegree.tools.app3d;
    038
    039    import java.awt.FlowLayout;
    040    import java.awt.GridBagConstraints;
    041    import java.awt.GridBagLayout;
    042    import java.awt.event.ActionEvent;
    043    import java.awt.event.ActionListener;
    044    import java.lang.reflect.Constructor;
    045    import java.util.HashMap;
    046    import java.util.Map;
    047
    048    import javax.media.j3d.BranchGroup;
    049    import javax.swing.ButtonGroup;
    050    import javax.swing.JButton;
    051    import javax.swing.JCheckBox;
    052    import javax.swing.JComponent;
    053    import javax.swing.JDialog;
    054    import javax.swing.JLabel;
    055    import javax.swing.JOptionPane;
    056    import javax.swing.JPanel;
    057    import javax.swing.JRadioButton;
    058    import javax.swing.JTextField;
    059
    060    import org.deegree.framework.log.ILogger;
    061    import org.deegree.framework.log.LoggerFactory;
    062
    063    /**
    064     * <code>Export3DFile</code> shows a dialog to the user in which export parameters can be set, and calls the
    065     * appropriate export method.
    066     *
    067     * @author <a href="mailto:bezema@lat-lon.de">Rutger Bezema</a>
    068     * @author last edited by: $Author: mschneider $
    069     *
    070     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
    071     */
    072    public class Export3DFile {
    073        private static ILogger LOG = LoggerFactory.getLogger( Export3DFile.class );
    074
    075        /**
    076         * Convenient map, which holds the mapping of the parameters to the components
    077         */
    078        Map<String, JComponent> paramFields;
    079
    080        /**
    081         * Holds all parameters set by the user
    082         */
    083        Map<String, String> resultMap;
    084
    085        /**
    086         * Holds all parameters which can be given to the exporter.
    087         */
    088        Map<String, String> originalMap;
    089
    090        /**
    091         * Will be true if the parameter dialog was ended with the 'ok' button.
    092         */
    093        boolean okClicked = false;
    094
    095        // private JDialog parameterDialog = null;
    096
    097        /**
    098         * the parent of all dialogs.
    099         */
    100        private View3DFile parent;
    101
    102        /**
    103         * A list of class which are able to export and their description.
    104         */
    105        Map<String, String> exportClasses = new HashMap<String, String>();
    106
    107        /**
    108         * Will hold a class name of the exporter selected by the user (set in the anonymous Actionlistener of the the
    109         * ok-button of the ExportDialog)
    110         */
    111        String selectedExporter = null;
    112
    113        /**
    114         * A button group to handle to activation of only one exporter.
    115         */
    116        ButtonGroup exporterGroup = new ButtonGroup();
    117
    118        /**
    119         * Shows the export dialog (wizard)
    120         */
    121        JDialog exportDialog;
    122
    123        /**
    124         * @param parent
    125         *            of the dialog
    126         *
    127         */
    128        public Export3DFile( View3DFile parent ) {
    129            this.resultMap = new HashMap<String, String>();
    130            this.paramFields = new HashMap<String, JComponent>();
    131            this.parent = parent;
    132
    133            findAvailableExports();
    134            createExportDialog();
    135        }
    136
    137        /**
    138         *
    139         */
    140        private void findAvailableExports() {
    141            exportClasses.put( new J3DToCityGMLExporter().getShortDescription(),
    142                               J3DToCityGMLExporter.class.getCanonicalName() );
    143        }
    144
    145        private void createExportDialog() {
    146            JPanel yesNoPanel = new JPanel();
    147            JButton tmpButton = new JButton( "yes" );
    148            tmpButton.addActionListener( new ActionListener() {
    149                public void actionPerformed( ActionEvent e ) {
    150                    if ( selectedExporter == null ) {
    151                        JOptionPane.showMessageDialog( exportDialog, "Please select one of the exporters." );
    152                    } else {
    153                        exportDialog.setVisible( false );
    154                    }
    155                }
    156            } );
    157            yesNoPanel.add( tmpButton );
    158            tmpButton = new JButton( "cancel" );
    159            tmpButton.addActionListener( new ActionListener() {
    160                public void actionPerformed( ActionEvent e ) {
    161                    selectedExporter = null;
    162                    exportDialog.setVisible( false );
    163                }
    164
    165            } );
    166            yesNoPanel.add( tmpButton );
    167            JPanel exportPane = new JPanel( new GridBagLayout() );
    168            GridBagConstraints gb = new GridBagConstraints();
    169            gb.gridx = 0;
    170            gb.gridy = 0;
    171
    172            for ( String desc : exportClasses.keySet() ) {
    173                JRadioButton exporter = new JRadioButton( desc );
    174                exporter.addActionListener( new ActionListener() {
    175                    public void actionPerformed( ActionEvent e ) {
    176                        JRadioButton jb = (JRadioButton) e.getSource();
    177                        if ( jb.isSelected() ) {
    178                            selectedExporter = exportClasses.get( jb.getText() );
    179                            jb.setSelected( false );
    180                        }
    181                    }
    182
    183                } );
    184                gb.gridy++;
    185                exportPane.add( exporter, gb );
    186                exporterGroup.add( exporter );
    187            }
    188            exportDialog = new JDialog( parent, true );
    189            exportDialog.getContentPane().setLayout( new GridBagLayout() );
    190            gb.gridy = 0;
    191            exportDialog.getContentPane().add( exportPane, gb );
    192            gb.gridy++;
    193            exportDialog.getContentPane().add( yesNoPanel, gb );
    194            exportDialog.pack();
    195            exportDialog.setVisible( false );
    196        }
    197
    198        /**
    199         *
    200         * @param toExport
    201         *            the actual branchgroup
    202         * @return the String representation of the exported branch group.
    203         */
    204        public StringBuilder exportBranchgroup( BranchGroup toExport ) {
    205            StringBuilder result = new StringBuilder( 20000 );
    206            this.selectedExporter = null;
    207            exportDialog.setLocationRelativeTo( parent );
    208            // will set the selectedExporter
    209            exportDialog.setVisible( true );
    210            if ( selectedExporter != null ) {
    211                J3DExporter tmpExporter = null;
    212                try {
    213                    Class<?> c = Class.forName( selectedExporter );
    214                    c.asSubclass( J3DExporter.class );
    215                    Constructor<?> cons = c.getConstructor();
    216                    tmpExporter = (J3DExporter) cons.newInstance();
    217                } catch ( Exception e ) {
    218                    LOG.logError( e.getMessage(), e );
    219                    parent.showExceptionDialog( "Could not create an exporter instance because:\n" + e.getMessage() );
    220                    return result;
    221                }
    222                JDialog parameterDialog = createParameterDialog( tmpExporter );
    223                parameterDialog.setLocationRelativeTo( parent );
    224                originalMap = tmpExporter.getParameterMap();
    225                parameterDialog.setVisible( true );
    226                if ( isOkClicked() ) {
    227                    try {
    228                        Constructor<?> constructor = tmpExporter.getClass().getConstructor( Map.class );
    229                        tmpExporter = (J3DExporter) constructor.newInstance( resultMap );
    230                        toExport.detach();
    231                        tmpExporter.export( result, toExport );
    232                    } catch ( Exception e ) {
    233                        LOG.logError( e.getMessage(), e );
    234                        parent.showExceptionDialog( "Could not create an exporter instance because:\n" + e.getMessage() );
    235                    }
    236                }
    237                originalMap = null;
    238            }
    239            return result;
    240        }
    241
    242        /**
    243         * Creates a parameter dialog which by clicking ok sets all the values fromt the original map to the result map if
    244         * they were changed by the user.
    245         *
    246         * @param exporter
    247         * @return the dialog.
    248         */
    249        private JDialog createParameterDialog( J3DExporter exporter ) {
    250            final JDialog parameterDialog = new JDialog( parent, true );// JOptionPane.QUESTION_MESSAGE,
    251            JPanel yesNoPanel = new JPanel();
    252            JButton tmpButton = new JButton( "yes" );
    253            tmpButton.addActionListener( new ActionListener() {
    254                public void actionPerformed( ActionEvent e ) {
    255                    okClicked = true;
    256                    for ( String key : originalMap.keySet() ) {
    257                        String value = originalMap.get( key );
    258                        if ( value != null && !"".equals( value.trim() ) ) {
    259                            JComponent paramField = paramFields.get( key );
    260                            if ( paramField != null ) {
    261                                String userInput = key;
    262                                if ( paramField instanceof JTextField ) {
    263                                    userInput = ( (JTextField) paramField ).getText();
    264                                } else if ( paramField instanceof JCheckBox ) {
    265                                    userInput = ( (JCheckBox) paramField ).isSelected() ? "yes" : "no";
    266                                }
    267                                if ( !value.equalsIgnoreCase( userInput ) ) {
    268                                    resultMap.put( key, userInput );
    269                                }
    270                            }
    271                        }
    272                    }
    273                    parameterDialog.setVisible( false );
    274                }
    275            } );
    276            yesNoPanel.add( tmpButton );
    277            tmpButton = new JButton( "cancel" );
    278            tmpButton.addActionListener( new ActionListener() {
    279                public void actionPerformed( ActionEvent e ) {
    280                    parameterDialog.setVisible( false );
    281                }
    282            } );
    283            yesNoPanel.add( tmpButton );
    284            Map<String, String> parameterMap = exporter.getParameterMap();
    285            JPanel parameterPane = new JPanel( new GridBagLayout() );
    286            GridBagConstraints gb = new GridBagConstraints();
    287            gb.gridx = 0;
    288            gb.gridy = 0;
    289            gb.anchor = GridBagConstraints.WEST;
    290            for ( String param : parameterMap.keySet() ) {
    291                JLabel label = new JLabel( param + ": " );
    292                JComponent parameter = null;
    293                if ( parameterMap.get( param ).contains( "(yes/no)" ) ) {
    294                    parameter = new JCheckBox();
    295                } else {
    296                    parameter = new JTextField( parameterMap.get( param ), 20 );
    297                }
    298                JPanel totalField = new JPanel( new FlowLayout() );
    299                totalField.add( label );
    300                totalField.add( parameter );
    301                gb.gridy++;
    302                gb.weightx = 1;
    303                parameterPane.add( totalField, gb );
    304                paramFields.put( param, parameter );
    305            }
    306
    307            // JOptionPane.YES_NO_CANCEL_OPTION,
    308            // null, buttons, buttons[0] );
    309            parameterDialog.getContentPane().setLayout( new GridBagLayout() );
    310            gb.gridy = 0;
    311            parameterDialog.getContentPane().add( parameterPane, gb );
    312            gb.gridy++;
    313            parameterDialog.getContentPane().add( yesNoPanel, gb );
    314            parameterDialog.pack();
    315            parameterDialog.setVisible( false );
    316            return parameterDialog;
    317        }
    318
    319        /**
    320         * @return the resultMap.
    321         */
    322        public Map<String, String> getResultMap() {
    323            return resultMap;
    324        }
    325
    326        /**
    327         * @return the okClicked.
    328         */
    329        public boolean isOkClicked() {
    330            return okClicked;
    331        }
    332
    333    }