LaunchConfigurationTab.java 9.66 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 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 88 89 90 91 92 93 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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 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
/*******************************************************************************
 * 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.tools;


import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;

import com.elphel.vdt.core.tools.params.Parameter;
import com.elphel.vdt.core.tools.params.types.ParamType;
import com.elphel.vdt.core.tools.params.types.ParamTypeBool;
import com.elphel.vdt.core.tools.params.types.ParamTypeEnum;
import com.elphel.vdt.core.tools.params.types.ParamTypeNumber;
import com.elphel.vdt.core.tools.params.types.ParamTypeString;
import com.elphel.vdt.ui.tools.params.AbstractTabComponent;
import com.elphel.vdt.ui.tools.params.BoolTabComponent;
import com.elphel.vdt.ui.tools.params.DirTabComponent;
import com.elphel.vdt.ui.tools.params.FileTabComponent;
import com.elphel.vdt.ui.tools.params.ListTabComponent;
import com.elphel.vdt.ui.tools.params.NumberTabComponent;
import com.elphel.vdt.ui.tools.params.TextTabComponent;
import com.elphel.vdt.ui.MessageUI;
import com.elphel.vdt.Txt;


/**
 * This tab appears for Verilog tool launch configurations and allows 
 * the user to edit tool parameters.
 * 
 * Created: 27.12.2005
 * @author  Lvov Konstantin
 */

public class LaunchConfigurationTab extends AbstractLaunchConfigurationTab {

    private String tabCaption;
    private ToolUI toolUI;
    private String paramGroup;
    private String toolLocation;    
    private List<AbstractTabComponent> components;
    
    private boolean isInitializing = false;
    private boolean userEdited     = false;
    
    public final static String FIRST_EDIT = "editedByVDTLaunchConfiguration";

    public ToolUI getToolUI() { return toolUI; }
    
    public LaunchConfigurationTab(String caption, ToolUI toolUI, String paramGroup) {
        super();
        
        this.tabCaption = caption;
        this.toolUI = toolUI;
        this.paramGroup = paramGroup;
        this.toolLocation = toolUI.getToolLocation();
        
        createComponents();
    } // LaunchConfigurationTab()
        
    private void createComponents() {
        components = new ArrayList<AbstractTabComponent>();
//        for (Iterator i = toolUI.getTool().getParams().iterator(); i.hasNext(); ) {
        for (Iterator<Parameter> i = toolUI.getTool().getParams().iterator(); i.hasNext(); ) {
            Parameter param = (Parameter)i.next();
            
            if(!param.isVisible())
                continue;
            
            String group = null;//param.getGroupName();
            
            if(group == paramGroup || paramGroup.equals(group)) {
                ParamType paramType = param.getType();
                
                if (paramType instanceof ParamTypeNumber) {
                    components.add(new NumberTabComponent(this, param));         
                } else if (paramType instanceof ParamTypeBool) {
                    components.add(new BoolTabComponent(this, param));         
                } else if (paramType instanceof ParamTypeString) {
                    if(((ParamTypeString)paramType).getKind() == ParamTypeString.KIND.FILE)
                        components.add(new FileTabComponent(this, param));
                    else if(((ParamTypeString)paramType).getKind() == ParamTypeString.KIND.DIR)
                        components.add(new DirTabComponent(this, param));
                    else
                        components.add(new TextTabComponent(this, param));
                } else if (paramType instanceof ParamTypeEnum) {
                    components.add(new ListTabComponent(this, param));         
                } else {
                    MessageUI.error(Txt.s("LaunchTab.Error.UnsupportedType", param.getType().getName()));
                }
            }
        }
    } // createComponets()
        
    public void createControl(Composite parent) {
        GridLayout layout = new GridLayout(2, false);
        layout.marginHeight = 5;
        layout.marginWidth  = 5;
        layout.verticalSpacing   = 3;
        layout.horizontalSpacing = 5;

        Composite panel = new Composite(parent, SWT.NONE);

        for (Iterator<AbstractTabComponent> i = components.iterator(); i.hasNext();)
            ((AbstractTabComponent)i.next()).createControl(panel);

        // do the common stuff
        setControl(panel);
        panel.setFont(parent.getFont());
        
        GridData gridData = new GridData(GridData.FILL_HORIZONTAL);

        panel.setLayout(layout);
        panel.setLayoutData(gridData);

        createVerticalSpacer(panel, 1);

        Dialog.applyDialogFont(parent);
    } // createControl()

    /**
     * Initializes the given launch configuration with default values for this 
     * tab.
     */ 
    public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
        configuration.setAttribute(FIRST_EDIT, true);
        for (Iterator<AbstractTabComponent> i = components.iterator(); i.hasNext(); ) {
            ((AbstractTabComponent)i.next()).setDefaults(configuration);
        }
    } // setDefaults()

    /**
     * Updates the tab's widgets to match the state of the given launch 
     * configuration.
     */
    public void initializeFrom(ILaunchConfiguration configuration) {
        isInitializing = true;
        for (Iterator<AbstractTabComponent> i = components.iterator(); i.hasNext(); ) {
            ((AbstractTabComponent)i.next()).initializeFrom(configuration);
        }
        isInitializing = false;
        setDirty(false);
    } // initializeFrom()

    /**
     * Copies values from this tab into the given launch configuration.
     */
    public void performApply(ILaunchConfigurationWorkingCopy configuration) {
        for (Iterator<AbstractTabComponent> i = components.iterator(); i.hasNext(); ) {
            AbstractTabComponent paramUI = ((AbstractTabComponent)i.next()); 
            paramUI.performApply(configuration);
        }
        if (userEdited) {
            configuration.setAttribute(FIRST_EDIT, false);
        }
    } // performApply()

    /**
     * Validates the content of the tab components.
     */
    public boolean isValid(ILaunchConfiguration launchConfig) {
        setErrorMessage(null);
        setMessage(null);
        boolean newConfig = false;
//        try {
//            newConfig = launchConfig.getAttribute(FIRST_EDIT, false);
//        } catch (CoreException e) {
//                //assume false is correct
//        }

        if (!validateToolLocation()) {
        	setErrorMessage(Txt.s("LaunchTab.Error.ToolLocation"
        			, new Object[] {toolUI.getTool().getLabel(), toolLocation}
        			));
            return false;
        }
                
        for (Iterator<AbstractTabComponent> i = components.iterator(); i.hasNext(); ) {
        if (!((AbstractTabComponent)i.next()).isValid(newConfig))
            return false;
        }
        return true;
    } // isValid()
        
    /**
     * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
     */
    public String getName() {
        return tabCaption;
    } // getName()

    /**
     * Returns the shell this tab is contained in, or <code>null</code>.
     */
    public Shell getShell() {
        return super.getShell();
    }
    
    public void textModifyedNotification() { 
        if (!isInitializing) {
            setDirty(true);
            userEdited = true;
            updateLaunchConfigurationDialog();
        }
    } // textModifyedNotification()
    
    public void dirtyNotification() {
        setDirty(true);
    }
    
    /**
     * Sets this page's error message, possibly <code>null</code>.
     * 
     * @param errorMessage the error message or <code>null</code>
     */
    public void setErrorMessage(String errorMessage) {
        super.setErrorMessage(errorMessage);
    }

    /**
     * Sets this page's message, possibly <code>null</code>.
     * 
     * @param message the message or <code>null</code>
     */
    public void setMessage(String message) {
        super.setMessage(message);
    }
        
    private boolean validateToolLocation() {
        File file = new File(toolLocation);
        return (file != null)
            &&  file.exists()
            &&  file.isFile()
             ;           
    }
        
} // LaunchConfigurationTab