001: /*
002: * Copyright 2005 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.antinstaller.renderer.swing;
017:
018: import java.awt.BorderLayout;
019: import java.awt.Color;
020: import java.awt.Dimension;
021: import java.awt.FlowLayout;
022: import java.awt.Font;
023: import java.awt.GridLayout;
024: import java.awt.event.ActionEvent;
025: import java.awt.event.ActionListener;
026: import java.io.ByteArrayOutputStream;
027: import java.io.InputStream;
028:
029: import javax.swing.BorderFactory;
030: import javax.swing.ImageIcon;
031: import javax.swing.JButton;
032: import javax.swing.JLabel;
033: import javax.swing.JPanel;
034: import javax.swing.border.Border;
035:
036: import org.tp23.antinstaller.InstallerContext;
037: import org.tp23.antinstaller.ValidationException;
038: import org.tp23.antinstaller.page.Page;
039: import org.tp23.antinstaller.renderer.AIResourceBundle;
040:
041: /**
042: *
043: * <p>
044: * Abstract super class for page renderers. setPage will always be called.
045: * </p>
046: * <p>
047: * Subclasses should implement instanceInit for initialising swing components on
048: * the page.
049: * This calss contains the initalisation of the buttons the title text and the side panel.
050: * </p>
051: *
052: * @author Paul Hinds
053: * @version $Id: SwingPageRenderer.java,v 1.10 2007/01/19 00:24:35 teknopaul Exp $
054: */
055: public abstract class SwingPageRenderer extends JPanel {
056:
057: private static final AIResourceBundle res = new AIResourceBundle();
058:
059: // gui components
060: protected JPanel dataPanel = new AIPanel();
061: private JPanel rightPanel = new AIPanel();
062:
063: // holds the next back buttons etc
064: private JPanel controlPanel = new AIPanel();
065: private JButton backButton = new JButton();
066: private JButton cancelButton = new JButton();
067: private JButton nextButton = new JButton();
068: private JButton finishButton = new JButton();
069:
070: private JPanel titlePanel;
071: private JLabel titleLabel;
072: private JLabel imagePanel; // Graphic for the installer
073:
074: // app components
075: protected Page page;
076: protected SwingInstallerContext swingCtx;
077: protected InstallerContext ctx;
078: protected PageCompletionListener listener;
079: private Border bevelTopBorder;
080: private Border bevelBottomBorder;
081:
082: private static Font titleFont;
083: static {
084: titleFont = new JLabel().getFont();
085: try {
086: titleFont = new Font(titleFont.getFamily(), Font.BOLD, 14);
087: } catch (Exception ex) {
088: // lets not fail due to font errors
089: }
090: }
091:
092: public SwingPageRenderer() {
093: super ();
094: //this.setOpaque(false);
095: }
096:
097: public void setPage(Page page) {
098: this .page = page;
099: try {
100: jbInit();
101: } catch (Exception e) {
102: ctx.log(e.getMessage());
103: if (ctx.getInstaller().isVerbose()) {
104: ctx.log(e);
105: }
106: }
107: }
108:
109: public void setContext(SwingInstallerContext swingCtx) {
110: this .ctx = swingCtx.getInstallerContext();
111: this .swingCtx = swingCtx;
112: }
113:
114: private void jbInit() throws Exception {
115:
116: this .setDoubleBuffered(true);
117:
118: // Color determination
119: JButton b = new JButton();
120: Color borderColor = b.getBackground().darker();
121: bevelTopBorder = BorderFactory.createCompoundBorder(
122: BorderFactory
123: .createMatteBorder(1, 0, 0, 0, borderColor),
124: BorderFactory.createEmptyBorder(2, 2, 2, 2));
125: bevelBottomBorder = BorderFactory.createCompoundBorder(
126: BorderFactory
127: .createMatteBorder(0, 0, 1, 0, borderColor),
128: BorderFactory.createEmptyBorder(2, 8, 2, 2));
129:
130: this .setLayout(new BorderLayout());
131: dataPanel.setLayout(new BorderLayout());
132: rightPanel.setLayout(new BorderLayout());
133:
134: controlPanel.setBorder(bevelTopBorder);
135:
136: rightPanel.add(dataPanel, BorderLayout.CENTER);
137: rightPanel.add(controlPanel, BorderLayout.SOUTH);
138: this .add(rightPanel, BorderLayout.EAST);
139:
140: dataPanel.setMinimumSize(new Dimension(
141: SizeConstants.PAGE_WIDTH,
142: SizeConstants.TITLE_PANEL_HEIGHT));
143: dataPanel.setMaximumSize(new Dimension(
144: SizeConstants.PAGE_WIDTH,
145: SizeConstants.TITLE_PANEL_HEIGHT));
146: dataPanel.setPreferredSize(new Dimension(
147: SizeConstants.PAGE_WIDTH,
148: SizeConstants.TITLE_PANEL_HEIGHT));
149:
150: initSidePanels();
151:
152: // Ctrl Panel
153: controlPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
154: controlPanel.add(cancelButton, null);
155: controlPanel.add(backButton, null);
156: controlPanel.add(nextButton, null);
157: controlPanel.add(finishButton, null);
158: backButton.setText(res.getString("button.back"));// "<< Back");
159: cancelButton.setText(res.getString("button.cancel"));// "Cancel");
160: nextButton.setText(res.getString("button.next"));// "Next >>");
161: finishButton.setText(ctx.getInstaller().getFinishButtonText());
162: finishButton.setEnabled(false);
163: setEventListeners();
164: setIcons();
165: }
166:
167: protected void initSidePanels() throws Exception {
168: initImagePanel();
169: initTitlePanel();
170: }
171:
172: protected void initTitlePanel() {
173: // title panel
174: titlePanel = new AIPanel();
175: titleLabel = new JLabel();
176:
177: titleLabel.setOpaque(false);
178: titlePanel.setLayout(new GridLayout(1, 1, 0, 0));
179: titlePanel.setBorder(bevelBottomBorder);
180: titlePanel.add(titleLabel);
181: titleLabel.setText(page.getDisplayText());
182: titleLabel.setFont(titleFont);
183:
184: titlePanel.setMinimumSize(new Dimension(
185: SizeConstants.PAGE_WIDTH,
186: SizeConstants.TITLE_PANEL_HEIGHT));
187: titlePanel.setMaximumSize(new Dimension(
188: SizeConstants.PAGE_WIDTH,
189: SizeConstants.TITLE_PANEL_HEIGHT));
190: titlePanel.setPreferredSize(new Dimension(
191: SizeConstants.PAGE_WIDTH,
192: SizeConstants.TITLE_PANEL_HEIGHT));
193: rightPanel.add(titlePanel, BorderLayout.NORTH);
194: }
195:
196: protected void initImagePanel() throws Exception {
197: imagePanel = new JLabel();
198: //imagePanel.setOpaque(false);
199: setImage(page.getImageResource());
200: imagePanel.setMinimumSize(new Dimension(
201: SizeConstants.IMAGE_PANEL_WIDTH,
202: SizeConstants.PAGE_HEIGHT));
203: imagePanel.setMaximumSize(new Dimension(
204: SizeConstants.IMAGE_PANEL_WIDTH,
205: SizeConstants.PAGE_HEIGHT));
206: imagePanel.setPreferredSize(new Dimension(
207: SizeConstants.IMAGE_PANEL_WIDTH,
208: SizeConstants.PAGE_HEIGHT));
209: this .add(imagePanel, BorderLayout.WEST);
210: }
211:
212: public abstract void instanceInit() throws Exception;
213:
214: public abstract void updateInputFields();
215:
216: public abstract void updateDefaultValues();
217:
218: public abstract boolean validateFields() throws ValidationException;
219:
220: public void setPageCompletionListener(
221: PageCompletionListener listener) {
222: this .listener = listener;
223: }
224:
225: private void setImage(String resource) throws Exception {
226: if (resource == null) {
227: resource = ctx.getInstaller().getDefaultImageResource();
228: }
229: imagePanel.setAlignmentY(JLabel.TOP_ALIGNMENT);
230: ImageIcon icon = getImage(resource);
231: imagePanel.setIcon(icon);
232:
233: }
234:
235: protected ImageIcon getImage(String resource) {
236: try {
237: if (resource != null) {
238: ByteArrayOutputStream baos = new ByteArrayOutputStream();
239: InputStream in = this .getClass().getResourceAsStream(
240: resource);
241: byte[] buffer = new byte[2048];
242: int read = -1;
243: while ((read = in.read(buffer)) != -1) {
244: baos.write(buffer, 0, read);
245: }
246: ImageIcon icon = new ImageIcon(baos.toByteArray());
247: return icon;
248: }
249: } catch (Exception ex) {
250: ctx.log("Can't load image resource:" + resource);
251: if (ctx.getInstaller().isVerbose()) {
252: ctx.log(ex);
253: }
254: }
255: return null;
256: }
257:
258: private void setEventListeners() {
259: backButton.addActionListener(new ActionListener() {
260: public void actionPerformed(ActionEvent e) {
261: listener.pageBack(page);
262: }
263: });
264: cancelButton.addActionListener(new ActionListener() {
265: public void actionPerformed(ActionEvent e) {
266: page.setAbort(true);
267: if (ctx.getInstaller().isVerbose()) {
268: ctx.log("Abort called");
269: }
270: listener.pageComplete(page);
271: }
272: });
273: nextButton.addActionListener(new ActionListener() {
274: public void actionPerformed(ActionEvent e) {
275: listener.pageComplete(page);
276:
277: }
278: });
279: finishButton.addActionListener(new ActionListener() {
280: public void actionPerformed(ActionEvent e) {
281: if (finishButton.getText().equals(
282: res.getString("button.exit"))) {
283: // TODO FindBugs this will prevent cleanup in
284: // FinalizerFilter
285: System.exit(0);
286: }
287: listener.pageComplete(page);
288: // ((SwingInstallerContext)ctx).getSwingRunner().finish();
289: }
290: });
291: }
292:
293: private void setIcons() {
294: backButton.setIcon(getImage("/resources/icons/back.png"));
295: cancelButton.setIcon(getImage("/resources/icons/cancel.png"));
296: nextButton.setIcon(getImage("/resources/icons/next.png"));
297: finishButton.setIcon(getImage("/resources/icons/finish.png"));
298: }
299:
300: public JButton getCancelButton() {
301: return cancelButton;
302: }
303:
304: public InstallerContext getCtx() {
305: return ctx;
306: }
307:
308: public JPanel getControlPanel() {
309: return controlPanel;
310: }
311:
312: public JLabel getImagePanel() {
313: return imagePanel;
314: }
315:
316: public JButton getNextButton() {
317: return nextButton;
318: }
319:
320: public JLabel getTitleLabel() {
321: return titleLabel;
322: }
323:
324: public JButton getFinishButton() {
325: return finishButton;
326: }
327:
328: public JButton getBackButton() {
329: return backButton;
330: }
331:
332: }
|