1 /* 
2  * Copyright 2005 Paul Hinds
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.tp23.antinstaller.renderer.swing;
17
18import java.awt.Dimension;
19import java.awt.Font;
20
21import javax.swing.JLabel;
22import javax.swing.JPanel;
23import javax.swing.text.JTextComponent;
24
25import org.tp23.antinstaller.input.CommentOutput;
26import org.tp23.antinstaller.input.OutputField;
27import org.tp23.gui.GBCF;
28
29public class CommentOutputRenderer
30    extends SwingOutputFieldRenderer {
31
32    protected AILabel fieldLabel = new AILabel();
33    // hack callback, should move this to superclass
34    protected JTextComponent explanatoryTextField;
35
36    private static Font boldCommentFont;
37    private static Font titleCommentFont;
38    static{
39        boldCommentFont = new JLabel().getFont();// reusing the variable
40        try {
41            boldCommentFont = new Font(boldCommentFont.getFamily(), Font.BOLD, boldCommentFont.getSize());
42            titleCommentFont = new Font(boldCommentFont.getFamily(), Font.BOLD, 16);
43        }
44        catch (Exception ex) {
45            // lets not fail due to font errors
46        }
47    }
48
49    public CommentOutputRenderer() {
50    }
51
52    public void initComponent(JPanel parent){
53        try {
54            jbInit();
55        }
56        catch(Exception e) {
57            e.printStackTrace();
58        }
59    }
60
61    public void setOutputField(OutputField outputField) {
62        this.outputField = (CommentOutput)outputField;// trap ClassCast bugs early
63    }
64    public void updateInputField(){
65    }
66
67//   hack callback, should move this to superclass
68    public JTextComponent getExplanatoryTextField() {
69        return explanatoryTextField;
70    }
71//  hack callback, should move this to superclass
72    public void setExplanatoryTextField(JTextComponent explanatoryTextField) {
73        this.explanatoryTextField = explanatoryTextField;
74    }
75    /**
76     * Since comments may now include expanded properties this should be called when the 
77     * field is rendered.  For no ONLY comment fields have property values expanded
78     */
79    public void updateDefaultValue(){
80        fieldLabel.setText(outputField.getDisplayText());
81        if(explanatoryTextField != null) {
82            explanatoryTextField.setText(outputField.getExplanatoryText());
83        }
84    }
85    private void jbInit() throws Exception {
86        
87        // FindBugs - cast is performed here to avoid overriding protected superclass field 
88        CommentOutput cOutputField = (CommentOutput)outputField;
89        
90        fieldLabel.setText(cOutputField.getDisplayText());
91        
92        if( OutputField.isTrue(cOutputField.getBold()) ){
93            fieldLabel.setFont(boldCommentFont);
94        }
95        if( OutputField.isTrue(cOutputField.getTitle()) ){
96            fieldLabel.setFont(titleCommentFont);
97        }
98    }
99    public int addSelf(JPanel content, GBCF cf,  int row, boolean overflow) {
00        content.add(fieldLabel, cf.getSpan(row));
01        if(overflow){
02            fieldLabel.setOverflow(SizeConstants.OVERFLOW_TOTAL_SIZE);
03        } else {
04            fieldLabel.setOverflow(new Dimension(SizeConstants.FIELD_WIDTH + SizeConstants.LABEL_WIDTH, SizeConstants.FIELD_HEIGHT));
05        }
06        return ++row;
07    }
08
09
10    /**
11     * renderError
12     */
13    public void renderError() {
14    }
15}
16