1 31 32 package org.opencms.util.ant; 33 34 import java.awt.BorderLayout ; 35 import java.awt.Dimension ; 36 import java.awt.GridLayout ; 37 import java.awt.Toolkit ; 38 import java.awt.event.ActionEvent ; 39 import java.awt.event.ActionListener ; 40 import java.awt.event.WindowAdapter ; 41 import java.awt.event.WindowEvent ; 42 43 import javax.swing.BorderFactory ; 44 import javax.swing.ButtonGroup ; 45 import javax.swing.JButton ; 46 import javax.swing.JCheckBox ; 47 import javax.swing.JDialog ; 48 import javax.swing.JFrame ; 49 import javax.swing.JLabel ; 50 import javax.swing.JPanel ; 51 import javax.swing.JRadioButton ; 52 import javax.swing.JScrollPane ; 53 import javax.swing.JToggleButton ; 54 import javax.swing.border.Border ; 55 56 68 public class CmsAntTaskSelectionDialog extends JDialog implements ActionListener { 69 70 71 private static final int C_BORDER_SIZE = 10; 72 73 74 private static final long serialVersionUID = -8439685952987222098L; 75 76 77 protected boolean m_aborted = true; 78 79 80 private String [] m_allList = null; 81 82 83 private final Border m_border = BorderFactory.createEmptyBorder(C_BORDER_SIZE, C_BORDER_SIZE, 0, C_BORDER_SIZE); 84 85 86 private final JPanel m_buttons = new JPanel (); 87 88 89 private final JButton m_cancel = new JButton ("Cancel"); 90 91 92 private final JPanel m_content = new JPanel (); 93 94 95 private String [] m_defList = null; 96 97 98 private JLabel m_label = null; 99 100 101 private final JButton m_ok = new JButton ("Ok"); 102 103 104 private final CmsAntTaskSelectionPrompt m_promptTask; 105 106 107 private final JButton m_selAll = new JButton ("All"); 108 109 110 private JToggleButton [] m_selections = null; 111 112 113 private final JButton m_selNone = new JButton ("None"); 114 115 116 private final JScrollPane m_view = new JScrollPane (m_content); 117 118 125 public CmsAntTaskSelectionDialog(CmsAntTaskSelectionPrompt promptTask) { 126 127 super((JFrame )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 (m_promptTask.getPrompt()); 133 134 addWindowListener(new WindowAdapter () { 135 136 public void windowClosed(WindowEvent 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 p1 = new JPanel (); 148 p1.add(new JLabel ("Select: ")); 149 m_selAll.addActionListener(this); 150 p1.add(m_selAll); 151 m_selNone.addActionListener(this); 152 p1.add(m_selNone); 153 JPanel p = new JPanel (new BorderLayout ()); 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 [m_promptTask.getAllValues().split(CmsAntTaskSelectionPrompt.LIST_SEPARATOR).length]; 163 164 int elements = m_selections.length; 166 167 GridLayout layout = new GridLayout (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 (m_allList[i].trim(), firstPositionOfItemInArray( 174 m_defList, 175 m_allList[i]) != -1); 176 } else { 177 m_selections[i] = new JCheckBox ( 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 group = new ButtonGroup (); 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 208 public void actionPerformed(ActionEvent 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 230 public String getSelection() { 231 232 center(); 233 setVisible(true); 234 235 String 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 257 private void center() { 258 259 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 260 Dimension window = getSize(); 261 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 288 private int firstPositionOfItemInArray(String [] array, String 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 311 private String [] 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 |