View Javadoc

1   package org.qdwizard;
2   
3   import java.awt.Color;
4   import java.awt.Dimension;
5   import java.awt.Font;
6   import java.awt.Graphics2D;
7   import java.awt.GridLayout;
8   import java.awt.RenderingHints;
9   import java.awt.event.ActionListener;
10  import java.util.Locale;
11  import java.util.ResourceBundle;
12  
13  import javax.swing.BorderFactory;
14  import javax.swing.Box;
15  import javax.swing.BoxLayout;
16  import javax.swing.JButton;
17  import javax.swing.JLabel;
18  import javax.swing.JPanel;
19  
20  /***
21   * Action panel
22   * <p>
23   * contains a problem area where problems are displayed and a buttons area
24   * (Previous, Next, Finish, Cancel)
25   * 
26   * @author Bertrand Florat
27   * @created 1 may 2006
28   */
29  class ActionsPanel extends JPanel {
30  
31  	private static final long serialVersionUID = 1L;
32  
33  	/*** Problem text area */
34  	JLabel jlProblem;
35  
36  	JButton jbPrevious;
37  
38  	JButton jbNext;
39  
40  	JButton jbFinish;
41  
42  	JButton jbCancel;
43  
44  	private Color backgroundColor;
45  
46  	private Color backgroundColorProblem;
47  
48  	ResourceBundle bundle;
49  
50  	/*** Associated action listener */
51  	ActionListener al;
52  
53  	/***
54  	 * @param al
55  	 *            associated action listener
56  	 */
57  	public ActionsPanel(ActionListener al, Locale locale) {
58  		// set locale
59  		try {
60  			bundle = ResourceBundle.getBundle("qdwizard", locale);
61  		} catch (Exception e) {
62  			System.err.println("unable to find resource bundle qdwizard.properties for locale "
63  					+ locale + " in classpath");
64  			e.printStackTrace();
65  		}
66  
67  		backgroundColor = Color.WHITE;
68  		backgroundColorProblem = Color.WHITE;
69  
70  		// Problem panel
71  		jlProblem = new JLabel();
72  		jlProblem.setForeground(Color.RED);
73  		jlProblem.setFont(new Font("Dialog", Font.BOLD, 12)); //$NON-NLS-1$
74  		jlProblem.setHorizontalAlignment(JLabel.CENTER);
75  
76  		// Action buttons
77  		JPanel jpButtons = new JPanel();
78  		jpButtons.setLayout(new BoxLayout(jpButtons, BoxLayout.X_AXIS));
79  		Dimension dimButtons = new Dimension(150, 20);
80  		jbPrevious = new JButton("< " + getMessage("Previous"));
81  		jbPrevious.setPreferredSize(dimButtons);
82  		jbPrevious.addActionListener(al);
83  		jbPrevious.setActionCommand("Prev"); //$NON-NLS-1$
84  
85  		jbNext = new JButton(getMessage("Next") + " >");
86  		jbNext.addActionListener(al);
87  		jbNext.setActionCommand("Next"); //$NON-NLS-1$
88  		jbNext.setPreferredSize(dimButtons);
89  
90  		jbFinish = new JButton(getMessage("Finish"));
91  		jbFinish.addActionListener(al);
92  		jbFinish.setActionCommand("Finish"); //$NON-NLS-1$
93  		jbFinish.setPreferredSize(dimButtons);
94  
95  		jbCancel = new JButton(getMessage("Cancel"));
96  		jbCancel.addActionListener(al);
97  		jbCancel.setActionCommand("Cancel"); //$NON-NLS-1$
98  		jbCancel.setPreferredSize(dimButtons);
99  
100 		jpButtons.add(Box.createHorizontalStrut(10)); //$NON-NLS-1$
101 		jpButtons.add(Box.createHorizontalGlue()); //$NON-NLS-1$
102 		jpButtons.add(jbPrevious); //$NON-NLS-1$
103 		jpButtons.add(Box.createHorizontalStrut(5)); //$NON-NLS-1$
104 		jpButtons.add(jbNext); //$NON-NLS-1$
105 		jpButtons.add(Box.createHorizontalStrut(10)); //$NON-NLS-1$
106 		jpButtons.add(jbFinish); //$NON-NLS-1$
107 		jpButtons.add(Box.createHorizontalStrut(20)); //$NON-NLS-1$
108 		jpButtons.add(jbCancel); //$NON-NLS-1$
109 		jpButtons.add(Box.createHorizontalStrut(10)); //$NON-NLS-1$
110 		jpButtons.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
111 
112 		jpButtons.setOpaque(false);
113 		jlProblem.setOpaque(false);
114 		setOpaque(false);
115 
116 		// Main panel
117 		setLayout(new GridLayout(2, 1));
118 		add(jlProblem); //$NON-NLS-1$
119 		add(jpButtons); //$NON-NLS-1$
120 	}
121 
122 	/***
123 	 * Set buttons states
124 	 * 
125 	 * @param bNext
126 	 * @param bFinish
127 	 */
128 	void setState(boolean bPrevious, boolean bNext, boolean bFinish, boolean bCancel) {
129 		jbPrevious.setEnabled(bPrevious);
130 		jbFinish.setEnabled(bFinish);
131 		jbNext.setEnabled(bNext);
132 		jbCancel.setEnabled(bCancel);
133 	}
134 
135 	void setProblem(String problem) {
136 		String sProblem = problem;
137 		jlProblem.setText(sProblem);
138 	}
139 
140 	/***
141 	 * I18n utility
142 	 * 
143 	 * @param key
144 	 * @return
145 	 */
146 	private String getMessage(String key) {
147 		try {
148 			return bundle.getString(key);
149 		} catch (Exception e) {
150 			System.err.println("missing resource " + key);
151 			e.printStackTrace();
152 			return key; // don't fail on missing resource or null bundle, return
153 						// the given key instead
154 		}
155 	}
156 
157 	/***
158 	 * Set the background color of the ActionPanel
159 	 * 
160 	 * @param color
161 	 */
162 	public void setBackgroundColor(Color color) {
163 		this.backgroundColor = color;
164 	}
165 
166 	/***
167 	 * Set the background color of the ActionPanel's Problem notification area
168 	 * 
169 	 * @param color
170 	 */
171 	public void setProblemBackgroundColor(Color color) {
172 		this.backgroundColorProblem = color;
173 	}
174 
175 	/***
176 	 * Set the background color of the ActionPanel's Problem notification area
177 	 * 
178 	 * @param color
179 	 */
180 	public void setProblemTextColor(Color color) {
181 		jlProblem.setForeground(color);
182 	}
183 
184 	public void paint(java.awt.Graphics g) {
185 		Graphics2D g2D = (Graphics2D) g;
186 		g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
187 		java.awt.Rectangle rect = getBounds();
188 		g2D.setColor(backgroundColor);
189 		g2D.fillRect(0, 0, rect.width, rect.height);
190 
191 		if ((jlProblem != null) && (jlProblem.getText() != null)
192 				&& jlProblem.getText().length() > 0) {
193 			rect = jlProblem.getBounds();
194 			g2D.setColor(backgroundColorProblem);
195 			g2D.fillRect(rect.x, rect.y, rect.width, rect.height);
196 		}
197 
198 		super.paint(g);
199 
200 		g2D.setColor(java.awt.Color.LIGHT_GRAY);
201 		g2D.drawLine(rect.x, 0, rect.width, 0);
202 	}
203 }