ComboBoxDemo.java revision 14246:0be735572230
1/*
2 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package com.sun.swingset3.demos.combobox;
24
25import java.awt.*;
26import java.awt.event.ActionEvent;
27import java.awt.event.ActionListener;
28import java.util.HashMap;
29import java.util.Map;
30import javax.accessibility.AccessibleRelation;
31import javax.swing.*;
32import javax.swing.border.BevelBorder;
33
34import com.sun.swingset3.demos.ResourceManager;
35import com.sun.swingset3.DemoProperties;
36
37/**
38 * JComboBox Demo
39 *
40 * @author Jeff Dinkins
41 * @version 1.13 11/17/05
42 */
43@DemoProperties(
44        value = "JComboBox Demo",
45        category = "Controls",
46        description = "Demonstrates JComboBox, a control which allows the user to make a selection from a popup list",
47        sourceFiles = {
48            "com/sun/swingset3/demos/combobox/ComboBoxDemo.java",
49            "com/sun/swingset3/demos/ResourceManager.java",
50            "com/sun/swingset3/demos/combobox/resources/ComboBoxDemo.properties",
51            "com/sun/swingset3/demos/combobox/resources/images/brenteyes.jpg",
52            "com/sun/swingset3/demos/combobox/resources/images/brenthair.jpg",
53            "com/sun/swingset3/demos/combobox/resources/images/brentmouth.jpg",
54            "com/sun/swingset3/demos/combobox/resources/images/ComboBoxDemo.gif",
55            "com/sun/swingset3/demos/combobox/resources/images/georgeseyes.jpg",
56            "com/sun/swingset3/demos/combobox/resources/images/georgeshair.jpg",
57            "com/sun/swingset3/demos/combobox/resources/images/georgesmouth.jpg",
58            "com/sun/swingset3/demos/combobox/resources/images/hanseyes.jpg",
59            "com/sun/swingset3/demos/combobox/resources/images/hanshair.jpg",
60            "com/sun/swingset3/demos/combobox/resources/images/hansmouth.jpg",
61            "com/sun/swingset3/demos/combobox/resources/images/howardeyes.jpg",
62            "com/sun/swingset3/demos/combobox/resources/images/howardhair.jpg",
63            "com/sun/swingset3/demos/combobox/resources/images/howardmouth.jpg",
64            "com/sun/swingset3/demos/combobox/resources/images/jameseyes.jpg",
65            "com/sun/swingset3/demos/combobox/resources/images/jameshair.jpg",
66            "com/sun/swingset3/demos/combobox/resources/images/jamesmouth.jpg",
67            "com/sun/swingset3/demos/combobox/resources/images/jeffeyes.jpg",
68            "com/sun/swingset3/demos/combobox/resources/images/jeffhair.jpg",
69            "com/sun/swingset3/demos/combobox/resources/images/jeffmouth.jpg",
70            "com/sun/swingset3/demos/combobox/resources/images/joneyes.jpg",
71            "com/sun/swingset3/demos/combobox/resources/images/jonhair.jpg",
72            "com/sun/swingset3/demos/combobox/resources/images/jonmouth.jpg",
73            "com/sun/swingset3/demos/combobox/resources/images/laraeyes.jpg",
74            "com/sun/swingset3/demos/combobox/resources/images/larahair.jpg",
75            "com/sun/swingset3/demos/combobox/resources/images/laramouth.jpg",
76            "com/sun/swingset3/demos/combobox/resources/images/larryeyes.jpg",
77            "com/sun/swingset3/demos/combobox/resources/images/larryhair.jpg",
78            "com/sun/swingset3/demos/combobox/resources/images/larrymouth.jpg",
79            "com/sun/swingset3/demos/combobox/resources/images/lisaeyes.jpg",
80            "com/sun/swingset3/demos/combobox/resources/images/lisahair.jpg",
81            "com/sun/swingset3/demos/combobox/resources/images/lisamouth.jpg",
82            "com/sun/swingset3/demos/combobox/resources/images/michaeleyes.jpg",
83            "com/sun/swingset3/demos/combobox/resources/images/michaelhair.jpg",
84            "com/sun/swingset3/demos/combobox/resources/images/michaelmouth.jpg",
85            "com/sun/swingset3/demos/combobox/resources/images/philipeyes.jpg",
86            "com/sun/swingset3/demos/combobox/resources/images/philiphair.jpg",
87            "com/sun/swingset3/demos/combobox/resources/images/philipmouth.jpg",
88            "com/sun/swingset3/demos/combobox/resources/images/scotteyes.jpg",
89            "com/sun/swingset3/demos/combobox/resources/images/scotthair.jpg",
90            "com/sun/swingset3/demos/combobox/resources/images/scottmouth.jpg"
91        }
92)
93public class ComboBoxDemo extends JPanel implements ActionListener {
94
95    private static final Dimension VGAP15 = new Dimension(1, 15);
96    private static final Dimension HGAP20 = new Dimension(20, 1);
97    private static final Dimension VGAP20 = new Dimension(1, 20);
98    private static final Dimension HGAP30 = new Dimension(30, 1);
99    private static final Dimension VGAP30 = new Dimension(1, 30);
100
101    private final ResourceManager resourceManager = new ResourceManager(this.getClass());
102    public static final String DEMO_TITLE = ComboBoxDemo.class.getAnnotation(DemoProperties.class).value();
103
104    private Face face;
105    private JLabel faceLabel;
106
107    private JComboBox<String> hairCB;
108    private JComboBox<String> eyesCB;
109    private JComboBox<String> mouthCB;
110
111    private JComboBox<String> presetCB;
112
113    private final Map<String, Object> parts = new HashMap<>();
114
115    /**
116     * main method allows us to run as a standalone demo.
117     *
118     * @param args
119     */
120    public static void main(String[] args) {
121        JFrame frame = new JFrame(DEMO_TITLE);
122
123        frame.getContentPane().add(new ComboBoxDemo());
124        frame.setPreferredSize(new Dimension(800, 600));
125        frame.pack();
126        frame.setLocationRelativeTo(null);
127        frame.setVisible(true);
128    }
129
130    /**
131     * ComboBoxDemo Constructor
132     */
133    public ComboBoxDemo() {
134        createComboBoxDemo();
135    }
136
137    private void createComboBoxDemo() {
138        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
139
140        JPanel innerPanel = new JPanel();
141        innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.X_AXIS));
142
143        add(Box.createRigidArea(VGAP20));
144        add(innerPanel);
145        add(Box.createRigidArea(VGAP20));
146
147        innerPanel.add(Box.createRigidArea(HGAP20));
148
149        // Create a panel to hold buttons
150        JPanel comboBoxPanel = new JPanel() {
151            @Override
152            public Dimension getMaximumSize() {
153                return new Dimension(getPreferredSize().width, super.getMaximumSize().height);
154            }
155        };
156        comboBoxPanel.setLayout(new BoxLayout(comboBoxPanel, BoxLayout.Y_AXIS));
157
158        comboBoxPanel.add(Box.createRigidArea(VGAP15));
159
160        JLabel l = (JLabel) comboBoxPanel.add(new JLabel(resourceManager.getString("ComboBoxDemo.presets")));
161        l.setAlignmentX(JLabel.LEFT_ALIGNMENT);
162        comboBoxPanel.add(presetCB = createPresetComboBox());
163        presetCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
164        l.setLabelFor(presetCB);
165        comboBoxPanel.add(Box.createRigidArea(VGAP30));
166
167        l = (JLabel) comboBoxPanel.add(new JLabel(resourceManager.getString("ComboBoxDemo.hair_description")));
168        l.setAlignmentX(JLabel.LEFT_ALIGNMENT);
169        comboBoxPanel.add(hairCB = createHairComboBox());
170        hairCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
171        l.setLabelFor(hairCB);
172        comboBoxPanel.add(Box.createRigidArea(VGAP15));
173
174        l = (JLabel) comboBoxPanel.add(new JLabel(resourceManager.getString("ComboBoxDemo.eyes_description")));
175        l.setAlignmentX(JLabel.LEFT_ALIGNMENT);
176        comboBoxPanel.add(eyesCB = createEyesComboBox());
177        eyesCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
178        l.setLabelFor(eyesCB);
179        comboBoxPanel.add(Box.createRigidArea(VGAP15));
180
181        l = (JLabel) comboBoxPanel.add(new JLabel(resourceManager.getString("ComboBoxDemo.mouth_description")));
182        l.setAlignmentX(JLabel.LEFT_ALIGNMENT);
183        comboBoxPanel.add(mouthCB = createMouthComboBox());
184
185        mouthCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
186        l.setLabelFor(mouthCB);
187        comboBoxPanel.add(Box.createRigidArea(VGAP15));
188
189        // Fill up the remaining space
190        comboBoxPanel.add(new JPanel(new BorderLayout()));
191
192        // Create and place the Face.
193        face = new Face();
194        JPanel facePanel = new JPanel();
195        facePanel.setLayout(new BorderLayout());
196        facePanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
197
198        faceLabel = new JLabel(face);
199        facePanel.add(faceLabel, BorderLayout.CENTER);
200        // Indicate that the face panel is controlled by the hair, eyes and
201        // mouth combo boxes.
202        Object[] controlledByObjects = new Object[3];
203        controlledByObjects[0] = hairCB;
204        controlledByObjects[1] = eyesCB;
205        controlledByObjects[2] = mouthCB;
206        AccessibleRelation controlledByRelation
207                = new AccessibleRelation(AccessibleRelation.CONTROLLED_BY_PROPERTY,
208                        controlledByObjects);
209        facePanel.getAccessibleContext().getAccessibleRelationSet().add(controlledByRelation);
210
211        // Indicate that the hair, eyes and mouth combo boxes are controllers
212        // for the face panel.
213        AccessibleRelation controllerForRelation
214                = new AccessibleRelation(AccessibleRelation.CONTROLLER_FOR_PROPERTY,
215                        facePanel);
216        hairCB.getAccessibleContext().getAccessibleRelationSet().add(controllerForRelation);
217        eyesCB.getAccessibleContext().getAccessibleRelationSet().add(controllerForRelation);
218        mouthCB.getAccessibleContext().getAccessibleRelationSet().add(controllerForRelation);
219
220        // add buttons and image panels to inner panel
221        innerPanel.add(comboBoxPanel);
222        innerPanel.add(Box.createRigidArea(HGAP30));
223        innerPanel.add(facePanel);
224        innerPanel.add(Box.createRigidArea(HGAP20));
225
226        // load up the face parts
227        addFace("brent", resourceManager.getString("ComboBoxDemo.brent"));
228        addFace("georges", resourceManager.getString("ComboBoxDemo.georges"));
229        addFace("hans", resourceManager.getString("ComboBoxDemo.hans"));
230        addFace("howard", resourceManager.getString("ComboBoxDemo.howard"));
231        addFace("james", resourceManager.getString("ComboBoxDemo.james"));
232        addFace("jeff", resourceManager.getString("ComboBoxDemo.jeff"));
233        addFace("jon", resourceManager.getString("ComboBoxDemo.jon"));
234        addFace("lara", resourceManager.getString("ComboBoxDemo.lara"));
235        addFace("larry", resourceManager.getString("ComboBoxDemo.larry"));
236        addFace("lisa", resourceManager.getString("ComboBoxDemo.lisa"));
237        addFace("michael", resourceManager.getString("ComboBoxDemo.michael"));
238        addFace("philip", resourceManager.getString("ComboBoxDemo.philip"));
239        addFace("scott", resourceManager.getString("ComboBoxDemo.scott"));
240
241        // set the default face
242        presetCB.setSelectedIndex(0);
243    }
244
245    private void addFace(String name, String i18n_name) {
246        ImageIcon i;
247        String i18n_hair = resourceManager.getString("ComboBoxDemo.hair");
248        String i18n_eyes = resourceManager.getString("ComboBoxDemo.eyes");
249        String i18n_mouth = resourceManager.getString("ComboBoxDemo.mouth");
250
251        parts.put(i18n_name, name); // i18n name lookup
252        parts.put(name, i18n_name); // reverse name lookup
253
254        i = resourceManager.createImageIcon(name + "hair.jpg", i18n_name + i18n_hair);
255        parts.put(name + "hair", i);
256
257        i = resourceManager.createImageIcon(name + "eyes.jpg", i18n_name + i18n_eyes);
258        parts.put(name + "eyes", i);
259
260        i = resourceManager.createImageIcon(name + "mouth.jpg", i18n_name + i18n_mouth);
261        parts.put(name + "mouth", i);
262    }
263
264    private JComboBox<String> createHairComboBox() {
265        JComboBox<String> cb = new JComboBox<>();
266        fillComboBox(cb);
267        cb.addActionListener(this);
268        return cb;
269    }
270
271    private JComboBox<String> createEyesComboBox() {
272        JComboBox<String> cb = new JComboBox<>();
273        fillComboBox(cb);
274        cb.addActionListener(this);
275        return cb;
276    }
277
278    private JComboBox<String> createMouthComboBox() {
279        JComboBox<String> cb = new JComboBox<>();
280        fillComboBox(cb);
281        cb.addActionListener(this);
282        return cb;
283    }
284
285    private JComboBox<String> createPresetComboBox() {
286        JComboBox<String> cb = new JComboBox<>();
287        cb.addItem(resourceManager.getString("ComboBoxDemo.preset1"));
288        cb.addItem(resourceManager.getString("ComboBoxDemo.preset2"));
289        cb.addItem(resourceManager.getString("ComboBoxDemo.preset3"));
290        cb.addItem(resourceManager.getString("ComboBoxDemo.preset4"));
291        cb.addItem(resourceManager.getString("ComboBoxDemo.preset5"));
292        cb.addItem(resourceManager.getString("ComboBoxDemo.preset6"));
293        cb.addItem(resourceManager.getString("ComboBoxDemo.preset7"));
294        cb.addItem(resourceManager.getString("ComboBoxDemo.preset8"));
295        cb.addItem(resourceManager.getString("ComboBoxDemo.preset9"));
296        cb.addItem(resourceManager.getString("ComboBoxDemo.preset10"));
297        cb.addActionListener(this);
298        return cb;
299    }
300
301    private void fillComboBox(JComboBox<String> cb) {
302        cb.addItem(resourceManager.getString("ComboBoxDemo.brent"));
303        cb.addItem(resourceManager.getString("ComboBoxDemo.georges"));
304        cb.addItem(resourceManager.getString("ComboBoxDemo.hans"));
305        cb.addItem(resourceManager.getString("ComboBoxDemo.howard"));
306        cb.addItem(resourceManager.getString("ComboBoxDemo.james"));
307        cb.addItem(resourceManager.getString("ComboBoxDemo.jeff"));
308        cb.addItem(resourceManager.getString("ComboBoxDemo.jon"));
309        cb.addItem(resourceManager.getString("ComboBoxDemo.lara"));
310        cb.addItem(resourceManager.getString("ComboBoxDemo.larry"));
311        cb.addItem(resourceManager.getString("ComboBoxDemo.lisa"));
312        cb.addItem(resourceManager.getString("ComboBoxDemo.michael"));
313        cb.addItem(resourceManager.getString("ComboBoxDemo.philip"));
314        cb.addItem(resourceManager.getString("ComboBoxDemo.scott"));
315    }
316
317    @Override
318    public void actionPerformed(ActionEvent e) {
319        if (e.getSource() == hairCB) {
320            String name = (String) parts.get(hairCB.getSelectedItem());
321            face.setHair((ImageIcon) parts.get(name + "hair"));
322            faceLabel.repaint();
323        } else if (e.getSource() == eyesCB) {
324            String name = (String) parts.get(eyesCB.getSelectedItem());
325            face.setEyes((ImageIcon) parts.get(name + "eyes"));
326            faceLabel.repaint();
327        } else if (e.getSource() == mouthCB) {
328            String name = (String) parts.get(mouthCB.getSelectedItem());
329            face.setMouth((ImageIcon) parts.get(name + "mouth"));
330            faceLabel.repaint();
331        } else if (e.getSource() == presetCB) {
332            String hair = null;
333            String eyes = null;
334            String mouth = null;
335            switch (presetCB.getSelectedIndex()) {
336                case 0:
337                    hair = (String) parts.get("philip");
338                    eyes = (String) parts.get("howard");
339                    mouth = (String) parts.get("jeff");
340                    break;
341                case 1:
342                    hair = (String) parts.get("jeff");
343                    eyes = (String) parts.get("larry");
344                    mouth = (String) parts.get("philip");
345                    break;
346                case 2:
347                    hair = (String) parts.get("howard");
348                    eyes = (String) parts.get("scott");
349                    mouth = (String) parts.get("hans");
350                    break;
351                case 3:
352                    hair = (String) parts.get("philip");
353                    eyes = (String) parts.get("jeff");
354                    mouth = (String) parts.get("hans");
355                    break;
356                case 4:
357                    hair = (String) parts.get("brent");
358                    eyes = (String) parts.get("jon");
359                    mouth = (String) parts.get("scott");
360                    break;
361                case 5:
362                    hair = (String) parts.get("lara");
363                    eyes = (String) parts.get("larry");
364                    mouth = (String) parts.get("lisa");
365                    break;
366                case 6:
367                    hair = (String) parts.get("james");
368                    eyes = (String) parts.get("philip");
369                    mouth = (String) parts.get("michael");
370                    break;
371                case 7:
372                    hair = (String) parts.get("philip");
373                    eyes = (String) parts.get("lisa");
374                    mouth = (String) parts.get("brent");
375                    break;
376                case 8:
377                    hair = (String) parts.get("james");
378                    eyes = (String) parts.get("philip");
379                    mouth = (String) parts.get("jon");
380                    break;
381                case 9:
382                    hair = (String) parts.get("lara");
383                    eyes = (String) parts.get("jon");
384                    mouth = (String) parts.get("scott");
385                    break;
386            }
387            if (hair != null) {
388                hairCB.setSelectedItem(hair);
389                eyesCB.setSelectedItem(eyes);
390                mouthCB.setSelectedItem(mouth);
391                faceLabel.repaint();
392            }
393        }
394    }
395
396    private static class Face implements Icon {
397
398        private ImageIcon hair;
399        private ImageIcon eyes;
400        private ImageIcon mouth;
401
402        void setHair(ImageIcon i) {
403            hair = i;
404        }
405
406        void setEyes(ImageIcon i) {
407            eyes = i;
408        }
409
410        void setMouth(ImageIcon i) {
411            mouth = i;
412        }
413
414        @Override
415        public void paintIcon(Component c, Graphics g, int x, int y) {
416            int height = y;
417            x = c.getWidth() / 2 - getIconWidth() / 2;
418
419            if (hair != null) {
420                hair.paintIcon(c, g, x, height);
421                height += hair.getIconHeight();
422            }
423
424            if (eyes != null) {
425                eyes.paintIcon(c, g, x, height);
426                height += eyes.getIconHeight();
427            }
428
429            if (mouth != null) {
430                mouth.paintIcon(c, g, x, height);
431            }
432        }
433
434        @Override
435        public int getIconWidth() {
436            return 344;
437        }
438
439        @Override
440        public int getIconHeight() {
441            return 455;
442        }
443    }
444}
445