001/**
002 * SelectParamsDialog -- Dialog allowing user to select a subset from a list of
003 * parameters.
004 *
005 * Copyright (C) 2003-2015, by Joseph A. Huwaldt. All rights reserved.
006 *
007 * This library is free software; you can redistribute it and/or modify it under the terms
008 * of the GNU Lesser General Public License as published by the Free Software Foundation;
009 * either version 2 of the License, or (at your option) any later version.
010 *
011 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
012 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
013 * PARTICULAR PURPOSE. See the GNU Library General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License along with
016 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place -
017 * Suite 330, Boston, MA 02111-1307, USA. Or visit: http://www.gnu.org/licenses/lgpl.html
018 */
019package jahuwaldt.js.datareader;
020
021import java.awt.Frame;
022import java.awt.event.ActionEvent;
023import java.util.List;
024
025/**
026 * A modal dialog the allows the user to select a subset of all the available parameters
027 * in a list of data cases.
028 *
029 * <p> Modified by: Joseph A. Huwaldt </p>
030 *
031 * @author Joseph A. Huwaldt, Date: May 7, 2003
032 * @version October 15, 2015
033 */
034public class SelectParamsDialog extends SelectCasesDialog {
035
036    //  A list of column names to display above the table.
037    private static final String[] columnNames = {
038        padRight(RESOURCES.getString("dataCaseColName"), 20),
039        padRight(RESOURCES.getString("paramColName"), 20)
040    };
041    
042    //  A list of the parameters that were selected by the user.
043    private DataSet selectedParams;
044    
045    //  The list of data cases we are selecting from.
046    private final List<DataCase> allCases;
047
048    /**
049     * Construct a model dialog that allows the user to select a subset of all the
050     * available cases in a list of data sets.
051     *
052     * @param parent          A reference to the parent frame that this dialog belongs to.
053     * @param title           The title for the dialog window.
054     * @param message         A message to display telling the user what to do.
055     * @param data            A list of DataCase objects containing the parameters to be
056     *                        chosen from.
057     * @param singleSelection Set to true to allow only a single parameter to be selected,
058     *                        false for multiple parameters.
059     * @param showArraySizes  Append the size of ArrayParam objects to the parameter names
060     *                        if set to true. Show only the parameter names if set to
061     *                        false.
062     */
063    public SelectParamsDialog(Frame parent, String title, String message, List<DataCase> data,
064            boolean singleSelection, boolean showArraySizes) {
065        super(parent, title, message, data, singleSelection, showArraySizes);
066
067        allCases = data;
068
069    }
070
071    /**
072     * This method returns a single DataSet object that contains all the parameters that
073     * were selected in this dialog stored in a set of new DataCase objects (one parameter
074     * per case). If the user has not clicked on the "OK" button, null is returned.
075     */
076    @Override
077    public DataSet getSelected() {
078        return selectedParams;
079    }
080
081    /**
082     * This method is called when the user clicks on the "OK Button".
083     */
084    @Override
085    public void actionPerformed(ActionEvent evt) {
086
087        //  Create a new data set to hold the cases we are creating to
088        //  hold the selected parameters.
089        selectedParams = DataSet.newInstance(RESOURCES.getString("selectedSetName"));
090
091        //  Get a list of all the selected rows in the table.
092        DataElement[] selected = getSelectedElements();
093        int numSelected = selected.length;
094
095        //  Add selected parameters to a new DataCase that corresponds
096        //  to the one the parameter originally came from.
097        int numCases = allCases.size();
098        for (int i = 0; i < numSelected; ++i) {
099            DataParam param = (DataParam)selected[i];
100
101            //  Figure out what case this parameter is from.
102            int caseID = 0;
103            DataCase parentCase = null;
104            for (; caseID < numCases; ++caseID) {
105                parentCase = (DataCase)allCases.get(caseID);
106                if (parentCase.contains(param))
107                    break;
108            }
109
110            //  Do we already have a case with this name?
111            boolean found = false;
112            DataCase selCase = null;
113            int selCases = selectedParams.size();
114            for (int j = 0; j < selCases; ++j) {
115                selCase = (DataCase)selectedParams.get(j);
116                CharSequence parentCaseName = (parentCase == null ? null : parentCase.getName());
117                if (selCase.getName().equals(parentCaseName)) {
118                    found = true;
119                    break;
120                }
121            }
122
123            //  If we already have this case, add the parameter to it.
124            if (found && selCase != null)
125                selCase.add(param);
126            else {
127                //  Create a new case having the same name as the case where this
128                //  parameter originally came from.
129                CharSequence parentCaseName = (parentCase == null ? null : parentCase.getName());
130                DataCase tempCase = DataCase.newInstance(parentCaseName);
131                tempCase.add(param);
132
133                selectedParams.add(tempCase);
134            }
135        }
136
137        //  Hide the window.
138        this.setVisible(false);
139    }
140
141    /**
142     * Method that returns the name to place above the specified column.
143     */
144    @Override
145    protected String getColumnName(int col) {
146        return columnNames[col];
147    }
148}