PackageLocationFieldEditor.java 4.86 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
/*******************************************************************************
 * 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.fieldeditor;

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

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.StringButtonFieldEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;

import com.elphel.vdt.core.options.OptionsCore;
import com.elphel.vdt.core.tools.ToolsCore;
import com.elphel.vdt.core.tools.contexts.PackageContext;
import com.elphel.vdt.core.tools.params.Tool;
import com.elphel.vdt.ui.options.SetupOptionsManager;


public class PackageLocationFieldEditor extends StringButtonFieldEditor {

    private PackageContext context;
    
    public PackageLocationFieldEditor(PackageContext context, Composite parent) {
        this.context = context;
        String preferenceName = OptionsCore.getLocationPreferenceName(context);
        init(preferenceName, "Package location: ");
        setChangeButtonText("Browse");
        setErrorMessage("Invalid package location");
//        setValidateStrategy(VALIDATE_ON_FOCUS_LOST);
        createControl(parent);
    }
    
    /* (non-Javadoc)
     * Method declared on StringButtonFieldEditor.
     * Opens the directory chooser dialog and returns the selected directory.
     */
    protected String changePressed() {
        File f = new File(getTextControl().getText());
        if (!f.exists())
            f = null;
        File d = getDirectory(f);
        if (d == null)
            return null;

        return d.getAbsolutePath();
    }

    /* (non-Javadoc)
     * Method declared on StringFieldEditor.
     * Checks whether the text input field contains a valid directory.
     */
    protected boolean doCheckState() {
        boolean ok;
        String fileName = getTextControl().getText();
        fileName = fileName.trim();
        if (fileName.length() == 0 && isEmptyStringAllowed()) {
            ok = true;
        } else {
            File file = new File(fileName);
            ok = file.isDirectory();
        }
        if (ok) {
            SetupOptionsManager.setCurrentLocation(context, fileName);
        }
        return ok;
    }

    /**
     * Helper that opens the directory chooser dialog.
     * @param startingDirectory The directory the dialog will open in.
     * @return File File or <code>null</code>.
     * 
     */
    private File getDirectory(File startingDirectory) {
        DirectoryDialog dialog = new DirectoryDialog(getShell(), SWT.OPEN);
        dialog.setMessage("Specify the location of \""+context.getLabel()+"\""); 
        if (startingDirectory != null)
            dialog.setFilterPath(startingDirectory.getPath());
        String dir = dialog.open();
        if (dir != null) {
            dir = dir.trim();
            if (dir.length() > 0)
                return new File(dir);
        }

        return null;
    }
    
    protected void doStore() {
        super.doStore();
        context.setWorkingDirectory(getTextControl().getText());
        List<Tool> tools = ToolsCore.getTools(context);
        for (Iterator<Tool> i = tools.iterator(); i.hasNext(); ) {
            Tool tool = i.next();
            if (OptionsCore.isLocationRelative(tool)) {
                OptionsCore.doLoadLocation(tool);
                String toolLocation = OptionsCore.getAbsoluteLocation(context, tool.getExeName());
                tool.setLocation(toolLocation);
            }
        }
    }

    protected void doLoad() {
        super.doLoad();
    }
    
    
    public static void doClear(PackageContext context, IPreferenceStore store) {
        String preferenceName = OptionsCore.getLocationPreferenceName(context);
        store.setToDefault(preferenceName);
        context.setWorkingDirectory(null);
    }
    
} // class PackageLocationFieldEditor