/* * Ant.java * * Created on December 10, 2004, 12:44 PM */ package org.netbeans.modules.java.j2seproject.support; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.swing.ButtonModel; import javax.swing.DefaultButtonModel; import javax.swing.text.BadLocationException; import javax.swing.text.Document; import javax.swing.text.PlainDocument; import org.netbeans.spi.project.support.ant.EditableProperties; import org.netbeans.spi.project.support.ant.PropertyEvaluator; import org.netbeans.spi.project.support.ant.PropertyUtils; import org.openide.ErrorManager; /** * * @author Petr Hrebejk */ public class Ant { /** Serves as utility class for storing Swing models into project p * properties. */ public static class StoreGroup { /** The object array serves as holder for various infos about models * first is allways the model. The rest depends on the model type * 1) Buttion model kind, inverted * 2) String model (not used) */ private Map /**/ models; private static final Integer BOOLAN_KIND_TF = new Integer( 0 ); private static final Integer BOOLAN_KIND_YN = new Integer( 1 ); private static final Integer BOOLAN_KIND_ED = new Integer( 2 ); public StoreGroup() { models = new HashMap(); } // Public methods ------------------------------------------------------ /** Stores all models created in the StoreGroup into given * EditableProperties. * @param editableProperties The properties where to store the * values. */ public void store( EditableProperties editableProperties ) { for( Iterator it = models.keySet().iterator(); it.hasNext(); ) { String key = (String)it.next(); Object[] params = (Object[])models.get( key ); if ( params[0] instanceof ButtonModel ) { ButtonModel model = (ButtonModel)params[0]; boolean value = model.isSelected(); if ( params[2] == Boolean.TRUE ) { value = !value; } editableProperties.put( key, encodeBoolean( value, (Integer)params[1] ) ); } else if ( params[0] instanceof Document ) { Document doc = (Document)params[0]; String txt; try { txt = doc.getText(0, doc.getLength()); } catch (BadLocationException e) { txt = ""; // NOI18N } editableProperties.put( key, txt ); } } } /** Creates button model representing a boolean in the StoreGroup.
* In case the value is one of "true", "yes" "on" the button model * will be "selcted". If the property does not exist or is set * to some other value the result of isPressed will be false.
* Call to the store() method stores the model in appropriate form * e.g "true/false", "yes/no", "enabled/disabled". * @param evaluator The PropertyEvaluator to be used to evaluate given * property * @param propertyName Name of the ANT property * @return ButtonModel representing the value */ public final ButtonModel createBooleanButtonModel( PropertyEvaluator evaluator, String propName ) { return createBooleanButtonModel( evaluator, propName, false ); } /** Creates button model representing a boolean in the StoreGroup.
* In case the value is one of "true", "yes" "on" the button model * will NOT be "selcted". If the property does not exist or is set * to some other value the result of isPressed will be true.
* Call to the store() method stores the model in appropriate form * e.g "true/false", "yes/no", "enabled/disabled". * @param evaluator The PropertyEvaluator to be used to evaluate given * property * @param propertyName Name of the ANT property * @return ButtonModel representing the value */ public final ButtonModel createInverseBooleanButtonModel( PropertyEvaluator evaluator, String propName ) { return createBooleanButtonModel( evaluator, propName, true ); } /** Creates Document containing the string value of given property. * If the property does not extsts or the value of it is null the * resulting document will be empty. * * @param evaluator The PropertyEvaluator to be used to evaluate given * property * @param propertyName Name of the ANT property * @return ButtonModel representing the value */ public final Document createStringDocument( PropertyEvaluator evaluator, String propName ) { String value = evaluator.getProperty( propName ); if ( value == null ) { value = ""; // NOI18N } System.out.println("PROP : " + propName + " - " + value ); try { Document d = new PlainDocument(); d.remove(0, d.getLength()); d.insertString(0, value, null); models.put( propName, d ); return d; } catch ( BadLocationException e ) { ErrorManager.getDefault().notify(e); return new PlainDocument(); } } // Private methods ----------------------------------------------------- private final ButtonModel createBooleanButtonModel( PropertyEvaluator evaluator, String propName, boolean invert ) { String value = evaluator.getProperty( propName ); boolean isSelected = false; Integer kind = BOOLAN_KIND_TF; if ( value != null ) { String lowercaseValue = value.toLowerCase(); if ( lowercaseValue.equals( "yes" ) || lowercaseValue.equals( "no" ) ) { // NOI18N kind = BOOLAN_KIND_YN; } else if ( lowercaseValue.equals( "enabled" ) || lowercaseValue.equals( "disabled" ) ) { // NOI18N kind = BOOLAN_KIND_ED; } if ( lowercaseValue.equals( "true") || // NOI18N lowercaseValue.equals( "yes") || // NOI18N lowercaseValue.equals( "enabled") ) {// NOI18N isSelected = true; } } ButtonModel bm = new DefaultButtonModel(); bm.setSelected( invert ? !isSelected : isSelected ); models.put( propName, new Object[] { bm, kind, Boolean.valueOf( invert ) } ); return bm; } private static String encodeBoolean( boolean value, Integer kind ) { if ( kind == BOOLAN_KIND_ED ) { return value ? "enabled" : "disabled"; // NOI18N } else if ( kind == BOOLAN_KIND_YN ) { // NOI18N return value ? "yes" : "no"; } else { return value ? "true" : "false"; // NOI18N } } } }