Component.java 10.2 KB
Newer Older
1 2 3
/*******************************************************************************
 * Copyright (c) 2014 Elphel, Inc.
 * Copyright (c) 2006 Elphel, Inc and Excelsior, LLC.
4 5
 * This file is a part of VDT plug-in.
 * VDT plug-in is free software; you can redistribute it and/or modify
6 7 8 9
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
10
 * VDT plug-in is distributed in the hope that it will be useful,
11 12 13 14 15 16
 * 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 this program.  If not, see <http://www.gnu.org/licenses/>.
Andrey Filippov's avatar
Andrey Filippov committed
17 18 19 20 21 22 23 24 25
 * 
 *  Additional permission under GNU GPL version 3 section 7:
 * If you modify this Program, or any covered work, by linking or combining it
 * with Eclipse or Eclipse plugins (or a modified version of those libraries),
 * containing parts covered by the terms of EPL/CPL, the licensors of this
 * Program grant you additional permission to convey the resulting work.
 * {Corresponding Source for a non-source form of such a combination shall
 * include the source code for the parts of Eclipse or Eclipse plugins used
 * as well as that of the covered work.}
26
 *******************************************************************************/
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 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
package com.elphel.vdt.ui.options.component;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Decorations;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.graphics.Color;

//import com.elphel.vdt.VDTPlugin;
import com.elphel.vdt.veditor.VerilogPlugin;
import com.elphel.vdt.core.tools.params.Parameter;

public abstract class Component {

    private static int TEXTFIELD_WIDTH = 250;

    private Label labelField;
    
    protected Composite parent;
    protected Parameter param;

    protected static Color colorBackground;
    protected static Color colorBackgroundDefault;

    private static Color colorForeground;
    private static Color colorForegroundDefault;

    private ChangeNotifier changeNotifier;
    
    protected boolean isDefault;
    protected boolean loaded;
    protected boolean focused;
    protected boolean activated;

    protected ChangeListener changeListener;
    
    public Component(Parameter param) {
        this(param, null);
    }
    
    public Component(Parameter param, ChangeListener changeListener) {
//        System.out.println("-- Component: id= "+param.getID()+"; label= "+param.getLabel());
        this.param = param;
        setChangeListener(changeListener);
        loaded    = false;
        focused   = false;
        activated = false;
    }

    public void createControl(Composite parent) {
//        System.out.println("-- Component.createControl: id= "+param.getID()+"; label= "+param.getLabel());
        this.parent = parent;
        activated = false;
88 89 90
        if (param!=null) {// Andrey - to add labels
        	labelField = createLabel(parent, param.getLabel());
        	labelField.setMenu(createPopupMenu(labelField.getShell()));
91 92 93
        	if (param.getToolTip()!=null){
        		labelField.setToolTipText(param.getToolTip());
        	}
94
        }
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    }

    protected void endCreateControl() {
//        System.out.println("-- Component.finalizeCreateControl: id= "+param.getID()+"; label= "+param.getLabel());
        setEnabled(isEnable());
        if (! loaded) {
            setDefault(isDefault);
            loaded = true;
        }
    }

    public void resumeControl() {
        if (isDisposed()) 
            return;
        
        if (! activated){
            setDefault(isDefault);
            activated = true;
        }
    }

    public void suspendControl() {
        if (isDisposed()) 
            return;

        if (activated) {
            removeListeners();
            activated = false;
            if (changeNotifier != null)
                changeNotifier.stop();
        }
    }
    
    
    public void setChangeListener(ChangeListener changeListener) {
        this.changeListener = changeListener;
    }
    
    public abstract String performApply();

    public abstract void setPreferenceStore(IPreferenceStore store);

    protected abstract void saveControlState();
    
    public abstract void setFocus();

    protected abstract void addListeners();
    protected abstract void removeListeners();
    
    protected abstract boolean isDisposed();
    
    public Parameter getParam() {
        return param;
    }

    public boolean isEnable() {
151
    	if (param==null) return true; // label
152 153 154 155
        return ! param.isReadOnly();
    }

    public void setVisible (boolean visible) {
156 157
    	if (labelField!=null) 
    		labelField.setVisible(visible);
158 159 160
    }
    
