001    /**###########################################################################
002       ##                                                                          ##
003       ## JQuickPlot - Java Quick XY Plots                                         ##
004       ##                                                                          ##
005       ## Copyright (C) 2008  Frederic Roudaut  <frederic.roudaut@free.fr>         ##
006       ##                                                                          ##
007       ##                                                                          ##
008       ## This program is free software; you can redistribute it and/or modify it  ##
009       ## under the terms of the GNU General Public License version 2 as           ##
010       ## published by the Free Software Foundation; version 2.                    ##
011       ##                                                                          ##
012       ## This program is distributed in the hope that it will be useful, but      ##
013       ## WITHOUT ANY WARRANTY; without even the implied warranty of               ##
014       ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        ##
015       ## General Public License for more details.                                 ##
016       ##                                                                          ##
017       ############################################################################**/
018    
019    package JQuickPlot;
020    
021    import org.jdesktop.layout.GroupLayout;
022    import org.jdesktop.layout.*;
023    import javax.swing.*;
024    import java.awt.event.*;
025    import java.awt.*;
026    import java.io.*;
027    
028    /**
029     * The Class used for the global plots configuration. Each plot may contains several subplot (ie graph) that may contain several curves.
030     *
031     */
032    public class mainConfiguration extends JPanel  implements ActionListener {
033    
034        private JToggleButton invokePopupButton;
035        private JFrame curveConfMenu;
036        private JTextField textFieldX = new JTextField();
037        private String _plotRefName = "";
038        private String []_labelX;
039    
040        
041        /**
042         * Constructor.
043         *
044         * @param plotRefName : Name to reference the subplot
045         * @param labelX  : Reference on the X column to use for the plot
046         **/
047        public mainConfiguration(String plotRefName, String []labelX) {
048            _plotRefName = plotRefName;
049            _labelX = labelX;
050            init();
051        }
052        
053    
054        /**
055         * Initialiser for the Main configuration Menu.
056         */
057        private void init() {
058            
059            invokePopupButton = new JToggleButton(_plotRefName);
060            invokePopupButton.addActionListener(new ActionListener() {
061                    
062                    public void actionPerformed(ActionEvent e) {
063                        
064                        if (!curveConfMenu.isVisible()) {
065                            
066                            // set location relative to button
067                            Point location = invokePopupButton.getLocation();
068                            SwingUtilities.convertPointToScreen(location, invokePopupButton.getParent());
069                            location.translate(0, invokePopupButton.getHeight()
070                                               + (invokePopupButton.getBorder() == null ? 0
071                                                  : invokePopupButton.getBorder().getBorderInsets(invokePopupButton).bottom));
072                            curveConfMenu.setLocation(location);
073                            
074                            // show the popup if not visible
075                            curveConfMenu.setVisible(true);
076                            curveConfMenu.requestFocus();
077                        } 
078                        
079                        else {
080                            // hide it otherwise
081                            curveConfMenu.setVisible(false);
082                            curveConfMenu.requestFocus();
083                        }
084                    }
085                }
086                );
087            
088            // add components to main panel
089            this.setLayout(new BorderLayout());
090            this.add(invokePopupButton, BorderLayout.CENTER);
091            
092            // use frame
093            curveConfMenu = new JFrame("[=] Main Configuration [=]");
094            
095            // Construction of the Options Panel
096            JLabel labelInfo1 = new JLabel("JQuickPlot v0.9 - Java Quick XY Plots");
097            JLabel labelInfo2 = new JLabel("Copyright (C) 2008  Frederic Roudaut  <frederic.roudaut@free.fr>");
098            JLabel labelX = new JLabel("X Label");
099            textFieldX.setColumns(20);
100            
101            JButton buttonSave = new JButton("Save");
102            buttonSave.setActionCommand("SAVE");
103            buttonSave.addActionListener(this);       
104    
105            JButton buttonClose = new JButton("Close");
106            buttonClose.setActionCommand("CLOSE");
107            buttonClose.addActionListener(this);       
108    
109            GroupLayout layout = new GroupLayout(curveConfMenu.getContentPane());
110            curveConfMenu.getContentPane().setLayout(layout);
111            layout.setAutocreateGaps(true);
112            layout.setAutocreateContainerGaps(true);
113            
114            layout.setHorizontalGroup(layout.createParallelGroup()
115                                      .add(layout.createParallelGroup()                    
116                                           .add(labelInfo1)
117                                           .add(labelInfo2)
118                                           )
119                                      .add(layout.createSequentialGroup()                  
120                                           
121                                           .add(layout.createParallelGroup(GroupLayout.LEADING)
122                                                .add(labelX)
123                                                )
124                                           
125                                           .add(layout.createParallelGroup(GroupLayout.CENTER)
126                                                .add(textFieldX)
127                                                .add(layout.createSequentialGroup()
128                                                     .add(buttonSave)
129                                                     .add(buttonClose)
130                                                     )
131                                                )                             
132                                           )
133                                      );
134           
135            layout.setVerticalGroup(layout.createSequentialGroup()
136                                    .add(labelInfo1)
137                                    .add(labelInfo2)
138                                    .add(layout.createParallelGroup(GroupLayout.BASELINE)
139                                         .add(labelX)
140                                         .add(textFieldX)
141                                         )
142                                    .add(layout.createParallelGroup(GroupLayout.LEADING)
143                                         .add(buttonSave)
144                                         .add(buttonClose)
145                                         )
146                                    );
147            curveConfMenu.pack();
148        }
149            
150        
151        /**
152         * Handles a click on the Save/Close buttons.
153         *
154         * @param e  the action event.
155         */   
156        public void actionPerformed(ActionEvent e) {
157            
158            if (e.getActionCommand().equals("CLOSE")) {                 
159                curveConfMenu.setVisible(false);                            
160            }
161    
162            else if (e.getActionCommand().equals("SAVE")) { 
163                _labelX[0] = textFieldX.getText();
164                curveConfMenu.setVisible(false);                            
165            }
166        }  
167    }