KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > util > ant > CmsAntTaskSelectionDialog


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-components/org/opencms/util/ant/CmsAntTaskSelectionDialog.java,v $
3  * Date : $Date: 2006/03/27 14:53:01 $
4  * Version: $Revision: 1.14 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.util.ant;
33
34 import java.awt.BorderLayout JavaDoc;
35 import java.awt.Dimension JavaDoc;
36 import java.awt.GridLayout JavaDoc;
37 import java.awt.Toolkit JavaDoc;
38 import java.awt.event.ActionEvent JavaDoc;
39 import java.awt.event.ActionListener JavaDoc;
40 import java.awt.event.WindowAdapter JavaDoc;
41 import java.awt.event.WindowEvent JavaDoc;
42
43 import javax.swing.BorderFactory JavaDoc;
44 import javax.swing.ButtonGroup JavaDoc;
45 import javax.swing.JButton JavaDoc;
46 import javax.swing.JCheckBox JavaDoc;
47 import javax.swing.JDialog JavaDoc;
48 import javax.swing.JFrame JavaDoc;
49 import javax.swing.JLabel JavaDoc;
50 import javax.swing.JPanel JavaDoc;
51 import javax.swing.JRadioButton JavaDoc;
52 import javax.swing.JScrollPane JavaDoc;
53 import javax.swing.JToggleButton JavaDoc;
54 import javax.swing.border.Border JavaDoc;
55
56 /**
57  * This is a highly configurable Swing GUI dialog for selection.
58  * <p>
59  *
60  * @author Michael Moossen
61  *
62  * @version $Revision: 1.14 $
63  *
64  * @since 6.0.0
65  *
66  * @see CmsAntTaskSelectionPrompt
67  */

