|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.web.project.ui.customizer; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Collections; import java.util.HashMap; import java.util.List; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JTextField; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import javax.swing.text.BadLocationException; import javax.swing.text.Document; import org.openide.util.NbBundle; /** Class which makes creation of the GUI easier. Registers JComponent * property names and handles reading/storing the values from the components * automaticaly. * * @author Petr Hrebejk */ public final class VisualPropertySupport { private static final String WRONG_TYPE = "WrongType"; private WebProjectProperties webProperties; private HashMap component2property; private ComponentListener componentListener; private int comboType; //0 ... display text == value //1 ... display text != value private String[] comboValues; public VisualPropertySupport( WebProjectProperties webProperties ) { this.webProperties = webProperties; this.component2property = new HashMap( 10 ); this.componentListener = new ComponentListener(); } /** Registers the component with given property, Fills the component * with given object. */ public void register( JCheckBox component, String propertyName ) { Boolean value = (Boolean)getAsType( propertyName, Boolean.class ); component2property.put( component, propertyName ); component.setSelected( value != null && value.booleanValue() ); component.removeActionListener( componentListener ); component.addActionListener( componentListener ); } /** Registers the component with given property, Fills the component * with given object. */ public void register( JTextField component, String propertyName ) { String value = (String)getAsType( propertyName, String.class ); component2property.put( component.getDocument(), propertyName ); component.setText( value != null ? value : "" ); component.getDocument().addDocumentListener( componentListener ); } /** Registers JTable containing VisualClassPath items and acompaniing * buttons for handling the class path */ public void register( VisualClasspathSupport component, String propertyName ) { List value = (List)getAsType( propertyName, List.class ); component2property.put( component, propertyName ); component.setVisualClassPathItems( value != null ? value : Collections.EMPTY_LIST ); component.removeActionListener( componentListener ); component.addActionListener( componentListener ); } /** Registers JList containing VisualClassPath items and acompaniing * buttons for handling the class path */ public void register(VisualWarIncludesSupport component, String propertyName) { List value = (List) getAsType(propertyName, List.class); component2property.put(component, propertyName); component.setVisualWarItems(value != null ? value : Collections.EMPTY_LIST); component.removeActionListener( componentListener ); component.addActionListener(componentListener); } /** Registers combo box. */ public void register( JComboBox component, String items[], String propertyName ) { comboType = 0; String value = (String)getAsType( propertyName, String.class ); component2property.put( component, propertyName ); // Add all items and find the selected one component.removeAllItems(); int selectedIndex = 0; for ( int i = 0; i < items.length; i++ ) { component.addItem( items[i] ); if ( items[i].equals( value ) ) { selectedIndex = i; } } component.setSelectedIndex( selectedIndex ); component.removeActionListener( componentListener ); component.addActionListener( componentListener ); } /** Registers combo box. */ public void register(JComboBox component, String displayNames[], String[] values, String propertyName) { comboType = 1; comboValues = values; String value = (String) getAsType(propertyName, String.class); component2property.put(component, propertyName); // Add all items and find the selected one component.removeAllItems(); int selectedIndex = -1; for (int i = 0; i < displayNames.length; i++) { component.addItem(displayNames[i]); if (values[i].equals(value)) selectedIndex = i; } if (selectedIndex >= 0) { component.setSelectedIndex(selectedIndex); } else { component.setSelectedItem(null); component.setToolTipText(NbBundle.getMessage(CustomizerRun.class, "LBL_Customize_NoValue_ToolTip")); } component.removeActionListener( componentListener ); component.addActionListener(componentListener); } // Static methods for reading components and models ------------------------ private static Boolean readValue( JCheckBox checkBox ) { return checkBox.isSelected() ? Boolean.TRUE : Boolean.FALSE; } private static String readValue( Document document ) { try { return document.getText( 0, document.getLength() ); } catch ( BadLocationException e ) { assert false : "Invalid document "; //NOI18N return ""; // NOI18N } } private static String readValue( JComboBox comboBox ) { return (String)comboBox.getSelectedItem(); } // Private methods --------------------------------------------------------- private Object getAsType( String propertyName, Class expectedType ) { return getAsType( propertyName, expectedType, true ); } private Object getAsType( String propertyName, Class expectedType, boolean throwException ) { Object value = webProperties.get( propertyName ); if ( value == null || expectedType.isInstance( value ) ) { return value; } else if ( throwException ) { throw new IllegalArgumentException( "Value of property: " + propertyName + // NOI18N " exbected to be: " + expectedType.getName() + // NOI18N " but was: " + value.getClass().getName() + "!" ); // NOI18N } else { return WRONG_TYPE; } } private class ComponentListener implements ActionListener, DocumentListener { // Implementation of action listener ----------------------------------- public void actionPerformed( ActionEvent e ) { Object source = e.getSource(); String propertyName = (String)component2property.get( source ); if( propertyName != null ) { if ( source instanceof JCheckBox ) { webProperties.put( propertyName, readValue( (JCheckBox)source ) ); } else if ( source instanceof VisualClasspathSupport ) { webProperties.put( propertyName, ((VisualClasspathSupport)source).getVisualClassPathItems() ); } else if ( source instanceof JComboBox ) { if (((JComboBox) source).getItemCount() == 0) { return; } ((JComboBox) source).setToolTipText(""); //NOI18N switch (comboType) { case 0: webProperties.put(propertyName, readValue((JComboBox) source)); break; case 1: webProperties.put(propertyName, comboValues[((JComboBox) source).getSelectedIndex()]); break; } } else if ( source instanceof VisualWarIncludesSupport ) { webProperties.put( propertyName, ((VisualWarIncludesSupport)source).getVisualWarItems() ); } } } // Implementation of document listener --------------------------------- public void changedUpdate( DocumentEvent e ) { Document document = e.getDocument(); String propertyName = (String)component2property.get( document ); if( propertyName != null ) { webProperties.put( propertyName, readValue( document ) ); } } public void insertUpdate( DocumentEvent e ) { changedUpdate( e ); } public void removeUpdate( DocumentEvent e ) { changedUpdate( e ); } } } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.