001    /*
002     * Copyright (c) 2005 Jens Schou, Staffan Gustafsson, Björn Lanneskog, 
003     * Einar Pehrson and Sebastian Kekkonen
004     *
005     * This file is part of
006     * CleanSheets Extension for Test Cases
007     *
008     * CleanSheets Extension for Test Cases is free software; you can
009     * redistribute it and/or modify it under the terms of the GNU General Public
010     * License as published by the Free Software Foundation; either version 2 of
011     * the License, or (at your option) any later version.
012     *
013     * CleanSheets Extension for Test Cases is distributed in the hope that
014     * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
015     * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016     * See the GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with CleanSheets Extension for Test Cases; if not, write to the
020     * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
021     * Boston, MA  02111-1307  USA
022     */
023    package csheets.ext.test.ui;
024    
025    import java.util.HashSet;
026    import java.util.Set;
027    
028    import javax.swing.JOptionPane;
029    
030    import csheets.core.Value;
031    import csheets.ext.assertion.AssertableCell;
032    import csheets.ext.assertion.Assertion;
033    import csheets.ext.assertion.AssertionExtension;
034    import csheets.ext.test.DuplicateUserTCPException;
035    import csheets.ext.test.TestCaseParam;
036    import csheets.ext.test.TestableCell;
037    import csheets.ui.ctrl.UIController;
038    
039    /**
040     * A controller for updating the test case parameters of a cell.
041     * @author Einar Pehrson
042     */
043    public class TestCaseParamController {
044    
045            /** The user interface controller */
046            private UIController uiController;
047    
048            /**
049             * Creates a new test case parameter controller.
050             * @param uiController the user interface controller
051             */
052            public TestCaseParamController(UIController uiController) {
053                    this.uiController = uiController;
054            }
055    
056            /**
057             * Sets the active cell's user-specified testcase parameters.
058             * @param cell the cell to add the parameter to
059             * @param content the content of the parameter to be added
060             * @param unChangedParams a set containing all parameters that were not changed
061             * @return true the parameter that was added, or null if the parameter was not added
062             */
063            public TestCaseParam setTestCaseParams(TestableCell cell, String content,
064                                    Set<TestCaseParam> unChangedParams) {
065                    // Checks if any parameters were removed
066                    Set<TestCaseParam> oldParams = new HashSet<TestCaseParam>(cell.getTestCaseParams());
067                    oldParams.removeAll(unChangedParams);
068    
069                    if (!oldParams.isEmpty())
070                            // Parameters were removed
071                            for (TestCaseParam removedParam : oldParams)
072                                    cell.removeTestCaseParam(removedParam);
073    
074                    // Checks if a parameter was added
075                    if(content != null) {
076                            Value value = Value.parseValue(content);
077    
078                            // Asserts if possible
079                            if (value.getType() == Value.Type.NUMERIC) {
080                                    AssertableCell assertableCell
081                                            = (AssertableCell)cell.getExtension(AssertionExtension.NAME);
082                                    if (assertableCell != null) {
083                                            if (assertableCell.isAsserted())
084                                                    if (assertableCell.assertAny(value) == Assertion.Result.FAILED)
085                                                            // Shows error, should perhaps ask for corfirmation?
086                                                            showError("The test case parameter that was entered for cell " + 
087                                                                    cell + " violated the cell's assertion.");
088                                    }
089                            }
090    
091                            // Attempts to add the parameter
092                            try {
093                                    TestCaseParam param = cell.addTestCaseParam(value);
094                                    uiController.setWorkbookModified(cell.getSpreadsheet().getWorkbook());
095                                    return param;
096                            } catch (DuplicateUserTCPException e) {
097                                    // Informs user that the parameter could not be added
098                                    showError(e.getMessage());
099                                    return null;
100                            }
101                    } else
102                            return null;
103            }
104    
105            /**
106             * Shows an error dialog displaying the given message.
107             */
108            private void showError(Object message) {
109                    JOptionPane.showMessageDialog(
110                            null,
111                            message,
112                            "Error",
113                            JOptionPane.ERROR_MESSAGE
114                    );
115            }
116    }