68 public class CmsAntTaskSelectionDialog extends JDialog JavaDoc implements ActionListener JavaDoc {
69
70     /** Constant for border width. */
71     private static final int C_BORDER_SIZE = 10;
72
73     /** Serial version UID required for safe serialization. */
74     private static final long serialVersionUID = -8439685952987222098L;
75
76     /** Aborted flag. */
77     protected boolean m_aborted = true;
78
79     /** Array of all entries. */
80     private String JavaDoc[] m_allList = null;
81
82     /** Border. */
83     private final Border JavaDoc m_border = BorderFactory.createEmptyBorder(C_BORDER_SIZE, C_BORDER_SIZE, 0, C_BORDER_SIZE);
84
85     /** Panel for buttons. */
86     private final JPanel JavaDoc m_buttons = new JPanel JavaDoc();
87
88     /** Cancel button. */
89     private final JButton JavaDoc m_cancel = new JButton JavaDoc("Cancel");
90
91     /** Main Panel. */
92     private final JPanel JavaDoc m_content = new JPanel JavaDoc();
93
94     /** Array of by default selected items. */
95     private String JavaDoc[] m_defList = null;
96
97     /** Label for prompt. */
98     private JLabel JavaDoc m_label = null;
99
100     /** Ok button. */
101     private final JButton JavaDoc m_ok = new JButton JavaDoc("Ok");
102
103     /** Associated ant task. */
104     private final CmsAntTaskSelectionPrompt m_promptTask;
105
106     /** Select all button. */
107     private final JButton JavaDoc m_selAll = new JButton JavaDoc("All");
108
109     /** Array of selection buttons, check boxes or radio buttons. */
110     private JToggleButton JavaDoc[] m_selections = null;
111
112     /** Select none button. */
113     private final JButton JavaDoc m_selNone = new JButton JavaDoc("None");
114
115     /** Scrollable view. */
116     private final JScrollPane JavaDoc m_view = new JScrollPane JavaDoc(m_content);
117
118     /**
119      * Default Constructor.
120      * <p>
121      *
122      * @param promptTask the <code>{@link CmsAntTaskSelectionPrompt}</code> object.
123      * <p>
124      */

125     public CmsAntTaskSelectionDialog(CmsAntTaskSelectionPrompt promptTask) {
126
127         super((JFrame JavaDoc)null, promptTask.getTitle(), true);
128         m_promptTask = promptTask;
129
130         m_allList = m_promptTask.getAllValues().split(CmsAntTaskSelectionPrompt.LIST_SEPARATOR);
131         m_defList = getDefaultList();
132         m_label = new JLabel JavaDoc(m_promptTask.getPrompt());
133
134         addWindowListener(new WindowAdapter JavaDoc() {
135
136             public void windowClosed(WindowEvent JavaDoc e) {
137
138                 m_aborted = true;
139                 setVisible(false);
140             }
141         });
142
143         getRootPane().setDefaultButton(m_ok);
144
145         m_label.setBorder(m_border);
146         if (!m_promptTask.isSingleSelection()) {
147             JPanel JavaDoc p1 = new JPanel JavaDoc();
148             p1.add(new JLabel JavaDoc("Select: "));
149             m_selAll.addActionListener(this);
150             p1.add(m_selAll);
151             m_selNone.addActionListener(this);
152             p1.add(m_selNone);
153             JPanel JavaDoc p = new JPanel JavaDoc(new BorderLayout JavaDoc());
154             p.add(m_label, BorderLayout.NORTH);
155             p.add(p1, BorderLayout.SOUTH);
156             getContentPane().add(p, BorderLayout.NORTH);
157         } else {
158             getContentPane().add(m_label, BorderLayout.NORTH);
159         }
160
161         m_view.setBorder(m_border);
162         m_selections = new JToggleButton JavaDoc[m_promptTask.getAllValues().split(CmsAntTaskSelectionPrompt.LIST_SEPARATOR).length];
163
164         // layout of selection elements
165
int elements = m_selections.length;
166
167         GridLayout JavaDoc layout = new GridLayout JavaDoc(elements / m_promptTask.getColumns()+1, m_promptTask.getColumns());
168         layout.setHgap(20);
169         m_content.setLayout(layout);
170
171         for (int i = 0; i < m_selections.length; i++) {
172             if (m_promptTask.isSingleSelection()) {
173                 m_selections[i] = new JRadioButton JavaDoc(m_allList[i].trim(), firstPositionOfItemInArray(
174                     m_defList,
175                     m_allList[i]) != -1);
176             } else {
177                 m_selections[i] = new JCheckBox JavaDoc(
178                     m_allList[i].trim(),
179                     firstPositionOfItemInArray(m_defList, m_allList[i]) != -1);
180             }
181             m_content.add(m_selections[i]);
182         }
183         if (m_promptTask.isSingleSelection()) {
184             ButtonGroup JavaDoc group = new ButtonGroup JavaDoc();
185             for (int i = 0; i < m_selections.length; i++) {
186                 group.add(m_selections[i]);
187             }
188         }
189         getContentPane().add(m_view, BorderLayout.CENTER);
190
191         m_buttons.setBorder(BorderFactory.createEmptyBorder(
192             C_BORDER_SIZE,
193             C_BORDER_SIZE,
194             C_BORDER_SIZE / 2,
195             C_BORDER_SIZE));
196         m_ok.addActionListener(this);
197         m_buttons.add(m_ok);
198         m_cancel.addActionListener(this);
199         m_buttons.add(m_cancel);
200         getContentPane().add(m_buttons, BorderLayout.SOUTH);
201
202         pack();
203     }
204
205     /**
206      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
207      */

208     public void actionPerformed(ActionEvent JavaDoc e) {
209
210         if (e.getActionCommand().equals(m_ok.getText()) || e.getActionCommand().equals(m_cancel.getText())) {
211             m_aborted = !e.getActionCommand().equals(m_ok.getText());
212             setVisible(false);
213         } else if (e.getActionCommand().equals(m_selAll.getText())) {
214             for (int i = 0; i < m_selections.length; i++) {
215                 m_selections[i].setSelected(true);
216             }
217         } else if (e.getActionCommand().equals(m_selNone.getText())) {
218             for (int i = 0; i < m_selections.length; i++) {
219                 m_selections[i].setSelected(false);
220             }
221         }
222     }
223
224     /**
225      * Returns <code>null</code> if the dialog was canceled, or a list of selected items if not.
226      * <p>
227      *
228      * @return the user selection
229      */

230     public String JavaDoc getSelection() {
231
232         center();
233         setVisible(true);
234
235         String JavaDoc ret = "";
236         for (int i = 0; i < m_selections.length; i++) {
237             if (m_selections[i].isSelected()) {
238                 ret += m_selections[i].getText() + CmsAntTaskSelectionPrompt.LIST_SEPARATOR;
239             }
240         }
241         if (m_aborted || ret.trim().length() < CmsAntTaskSelectionPrompt.LIST_SEPARATOR.length()) {
242             dispose();
243             return null;
244         }
245         dispose();
246         return ret.substring(0, ret.length() - CmsAntTaskSelectionPrompt.LIST_SEPARATOR.length());
247     }
248
249     /**
250      * Centers the dialog on the screen.
251      * <p>
252      *
253      * If the size of the dialog exceeds that of the screen, then the size of the dialog is reset to
254      * the size of the screen.
255      * <p>
256      */

257     private void center() {
258
259         Dimension JavaDoc screen = Toolkit.getDefaultToolkit().getScreenSize();
260         Dimension JavaDoc window = getSize();
261         // ensure that no parts of the dialog will be off-screen
262
int height = window.height;
263         int width = window.width;
264         if (window.height > screen.height) {
265             window.height = screen.height;
266             height = screen.height - 50;
267             width = width + 50;
268         }
269         if (window.width > screen.width) {
270             window.width = screen.width;
271         }
272         int xCoord = (screen.width / 2 - window.width / 2);
273         int yCoord = (screen.height / 2 - window.height / 2);
274         setLocation(xCoord, yCoord);
275         setSize(width, height);
276     }
277
278     /**
279      * Looks for the position of a string in an array of string, performing triming and taking into
280      * account the null cases.
281      * <p>
282      *
283      * @param array the string array to search in
284      * @param item the item to search for
285      *
286      * @return the position of the item in the array or -1 if not found
287      */

288     private int firstPositionOfItemInArray(String JavaDoc[] array, String JavaDoc item) {
289
290         for (int i = 0; i < array.length; i++) {
291             if (array[i] == null) {
292                 if (item == null) {
293                     return i;
294                 }
295             } else {
296                 if (item != null && array[i].trim().equals(item.trim())) {
297                     return i;
298                 }
299             }
300         }
301         return -1;
302     }
303
304     /**
305      * Returns the array of items selected by default, if no one is given all items will be
306      * considered.
307      * <p>
308      *
309      * @return the array of items selected by default
310      */

311     private String JavaDoc[] getDefaultList() {
312
313         if (m_promptTask.getDefaultValue() == null || m_promptTask.getDefaultValue().trim().equals("")) {
314             return m_allList;
315         }
316         return m_promptTask.getDefaultValue().split(CmsAntTaskSelectionPrompt.LIST_SEPARATOR);
317     }
318 }
Popular Tags