001    /*
002     * Copyright (c) 2005 Peter Palotas, Fredrik Johansson, Einar Pehrson,
003     * Sebastian Kekkonen, Lars Magnus Lång, 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     */
023    package csheets.ext.assertion.ui;
024    
025    import javax.swing.Icon;
026    import javax.swing.ImageIcon;
027    import javax.swing.JLabel;
028    import javax.swing.JTextField;
029    import javax.swing.JToolBar;
030    
031    import csheets.core.Cell;
032    import csheets.ext.assertion.AssertableCell;
033    import csheets.ext.assertion.AssertableCellListener;
034    import csheets.ext.assertion.Assertion;
035    import csheets.ext.assertion.AssertionExtension;
036    import csheets.ui.ctrl.SelectionEvent;
037    import csheets.ui.ctrl.SelectionListener;
038    
039    /**
040     * A toolbar that shows information about assertions.
041     * @author Lars Magnus "Burken" Lång
042     * @author Sebastian Kekkonen
043     */
044    @SuppressWarnings("serial")
045    public class AssertionToolBar extends JToolBar implements SelectionListener,
046                    AssertableCellListener {
047    
048            private JTextField usAssertion;
049            private JTextField sgAssertion;
050    
051            private Icon redIcon;
052            private Icon yellowIcon;
053            private Icon greenIcon;
054    
055            private JLabel lightLabelAssertion;
056    
057            public AssertionToolBar(){
058                    setName(AssertionExtension.NAME);
059                    redIcon = new ImageIcon(AssertionExtension.class.getResource(
060                            "res/img/light_red.gif"), "Red light");
061                    yellowIcon = new ImageIcon(AssertionExtension.class.getResource(
062                            "res/img/light_yellow.gif"), "Yellow light");
063                    greenIcon = new ImageIcon(AssertionExtension.class.getResource(
064                            "res/img/light_green.gif"), "Green light");
065    
066                    lightLabelAssertion = new JLabel(yellowIcon);
067                    //yellowIcon.paintIcon(lightPanelAssertion, lightPanelAssertion.getGraphics(), 0, 0);
068    
069                    usAssertion = new JTextField(5);
070                    usAssertion.setEditable(false);
071                    sgAssertion = new JTextField(5);
072                    sgAssertion.setEditable(false);
073    
074                    add(new JLabel(new ImageIcon(
075                                    AssertionExtension.class.getResource("res/img/logo.gif"))));
076                    addSeparator();
077                    add(lightLabelAssertion);
078                    addSeparator();
079                    add(usAssertion);
080                    addSeparator();
081                    add(new JLabel("="));
082                    addSeparator();
083                    add(sgAssertion);
084            }
085    
086            private void setUSAssertionText(String s){
087                    usAssertion.setText(s);
088            }
089    
090            private void setSGAssertionText(String s){
091                    sgAssertion.setText(s);
092            }
093    
094    
095            /*
096             *
097             * @param lamp 1-green, 2-yellow, 3-red
098             */
099            private void setAssertionLamp(int lamp){
100                    switch(lamp){
101                    case 1: 
102                            lightLabelAssertion.setIcon(greenIcon);
103                            break;
104                    case 2:
105                            lightLabelAssertion.setIcon(yellowIcon);
106                            break;
107                    case 3:
108                            lightLabelAssertion.setIcon(redIcon);
109                            break;
110                    default:
111                            lightLabelAssertion.setIcon(yellowIcon);
112                            break;
113                    }
114            }
115    
116            public void selectionChanged(SelectionEvent event) {
117                    Cell cell = event.getCell();
118                    if (cell != null) {
119                            AssertableCell activeCell
120                                    = (AssertableCell)cell.getExtension(AssertionExtension.NAME);
121                            activeCell.addAssertableCellListener(this);
122                            assertionsChanged(activeCell);
123                    } else
124                            setAssertionLamp(2);
125    
126                    // Stops listening to previous active cell
127                    if (event.getPreviousCell() != null)
128                            ((AssertableCell)event.getPreviousCell().getExtension(AssertionExtension.NAME))
129                                    .removeAssertableCellListener(this);
130            }
131    
132            /**
133             * Updates the assertion field and status label when the assertion of the
134             * active cell is changed.
135             * @param cell the cell whose assertion changed
136             */
137            public void assertionsChanged(AssertableCell cell) {
138                    Assertion usAssertion = cell.getUSAssertion();
139                    Assertion sgAssertion = cell.getSGAssertion();
140    
141                    //Set lights depending on the circumstances
142                    // Code for setting lamps simplified by Peter after the addition of some new methods in cell.
143                    if (cell.hasAssertionError()) {
144                            if (cell.assertAssertions() == Assertion.ComparisonResult.OK
145                                            && cell.assertAny() == Assertion.Result.NO_DATA)
146                                    setAssertionLamp(2);
147                            else
148                                    setAssertionLamp(3);
149                    } else if (cell.isAsserted())
150                            setAssertionLamp(1);
151                    else
152                            setAssertionLamp(2);
153    
154                    if(usAssertion != null)
155                            setUSAssertionText(usAssertion.toString());
156                    else
157                            setUSAssertionText("");
158    
159                    if(sgAssertion != null)
160                            setSGAssertionText(sgAssertion.toString());
161                    else
162                            setSGAssertionText("");
163            }
164    }