OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / classpath / examples / gnu / classpath / examples / swing / ListDemo.java
1 /* ListDemo.java -- Demostrates JList
2    Copyright (C) 2006 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37
38
39 package gnu.classpath.examples.swing;
40
41 import java.awt.BorderLayout;
42 import java.awt.Component;
43 import java.awt.GridLayout;
44 import java.awt.event.ActionEvent;
45 import java.awt.event.ActionListener;
46
47 import javax.swing.DefaultListCellRenderer;
48 import javax.swing.DefaultListModel;
49 import javax.swing.JButton;
50 import javax.swing.JCheckBox;
51 import javax.swing.JComponent;
52 import javax.swing.JFrame;
53 import javax.swing.JList;
54 import javax.swing.JPanel;
55 import javax.swing.JScrollPane;
56 import javax.swing.JSplitPane;
57 import javax.swing.ListCellRenderer;
58 import javax.swing.SwingUtilities;
59
60 public class ListDemo
61   extends JPanel
62   implements ActionListener
63 {
64
65   private static class LabelCellRenderer 
66     extends DefaultListCellRenderer
67   {
68     public Component getListCellRendererComponent(JList list,
69                                                   Object value,
70                                                   int index,
71                                                   boolean isSelected,
72                                                   boolean cellHasFocus)
73     {
74       Component c = super.getListCellRendererComponent(list, value, index, 
75                                                        isSelected,
76                                                        cellHasFocus);
77       return c;
78     }
79   }
80
81   private static class CheckCellRenderer 
82     extends JCheckBox
83     implements ListCellRenderer
84   {
85     public Component getListCellRendererComponent(JList list,
86                                                   Object value,
87                                                   int index,
88                                                   boolean isSelected,
89                                                   boolean cellHasFocus)
90     {
91       setSelected(isSelected);
92       setText(value.toString());
93       
94       return this;
95     }
96   }
97
98   ListDemo()
99   {
100     super();
101     createContent();
102   }
103
104   private void createContent()
105   {
106
107     String foo[] = new String[] { 
108       "non alcoholic ",
109       "carbonated ",
110       "malted ",
111       "fresh squeezed ",
112       "imported ",
113       "high fructose ",
114       "enriched "
115     };
116     
117     String bar[] = new String[] { 
118       "orange juice",
119       "ginger beer",
120       "yak milk",
121       "corn syrup",
122       "herbal remedy"
123     };
124
125     final DefaultListModel mod = new DefaultListModel();
126     final JList list1 = new JList(mod);
127     final JList list2 = new JList(mod);
128
129     list2.setSelectionModel(list1.getSelectionModel());
130     for (int i = 0; i < bar.length; ++i)
131       for (int j = 0; j < foo.length; ++j)
132         mod.addElement(foo[j] + bar[i]);
133
134     list1.setCellRenderer(new LabelCellRenderer());
135     list2.setCellRenderer(new CheckCellRenderer());
136
137     JButton add = new JButton("add element");
138     add.addActionListener(new ActionListener()
139       {
140         int i = 0;
141         public void actionPerformed(ActionEvent e)
142         {
143           mod.addElement("new element " + i);
144           ++i;
145         }
146       });
147
148     JButton del = new JButton("delete selected");
149     del.addActionListener(new ActionListener()
150       {
151         public void actionPerformed(ActionEvent e)
152         {
153           for (int i = 0; i < mod.getSize(); ++i)
154             if (list1.isSelectedIndex(i))
155               mod.remove(i);
156         }
157       });
158
159
160     JSplitPane splitter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
161     splitter.add(new JScrollPane(list1), JSplitPane.LEFT);
162     splitter.add(new JScrollPane(list2), JSplitPane.RIGHT);
163
164     setLayout(new BorderLayout());
165     JPanel p2 = new JPanel(); 
166     p2.setLayout(new GridLayout(1, 2));
167     p2.add(add);
168     p2.add(del);
169
170     add(p2, BorderLayout.NORTH);
171     add(splitter, BorderLayout.CENTER);
172   }
173
174   public void actionPerformed(ActionEvent e) 
175   {
176     if (e.getActionCommand().equals("CLOSE"))
177     {
178       System.exit(0);
179     }
180   }
181
182   /**
183    * When the demo is run independently, the frame is displayed, so we should
184    * initialise the content panel (including the demo content and a close 
185    * button).  But when the demo is run as part of the Swing activity board,
186    * only the demo content panel is used, the frame itself is never displayed,
187    * so we can avoid this step.
188    */
189   void initFrameContent()
190   {
191     JPanel closePanel = new JPanel();
192     JButton closeButton = new JButton("Close");
193     closeButton.setActionCommand("CLOSE");
194     closeButton.addActionListener(this);
195     closePanel.add(closeButton);
196     add(closePanel, BorderLayout.SOUTH);
197   }
198
199   public static void main(String[] args)
200   {
201     SwingUtilities.invokeLater
202     (new Runnable()
203      {
204        public void run()
205        {
206          ListDemo app = new ListDemo();
207          app.initFrameContent();
208          JFrame frame = new JFrame("List Demo");
209          frame.getContentPane().add(app);
210          frame.pack();
211          frame.setVisible(true);
212        }
213      });
214   }
215
216   /**
217    * Returns a DemoFactory that creates a ListDemo.
218    *
219    * @return a DemoFactory that creates a ListDemo
220    */
221   public static DemoFactory createDemoFactory()
222   {
223     return new DemoFactory()
224     {
225       public JComponent createDemo()
226       {
227         return new ListDemo();
228       }
229     };
230   }
231 }