org.openscada.eclipse.classpath.dir.ui.ClassPathContainerPageImpl.java Source code

Java tutorial

Introduction

Here is the source code for org.openscada.eclipse.classpath.dir.ui.ClassPathContainerPageImpl.java

Source

/*
 * This file is part of the OpenSCADA project
 * Copyright (C) 2009-2010 inavare GmbH (http://inavare.com)
 *
 * OpenSCADA is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenSCADA 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 Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenSCADA. If not, see
 * <http://opensource.org/licenses/lgpl-3.0.html> for a copy of the LGPLv3 License.
 */

package org.openscada.eclipse.classpath.dir.ui;

import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.variables.IStringVariableManager;
import org.eclipse.core.variables.VariablesPlugin;
import org.eclipse.debug.ui.StringVariableSelectionDialog;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.ui.wizards.IClasspathContainerPage;
import org.eclipse.jdt.ui.wizards.NewElementWizardPage;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.openscada.eclipse.classpath.dir.Activator;
import org.openscada.eclipse.classpath.dir.Helper;

public class ClassPathContainerPageImpl extends NewElementWizardPage implements IClasspathContainerPage {

    private boolean exported;

    private String path;

    private Text pathEntry;

    public ClassPathContainerPageImpl() {
        super("Directory library");
        setTitle("Directory library");
        this.path = "not/set";
    }

    public boolean finish() {
        return true;
    }

    public void createControl(final Composite parent) {
        final Composite composite = new Composite(parent, SWT.NONE);

        composite.setLayout(new GridLayout(3, false));

        Label label;

        label = new Label(composite, SWT.NONE);
        label.setText("Path:");
        label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));

        this.pathEntry = new Text(composite, SWT.BORDER);
        this.pathEntry.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

        final Button varButton = new Button(composite, SWT.NONE);
        varButton.setText("Variables ...");
        varButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(final SelectionEvent e) {
                showVariableDialog();
            }
        });
        varButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));

        setControl(composite);

        update();
    }

    private Collection<IStatus> validate() {
        final Collection<IStatus> result = new ArrayList<IStatus>();

        result.add(new Status(Status.OK, Activator.PLUGIN_ID, "Select location to add"));

        if (this.path != null) {
            this.pathEntry.setText(this.path);
            return result;
        }

        final IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager();
        try {
            final String value = manager.performStringSubstitution(this.path);
            Path.fromPortableString(value);
        } catch (final CoreException e) {
            result.add(e.getStatus());
        }

        return result;
    }

    private void update() {
        updateStatus(validate().toArray(new IStatus[0]));
    }

    protected void showVariableDialog() {
        final StringVariableSelectionDialog dlg = new StringVariableSelectionDialog(getShell());
        if (dlg.open() == Dialog.OK) {
            this.path = dlg.getVariableExpression();
            update();
        }
    }

    public IClasspathEntry getSelection() {
        return JavaCore.newContainerEntry(Helper.makePath(this.path), this.exported);
    }

    public void setSelection(final IClasspathEntry containerEntry) {
        if (containerEntry != null) {
            this.exported = containerEntry.isExported();
            this.path = Helper.getPath(containerEntry.getPath());
        }
    }

}