OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / classpath / examples / gnu / classpath / examples / swing / TabbedPaneDemo.java
1 /* TabbedPaneDemo.java -- Demonstrates JTabbedPane
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.Color;
43 import java.awt.GridLayout;
44 import java.awt.event.ActionEvent;
45 import java.awt.event.ActionListener;
46 import java.awt.event.MouseAdapter;
47 import java.awt.event.MouseEvent;
48
49 import javax.swing.JButton;
50 import javax.swing.JComponent;
51 import javax.swing.JFrame;
52 import javax.swing.JLabel;
53 import javax.swing.JMenu;
54 import javax.swing.JMenuItem;
55 import javax.swing.JPanel;
56 import javax.swing.JPopupMenu;
57 import javax.swing.JScrollPane;
58 import javax.swing.JTabbedPane;
59 import javax.swing.JTextArea;
60 import javax.swing.SwingConstants;
61 import javax.swing.SwingUtilities;
62
63 public class TabbedPaneDemo
64   extends JPanel
65   implements ActionListener
66 {
67   static Color[] colors = { Color.BLUE, Color.CYAN, Color.GRAY, Color.GREEN,
68                             Color.MAGENTA, Color.ORANGE, Color.PINK,
69                             Color.ORANGE, Color.RED, Color.BLUE, Color.YELLOW
70                           };
71   TabbedPaneDemo()
72   {
73     super();
74     createContent();
75   }
76
77   private void createContent()
78   {
79     JPanel p = new JPanel();
80     p.setLayout(new GridLayout(1, 1));
81     
82     int COUNT = 25;
83     JTabbedPane tp = createTabbedPane(SwingConstants.TOP, "tab", COUNT);
84     p.add(tp);
85     
86     final JPopupMenu popup = new JPopupMenu();
87
88     JMenu menu = new JMenu("tab placement");
89     menu.add(createPlacementChangingMenuItem("top",
90                                              SwingConstants.TOP,
91                                              tp));
92     
93     menu.add(createPlacementChangingMenuItem("bottom",
94                                              SwingConstants.BOTTOM,
95                                              tp));
96     
97     menu.add(createPlacementChangingMenuItem("left",
98                                              SwingConstants.LEFT,
99                                              tp));
100     
101     menu.add(createPlacementChangingMenuItem("right",
102                                              SwingConstants.RIGHT,
103                                              tp));
104     popup.add(menu);
105
106     menu = new JMenu("tab layout");
107     menu.add(createLayoutPolicyChangingMenuItem("wrapping tabs",
108                                                 JTabbedPane.WRAP_TAB_LAYOUT,
109                                                 tp));
110     
111     menu.add(createLayoutPolicyChangingMenuItem("scrolling tabs",
112                                                 JTabbedPane.SCROLL_TAB_LAYOUT,
113                                                 tp));
114     popup.add(menu);
115     
116     tp.addMouseListener(new MouseAdapter()
117                        {
118                          public void mousePressed(MouseEvent e) {
119                            showPopup(e);
120                          }
121                          
122                          public void mouseReleased(MouseEvent e) {
123                            showPopup(e);
124                          }
125                          
126                          void showPopup(MouseEvent e) {
127                            if (e.isPopupTrigger()) {
128                              popup.show(e.getComponent(), e.getX(), e.getY());
129                            }
130                          }
131                        });
132     
133     setLayout(new BorderLayout());
134     add(p, BorderLayout.CENTER);
135     
136   }
137   
138   private JMenuItem createPlacementChangingMenuItem(String t,
139                                                     final int v,
140                                                     final JTabbedPane dst)
141   {
142     JMenuItem item = new JMenuItem(t);
143     
144     item.addActionListener(new ActionListener()
145                            {
146                              public void actionPerformed(ActionEvent ae)
147                              {
148                               dst.setTabPlacement(v);
149                              }
150                            });
151   
152     return item;
153   }
154   
155   private JMenuItem createLayoutPolicyChangingMenuItem(String t,
156                                                        final int v,
157                                                        final JTabbedPane dst)
158   {
159     JMenuItem item = new JMenuItem(t);
160     
161     item.addActionListener(new ActionListener()
162                            {
163                              public void actionPerformed(ActionEvent ae)
164                              {
165                               dst.setTabLayoutPolicy(v);
166                              }
167                            });
168   
169     return item;
170   }
171   
172   private JTabbedPane createTabbedPane(int direction, String name, int count)
173   {
174     JTabbedPane pane = new JTabbedPane(direction);
175     
176     for(int i = 0; i< count; i++)
177       {
178         pane.addTab(name + " " + i, createTabContent(name + " " + i));
179         if (Math.random() >= 0.75)
180           pane.setEnabledAt(i, false);
181       }
182   
183     return pane;
184   }
185   
186   private JPanel createTabContent(String name)
187   {
188     JTextArea ta;
189     JPanel panel = new JPanel();
190     panel.add(new JLabel(name));
191     panel.add(new JButton(name));
192     panel.add(new JScrollPane(ta = new JTextArea(5, 5)));
193     
194     ta.setBackground(colors[(int) (Math.random() * colors.length)]);
195     
196     return panel;
197   }
198
199   public void actionPerformed(ActionEvent e) 
200   {
201     if (e.getActionCommand().equals("CLOSE"))
202     {
203       System.exit(0);
204     }
205   }
206
207   /**
208    * When the demo is run independently, the frame is displayed, so we should
209    * initialise the content panel (including the demo content and a close 
210    * button).  But when the demo is run as part of the Swing activity board,
211    * only the demo content panel is used, the frame itself is never displayed,
212    * so we can avoid this step.
213    */
214   void initFrameContent()
215   {
216     JPanel closePanel = new JPanel();
217     JButton closeButton = new JButton("Close");
218     closeButton.setActionCommand("CLOSE");
219     closeButton.addActionListener(this);
220     closePanel.add(closeButton);
221     add(closePanel, BorderLayout.SOUTH);
222   }
223
224   public static void main(String[] args)
225   {
226     SwingUtilities.invokeLater
227     (new Runnable()
228      {
229        public void run()
230        {
231          TabbedPaneDemo app = new TabbedPaneDemo();
232          app.initFrameContent();
233          JFrame frame = new JFrame("TabbedPane Demo");
234          frame.getContentPane().add(app);
235          frame.pack();
236          frame.setVisible(true);
237        }
238      });
239   }
240
241   /**
242    * Returns a DemoFactory that creates a TabbedPaneDemo.
243    *
244    * @return a DemoFactory that creates a TabbedPaneDemo
245    */
246   public static DemoFactory createDemoFactory()
247   {
248     return new DemoFactory()
249     {
250       public JComponent createDemo()
251       {
252         return new TabbedPaneDemo();
253       }
254     };
255   }
256 }