001    /**
002     * planningtool - A Planning Tool with Critiquing Support.
003     * 
004     * Copyright (C) 2006 olale
005    
006     * This program is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU General Public License
008     * as published by the Free Software Foundation; either version 2
009     * of the License, or (at your option) any later version.
010    
011     * This program is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU General Public License for more details.
015    
016     * You should have received a copy of the GNU General Public License
017     * along with this program; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019    
020     * Contact information: 
021     * E-mail: olale@ida.liu.se
022     *         olale@lysator.liu.se
023     */
024    package se.liu.ida.critiquer.gui;
025    
026    import java.awt.BorderLayout;
027    import java.awt.Component;
028    import java.util.ArrayList;
029    import java.util.Vector;
030    
031    import javax.swing.DefaultListModel;
032    import javax.swing.JPanel;
033    import javax.swing.JScrollPane;
034    import javax.swing.JTabbedPane;
035    import javax.swing.event.ChangeEvent;
036    import javax.swing.event.ChangeListener;
037    
038    import se.liu.ida.critiquer.constraints.ConstraintFactory;
039    import se.liu.ida.critiquer.constraints.SingletonConstraint;
040    import se.liu.ida.critiquer.constraints.StandardConstraint;
041    import se.liu.ida.critiquer.mics.ReferenceHolder;
042    
043    /**
044     * 
045     * <p>A panel that displays a list of visual constraints that may be enabled on a
046     * per-view basis. Each constraint may be applicable to one or more views, and
047     * only those applicable to the current view are displayed as options in the
048     * pane.
049     * </p> 
050     * 
051     * <p>All constraints represent singleton constraint objects listed in the VisualConstraints class.
052     * </p> 
053     * 
054     * @author olale
055     * 
056     */
057    public class CritiquePanel extends JPanel {
058    
059            /**
060         * 
061         */
062            private static final long serialVersionUID = 1L;
063    
064            private Vector<ConstraintCheckBox> createCheckboxArray(ArrayList<Class<? extends StandardConstraint>> constraintClasses) {
065                    Vector<ConstraintCheckBox> checkboxes = new Vector<ConstraintCheckBox>();
066                    ConstraintCheckBox checkbox = null;
067                    for (Class constraintClass : constraintClasses) {
068                            SingletonConstraint singletonConstraint = ConstraintFactory.getSingletonConstraint(constraintClass);
069                            if (singletonConstraint instanceof StandardConstraint) {
070                                    StandardConstraint standardConstraint = (StandardConstraint) singletonConstraint;                               
071                                    checkbox = new ConstraintCheckBox(standardConstraint);
072                                    checkbox.setToolTipText(standardConstraint.getDescription());
073                                    
074                                    checkboxes.add(checkbox);
075                            }
076                    }
077                    return checkboxes;
078            }
079    
080            private CheckBoxList                       chooserList;
081    
082            private CriticTextArea                            infoArea = new CriticTextArea();
083    
084            private DefaultListModel                   chooserListModel;
085    
086            private Vector<ConstraintCheckBox> checkBoxes;
087    
088            /**
089         * User the tabbed pane as an argument to select which critics should be
090         * selectable based on the current view
091         */
092            public CritiquePanel(final JTabbedPane pane) {
093                    super(new BorderLayout());
094                    chooserListModel = new DefaultListModel();
095                    checkBoxes = createCheckboxArray(ReferenceHolder.constraintClasses);
096                    pane.addChangeListener(new ChangeListener() {
097    
098                            public void stateChanged(ChangeEvent e) {
099                                    Component component = pane.getSelectedComponent();
100                                    if (component instanceof View) {
101                                            View currentView = (View) component;
102                                            chooserListModel.removeAllElements();
103                                            for (ConstraintCheckBox box : checkBoxes) {
104    
105                                                    if (box.isApplicableFor(currentView)) {
106                                                            chooserListModel.addElement(box);
107    
108                                                    }
109    
110                                            }
111                                    }
112                            }
113    
114                    });
115                    chooserList = new CheckBoxList(chooserListModel);
116                    ReferenceHolder.critiqueInfoArea = infoArea;
117    
118                    add(new JScrollPane(chooserList), BorderLayout.NORTH);
119                    add(new JScrollPane(infoArea.getTextArea()), BorderLayout.CENTER);
120            }
121    
122    }