View Javadoc

1   package com.melloware.jukes.gui.view.preferences;
2   
3   import java.awt.BorderLayout;
4   import java.awt.Dimension;
5   import java.awt.Graphics;
6   import java.awt.GridLayout;
7   import java.awt.Image;
8   
9   import javax.swing.BorderFactory;
10  import javax.swing.JButton;
11  import javax.swing.JCheckBox;
12  import javax.swing.JComboBox;
13  import javax.swing.JComponent;
14  import javax.swing.JLabel;
15  import javax.swing.JPanel;
16  import javax.swing.JProgressBar;
17  import javax.swing.JRadioButton;
18  import javax.swing.JScrollPane;
19  import javax.swing.JSlider;
20  import javax.swing.JTabbedPane;
21  import javax.swing.JTable;
22  import javax.swing.JTextField;
23  import javax.swing.JTree;
24  import javax.swing.ScrollPaneConstants;
25  import javax.swing.SwingConstants;
26  import javax.swing.tree.DefaultMutableTreeNode;
27  import javax.swing.tree.DefaultTreeModel;
28  import javax.swing.tree.TreeModel;
29  
30  import com.jgoodies.forms.factories.Borders;
31  import com.jgoodies.forms.layout.CellConstraints;
32  import com.jgoodies.forms.layout.FormLayout;
33  import com.jgoodies.looks.Options;
34  import com.jgoodies.uif.component.UIFTree;
35  import com.jgoodies.uif.panel.CardPanel;
36  import com.jgoodies.uif.util.ComponentTreeUtils;
37  import com.jgoodies.uifextras.util.ExtTable;
38  import com.jgoodies.uifextras.util.ThinBevelBorder;
39  
40  
41  /**
42   * A panel that provides a preview for a look using a bunch of sample widgets.
43   *
44   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
45   * @author Emil A. Lefkof III <info@melloware.com>
46   */
47  
48  final class LookAndFeelPreviewPanel extends CardPanel {
49  
50      private static final Dimension PREFERRED_SIZE = new Dimension(300, 170);
51      private static final String    PREVIEW_CARD   = "preview";
52      private static final String    FAKE_CARD      = "fake";
53  
54      private final JPanel delegate;
55      private Image buffer;
56      
57      
58      // Instance Creation ****************************************************
59  
60      /**
61       * Constructs a <code>LookAndFeelPreviewPanel</code>.
62       */
63      LookAndFeelPreviewPanel() {
64          delegate = buildSampleWidgetsPanel();
65          add(delegate, PREVIEW_CARD);
66          add(new JLabel("Hallo"), FAKE_CARD);
67      }
68  
69  
70      // Updating *************************************************************
71  
72      /**
73       * Updates the UI delegates of all sample widgets.
74       */
75      void updateComponentTree() {
76          ComponentTreeUtils.updateComponentTreeUI(delegate);
77      }
78  
79      /**
80       * Updates and repaints the UI delegates of all sample widgets.
81       */
82      void updateAndValidate() {
83          updateComponentTree();
84          ensureHasBuffer();
85          updateBuffer();
86      }
87  
88      public void paint(Graphics g) {
89          ensureHasBuffer();
90          g.drawImage(buffer, 0, 0, null);
91      }
92  
93      /**
94       * Ensures that a background image buffer has been created.
95       */
96      private void ensureHasBuffer() {
97          if (buffer != null)
98              return;
99          buffer = createImage(getWidth(), getHeight());
100         updateBuffer();
101     }
102 
103     /**
104      * Updates the background image buffer. Therefore switches to
105      * the preview card, paints to the buffer, and finally switches back 
106      * to the fake card.
107      */
108     private void updateBuffer() {
109         validate();
110         showCard(PREVIEW_CARD);
111 
112         int w = buffer.getWidth(null);
113         int h = buffer.getHeight(null);
114         Graphics g = buffer.getGraphics();
115         g.setClip(0, 0, w, h);
116         super.paint(g);
117         g.dispose();
118 
119         showCard(FAKE_CARD);
120     }
121 
122     // Building the Sample Widgets ******************************************************
123 
124     /**
125      * Builds a panel full of sample widgets.
126      */
127     private JPanel buildSampleWidgetsPanel() {
128         JComponent textfield = new JTextField("Margin, Baseline, Space");
129 
130         FormLayout fl = new FormLayout(
131                 "0:grow, 7dlu, 0:grow, 7dlu, 0:grow",
132                 "fill:min:grow, 7dlu, pref");
133         fl.setColumnGroups(new int[][] { { 1, 3, 5 } });
134         JPanel panel = new JPanel(fl);
135         panel.setBorder(BorderFactory.createCompoundBorder(
136                             new ThinBevelBorder(ThinBevelBorder.LOWERED),
137                             Borders.DIALOG_BORDER));
138         panel.setPreferredSize(PREFERRED_SIZE);
139 
140         CellConstraints cc = new CellConstraints();
141         panel.add(buildTree(),       cc.xy(1, 1));
142         panel.add(buildTable(),      cc.xy(3, 1));
143         panel.add(buildTabbedPane(), cc.xy(5, 1));
144 
145         panel.add(textfield, cc.xy(1, 3));
146         panel.add(buildComboBoxes(), cc.xy(3, 3));
147         panel.add(buildButtons(),    cc.xy(5, 3));
148 
149         return panel;
150     }
151 
152     /**
153      * Builds and answers a sample tree.
154      */
155     private JComponent buildTree() {
156         final JTree tree = new UIFTree(createPreviewTreeModel());
157         tree.expandRow(2);
158         return new JScrollPane(tree);
159     }
160 
161     /**
162      * Builds and answers a sample table.
163      */
164     private JComponent buildTable() {
165         JTable table =
166             new ExtTable(new String[][] { { "Ayler",   "Greenwich Village" }, {
167                                             "Coltrane", "Ascension" }, {
168                                             "Davis",    "In a Silent Way" }, {
169                                             "Sanders",  "Karma" }, {
170                                             "Shorter",  "Juju" },
171                     },
172                     new String[] { "Artist", "Title      " });
173 
174         table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
175         table.getColumnModel().getColumn(0).setPreferredWidth(55);
176         table.getColumnModel().getColumn(1).setPreferredWidth(110);
177         table.setRowSelectionInterval(2, 2);
178         JScrollPane scrollPane =
179             new JScrollPane(
180                 table,
181                 ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
182                 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
183         return scrollPane;
184     }
185 
186     /**
187      * Builds and answers the sample tabbed pane.
188      */
189     private JComponent buildTabbedPane() {
190         final int REDUCED_HEIGHT = 50;
191 
192         JRadioButton radio = new JRadioButton("Radio", true);
193         JCheckBox check = new JCheckBox("Check", true);
194         JLabel label = new JLabel("Label");
195         JProgressBar bar = new JProgressBar(SwingConstants.VERTICAL, 0, 100) {
196             public Dimension getPreferredSize() {
197                 return new Dimension(
198                     super.getPreferredSize().width,
199                     REDUCED_HEIGHT);
200             }
201         };
202         bar.setValue(50);
203         JSlider slider = new JSlider(SwingConstants.VERTICAL) {
204             public Dimension getPreferredSize() {
205                 Dimension size =
206                     new Dimension(
207                         super.getPreferredSize().width,
208                         REDUCED_HEIGHT);
209                 //System.out.println("pref size=" + size);				
210                 return size;
211             }
212         };
213 
214         FormLayout fl = new FormLayout(
215                 "pref:grow, 4dlu, pref, 4dlu, min",
216                 "pref, pref, 2dlu, top:pref:grow");
217         JPanel panel = new JPanel(fl);
218         CellConstraints cc = new CellConstraints();
219 
220         panel.add(radio,   cc.xy(1, 1));
221         panel.add(check,   cc.xy(1, 2));
222         panel.add(label,   cc.xy(1, 4));
223         panel.add(bar,     cc.xywh(3, 1, 1, 4));
224         panel.add(slider,  cc.xywh(5, 1, 1, 4));
225 
226         panel.setBorder(BorderFactory.createEmptyBorder(4, 10, 4, 10));
227 
228         JTabbedPane tabbedPane = new JTabbedPane();
229         tabbedPane.add("Tab1", panel);
230         tabbedPane.add("Tab2", new JLabel("Hidden"));
231         return tabbedPane;
232     }
233 
234     /**
235      * Builds and answers a panel with two combo boxes: one is editable the other is not.
236      */
237     private JComponent buildComboBoxes() {
238         JComboBox combobox1 = new JComboBox(new String[] { "Margin", "Two", "Three" });
239         combobox1.setEditable(true);
240         JComboBox combobox2 = new JComboBox(new String[] { "Height", "Three", "Four" });
241 
242         JPanel panel = new JPanel(new GridLayout(1, 2, 6, 0));
243         panel.add(combobox1);
244         panel.add(combobox2);
245         return panel;
246     }
247 
248     /**
249      * Builds and answers a panel that contains two buttons: one is wide, the
250      * other has the JGoodies Look narrow property set.
251      */
252     private JComponent buildButtons() {
253         JButton wide = new JButton("Wide");
254         wide.setMnemonic('W');
255         JButton narrow = new JButton("Narrow");
256         Options.setUseNarrowButtons(true);
257         JPanel panel = new JPanel(new BorderLayout(6, 0));
258         panel.add(wide, BorderLayout.CENTER);
259         panel.add(narrow, BorderLayout.EAST);
260         return panel;
261     }
262 
263     /**
264      * Creates and answers a sample tree model.
265      */
266     private TreeModel createPreviewTreeModel() {
267         DefaultMutableTreeNode root = new DefaultMutableTreeNode("Musicians");
268         DefaultMutableTreeNode parent;
269 
270         parent = new DefaultMutableTreeNode("Piano");
271         root.add(parent);
272         parent.add(new DefaultMutableTreeNode("Alexander von Schlippenbach"));
273         parent.add(new DefaultMutableTreeNode("McCoy Tyner"));
274         parent.add(new DefaultMutableTreeNode("Sun Ra"));
275 
276         parent = new DefaultMutableTreeNode("Saxophon");
277         root.add(parent);
278         parent.add(new DefaultMutableTreeNode("Albert Ayler"));
279         parent.add(new DefaultMutableTreeNode("Archie Shepp"));
280         parent.add(new DefaultMutableTreeNode("Charlie Parker"));
281         parent.add(new DefaultMutableTreeNode("John Coltrane"));
282         parent.add(new DefaultMutableTreeNode("Ornette Coleman"));
283         parent.add(new DefaultMutableTreeNode("Sonny Rollins"));
284 
285         return new DefaultTreeModel(root);
286     }
287 
288 }