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    /**
030     * The Class used to configure subplots. Each plot may contains several subplot (ie graph) that may contain several curves.
031     *
032     */
033    public class PlotConfiguration extends JPanel  implements ActionListener {
034    
035        private JToggleButton invokePopupButton;
036        private JFrame curveConfMenu;
037        private JTextField _textFieldFile = new JTextField();
038        private JTextField _textFieldX = new JTextField();
039        private JTextField _textFieldY = new JTextField();
040        private JTextField _textFieldLabelY = new JTextField();
041        private String _plotRefName = "";
042        private int _plotRefNum = -1;
043        private ColumnList[] _datasetColumnListX;
044        private ColumnList[] _datasetColumnListY;
045        private String [] _datasetFile; 
046        private String [] _labelY; 
047    
048        /**
049         * Constructor.
050         *
051         * @param plotRefName : Name to reference the subplot
052         * @param plotRefNum  : Number to reference the subplot
053         * @param datasetColumnListX : Reference on the X columns to use for the subplots
054         * @param datasetColumnListY : Reference on the Y columns to use for the subplots
055         * @param datasetFile : Reference on the dots Files for the subplots
056         **/
057        public PlotConfiguration(String plotRefName, int plotRefNum, ColumnList[] datasetColumnListX, ColumnList[] datasetColumnListY, String [] datasetFile, String [] labelY ) {
058            _plotRefName = plotRefName;
059            _plotRefNum = plotRefNum;
060            _datasetColumnListX = datasetColumnListX;
061            _datasetColumnListY = datasetColumnListY;
062            _datasetFile = datasetFile;
063            _labelY = labelY;
064            init();
065        }
066        
067    
068        /**
069         * Initialiser for the Plot configuration Menu.
070         */
071        private void init() {
072            
073            invokePopupButton = new JToggleButton(_plotRefName + _plotRefNum);
074            invokePopupButton.addActionListener(new ActionListener() {
075                    
076                    public void actionPerformed(ActionEvent e) {
077                        
078                        if (!curveConfMenu.isVisible()) {
079                            
080                            // set location relative to button
081                            Point location = invokePopupButton.getLocation();
082                            SwingUtilities.convertPointToScreen(location, invokePopupButton.getParent());
083                            location.translate(0, invokePopupButton.getHeight()
084                                               + (invokePopupButton.getBorder() == null ? 0
085                                                  : invokePopupButton.getBorder().getBorderInsets(invokePopupButton).bottom));
086                            curveConfMenu.setLocation(location);
087                            
088                            // show the popup if not visible
089                            curveConfMenu.setVisible(true);
090                            curveConfMenu.requestFocus();
091                        } 
092                        
093                        else {
094                            // hide it otherwise
095                            curveConfMenu.setVisible(false);
096                            curveConfMenu.requestFocus();
097                        }
098                    }
099                }
100                );
101            
102            // add components to main panel
103            this.setLayout(new BorderLayout());
104            this.add(invokePopupButton, BorderLayout.CENTER);
105            
106            // use frame
107            curveConfMenu = new JFrame("[=] Configuration Graph " + _plotRefNum + " [=]");
108            
109            // Construction of the Options Panel
110            JLabel labelFile = new JLabel("File");
111            JLabel labelX = new JLabel("X");
112            JLabel labelY = new JLabel("Y");
113            JLabel labelLabelY = new JLabel("Y Label");
114            _textFieldFile.setColumns(20);
115            _textFieldX.setColumns(20);
116            _textFieldY.setColumns(20);
117            _textFieldLabelY.setColumns(20);
118            JButton buttonRun = new JButton("Save");
119            buttonRun.setActionCommand("SAVE");
120            buttonRun.addActionListener(this);       
121            JButton buttonClose = new JButton("Close");
122            buttonClose.setActionCommand("CLOSE");
123            buttonClose.addActionListener(this);       
124            JButton buttonFile = new JButton("File");
125            buttonFile.setActionCommand("SET_FILE");
126            buttonFile.addActionListener(this);       
127    
128            GroupLayout layout = new GroupLayout(curveConfMenu.getContentPane());
129            curveConfMenu.getContentPane().setLayout(layout);
130            layout.setAutocreateGaps(true);
131            layout.setAutocreateContainerGaps(true);
132            
133            layout.setHorizontalGroup(layout.createSequentialGroup()
134                                      .add(layout.createParallelGroup(GroupLayout.LEADING)
135                                           .add(labelLabelY)
136                                           .add(labelFile)
137                                           .add(labelX)
138                                           .add(labelY)                               
139                                           )
140                                      
141                                      .add(layout.createParallelGroup(GroupLayout.CENTER)
142                                           .add(_textFieldLabelY)
143                                           .add(_textFieldFile)
144                                           .add(_textFieldX)
145                                           .add(_textFieldY)
146                                           .add(layout.createSequentialGroup()
147                                                .add(buttonRun)
148                                                .add(buttonClose)
149                                                )
150                                           )
151                                      
152                                      .add(layout.createParallelGroup(GroupLayout.LEADING)
153                                           .add(buttonFile)
154                                           )
155                                      );
156           
157            layout.setVerticalGroup(layout.createSequentialGroup()
158                                    .add(layout.createParallelGroup(GroupLayout.LEADING)
159                                         .add(labelLabelY)                               
160                                         .add(_textFieldLabelY)
161                                         )
162                                    .add(layout.createParallelGroup(GroupLayout.BASELINE)
163                                         .add(labelFile)
164                                         .add(_textFieldFile)
165                                         .add(buttonFile)
166                                         )
167                                    .add(layout.createParallelGroup(GroupLayout.LEADING)
168                                         .add(labelX)
169                                         .add(_textFieldX)
170                                         )
171                                    .add(layout.createParallelGroup(GroupLayout.LEADING)
172                                         .add(labelY)
173                                         .add(_textFieldY)
174                                         )
175                                    .add(layout.createParallelGroup(GroupLayout.LEADING)
176                                         .add(layout.createParallelGroup(GroupLayout.LEADING)
177                                              .add(buttonRun)
178                                              .add(buttonClose)
179                                              )
180                                         )
181                                    );
182            curveConfMenu.pack();
183        }
184        
185        
186        /**
187         * Handles a click on the Save/Close buttons.
188         *
189         * @param e  the action event.
190         */   
191        public void actionPerformed(ActionEvent e) {
192            
193            if (e.getActionCommand().equals("SAVE")) {      
194                
195                _datasetFile[_plotRefNum] = _textFieldFile.getText();
196    
197                ColumnList clX = ColumnList.parse(_textFieldX.getText());
198                if (clX.size() != 1)
199                    {
200                        System.out.println("Error : Bad Syntax for X in <" + _plotRefName + _plotRefNum + ">");                   
201                    }
202    
203                _datasetColumnListX[_plotRefNum] = clX;
204    
205                ColumnList clY = ColumnList.parse(_textFieldY.getText());
206                if (clY.size() < 1)
207                    {
208                        System.out.println("Error : Bad Syntax for Y in <" + _plotRefName + _plotRefNum + ">");                   
209                    }
210    
211                _datasetColumnListY[_plotRefNum] = clY;
212    
213                _labelY[_plotRefNum] = _textFieldLabelY.getText();
214    
215    
216                curveConfMenu.setVisible(false);                            
217            }
218            
219            else if (e.getActionCommand().equals("CLOSE")) {                    
220                curveConfMenu.setVisible(false);                            
221            }
222            
223            else if (e.getActionCommand().equals("SET_FILE")) 
224                {               
225                    JFileChooser chooser = new JFileChooser();          
226                    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
227                        {
228                            _textFieldFile.setText(chooser.getSelectedFile().getAbsolutePath());                    
229                        }
230                }
231        }
232    
233    }