1 package org.enhydra.kelp.ant.deployer; 2 3 25 26 import org.enhydra.kelp.common.event.ExtensionChangeEvent; 28 import org.enhydra.kelp.common.event.ExtensionChangeListener; 29 import org.enhydra.kelp.common.Constants; 30 31 import java.awt.*; 33 import java.beans.*; 34 import javax.swing.*; 35 import javax.swing.event.*; 36 import java.awt.event.*; 37 import java.util.ArrayList ; 38 import java.util.Arrays ; 39 import org.enhydra.kelp.ant.node.AntProject; 40 import org.enhydra.kelp.common.node.OtterProject; 41 42 public class AntDeployContentTypesPanel extends JPanel { 44 private GridBagLayout layoutRoot; 45 private JPanel panelList; 46 private JPanel panelButtons; 47 private GridBagLayout layoutList; 48 private GridBagLayout layoutButtons; 49 private JLabel labelList; 50 private JList listExt; 51 private JButton buttonAdd; 52 private JButton buttonRemove; 53 private JButton buttonReset; 54 private String [] defaults = null; 55 private LocalButtonListener buttonListener = null; 56 private LocalListListener listListener = null; 57 private JScrollPane scrollPane; 58 private String [] readOnly = new String [0]; 59 private ExtensionChangeListener[] listeners = 60 new ExtensionChangeListener[0]; 61 private AntProject project = null; 62 63 public AntDeployContentTypesPanel() { 64 try { 65 jbInit(); 66 pmInit(); 67 } catch (Exception e) { 68 e.printStackTrace(); 69 } 70 } 71 72 91 public void clearAll() { 92 buttonRemove.removeActionListener(buttonListener); 93 buttonReset.removeActionListener(buttonListener); 94 buttonAdd.removeActionListener(buttonListener); 95 listExt.removeListSelectionListener(listListener); 96 removeAll(); 97 buttonListener = null; 98 listListener = null; 99 listeners = null; 100 } 101 102 public void addListener(ExtensionChangeListener l) { 103 ArrayList list = null; 104 105 list = new ArrayList (Arrays.asList(listeners)); 106 if (list.contains(l)) {} 107 else { 108 list.add(l); 109 list.trimToSize(); 110 listeners = new ExtensionChangeListener[list.size()]; 111 listeners = (ExtensionChangeListener[]) list.toArray(listeners); 112 } 113 list.clear(); 114 } 115 116 public void removeListener(ExtensionChangeListener l) { 117 ArrayList list = null; 118 119 list = new ArrayList (Arrays.asList(listeners)); 120 if (list.contains(l)) { 121 list.remove(l); 122 list.trimToSize(); 123 listeners = new ExtensionChangeListener[list.size()]; 124 listeners = (ExtensionChangeListener[]) list.toArray(listeners); 125 } 126 list.clear(); 127 } 128 129 public ExtensionChangeListener[] getListeners() { 130 return listeners; 131 } 132 133 public String [] getReadOnly() { 134 return readOnly; 135 } 136 137 public void setReadOnly(String [] r) { 138 readOnly = r; 139 } 140 141 public String [] getExtensions() { 142 String [] exts = new String [0]; 143 ListModel model = listExt.getModel(); 144 145 exts = new String [model.getSize()]; 146 for (int i = 0; i < model.getSize(); i++) { 147 exts[i] = model.getElementAt(i).toString(); 148 } 149 return exts; 150 } 151 152 public void setExtensions(String [] exts) { 153 if (exts == null) { 154 exts = new String [0]; 155 } 156 listExt.setListData(exts); 157 listExt.updateUI(); 158 if (defaults == null) { 159 defaults = new String [exts.length]; 160 for (int i = 0; i < exts.length; i++) { 161 defaults[i] = new String (exts[i]); 162 } 163 } 164 } 165 166 public void setDefaults(String [] defs) { 167 defaults = defs; 168 } 169 170 public String [] getDefaults() { 171 return defaults; 172 } 173 174 private void notifyListeners() { 178 ExtensionChangeEvent event = null; 179 180 event = new ExtensionChangeEvent(this, getExtensions()); 181 for (int i = 0; i < listeners.length; i++) { 182 listeners[i].onChange(event); 183 } 184 } 185 186 private void pmInit() { 187 buttonListener = new LocalButtonListener(); 188 listListener = new LocalListListener(); 189 buttonRemove.addActionListener(buttonListener); 190 buttonReset.addActionListener(buttonListener); 191 buttonAdd.addActionListener(buttonListener); 192 listExt.addListSelectionListener(listListener); 193 buttonRemove.setEnabled(false); 194 } 195 196 private void jbInit() throws Exception { 197 panelList = (JPanel) Beans.instantiate(getClass().getClassLoader(), 198 JPanel.class.getName()); 199 panelButtons = (JPanel) Beans.instantiate(getClass().getClassLoader(), 200 JPanel.class.getName()); 201 layoutList = 202 (GridBagLayout) Beans.instantiate(getClass().getClassLoader(), 203 GridBagLayout.class.getName()); 204 layoutRoot = 205 (GridBagLayout) Beans.instantiate(getClass().getClassLoader(), 206 GridBagLayout.class.getName()); 207 layoutButtons = 208 (GridBagLayout) Beans.instantiate(getClass().getClassLoader(), 209 GridBagLayout.class.getName()); 210 labelList = (JLabel) Beans.instantiate(getClass().getClassLoader(), 211 JLabel.class.getName()); 212 listExt = (JList) Beans.instantiate(getClass().getClassLoader(), 213 JList.class.getName()); 214 buttonAdd = (JButton) Beans.instantiate(getClass().getClassLoader(), 215 JButton.class.getName()); 216 buttonRemove = 217 (JButton) Beans.instantiate(getClass().getClassLoader(), 218 JButton.class.getName()); 219 buttonReset = (JButton) Beans.instantiate(getClass().getClassLoader(), 220 JButton.class.getName()); 221 labelList.setDisplayedMnemonic('X'); 222 labelList.setLabelFor(listExt); 223 labelList.setText("Associated extensions:"); 224 buttonAdd.setPreferredSize(new Dimension(85, 27)); 225 buttonAdd.setMnemonic('A'); 226 buttonAdd.setText("Add..."); 227 buttonRemove.setPreferredSize(new Dimension(85, 27)); 228 buttonRemove.setMnemonic('V'); 229 buttonRemove.setText("Remove"); 230 buttonReset.setPreferredSize(new Dimension(85, 27)); 231 buttonReset.setText("Reset"); 232 scrollPane = new JScrollPane(listExt); 233 scrollPane.setMinimumSize(new Dimension(100, 100)); 234 scrollPane.setPreferredSize(new Dimension(100, 100)); 235 panelList.setLayout(layoutList); 236 panelList.add(labelList, 237 new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 238 GridBagConstraints.CENTER, 239 GridBagConstraints.NONE, 240 new Insets(10, 10, 2, 10), 0, 241 0)); 242 panelList.add(scrollPane, 243 new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, 244 GridBagConstraints.CENTER, 245 GridBagConstraints.BOTH, 246 new Insets(2, 10, 10, 10), 0, 247 0)); 248 panelButtons.setLayout(layoutButtons); 249 panelButtons.add(buttonAdd, 250 new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 251 GridBagConstraints.CENTER, 252 GridBagConstraints.NONE, 253 new Insets(5, 5, 5, 5), 0, 254 0)); 255 panelButtons.add(buttonRemove, 256 new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, 257 GridBagConstraints.CENTER, 258 GridBagConstraints.NONE, 259 new Insets(5, 5, 5, 5), 0, 260 0)); 261 panelButtons.add(buttonReset, 262 new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, 263 GridBagConstraints.CENTER, 264 GridBagConstraints.NONE, 265 new Insets(5, 5, 5, 5), 0, 266 0)); 267 this.setLayout(layoutRoot); 268 this.add(panelList, 269 new GridBagConstraints(0, 0, 1, 1, 0.2, 0.1 270 ,GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 136, 0, 0), 0, 0)); 271 this.add(panelButtons, 272 new GridBagConstraints(1, 0, 1, 1, 0.1, 0.1 273 ,GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(13, 0, 0, 106), 0, 0)); 274 } 275 276 private void reset() { 277 setExtensions(defaults); 278 } 279 280 private String [] getSelections() { 281 Object [] values = new Object [0]; 282 String [] selections = new String [0]; 283 284 values = listExt.getSelectedValues(); 285 selections = new String [values.length]; 286 for (int i = 0; i < selections.length; i++) { 287 selections[i] = values[i].toString(); 288 } 289 values = new Object [0]; 290 return selections; 291 } 292 293 private void removeSelection() { 294 ArrayList list = null; 295 String [] exts = getSelections(); 296 297 if (exts.length >= 1) { 298 list = new ArrayList (Arrays.asList(getExtensions())); 299 for (int i = 0; i < exts.length; i++) { 300 list.remove(exts[i]); 301 } 302 list.trimToSize(); 303 exts = new String [list.size()]; 304 exts = (String []) list.toArray(exts); 305 setExtensions(exts); 306 list.clear(); 307 } 308 } 309 310 private boolean promptForAdd() { 311 boolean change = false; 312 Object input = null; 313 314 input = JOptionPane.showInputDialog(this, "Enter new extension", 315 "Add Custom Extension", 316 JOptionPane.QUESTION_MESSAGE); 317 if (input == null) {} 318 else { 319 String [] exts = new String [0]; 320 String newExt = new String (); 321 ArrayList list = null; 322 323 newExt = input.toString(); 324 list = new ArrayList (Arrays.asList(getExtensions())); 325 if (list.contains(newExt)) {} 326 else if (extensionValid(newExt)) { 327 list.add(newExt); 328 } 329 list.trimToSize(); 330 exts = new String [list.size()]; 331 exts = (String []) list.toArray(exts); 332 setExtensions(exts); 333 list.clear(); 334 change = true; 335 } 336 return change; 337 } 338 339 private boolean extensionValid(String testExt) { 340 boolean valid = true; 341 342 testExt = testExt.trim(); 343 if (testExt.length() == 0) { 344 valid = false; 345 } else if (testExt.indexOf(' ') > -1) { 346 valid = false; 347 } else { 348 for (int i = 0; i < testExt.length(); i++) { 349 char c = testExt.charAt(i); 350 351 if (!Character.isLetterOrDigit(c)) { 352 valid = false; 353 break; 354 } 355 } 356 } 357 return valid; 358 } 359 360 public void setProject(OtterProject otterProject){ 361 if(otterProject instanceof AntProject) { 362 project = (AntProject) otterProject; 363 initOptions(); 364 } 365 else 366 System.err.println("DEBUG project must be AntProject"); } 368 369 private void initOptions(){ 370 setExtensions(project.getExtensions(AntProject.CONTENT_TYPE_EXTENSIONS)); 371 } 372 373 private class LocalButtonListener implements ActionListener { 374 public void actionPerformed(ActionEvent e) { 375 Object source = e.getSource(); 376 377 if (source == buttonRemove) { 378 removeSelection(); 379 notifyListeners(); 380 } else if (source == buttonReset) { 381 reset(); 382 notifyListeners(); 383 } else if (source == buttonAdd) { 384 if (promptForAdd()) { 385 notifyListeners(); 386 } 387 } 388 project.setExtensions(AntProject.CONTENT_TYPE_EXTENSIONS, getExtensions()); 389 } 390 391 } 392 393 394 private class LocalListListener implements ListSelectionListener { 396 public void valueChanged(ListSelectionEvent e) { 397 if (e.getValueIsAdjusting()) { 398 399 } else { 401 boolean enable = true; 402 String [] selections = getSelections(); 403 404 if (selections.length >= 1) { 405 ArrayList list = null; 406 407 list = new ArrayList (Arrays.asList(getReadOnly())); 408 for (int i = 0; i < selections.length; i++) { 409 if (list.contains(selections[i])) { 410 enable = false; 411 break; 412 } 413 } 414 } 415 buttonRemove.setEnabled(enable); 416 } 417 } 418 419 } 420 } 421 | Popular Tags |