ListOption.java 2.97 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/*******************************************************************************
 * Copyright (c) 2006 Elphel, Inc and Excelsior, LLC.
 * This file is a part of Eclipse/VDT plug-in.
 * Eclipse/VDT plug-in is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * Eclipse/VDT plug-in is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with Eclipse VDT plug-in; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *******************************************************************************/
package com.elphel.vdt.ui.options.project;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.widgets.Combo;

/**
 * Option represented by the STW Combo control
 * 
 * Created: 16.02.2006
 * @author  Lvov Konstantin
 */
public class ListOption implements IOption {

    private Combo combo;
    private String[] items;
    private Option option;
    
    ListOption(Combo combo, QualifiedName name, String[] items, int defaultValue) {
        option = new Option(name, items[defaultValue]); 
        this.combo = combo;
        this.items = items;
        this.combo.setItems(items);
        this.combo.select(0);
    }
    
    /**
46
     * Return core option (option without UI elements)
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
     */
    public Option getOption() {
        return option;
    }
    
    public void setPreferenceStore(IPreferenceStore store) {
        option.setPreferenceStore(store);
    }
    
    public void setResourceStore(IResource resource) {
        option.setResourceStore(resource);
    }
    
    /**
     * Set default value to STW control
     */
    public void reset() {
        option.reset();
        setSelection(option.getDefaultValue());
    } // reset()

    /**
     * Set value to STW control
     */
    public void read() {
        option.read();
        setSelection(option.getValue());
    } // read()

    /**
     * Save value from STW control
     */
    public boolean save() {
        String value = combo.getText();
        option.setValue(value);
        return option.save();
    } // save()

    private void setSelection(String value) {
        if (value != null) {
		    for (int i=0; i < items.length; i++) {
		        if (value.equals(items[i])){
		            combo.select(i);
		            return;
		        }
		    }
        }
        combo.select(0);
    } // setSelection()
    
} // class ListOption