    public void setEnabled (boolean enabled) {
161 162
    	if (labelField!=null) 
    		labelField.setEnabled(enabled);
163 164 165 166 167
    }
    
    protected void setDefault(boolean defaulted) {
//        System.out.println("-- Component.setDefault: id= "+param.getID()+"; label= "+param.getLabel());
        isDefault = defaulted;
168 169 170
    	if (labelField!=null) 
    		labelField.setForeground(defaulted ? colorForegroundDefault
    				: colorForeground );     
171 172 173 174
    }
    
    protected void selectionChanged() {
//        System.out.println("-- Component.selectionChanged: id= "+param.getID()+"; label= "+param.getLabel());
175
        if ((param == null) || (! param.hasDependentParameters()))
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
            return;
            
        if (changeNotifier == null)
            changeNotifier = new ChangeNotifier();
        changeNotifier.valueChanged();
    }
    
    protected Menu createPopupMenu(Decorations parent) {
        Menu menu = new Menu(parent.getShell(), SWT.POP_UP);
        MenuItem actionItem = new MenuItem(menu, SWT.PUSH);
        actionItem.setText("Set to default");
        actionItem.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event e) {
                if (! isDefault) {
                    setDefault(true);
                    notifyChangeListener();
                }
            }
        });
        return menu;
    }

    protected static Label createLabel(Composite parent, String text) {
        GridData labelData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
        Label label = new Label(parent, SWT.NONE);
        label.setText(text);
        label.setLayoutData(labelData);
        return label;
    }

    public static Text createTextControl(Composite parent) {
        Text textControl = new Text(parent, SWT.BORDER | SWT.SINGLE);
        GridData gridData = new GridData( GridData.HORIZONTAL_ALIGN_FILL | 
                                          GridData.GRAB_HORIZONTAL );
        gridData.widthHint = TEXTFIELD_WIDTH;
        textControl.setLayoutData(gridData);
        return textControl;
    }

    //-------------------------------------------------------------------------
    public interface ChangeListener {
        public void valueChanged(Component componenet);
    }

    private synchronized void notifyChangeListener() {
//        System.out.println("-- Component.notifyChangeListener: id= "+param.getID()+"; label= "+param.getLabel());
        if (changeListener != null) {
            removeListeners();
            saveControlState();
            changeListener.valueChanged(this);
            if (!isDisposed())
                setFocus();            
        }
    }
    
    private class ChangeNotifier {
        private static final int SLEEP = 15;

        private static final int STOP      = 0;
        private static final int CONTIONUE = 1;
        private static final int NOTIFY    = 2;
        
        private boolean active = false;
        private int count = 0;
        
        private Display display = VerilogPlugin.getStandardDisplay();
        private Runnable [] timer = new Runnable [] {
                new Runnable() {
                    public void run () {
                        switch (getStatus()) {
                        case CONTIONUE:
                            display.timerExec(SLEEP, timer [0]);
                            break;
                        case NOTIFY:    
                            notifyChangeListener();
                            stop();
                            break;
                        }
                    }
                }    
        };

        public synchronized void valueChanged() {
            count++;
            if (! active) {
                active = true;
                display.timerExec(SLEEP, timer [0]);
            }
        }

        public synchronized void stop() {
            active = false;
        }
        
        private synchronized int getStatus() {
            int status;
            if (active) {
                if (count > 1) {
                    status = CONTIONUE;
                } else {
                    status = NOTIFY;
                }    
            } else {
                status = STOP;
            }
            count = 0;
            return status;
        }

    } // class ChangeHandler
    
    //-------------------------------------------------------------------------
    static {
        colorBackground = VerilogPlugin.getStandardDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND);
//        colorBackgroundDefault = new Color(VDTPlugin.getStandardDisplay(), 234, 249, 255);
        colorBackgroundDefault = new Color(VerilogPlugin.getStandardDisplay(), 232, 242, 254);

        colorForeground = VerilogPlugin.getStandardDisplay().getSystemColor(SWT.COLOR_LIST_FOREGROUND);
        colorForegroundDefault = VerilogPlugin.getStandardDisplay().getSystemColor(SWT.COLOR_BLUE);
    }
    
} // class Component