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.BorderLayout;
19import java.awt.Color;
20import java.awt.Dimension;
21import java.awt.Font;
22import java.awt.GridLayout;
23import java.awt.event.ActionEvent;
24import java.awt.event.ActionListener;
25import java.io.ByteArrayOutputStream;
26import java.io.InputStream;
27import java.util.ResourceBundle;
28
29import javax.swing.BorderFactory;
30import javax.swing.ImageIcon;
31import javax.swing.JButton;
32import javax.swing.JLabel;
33import javax.swing.JPanel;
34import javax.swing.border.Border;
35
36import org.tp23.antinstaller.InstallerContext;
37import org.tp23.antinstaller.ValidationException;
38import org.tp23.antinstaller.page.Page;
39
40/**
41 *
42 * <p>Abstract super class for page renderers.  setPage will always be called. </p>
43 * <p>Subclasses should implement instanceInit for initialising swing components
44 * on the page. </p>
45 * @author Paul Hinds
46 * @version $Id: SwingPageRenderer.java,v 1.10 2007/01/19 00:24:35 teknopaul Exp $
47 */
48public abstract class SwingPageRenderer
49    extends JPanel {
50    
51    private static final ResourceBundle res = ResourceBundle.getBundle("org.tp23.antinstaller.renderer.Res");
52
53    // gui components
54    private BorderLayout borderLayout1 = new BorderLayout();
55    
56    // holds the next back buttons etc
57    private JPanel controlPanel = new JPanel();
58
59    private JButton backButton = new JButton();
60    private JButton cancelButton = new JButton();
61    private JButton nextButton = new JButton();
62    private JButton finishButton = new JButton();
63
64    private JPanel titlePanel = new JPanel();
65    private JLabel titleLabel = new JLabel();
66    private JLabel imagePanel = new JLabel(); // Graphic for the installer
67    private GridLayout titleLayout = new GridLayout();
68
69    // app components
70    protected Page page;
71    protected SwingInstallerContext swingCtx;
72    protected InstallerContext ctx;
73    protected PageCompletionListener listener;
74    private Border bevelBorder;
75        
76
77    private static Font titleFont;
78    static{
79        titleFont = new JLabel().getFont();
80        try {
81            titleFont = new Font(titleFont.getFamily(), Font.BOLD, 14);
82        }
83        catch (Exception ex) {
84            // lets not fail due to font errors
85        }
86    }
87
88    public SwingPageRenderer(){
89        super();
90    }
91    public void setPage(Page page){
92        this.page = page;
93        try {
94            jbInit();
95        }
96        catch (Exception e) {
97            ctx.log(e.getMessage());
98            if(ctx.getInstaller().isVerbose()) {
99                ctx.log(e);
00            }
01        }
02    }
03
04    public void setContext(SwingInstallerContext swingCtx){
05        this.ctx = swingCtx.getInstallerContext();
06        this.swingCtx = swingCtx;
07    }
08        
09    
10    private void jbInit() throws Exception {
11        
12        this.setDoubleBuffered(true);
13        //emptyBorder = BorderFactory.createEmptyBorder(2,5,2,2);
14        //bevelBorder = BorderFactory.createCompoundBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED,Color.white,Color.white,new Color(116, 116, 112),new Color(166, 166, 161)),BorderFactory.createEmptyBorder(2,SwingInputFieldRenderer.LEFT_INDENT,2,2));
15        bevelBorder = BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.black,1),BorderFactory.createEmptyBorder(2,SizeConstants.LEFT_INDENT,2,2));
16        bevelBorder = BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(),BorderFactory.createEmptyBorder(2,SizeConstants.LEFT_INDENT,2,2));
17        Border tripleBorder = BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(3,4,1,4),bevelBorder);
18        this.setLayout(borderLayout1);
19        titleLabel.setBorder(tripleBorder);
20        controlPanel.setBorder(tripleBorder);
21
22        this.add(titlePanel, BorderLayout.NORTH);
23        this.add(controlPanel, BorderLayout.SOUTH);
24
25        // title panel
26        titlePanel.add(imagePanel, null);
27        titlePanel.add(titleLabel, null);
28
29        titlePanel.setLayout(titleLayout);
30        titleLayout.setColumns(1);
31        titleLayout.setHgap(0);
32        titleLayout.setRows(2);
33        titleLayout.setVgap(2);
34        titlePanel.setMinimumSize(new Dimension(SizeConstants.PAGE_WIDTH, SizeConstants.TITLE_PANEL_HEIGHT));
35        titlePanel.setMaximumSize(new Dimension(SizeConstants.PAGE_WIDTH, SizeConstants.TITLE_PANEL_HEIGHT));
36        titlePanel.setPreferredSize(new Dimension(SizeConstants.PAGE_WIDTH, SizeConstants.TITLE_PANEL_HEIGHT));
37
38        titleLabel.setText(page.getDisplayText());
39        titleLabel.setFont(titleFont);
40        setImage(page.getImageResource());
41        imagePanel.setMinimumSize(new Dimension(SizeConstants.PAGE_WIDTH, SizeConstants.TITLE_IMAGE_HEIGHT));
42        imagePanel.setMaximumSize(new Dimension(SizeConstants.PAGE_WIDTH, SizeConstants.TITLE_IMAGE_HEIGHT));
43        imagePanel.setPreferredSize(new Dimension(SizeConstants.PAGE_WIDTH, SizeConstants.TITLE_IMAGE_HEIGHT));
44
45        // Ctrl Panel
46        controlPanel.add(cancelButton, null);
47        controlPanel.add(backButton, null);
48        controlPanel.add(nextButton, null);
49        controlPanel.add(finishButton, null);
50        backButton.setText(res.getString("backButton"));// "<< Back");
51        cancelButton.setText(res.getString("cancelButton"));// "Cancel");
52        nextButton.setText(res.getString("nextButton"));// "Next >>");
53        finishButton.setText(ctx.getInstaller().getFinishButtonText());
54        finishButton.setEnabled(false);
55        setEventListeners();
56        setIcons();
57    }
58    public abstract void instanceInit() throws Exception ;
59    public abstract void updateInputFields();
60    public abstract void updateDefaultValues();
61    public abstract boolean validateFields()throws ValidationException;
62
63    public void setPageCompletionListener(PageCompletionListener listener){
64        this.listener = listener;
65    }
66
67    private void setImage(String resource) throws Exception{
68        if(resource == null){
69           resource = ctx.getInstaller().getDefaultImageResource();
70        }
71        ImageIcon icon = getImage(resource);
72        imagePanel.setIcon(icon);
73    }
74    
75    protected ImageIcon getImage(String resource){
76        try {
77            if (resource != null) {
78                ByteArrayOutputStream baos = new ByteArrayOutputStream();
79                InputStream in = this.getClass().getResourceAsStream(resource);
80                byte[] buffer = new byte[2048];
81                int read = -1;
82                while ( (read = in.read(buffer)) != -1) {
83                    baos.write(buffer, 0, read);
84                }
85                ImageIcon icon = new ImageIcon(baos.toByteArray());
86                return icon;
87            }
88        }
89        catch (Exception ex) {
90            ctx.log("Can't load image resource:" + resource);
91            if(ctx.getInstaller().isVerbose()){
92                ctx.log(ex);
93            }
94        }
95        return null;
96    }
97    
98    
99    private void setEventListeners(){
00        backButton.addActionListener(new ActionListener(){
01            public void actionPerformed(ActionEvent e){
02                listener.pageBack(page);
03            }
04        });
05        cancelButton.addActionListener(new ActionListener(){
06            public void actionPerformed(ActionEvent e){
07                page.setAbort(true);
08                if (ctx.getInstaller().isVerbose()) {
09                    ctx.log("Abort called");
10                }
11                listener.pageComplete(page);
12            }
13        });
14        nextButton.addActionListener(new ActionListener(){
15            public void actionPerformed(ActionEvent e){
16                listener.pageComplete(page);
17
18            }
19        });
20        finishButton.addActionListener(new ActionListener(){
21            public void actionPerformed(ActionEvent e){
22                if(finishButton.getText().equals(res.getString("exit"))) {
23                    //TODO FindBugs this will prevent cleanup in FinalizerFilter
24                    System.exit(0);
25                }
26                listener.pageComplete(page);
27                //((SwingInstallerContext)ctx).getSwingRunner().finish();
28            }
29        });
30    }
31    
32    private void setIcons(){
33        backButton.setIcon(getImage("/resources/icons/back.png"));
34        cancelButton.setIcon(getImage("/resources/icons/cancel.png"));
35        nextButton.setIcon(getImage("/resources/icons/next.png"));
36        finishButton.setIcon(getImage("/resources/icons/finish.png"));
37    }
38
39    public JButton getCancelButton() {
40        return cancelButton;
41    }
42
43    public InstallerContext getCtx() {
44        return ctx;
45    }
46
47    public JPanel getControlPanel() {
48        return controlPanel;
49    }
50
51    public JLabel getImagePanel() {
52        return imagePanel;
53    }
54
55    public JButton getNextButton() {
56        return nextButton;
57    }
58
59    public JLabel getTitleLabel() {
60        return titleLabel;
61    }
62    public JButton getFinishButton() {
63        return finishButton;
64    }
65    public JButton getBackButton() {
66        return backButton;
67    }
68
69}
70