TabsDlg.java revision 9883:903a2e023ffb
1/*
2 * Copyright (c) 2000, 2008, 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 *
23 */
24
25
26package com.sun.java.swing.ui;
27
28import com.sun.java.swing.action.*;
29import java.awt.*;
30import java.awt.event.ActionEvent;
31import java.awt.event.ActionListener;
32import java.util.Vector;
33import javax.swing.*;
34
35// Referenced classes of package com.sun.java.swing.ui:
36//            CommonUI
37
38public class TabsDlg extends JDialog
39{
40    private class ApplyListener
41        implements ActionListener
42    {
43
44        public void actionPerformed(ActionEvent evt)
45        {
46            if(applyListener != null)
47            {
48                applyListener.actionPerformed(evt);
49                enableApplyButton(false);
50            }
51        }
52
53        private ApplyListener()
54        {
55        }
56
57    }
58
59    private class CancelListener
60        implements ActionListener
61    {
62
63        public void actionPerformed(ActionEvent evt)
64        {
65            if(cancelListener != null)
66                cancelListener.actionPerformed(evt);
67            setVisible(false);
68        }
69
70        private CancelListener()
71        {
72        }
73
74    }
75
76    private class OkListener
77        implements ActionListener
78    {
79
80        public void actionPerformed(ActionEvent evt)
81        {
82            if(okListener != null)
83                okListener.actionPerformed(evt);
84            setVisible(false);
85        }
86
87        private OkListener()
88        {
89        }
90
91    }
92
93
94    public TabsDlg(String title, Vector panels)
95    {
96        super(new JFrame(), title, true);
97        okListener = null;
98        cancelListener = null;
99        applyListener = null;
100        Container pane = getContentPane();
101        pane.setLayout(new BorderLayout());
102        tabsPanel = new JTabbedPane();
103        int numPanels = panels.size();
104        for(int i = 0; i < numPanels; i++)
105        {
106            JPanel panel = (JPanel)panels.elementAt(i);
107            tabsPanel.addTab(panel.getName(), panel);
108        }
109
110        pane.add(tabsPanel, "Center");
111        pane.add(createButtonPanel(), "South");
112        pack();
113        CommonUI.centerComponent(this);
114    }
115
116    public static void main(String args[])
117    {
118        JPanel p1 = new JPanel();
119        p1.add(new JButton("One"));
120        p1.setName("One");
121        JPanel p2 = new JPanel();
122        p2.add(new JButton("Two"));
123        p2.setName("Two");
124        JPanel p3 = new JPanel();
125        p3.add(new JButton("Three"));
126        p3.setName("Three");
127        JPanel p4 = new JPanel();
128        p4.add(new JButton("Four"));
129        p4.setName("Four");
130        Vector panels = new Vector();
131        panels.addElement(p1);
132        panels.addElement(p2);
133        panels.addElement(p3);
134        panels.addElement(p4);
135        tabsDlg = new TabsDlg("Test Dialog", panels);
136        tabsDlg.addOkListener(new ActionListener() {
137
138            public void actionPerformed(ActionEvent evt)
139            {
140                System.exit(0);
141            }
142
143        }
144);
145        tabsDlg.addCancelListener(new ActionListener() {
146
147            public void actionPerformed(ActionEvent evt)
148            {
149                System.exit(0);
150            }
151
152        }
153);
154        tabsDlg.setVisible(true);
155    }
156
157    private JPanel createButtonPanel()
158    {
159        JPanel panel = new JPanel();
160        okAction = new OkAction();
161        cancelAction = new CancelAction();
162        applyAction = new ApplyAction();
163        okAction.addActionListener(new OkListener());
164        cancelAction.addActionListener(new CancelListener());
165        applyAction.addActionListener(new ApplyListener());
166        panel.add(CommonUI.createButton(okAction));
167        panel.add(CommonUI.createButton(cancelAction));
168        panel.add(CommonUI.createButton(applyAction));
169        JPanel p2 = new JPanel(new BorderLayout());
170        p2.add(panel, "Center");
171        p2.add(new JSeparator(), "North");
172        return p2;
173    }
174
175    public void enableApplyButton(boolean enabled)
176    {
177        applyAction.setEnabled(enabled);
178    }
179
180    public synchronized void addOkListener(ActionListener l)
181    {
182        okListener = AWTEventMulticaster.add(okListener, l);
183    }
184
185    public synchronized void removeOkListener(ActionListener l)
186    {
187        okListener = AWTEventMulticaster.remove(okListener, l);
188    }
189
190    public synchronized void addCancelListener(ActionListener l)
191    {
192        cancelListener = AWTEventMulticaster.add(cancelListener, l);
193    }
194
195    public synchronized void removeCancelListener(ActionListener l)
196    {
197        cancelListener = AWTEventMulticaster.remove(cancelListener, l);
198    }
199
200    public synchronized void addApplyListener(ActionListener l)
201    {
202        applyListener = AWTEventMulticaster.add(applyListener, l);
203    }
204
205    public synchronized void removeApplyListener(ActionListener l)
206    {
207        applyListener = AWTEventMulticaster.remove(applyListener, l);
208    }
209
210    private JTabbedPane tabsPanel;
211    private DelegateAction okAction;
212    private DelegateAction cancelAction;
213    private DelegateAction applyAction;
214    private ActionListener okListener;
215    private ActionListener cancelListener;
216    private ActionListener applyListener;
217    private static TabsDlg tabsDlg;
218
219
220
221}
222