FileDocCategorySizeDatePackage
StringListParameter.javaAPI DocAzureus 3.0.3.46433Tue Jun 05 00:46:58 BST 2007org.gudy.azureus2.ui.swt.config

StringListParameter

public class StringListParameter extends Parameter
author
Olivier

Fields Summary
Control
list
final String
name
final String
default_value
private final String[]
values
private final boolean
useCombo
Constructors Summary
public StringListParameter(Composite composite, String _name, String[] labels, String[] values, boolean bUseCombo)

param
composite
param
_name
param
labels
param
values
param
bUseCombo

		this(composite, _name, COConfigurationManager.getStringParameter(_name),
				labels, values, bUseCombo);
	
public StringListParameter(Composite composite, String _name, String[] labels, String[] values)

param
composite
param
_name
param
labels
param
values

		this(composite, _name, COConfigurationManager.getStringParameter(_name),
				labels, values, true);
	
public StringListParameter(Composite composite, String _name, String defaultValue, String[] labels, String[] values)

param
composite
param
_name
param
defaultValue
param
labels
param
values

		this(composite, _name, defaultValue, labels, values, true);
	
public StringListParameter(Composite composite, String _name, String defaultValue, String[] labels, String[] values, boolean bUseCombo)

param
composite
param
_name
param
defaultValue
param
labels
param
values
param
bUseCombo

  	super(_name);
    this.name = _name;
    this.default_value = defaultValue;
		this.values = values;
		useCombo = bUseCombo;
    
    if(labels.length != values.length) {
      return;
    }
    
    String value = COConfigurationManager.getStringParameter(name,defaultValue);
    int index = findIndex(value,values);
    if (bUseCombo) {
    	list = new Combo(composite,SWT.SINGLE | SWT.READ_ONLY);
    } else {
    	list = new List(composite, SWT.SINGLE | SWT.BORDER | SWT.HORIZONTAL | SWT.VERTICAL) {
    		// I know what I'm doing. Maybe ;)
    		public void checkSubclass() {
    		}

      	// @see org.eclipse.swt.widgets.Text#computeSize(int, int, boolean)
      	public Point computeSize(int wHint, int hHint, boolean changed) {
      		// List widget, at least on Windows, forces the preferred height
      		Point pt = super.computeSize(wHint, hHint, changed);
      		
      		if (hHint == SWT.DEFAULT) {
        		Object ld = getLayoutData();
        		if (ld instanceof GridData) {
        			if (((GridData)ld).grabExcessVerticalSpace) {
        				pt.y = 20;
        			}
        		}
      		}

      		return pt;
      	}
    	};
    }
    
    for(int i = 0 ; i < labels.length  ;i++) {
    	if (bUseCombo)
    		((Combo)list).add(labels[i]);
    	else
    		((List)list).add(labels[i]);
    }
      
    setIndex(index);
      
    list.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event e) {
      	int index;
      	if (bUseCombo)
      		index = ((Combo)list).getSelectionIndex();
      	else
      		index = ((List)list).getSelectionIndex();
      	setIndex(index);
        
        if( change_listeners != null ) {
          for (int i=0;i<change_listeners.size();i++){
            ((ParameterChangeListener)change_listeners.get(i)).parameterChanged(StringListParameter.this,false);
          }
        }
      }
    }); 
  
Methods Summary
private intfindIndex(java.lang.String value, java.lang.String[] values)

    for(int i = 0 ; i < values.length ;i++) {
      if(values[i].equals( value))
        return i;
    }
    return -1;
  
public ControlgetControl()

    return list;
  
public java.lang.StringgetValue()

    return COConfigurationManager.getStringParameter( name, default_value );
  
public java.lang.ObjectgetValueObject()

  	return getValue();
  
protected voidsetIndex(int index)

		if (index < 0) {
			COConfigurationManager.removeParameter(name);

			String defValue = COConfigurationManager.getStringParameter(name);
			int i = findIndex(defValue, values);
			if (i >= 0) {
				// no recursion, because this area only gets called again if i was < 0
				setIndex(i);
			} else {
				Utils.execSWTThread(new AERunnable() {
					public void runSupport() {
						if (list == null || list.isDisposed()) {
							return;
						}

				  	if (useCombo) {
			  			((Combo)list).deselectAll();
				  	} else {
			  			((List)list).deselectAll();
				  	}
					}
				});
			}
			return;
		}
		
  	String selected_value = values[index];

		Utils.execSWTThread(new AERunnable() {
			public void runSupport() {
				if (list == null || list.isDisposed()) {
					return;
				}

		  	if (useCombo) {
		  		if (((Combo)list).getSelectionIndex() != index) {
		  			((Combo)list).select(index);
		  		}
		  	} else {
		  		if (((List)list).getSelectionIndex() != index) {
		  			((List)list).select(index);
		  		}
		  	}
			}
		});
  	
  	if (!COConfigurationManager.getStringParameter(name).equals(selected_value)) {
  		COConfigurationManager.setParameter(name, selected_value);
  	}
	
public voidsetLayoutData(java.lang.Object layoutData)

    list.setLayoutData(layoutData);
   
public voidsetValue(java.lang.Object value)

  	if (value instanceof String) {
  		String s = (String)value;
      setIndex(findIndex(s, values));
  	}