FileDocCategorySizeDatePackage
GenericIntParameter.javaAPI DocAzureus 3.0.3.47169Tue Jul 10 00:10:06 BST 2007org.gudy.azureus2.ui.swt.config.generic

GenericIntParameter

public class GenericIntParameter extends Object

Fields Summary
private static boolean
DEBUG
private GenericParameterAdapter
adapter
private int
iMinValue
private int
iMaxValue
private int
iDefaultValue
private String
sParamName
private boolean
bGenerateIntermediateEvents
private boolean
bTriggerOnFocusOut
private Spinner
spinner
private TimerEvent
timedSaveEvent
private TimerEventPerformer
timerEventSave
Constructors Summary
public GenericIntParameter(GenericParameterAdapter adapter, Composite composite, String name)


	  
			     
		iDefaultValue = adapter.getIntValue(name);
		initialize(adapter, composite, name);
	
public GenericIntParameter(GenericParameterAdapter adapter, Composite composite, String name, int defaultValue)

deprecated

		iDefaultValue = defaultValue;
		initialize(adapter, composite, name);
	
public GenericIntParameter(GenericParameterAdapter adapter, Composite composite, String name, int minValue, int maxValue)

		iDefaultValue = adapter.getIntValue(name);
		iMinValue = minValue;
		iMaxValue = maxValue;
		initialize(adapter, composite, name);
	
Methods Summary
private voidcancelTimedSaveEvent()

		if (timedSaveEvent != null
				&& (!timedSaveEvent.hasRun() || !timedSaveEvent.isCancelled())) {
			if (DEBUG) {
				debug("cancel timeSaveEvent");
			}
			timedSaveEvent.cancel();
		}
	
private voiddebug(java.lang.String string)

param
string

		System.out.println("[GenericIntParameter:" + sParamName + "] " + string);
	
public ControlgetControl()

		return spinner;
	
public java.lang.StringgetName()

		return (sParamName);
	
public intgetValue()

		return (adapter.getIntValue(sParamName, iDefaultValue));
	
public voidinitialize(GenericParameterAdapter _adapter, Composite composite, java.lang.String name)

		adapter = _adapter;
		sParamName = name;

		timerEventSave = new TimerEventPerformer() {
			public void perform(TimerEvent event) {
				Utils.execSWTThread(new AERunnable() {
					public void runSupport() {
						if (spinner.isDisposed()) {
							return;
						}
						if (DEBUG) {
							debug("setIntValue to " + spinner.getSelection()
									+ " via timeEventSave");
						}
						adapter.setIntValue(sParamName, spinner.getSelection());
					}
				});
			}
		};

		int value = adapter.getIntValue(name, iDefaultValue);

		spinner = new Spinner(composite, SWT.BORDER);
		setMinimumValue(iMinValue);
		setMaximumValue(iMaxValue);
		spinner.setSelection(value);

		spinner.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				if (bGenerateIntermediateEvents || !spinner.isFocusControl()) {
					adapter.setIntValue(sParamName, spinner.getSelection());
				} else {
					bTriggerOnFocusOut = true;
					cancelTimedSaveEvent();

					if (DEBUG) {
						debug("create timeSaveEvent (" + spinner.getSelection() + ") ");
					}
					timedSaveEvent = SimpleTimer.addEvent("IntParam Saver",
							SystemTime.getOffsetTime(750), timerEventSave);
				}
			}
		});

		/*
		 * Primarily for OSX, since it doesn't validate or trigger selection
		 * while typing numbers.
		 * 
		 * Force into next tab, which will result in a selection
		 */
		spinner.addListener(SWT.Dispose, new Listener() {
			public void handleEvent(Event event) {
				if (spinner.isFocusControl()) {
					if (DEBUG) {
						debug("next");
					}
					spinner.traverse(SWT.TRAVERSE_TAB_NEXT);
				}
			}
		});

		spinner.addListener(SWT.FocusOut, new Listener() {
			public void handleEvent(Event event) {
				if (bTriggerOnFocusOut) {
					if (DEBUG) {
						debug("focus out setIntValue(" + spinner.getSelection()
								+ "/trigger");
					}
					cancelTimedSaveEvent();
					adapter.setIntValue(sParamName, spinner.getSelection());
				}
			}
		});
	
public booleanisGeneratingIntermediateEvents()

		return bGenerateIntermediateEvents;
	
public voidresetToDefault()

		if (adapter.resetIntDefault(sParamName)) {
			setValue(adapter.getIntValue(sParamName));
		} else {
			setValue(getValue());
		}
	
public voidsetGenerateIntermediateEvents(boolean generateIntermediateEvents)

		bGenerateIntermediateEvents = generateIntermediateEvents;
	
public voidsetLayoutData(java.lang.Object layoutData)

		spinner.setLayoutData(layoutData);
	
public voidsetMaximumValue(int value)

		iMaxValue = value;
		if (iMaxValue != -1 && getValue() > iMaxValue) {
			setValue(iMaxValue);
		}
		Utils.execSWTThread(new AERunnable() {
			public void runSupport() {
				spinner.setMaximum(iMaxValue == -1 ? Integer.MAX_VALUE : iMaxValue);
			}
		});
	
public voidsetMinimumValue(int value)

		iMinValue = value;
		if (iMinValue > 0 && getValue() < iMinValue) {
			setValue(iMinValue);
		}
		Utils.execSWTThread(new AERunnable() {
			public void runSupport() {
				spinner.setMinimum(value);
			}
		});
	
public voidsetValue(int value)

		int newValue;
		if (iMaxValue != -1 && value > iMaxValue) {
			newValue = iMaxValue;
		} else if (iMinValue > 0 && value < iMinValue) {
			newValue = iMinValue;
		} else {
			newValue = value;
		}
		
		final int finalNewValue = newValue;
		
		Utils.execSWTThread(new AERunnable() {
			public void runSupport() {
				if (!spinner.isDisposed()) {
					if (spinner.getSelection() != finalNewValue) {
						if (DEBUG) {
							debug("spinner.setSelection(" + finalNewValue + ")");
						}
						spinner.setSelection(finalNewValue);
					}
					if (DEBUG) {
						debug("setIntValue to " + spinner.getSelection()
								+ " via setValue(int)");
					}
				}
			}
		});
		if (finalNewValue != getValue()) {
			adapter.setIntValue(sParamName, finalNewValue);
		}
	
public voidsetValue(int value, boolean force_adapter_set)

		if (force_adapter_set) {
			setValue(value);
		} else {
			Utils.execSWTThread(new AERunnable() {
				public void runSupport() {
					if (spinner.getSelection() != value) {
						spinner.setSelection(value);
					}
				}
			});
		}