|
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-2003 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.java.j2seproject.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.ComboBoxEditor; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JList; import javax.swing.JTextField; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import javax.swing.event.ListDataListener; import javax.swing.text.BadLocationException; import javax.swing.text.Document; import org.netbeans.modules.java.j2seproject.J2SEProjectUtil; /** 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"; // NOI18N private J2SEProjectProperties j2seProperties; private HashMap component2property; private ComponentListener componentListener; public VisualPropertySupport( J2SEProjectProperties j2seProperties ) { this.j2seProperties = j2seProperties; 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.removeActionListener( componentListener ); component.setSelected( value != null && value.booleanValue() ); 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.getDocument().removeDocumentListener( componentListener ); component.setText( value != null ? value : "" ); component.getDocument().addDocumentListener( componentListener ); } /** Registers JList 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.removeActionListener( componentListener ); component.setVisualClassPathItems( value != null ? value : Collections.EMPTY_LIST ); component.addActionListener( componentListener ); } /** */ public void register (VisualMainClassSupport component, String propertyName) { String value = (String)getAsType (propertyName, String.class); component2property.put (component, propertyName); component.removeActionListener (componentListener); component.setMainClass (value); component.addActionListener (componentListener); } /** Registers combo box. */ public void register(JComboBox component, String items[], String propertyName) { String value = (String)getAsType( propertyName, String.class ); register(component, items, propertyName, value); } public void register(JComboBox component, String items[], String propertyName, String value) { component2property.put( component, propertyName ); component.removeActionListener( componentListener ); // 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.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; if (expectedType.equals (String.class)) { value = J2SEProjectUtil.getEvaluatedProperty (j2seProperties.getProject (), j2seProperties, propertyName); } else { value = j2seProperties.get( propertyName ); } if ( value == null || expectedType.isInstance( value ) ) { return value; } else if ( throwException ) { throw new IllegalArgumentException( "Value of property: " + propertyName + // NOI18N " expected 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 ) { j2seProperties.put( propertyName, readValue( (JCheckBox)source ) ); } else if ( source instanceof VisualClasspathSupport ) { j2seProperties.put( propertyName, ((VisualClasspathSupport)source).getVisualClassPathItems() ); } else if ( source instanceof VisualMainClassSupport ) { j2seProperties.put (propertyName, ((VisualMainClassSupport)source).getMainClass ()); } else if ( source instanceof JComboBox ) { j2seProperties.put( propertyName, readValue( (JComboBox)source ) ); } } } // Implementation of document listener --------------------------------- public void changedUpdate( DocumentEvent e ) { Document document = e.getDocument(); String propertyName = (String)component2property.get( document ); if( propertyName != null ) { j2seProperties.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.