1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hivegui.dialog; |
16 | |
17 | import java.awt.LayoutManager; |
18 | import java.util.ArrayList; |
19 | import java.util.List; |
20 | |
21 | import javax.swing.Box; |
22 | import javax.swing.BoxLayout; |
23 | import javax.swing.JButton; |
24 | import javax.swing.JComponent; |
25 | import javax.swing.JPanel; |
26 | import javax.swing.SwingConstants; |
27 | import javax.swing.border.EmptyBorder; |
28 | |
29 | import org.jdesktop.layout.LayoutStyle; |
30 | |
31 | public abstract class AbstractCommandPanelAdder implements CommandPanelAdder |
32 | { |
33 | protected AbstractCommandPanelAdder(Class<? extends LayoutManager> layoutClass) |
34 | { |
35 | _layoutClass = layoutClass; |
36 | } |
37 | |
38 | final public Class<? extends LayoutManager> getLayoutClass() |
39 | { |
40 | return _layoutClass; |
41 | } |
42 | |
43 | public void addCommands(JComponent container, JButton... buttons) |
44 | { |
45 | List<JButton> actualButtons = new ArrayList<JButton>(); |
46 | for (JButton button: buttons) |
47 | { |
48 | if (button != null) |
49 | { |
50 | actualButtons.add(button); |
51 | } |
52 | } |
53 | JPanel commands = createCommandPanel(actualButtons.toArray(new JButton[0])); |
54 | addCommandPanel(container, commands); |
55 | } |
56 | |
57 | abstract protected void addCommandPanel(JComponent container, JPanel commands); |
58 | |
59 | static protected JPanel createCommandPanel(JButton[] buttons) |
60 | { |
61 | LayoutStyle style = LayoutStyle.getSharedInstance(); |
62 | JPanel commandPanel = new JPanel(); |
63 | commandPanel.setLayout(new BoxLayout(commandPanel, BoxLayout.X_AXIS)); |
64 | commandPanel.add(Box.createHorizontalGlue()); |
65 | int vgap = 0; |
66 | |
67 | for (int i = 0; i < buttons.length; i++) |
68 | { |
69 | JButton left = buttons[i]; |
70 | commandPanel.add(left); |
71 | if (i != buttons.length - 1) |
72 | { |
73 | JButton right = buttons[i + 1]; |
74 | int gap = style.getPreferredGap(left, |
75 | right, |
76 | LayoutStyle.RELATED, |
77 | SwingConstants.EAST, |
78 | commandPanel); |
79 | commandPanel.add(Box.createHorizontalStrut(gap)); |
80 | } |
81 | else |
82 | { |
83 | int hgap = style.getContainerGap(left, SwingConstants.EAST, commandPanel); |
84 | vgap = style.getContainerGap(left, SwingConstants.SOUTH, commandPanel); |
85 | commandPanel.add(Box.createHorizontalStrut(hgap)); |
86 | } |
87 | } |
88 | commandPanel.setBorder(new EmptyBorder(VGAP, 0, vgap, 0)); |
89 | return commandPanel; |
90 | } |
91 | |
92 | private final Class<? extends LayoutManager> _layoutClass; |
93 | } |