org.kalypso.ui.wizard.raster.ImportRasterSourceWizard.java Source code

Java tutorial

Introduction

Here is the source code for org.kalypso.ui.wizard.raster.ImportRasterSourceWizard.java

Source

/*----------------    FILE HEADER KALYPSO ------------------------------------------
 *
 *  This file is part of kalypso.
 *  Copyright (C) 2004 by:
 *
 *  Technical University Hamburg-Harburg (TUHH)
 *  Institute of River and coastal engineering
 *  Denickestrae 22
 *  21073 Hamburg, Germany
 *  http://www.tuhh.de/wb
 *
 *  and
 *
 *  Bjoernsen Consulting Engineers (BCE)
 *  Maria Trost 3
 *  56070 Koblenz, Germany
 *  http://www.bjoernsen.de
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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 for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  Contact:
 *
 *  E-Mail:
 *  belger@bjoernsen.de
 *  schlienger@bjoernsen.de
 *  v.doemming@tuhh.de
 *
 *  ---------------------------------------------------------------------------*/

package org.kalypso.ui.wizard.raster;

import java.net.URL;

import org.apache.commons.io.FilenameUtils;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
import org.kalypso.commons.command.ICommandTarget;
import org.kalypso.contribs.eclipse.core.resources.ResourceUtilities;
import org.kalypso.contribs.eclipse.jface.operation.ICoreRunnableWithProgress;
import org.kalypso.contribs.eclipse.jface.operation.RunnableContextHelper;
import org.kalypso.ogc.gml.IKalypsoLayerModell;
import org.kalypso.ui.KalypsoAddLayerPlugin;
import org.kalypso.ui.i18n.Messages;
import org.kalypso.ui.wizard.AbstractDataImportWizard;

public class ImportRasterSourceWizard extends AbstractDataImportWizard {
    private ImportRasterSourceWizardPage m_page;

    private IProject m_project;

    private WizardNewFileCreationPage m_gmlFilePage;

    private IFile m_mapFile;

    @Override
    public void addPages() {
        m_project = ResourceUtilities.findProjectFromURL(getMapModel().getContext());

        final URL context = getMapModel().getContext();
        m_mapFile = ResourceUtilities.findFileFromURL(context);

        final IContainer mapContainer = m_mapFile == null ? null : m_mapFile.getParent();
        final IStructuredSelection selection = mapContainer == null ? StructuredSelection.EMPTY
                : new StructuredSelection(mapContainer);

        m_gmlFilePage = new WizardNewFileCreationPage("gmlFile", selection); //$NON-NLS-1$
        m_gmlFilePage.setFileExtension("gml"); //$NON-NLS-1$
        m_gmlFilePage.setAllowExistingResources(true);
        m_gmlFilePage.setTitle(Messages.getString("ImportRasterSourceWizard.2")); //$NON-NLS-1$
        m_gmlFilePage.setDescription(Messages.getString("ImportRasterSourceWizard.3")); //$NON-NLS-1$
        addPage(m_gmlFilePage);

        m_page = new ImportRasterSourceWizardPage("Add RasterDataModel"); //$NON-NLS-1$
        if (m_project != null)
            m_page.setProject(m_project);
        addPage(m_page);
    }

    @Override
    public boolean performFinish() {
        final IFile coverageFile = getGmlFile();

        final IFile styleFile = getSldFile(coverageFile);
        final boolean generateDefaultStyle = m_page.checkDefaultStyle();
        final String styleName = generateDefaultStyle ? null : m_page.getStyleName();

        final ICommandTarget commandTarget = getCommandTarget();
        final IKalypsoLayerModell mapModel = getMapModel();

        final ICoreRunnableWithProgress operation = new ImportRasterOperation(coverageFile, styleFile, styleName,
                commandTarget, mapModel);

        final IStatus status = RunnableContextHelper.execute(getContainer(), true, false, operation);
        KalypsoAddLayerPlugin.getDefault().getLog().log(status);
        ErrorDialog.openError(getShell(), getWindowTitle(),
                Messages.getString("org.kalypso.ui.wizard.raster.ImportRasterSourceWizard.2"), status); //$NON-NLS-1$

        return status.isOK();
    }

    private IFile getSldFile(final IFile coverageFile) {
        final boolean useDefaultStyle = m_page.checkDefaultStyle();
        if (useDefaultStyle) {
            final String gmlName = coverageFile.getName().toString();
            final String basicName = FilenameUtils.removeExtension(gmlName);
            final String sldName = basicName + ".sld"; //$NON-NLS-1$
            return coverageFile.getParent().getFile(new Path(sldName));
        }

        final IPath stylePath = m_page.getStylePath();
        return coverageFile.getWorkspace().getRoot().getFile(stylePath);
    }

    private IFile getGmlFile() {
        final IPath containerFullPath = m_gmlFilePage.getContainerFullPath();
        String fileName = m_gmlFilePage.getFileName();
        if (!FilenameUtils.isExtension(fileName.toLowerCase(), "gml")) //$NON-NLS-1$
            fileName += ".gml"; //$NON-NLS-1$

        final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        final IResource folder = root.findMember(containerFullPath);
        if (folder instanceof IContainer) {
            final IContainer container = (IContainer) folder;
            return container.getFile(new Path(fileName));
        }

        // TODO: error handling
        return null;
    }
}