pl.wroc.pwr.jbehaveplugin.configuration.wizard.SelectLocationPage.java Source code

Java tutorial

Introduction

Here is the source code for pl.wroc.pwr.jbehaveplugin.configuration.wizard.SelectLocationPage.java

Source

/*******************************************************************************
 * Copyright (c) 2012 M. Cenkar, P. Nurkowski, A. Pawelec, M. Piatek
 * 
 * Permission is hereby granted,  free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal 
 * in the Software without  restriction, including without limitation the rights 
 * to use, copy,  modify,  merge,  publish,  distribute, sublicense, and/or sell 
 * copies  of  the  Software,  and  to  permit  persons  to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The  above  copyright  notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE  SOFTWARE  IS  PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED,  INCLUDING  BUT  NOT  LIMITED  TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS  FOR  A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS  OR  COPYRIGHT  HOLDERS  BE  LIABLE  FOR  ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF  OR  IN CONNECTION  WITH  THE  SOFTWARE  OR  THE USE OR OTHER DEALINGS 
 * IN THE SOFTWARE.
 ******************************************************************************/
package pl.wroc.pwr.jbehaveplugin.configuration.wizard;

import java.io.File;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;

/**
 * Strona konfiguracji suca do wyboru pooenia pliku konfiguracyjnego.
 */
class SelectLocationPage extends WizardNewFileCreationPage {
    /**
     * Tworzy now stron kreatora.
     * 
     * @param pageName nazwa strony
     * @param selection wstpne zaznaczenie
     */
    public SelectLocationPage(String pageName, IStructuredSelection selection) {
        super(pageName, selection);

        setTitle("Select configuration file location");
        setDescription("Provide location of created configuration file.");

        // rozszerzenie pliku
        setFileExtension("java");
        setAllowExistingResources(false);
    }

    @Override
    protected boolean validatePage() {
        super.validatePage();

        if (checkIfFileExists(getFileName())) {
            return false;
        }

        // dokonanie walidacji dla nazwy pliku - musi by poprawn nazw pliku
        // dla typu Java
        if (!isValidJavaFileName(getFileName())) {
            setMessage("Invalid file name - " + "must be legal Java file, ie. Test.java.", ERROR);
            return false;
        } else {
            return true;
        }
    }

    /**
     * Sprawdza czy podany plik istnieje.
     * 
     * @param fileName nazwa pliku
     * @return <code>true</code> jeeli plik istnieje
     */
    protected static boolean checkIfFileExists(String fileName) {
        File file = new File(fileName);
        return file.exists();
    }

    /**
     * Sprawdza czy podany plik jest poprawn nazw dla kodu rdowego Java.
     * 
     * @param fileName nazwa pliku
     * @return true jeeli plik ma poprawn nazw
     */
    protected static boolean isValidJavaFileName(String fileName) {
        return StringUtils.countMatches(fileName, ".") == 1 && fileName.endsWith(".java");
    }

}