View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
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   */
16  package p3j.gui.panels;
17  
18  import java.awt.Component;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import javax.swing.JButton;
23  import javax.swing.JComponent;
24  import javax.swing.JLabel;
25  import javax.swing.JPanel;
26  import javax.swing.JScrollPane;
27  
28  import com.jgoodies.forms.builder.ButtonBarBuilder2;
29  import com.jgoodies.forms.builder.DefaultFormBuilder;
30  import com.jgoodies.forms.layout.CellConstraints;
31  import com.jgoodies.forms.layout.ColumnSpec;
32  import com.jgoodies.forms.layout.FormLayout;
33  import com.jgoodies.forms.layout.RowSpec;
34  
35  /**
36   * Panel to conveniently display the properties of a PPPM entity.
37   * 
38   * Created: August 25, 2008
39   * 
40   * @author Christina Bohk
41   * @author Roland Ewald
42   */
43  public class PropertiesShowPanelFactory {
44  
45  	/** The column at which the preview panel is inserted. */
46  	private static final int PREVIEW_COLUMN_INDEX = 1;
47  
48  	/** The height of the preview panel (in rows). */
49  	private static final int PREVIEW_ROW_HEIGHT = 1;
50  
51  	/** The width of the preview panel (in columns). */
52  	private static final int PREVIEW_COLUMN_WIDTH = 5;
53  
54  	/** The height of the button bar (in rows). */
55  	private static final int BUTTON_BAR_ROW_HEIGHT = 1;
56  
57  	/** The width of the button bar (in columns). */
58  	private static final int BUTTON_BAR_COLUMN_WIDTH = 5;
59  
60  	/** The column at which the button bar is inserted. */
61  	private static final int BUTTON_BAR_COLUMN_INDEX = 1;
62  
63  	/** The default key column width. */
64  	private static final int KEY_WIDTH = 100;
65  
66  	/** The cell constraints that are used. */
67  	private final CellConstraints cc = new CellConstraints();
68  
69  	/** The default form builder (for general tasks). */
70  	private final DefaultFormBuilder builder;
71  
72  	/** The builder for the button list. */
73  	private final ButtonBarBuilder2 bbBuilder;
74  
75  	/** Flag to determine whether this panel has any buttons (= options). */
76  	private boolean hasButtons;
77  
78  	/** Component containing preview. */
79  	private JComponent preview;
80  
81  	/**
82  	 * The key width.
83  	 * 
84  	 * @param keyWidth
85  	 *          the width of the key column
86  	 */
87  	public PropertiesShowPanelFactory(int keyWidth) {
88  		FormLayout layout = new FormLayout("right:" + keyWidth
89  		    + "dlu, 6dlu, f:250dlu, 4dlu, right:default:grow, 40dlu", "15dlu");
90  		builder = new DefaultFormBuilder(layout);
91  		builder.setDefaultDialogBorder();
92  		bbBuilder = new ButtonBarBuilder2();
93  		bbBuilder.setLeftToRightButtonOrder(true);
94  		init(null, 0);
95  	}
96  
97  	/**
98  	 * Simple constructor. No buttons are added.
99  	 */
100 	public PropertiesShowPanelFactory() {
101 		this(KEY_WIDTH);
102 	}
103 
104 	/**
105 	 * Constructor with only one kind of buttons.
106 	 * 
107 	 * @param buttons
108 	 *          list of buttons
109 	 */
110 	public PropertiesShowPanelFactory(List<JButton> buttons) {
111 		this(KEY_WIDTH);
112 		init(buttons, 0);
113 	}
114 
115 	/**
116 	 * Constructor for a single button.
117 	 * 
118 	 * @param button
119 	 *          the button
120 	 */
121 	public PropertiesShowPanelFactory(JButton button) {
122 		this();
123 		List<JButton> buttons = new ArrayList<JButton>();
124 		buttons.add(button);
125 		init(buttons, 0);
126 	}
127 
128 	/**
129 	 * Default constructor.
130 	 * 
131 	 * @param keyWidth
132 	 *          the width of the column containing the keys
133 	 * @param buttons
134 	 *          the list of buttons to be displayed
135 	 * @param numOfGeneralButtons
136 	 *          of general buttons (will be displayed separately), has to be in
137 	 *          [0, size of buttons list]
138 	 */
139 	public PropertiesShowPanelFactory(int keyWidth, List<JButton> buttons,
140 	    int numOfGeneralButtons) {
141 		this(keyWidth);
142 		init(buttons, numOfGeneralButtons);
143 	}
144 
145 	/**
146 	 * Default constructor.
147 	 * 
148 	 * @param buttons
149 	 *          the list of buttons to be displayed
150 	 * @param numOfGeneralButtons
151 	 *          of general buttons (will be displayed separately), has to be in
152 	 *          [0, size of buttons list]
153 	 */
154 	public PropertiesShowPanelFactory(List<JButton> buttons,
155 	    int numOfGeneralButtons) {
156 		this();
157 		init(buttons, numOfGeneralButtons);
158 	}
159 
160 	/**
161 	 * Initializes factory.
162 	 * 
163 	 * @param buttons
164 	 *          the buttons to be added
165 	 * @param numOfGeneralButtons
166 	 *          the number of general buttons (to be separated from the rest)
167 	 */
168 	private void init(List<JButton> buttons, int numOfGeneralButtons) {
169 		if (buttons == null) {
170 			return;
171 		}
172 		for (int i = 0; i < buttons.size(); i++) {
173 			JButton button = buttons.get(i);
174 			if (i == buttons.size() - numOfGeneralButtons) {
175 				bbBuilder.addUnrelatedGap();
176 			} else {
177 				bbBuilder.addRelatedGap();
178 			}
179 			bbBuilder.addButton(button);
180 		}
181 		if (buttons.size() > 0) {
182 			bbBuilder.getLayout().setColumnSpec(1, ColumnSpec.decode("right:d:grow"));
183 			hasButtons = true;
184 		}
185 	}
186 
187 	/**
188 	 * Wrapper for adding a separator.
189 	 * 
190 	 * @param separator
191 	 *          name of the separator
192 	 */
193 	public void sep(String separator) {
194 		builder.addSeparator(separator);
195 		builder.nextLine();
196 	}
197 
198 	/**
199 	 * Add label and component.
200 	 * 
201 	 * @param label
202 	 *          the label to be added
203 	 * @param component
204 	 *          the associated (input) component
205 	 */
206 	public void app(String label, Component component) {
207 		builder.append(label, component);
208 		builder.nextLine();
209 	}
210 
211 	/**
212 	 * Add label and information as a string.
213 	 * 
214 	 * @param label
215 	 *          the label
216 	 * @param object
217 	 *          the associated information, {@link Object#toString()} will be used
218 	 */
219 	public void app(String label, Object object) {
220 		builder.append(label, new JLabel(object.toString()));
221 		builder.nextLine();
222 	}
223 
224 	/**
225 	 * Add label and component that spans multiple rows. Default row height is 40
226 	 * dlu.
227 	 * 
228 	 * @param label
229 	 *          the label
230 	 * @param component
231 	 *          the associated component
232 	 * @param rowSpan
233 	 *          the number of rows to be spanned (>=1)
234 	 */
235 	public void app(String label, Component component, int rowSpan) {
236 		app(label, component, rowSpan, "40dlu");
237 	}
238 
239 	/**
240 	 * Add label and component that spans multiple rows.
241 	 * 
242 	 * @param label
243 	 *          the label
244 	 * @param component
245 	 *          the associated component
246 	 * @param rowSpan
247 	 *          the number of rows to be spanned (>=1)
248 	 * @param rowHeight
249 	 *          the row height
250 	 */
251 	public void app(String label, Component component, int rowSpan,
252 	    String rowHeight) {
253 		builder.append(label);
254 		for (int i = 0; i < rowSpan - 1; i++) {
255 			builder.appendRow(RowSpec.decode(rowHeight));
256 		}
257 		builder.add(new JScrollPane(component),
258 		    cc.xywh(builder.getColumn(), builder.getRow(), 1, rowSpan));
259 		builder.nextLine(rowSpan);
260 	}
261 
262 	/**
263 	 * Constructs overall panel.
264 	 * 
265 	 * @return the desired panel
266 	 */
267 	public JPanel constructPanel() {
268 		builder.appendSeparator(hasButtons ? "Options" : "");
269 		builder.nextLine();
270 		builder.appendRow(RowSpec.decode("top:20dlu"));
271 		builder.add(bbBuilder.getPanel(), cc.xywh(BUTTON_BAR_COLUMN_INDEX,
272 		    builder.getRow(), BUTTON_BAR_COLUMN_WIDTH, BUTTON_BAR_ROW_HEIGHT));
273 
274 		if (preview != null) {
275 			builder.nextLine();
276 			builder.appendRow(RowSpec.decode("10dlu"));
277 			sep("Summary:");
278 			builder.appendRow(RowSpec.decode("top:d:grow"));
279 			builder.add(preview, cc.xywh(PREVIEW_COLUMN_INDEX, builder.getRow(),
280 			    PREVIEW_COLUMN_WIDTH, PREVIEW_ROW_HEIGHT));
281 		}
282 		return builder.getPanel();
283 	}
284 
285 	/**
286 	 * Add preview to properties panel.
287 	 * 
288 	 * @param component
289 	 *          the component containing the preview
290 	 */
291 	public void appPreview(JComponent component) {
292 		preview = component;
293 	}
294 
295 }