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 default value column width. */
67    private static final int VALUE_WIDTH = 250;
68  
69    /** The cell constraints that are used. */
70    private final CellConstraints cc = new CellConstraints();
71  
72    /** The default form builder (for general tasks). */
73    private final DefaultFormBuilder builder;
74  
75    /** The builder for the button list. */
76    private final ButtonBarBuilder2 bbBuilder;
77  
78    /** Flag to determine whether this panel has any buttons (= options). */
79    private boolean hasButtons;
80  
81    /** Component containing preview. */
82    private JComponent preview;
83  
84    /**
85     * Instantiates a new properties show panel factory.
86     * 
87     * @param keyWidth
88     *          the width of the key column
89     */
90    public PropertiesShowPanelFactory(int keyWidth) {
91      this(keyWidth, VALUE_WIDTH);
92    }
93  
94    /**
95     * Instantiates a new properties show panel factory.
96     * 
97     * @param keyWidth
98     *          the width of the key column
99     * @param valueWidth
100    *          the width of the value column
101    */
102   public PropertiesShowPanelFactory(int keyWidth, int valueWidth) {
103     FormLayout layout = new FormLayout("right:" + keyWidth + "dlu, 6dlu, f:"
104         + valueWidth + "dlu, 4dlu, right:default:grow, 40dlu", "15dlu");
105     builder = new DefaultFormBuilder(layout);
106     builder.setDefaultDialogBorder();
107     bbBuilder = new ButtonBarBuilder2();
108     bbBuilder.setLeftToRightButtonOrder(true);
109     init(null, 0);
110   }
111 
112   /**
113    * Simple constructor. No buttons are added.
114    */
115   public PropertiesShowPanelFactory() {
116     this(KEY_WIDTH, VALUE_WIDTH);
117   }
118 
119   /**
120    * Constructor with only one kind of buttons.
121    * 
122    * @param buttons
123    *          list of buttons
124    */
125   public PropertiesShowPanelFactory(List<JButton> buttons) {
126     this(KEY_WIDTH);
127     init(buttons, 0);
128   }
129 
130   /**
131    * Constructor for a single button.
132    * 
133    * @param button
134    *          the button
135    */
136   public PropertiesShowPanelFactory(JButton button) {
137     this();
138     List<JButton> buttons = new ArrayList<JButton>();
139     buttons.add(button);
140     init(buttons, 0);
141   }
142 
143   /**
144    * Default constructor.
145    * 
146    * @param keyWidth
147    *          the width of the column containing the keys
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(int keyWidth, List<JButton> buttons,
155       int numOfGeneralButtons) {
156     this(keyWidth);
157     init(buttons, numOfGeneralButtons);
158   }
159 
160   /**
161    * Comprehensive constructor.
162    * 
163    * @param keyWidth
164    *          the width of the column containing the keys
165    * @param valueWidth
166    *          the width of the column containing the values
167    * @param buttons
168    *          the list of buttons to be displayed
169    * @param numOfGeneralButtons
170    *          of general buttons (will be displayed separately), has to be in
171    *          [0, size of buttons list]
172    */
173   public PropertiesShowPanelFactory(int keyWidth, int valueWidth,
174       List<JButton> buttons, int numOfGeneralButtons) {
175     this(keyWidth, valueWidth);
176     init(buttons, numOfGeneralButtons);
177   }
178 
179   /**
180    * Default constructor.
181    * 
182    * @param buttons
183    *          the list of buttons to be displayed
184    * @param numOfGeneralButtons
185    *          of general buttons (will be displayed separately), has to be in
186    *          [0, size of buttons list]
187    */
188   public PropertiesShowPanelFactory(List<JButton> buttons,
189       int numOfGeneralButtons) {
190     this();
191     init(buttons, numOfGeneralButtons);
192   }
193 
194   /**
195    * Initializes factory.
196    * 
197    * @param buttons
198    *          the buttons to be added
199    * @param numOfGeneralButtons
200    *          the number of general buttons (to be separated from the rest)
201    */
202   private void init(List<JButton> buttons, int numOfGeneralButtons) {
203     if (buttons == null) {
204       return;
205     }
206     for (int i = 0; i < buttons.size(); i++) {
207       JButton button = buttons.get(i);
208       if (i == buttons.size() - numOfGeneralButtons) {
209         bbBuilder.addUnrelatedGap();
210       } else {
211         bbBuilder.addRelatedGap();
212       }
213       bbBuilder.addButton(button);
214     }
215     if (buttons.size() > 0) {
216       bbBuilder.getLayout().setColumnSpec(1, ColumnSpec.decode("right:d:grow"));
217       hasButtons = true;
218     }
219   }
220 
221   /**
222    * Wrapper for adding a separator.
223    * 
224    * @param separator
225    *          name of the separator
226    */
227   public void sep(String separator) {
228     builder.addSeparator(separator);
229     builder.nextLine();
230   }
231 
232   /**
233    * Add label and component.
234    * 
235    * @param label
236    *          the label to be added
237    * @param component
238    *          the associated (input) component
239    */
240   public void app(String label, Component component) {
241     builder.append(label, component);
242     builder.nextLine();
243   }
244 
245   /**
246    * Add label and information as a string.
247    * 
248    * @param label
249    *          the label
250    * @param object
251    *          the associated information, {@link Object#toString()} will be used
252    */
253   public void app(String label, Object object) {
254     builder.append(label, new JLabel(object.toString()));
255     builder.nextLine();
256   }
257 
258   /**
259    * Add label and component that spans multiple rows. Default row height is 40
260    * dlu.
261    * 
262    * @param label
263    *          the label
264    * @param component
265    *          the associated component
266    * @param rowSpan
267    *          the number of rows to be spanned (>=1)
268    */
269   public void app(String label, Component component, int rowSpan) {
270     app(label, component, rowSpan, "40dlu");
271   }
272 
273   /**
274    * Add label and component that spans multiple rows.
275    * 
276    * @param label
277    *          the label
278    * @param component
279    *          the associated component
280    * @param rowSpan
281    *          the number of rows to be spanned (>=1)
282    * @param rowHeight
283    *          the row height
284    */
285   public void app(String label, Component component, int rowSpan,
286       String rowHeight) {
287     builder.append(label);
288     for (int i = 0; i < rowSpan - 1; i++) {
289       builder.appendRow(RowSpec.decode(rowHeight));
290     }
291     builder.add(new JScrollPane(component),
292         cc.xywh(builder.getColumn(), builder.getRow(), 1, rowSpan));
293     builder.nextLine(rowSpan);
294   }
295 
296   /**
297    * Constructs overall panel.
298    * 
299    * @return the desired panel
300    */
301   public JPanel constructPanel() {
302     builder.appendSeparator(hasButtons ? "Options" : "");
303     builder.nextLine();
304     builder.appendRow(RowSpec.decode("top:20dlu"));
305     builder.add(bbBuilder.getPanel(), cc.xywh(BUTTON_BAR_COLUMN_INDEX,
306         builder.getRow(), BUTTON_BAR_COLUMN_WIDTH, BUTTON_BAR_ROW_HEIGHT));
307 
308     if (preview != null) {
309       builder.nextLine();
310       builder.appendRow(RowSpec.decode("10dlu"));
311       sep("Summary:");
312       builder.appendRow(RowSpec.decode("top:d:grow"));
313       builder.add(preview, cc.xywh(PREVIEW_COLUMN_INDEX, builder.getRow(),
314           PREVIEW_COLUMN_WIDTH, PREVIEW_ROW_HEIGHT));
315     }
316     return builder.getPanel();
317   }
318 
319   /**
320    * Add preview to properties panel.
321    * 
322    * @param component
323    *          the component containing the preview
324    */
325   public void appPreview(JComponent component) {
326     preview = component;
327   }
328 
329 }