src/java/isac/gui/calcheadviews/CalcHeadView.java
author wneuper
Thu, 17 Jan 2008 16:27:03 +0100
changeset 3881 72f0be16d83b
parent 3418 06618b7ea66c
child 3928 d38196e9b162
permissions -rw-r--r--
start-work-070517 merged into HEAD
     1 /****************************************************************** 
     2  * Copyright (c) 2003, Mario Hochreiter, Student of 
     3  * Software Engineering at FH-Hagenberg, Austria. 
     4  * Project member of the isac team at the Institute for 
     5  * Software-Technologie, Graz University of Technology, Austria. 
     6  * 
     7  * Use is subject to license terms.
     8  ******************************************************************/
     9 
    10 package isac.gui.calcheadviews;
    11 
    12 import isac.util.formulae.CalcHead;
    13 
    14 import java.awt.BorderLayout;
    15 import java.util.Locale;
    16 import java.util.ResourceBundle;
    17 
    18 import javax.swing.BorderFactory;
    19 import javax.swing.JPanel;
    20 
    21 import org.apache.log4j.Logger;
    22 
    23 /**
    24  * @author Mario Hochreiter
    25  * @author Manuel Koschuch
    26  * @version 1.0
    27  * 
    28  */
    29 public class CalcHeadView extends JPanel implements ICalcHeadView {
    30 
    31     /**
    32      * Constant used for serialization (@see corresponding API Documentation)
    33      */
    34     private static final long serialVersionUID = 1L;
    35 
    36     private static final Logger logger = Logger.getLogger(CalcHeadView.class
    37             .getName());
    38 
    39     private static final int HEADLINE_PANEL_INDEX = 0;
    40 
    41     private static final int MODEL_PANEL_INDEX = 1;
    42 
    43     private static final int SPECIFICATION_PANEL_INDEX = 2;
    44 
    45     private ResourceBundle res_bundle_;
    46 
    47     private ModelPanel model_panel_;
    48 
    49     private SpecificationPanel specification_panel_;
    50 
    51     private HeadLinePanel head_line_panel_;
    52 
    53     private int current_panel_index_;
    54 
    55     /**
    56      * Creates a new CalcHeadView, consisting of 3 Panels: a HeadLinePanel,
    57      * presenting the CalcHead's Headline, a ModelPanel, presenting the
    58      * CalcHead's Model, a SpecificationPanel, presenting the CalcHead's
    59      * Specification
    60      * 
    61      * depending on the selected Viewstyle in the supplied CalcHead all or some
    62      * of these panels are drawn
    63      * 
    64      */
    65 
    66     public CalcHeadView(CalcHead ch) {
    67         res_bundle_ = ResourceBundle.getBundle("CalcHeadPanel");
    68         model_panel_ = new ModelPanel(ch.getModel(), res_bundle_);
    69         specification_panel_ = new SpecificationPanel(ch.getSpecification(),
    70                 res_bundle_);
    71         head_line_panel_ = new HeadLinePanel(ch.getHeadLine());
    72         current_panel_index_ = 0;
    73         this.init(ch);
    74         this.fillTextFieldsFromCalcHead(ch);
    75     }
    76 
    77     private void init(CalcHead ch) {
    78         this.layoutComponents(ch);
    79     }
    80 
    81     private void layoutComponents(CalcHead ch) {
    82         this.setLayout(new BorderLayout());
    83         this.add(head_line_panel_, BorderLayout.NORTH);
    84         if (ch.getBelongsTo().equals("Met")) {
    85             buildMethodLayout();
    86         } else {
    87             buildProblemLayout(ch);
    88         }
    89 
    90     }
    91 
    92     /**
    93      * @param ch
    94      *            CalcHead to wich the data read from the text fields is written
    95      */
    96     public void fillCalcHeadFromTextFields(CalcHead ch) {
    97         model_panel_.fillModelFromTextFields(ch.getModel());
    98         specification_panel_.fillSpecificationFromTextFields(ch
    99                 .getSpecification());
   100         ch.setBelongsTo(specification_panel_.getBelongsTo());
   101     }
   102 
   103     /**
   104      * @param ch
   105      *            CalcHead from wich the data is read to fill out the text
   106      *            fields
   107      */
   108     public void fillTextFieldsFromCalcHead(CalcHead ch) {
   109         if (logger.isDebugEnabled())
   110             logger.debug(ch.getModel().getGiven() + " "
   111                     + ch.getModel().getFind());
   112 
   113         reset();
   114         head_line_panel_.fillHeadLinePanelFromFormula(ch.getHeadLine());
   115         model_panel_.fillTextFieldsFromModel(ch.getModel(), true);
   116         specification_panel_.fillTextFieldsFromSpecification(ch
   117                 .getSpecification(), ch.getBelongsTo());
   118     }
   119 
   120     private void buildMethodLayout() {
   121         // FIXME find better representation?
   122         specification_panel_.highlightLabel(SpecificationPanel.LABEL_METHOD);
   123         buildFullLayout("Guard");
   124     }
   125 
   126     private void buildProblemLayout(CalcHead ch) {
   127         specification_panel_.highlightLabel(SpecificationPanel.LABEL_PROBLEM);
   128         switch (ch.getViewstyle()) {
   129         case CalcHead.VIEWSTYLE_FULL:
   130             buildFullLayout("Model");
   131             break;
   132         case CalcHead.VIEWSTYLE_MODEL:
   133             buildModelLayout();
   134             break;
   135         case CalcHead.VIEWSTYLE_SPECIFICATION:
   136             buildSpecificationLayout();
   137             break;
   138         default:
   139             buildFullLayout("Model");
   140         }
   141     }
   142 
   143     /**
   144      * Builds ModelPanel and SpecificationPanel, the ModelPanel is titled with
   145      * the supplied string (either Model or Guard)
   146      * 
   147      * @param title
   148      *            title for the ModelPanel
   149      */
   150 
   151     private void buildFullLayout(String title) {
   152         this.add(model_panel_, BorderLayout.CENTER);
   153         model_panel_.setBorder(BorderFactory.createTitledBorder(title));
   154 
   155         this.add(specification_panel_, BorderLayout.SOUTH);
   156         specification_panel_.setBorder(BorderFactory
   157                 .createTitledBorder("Specification"));
   158 
   159     }
   160 
   161     /**
   162      * Build only ModelPanel
   163      * 
   164      */
   165 
   166     private void buildModelLayout() {
   167         this.add(model_panel_, BorderLayout.CENTER);
   168         model_panel_.setBorder(BorderFactory.createTitledBorder("Model"));
   169 
   170     }
   171 
   172     /**
   173      * Build only SpecificationPanel
   174      * 
   175      */
   176 
   177     private void buildSpecificationLayout() {
   178         this.add(specification_panel_, BorderLayout.CENTER);
   179         specification_panel_.setBorder(BorderFactory
   180                 .createTitledBorder("Specification"));
   181 
   182     }
   183 
   184     public ModelPanel getModelPanel() {
   185         return model_panel_;
   186     }
   187 
   188     public SpecificationPanel getSpecificationPanel() {
   189         return specification_panel_;
   190     }
   191 
   192     /**
   193      * Clears all fields
   194      */
   195 
   196     public void reset() {
   197         current_panel_index_ = 0;
   198         head_line_panel_.reset();
   199         model_panel_.reset();
   200         specification_panel_.reset();
   201 
   202     }
   203 
   204     /**
   205      * Fills Textfields stepwise
   206      * 
   207      * @param ch
   208      *            CalcHead to fill TextFields from
   209      * @return false, when Textfields left to fill, true otherwise
   210      * @deprecated was misunderstanding
   211      */
   212 
   213     public boolean next(CalcHead ch) {
   214         switch (current_panel_index_) {
   215         case HEADLINE_PANEL_INDEX:
   216             if (head_line_panel_.next(ch.getHeadLine())) {
   217                 current_panel_index_++;
   218                 return false;
   219             }
   220             current_panel_index_++;
   221             return false;
   222 
   223         case MODEL_PANEL_INDEX:
   224             if (model_panel_.next(ch.getModel())) {
   225                 current_panel_index_++;
   226                 return false;
   227             }
   228             return false;
   229 
   230         case SPECIFICATION_PANEL_INDEX:
   231             if (specification_panel_.next(ch.getSpecification())) {
   232                 current_panel_index_++;
   233                 return true;
   234             }
   235             return false;
   236 
   237         default:
   238             return true;
   239         }
   240     }
   241 }