001/* 002 * Copyright (c) 2005 Peter Palotas, Fredrik Johansson, Einar Pehrson, 003 * Sebastian Kekkonen, Lars Magnus Lang, Malin Johansson and Sofia Nilsson 004 * 005 * This file is part of 006 * CleanSheets Extension for Assertions 007 * 008 * CleanSheets Extension for Assertions 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 Assertions 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 Assertions; if not, write to the 020 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, 021 * Boston, MA 02111-1307 USA 022 */ 023package csheets.ext.assertion.ui; 024 025import java.awt.Color; 026 027import javax.swing.JOptionPane; 028 029import csheets.ext.assertion.AssertableCell; 030import csheets.ext.assertion.Assertion; 031import csheets.ext.assertion.AssertionException; 032import csheets.ext.assertion.AssertionWarning; 033import csheets.ext.assertion.USAssertion; 034import csheets.ui.ctrl.UIController; 035 036/** 037 * A controller for updating the user-specified assertion of a cell. 038 * @author Einar Pehrson 039 */ 040public class AssertionController { 041 042 /** The user interface controller */ 043 private UIController uiController; 044 045 /** 046 * Creates a new assertion controller. 047 * @param uiController the user interface controller 048 */ 049 public AssertionController(UIController uiController) { 050 this.uiController = uiController; 051 } 052 053 /** 054 * Attempts to create a new assertion from the given string. 055 * If successful, adds the assertion to the given cell. 056 * Otherwise, displays an error message. 057 * If the input string is empty or null, the assertion is set to null. 058 * @param cell the cell for which the assertion should be set 059 * @param assertionString the assertion, as entered by the user 060 * @return true if the cell's assertion was changed 061 */ 062 public boolean setAssertion(AssertableCell cell, String assertionString) { 063 // Clears assertion, if insufficient input 064 if (assertionString == null || assertionString.equals("")) { 065 cell.setUSAssertion(null); 066 return true; 067 } 068 069 // Attempts to create the assertion 070 USAssertion assertion; 071 try { 072 assertion = new USAssertion(assertionString); 073 } catch (AssertionException e) { 074 // Informs user that the assertion syntax was erroneous 075 showError(e); 076 return false; 077 } 078 079 // Stores the assertion 080 cell.setUSAssertion(assertion); 081 uiController.setWorkbookModified(cell.getSpreadsheet().getWorkbook()); 082 083 if (!assertion.isConsistent()) { 084 // Informs user that the assertion has warnings 085 String message = "The assertion may be incorrect or inconsistent:\n"; 086 for (AssertionWarning warning : assertion.getWarnings()) 087 message += "\n" + warning; 088 showError(message); 089 } 090 091 return true; 092 } 093 094 /** 095 * Assigns a color to the given assertion result. 096 * @param result the assertion result for which a color is wanted 097 * @return the appropriate color to use in the interface 098 */ 099 public static Color getAssertionResultColor(Assertion.Result result){ 100 switch (result) { 101 case OK: 102 return new Color(0f, 0.7f, 0f); 103 case NAN: 104 return new Color(0.7f, 0.0f, 0f); 105 case FAILED: 106 return new Color(0.7f, 0.0f, 0f); 107 case NO_DATA: 108 return new Color (0.7f, 0.7f, 0f); 109 default: 110 return Color.BLACK; 111 } 112 } 113 114 /** 115 * Shows an error dialog displaying the given message. 116 */ 117 private void showError(Object message) { 118 JOptionPane.showMessageDialog( 119 null, 120 message, 121 "Error", 122 JOptionPane.ERROR_MESSAGE 123 ); 124 } 125}