org.kalypso.model.wspm.tuhh.ui.imports.WProfProfileStrategyOptions.java Source code

Java tutorial

Introduction

Here is the source code for org.kalypso.model.wspm.tuhh.ui.imports.WProfProfileStrategyOptions.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.model.wspm.tuhh.ui.imports;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
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.kalypso.model.wspm.tuhh.core.profile.importer.wprof.IProfileCreatorStrategy;

/**
 * @author Gernot Belger
 */
public class WProfProfileStrategyOptions {
    private static final String SETTINGS_INDEX = "strategyIndex"; //$NON-NLS-1$

    private final WProfOptionsPage m_page;

    private final List<IProfileCreatorStrategy> m_strategies = new ArrayList<>();

    private IProfileCreatorStrategy m_currentStrategy;

    public WProfProfileStrategyOptions(final WProfOptionsPage page) {
        m_page = page;
    }

    public void addStrategy(final IProfileCreatorStrategy strategy) {
        m_strategies.add(strategy);
    }

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

        final ComboViewer strategyViewer = new ComboViewer(panel, SWT.DROP_DOWN | SWT.READ_ONLY);
        strategyViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        strategyViewer.setContentProvider(new ArrayContentProvider());
        strategyViewer.setLabelProvider(new LabelProvider());
        strategyViewer.setInput(m_strategies);

        if (m_strategies.size() > 0) {
            m_currentStrategy = m_strategies.get(0);
        }

        final IDialogSettings dialogSettings = m_page.getWizard().getDialogSettings();
        if (dialogSettings != null) {
            try {
                final int index = dialogSettings.getInt(SETTINGS_INDEX);
                if (index > 0 && index < m_strategies.size()) {
                    m_currentStrategy = m_strategies.get(index);
                }
            } catch (final NumberFormatException e) {
                // just ignored
            }
        }

        if (m_currentStrategy != null) {
            strategyViewer.setSelection(new StructuredSelection(m_currentStrategy));
        }

        strategyViewer.addSelectionChangedListener(new ISelectionChangedListener() {
            @Override
            public void selectionChanged(final SelectionChangedEvent event) {
                handleSelectionChanged((IStructuredSelection) event.getSelection());
            }
        });
    }

    protected void handleSelectionChanged(final IStructuredSelection selection) {
        m_currentStrategy = (IProfileCreatorStrategy) selection.getFirstElement();

        m_page.validate();
    }

    public IProfileCreatorStrategy getCurrentStrategy() {
        return m_currentStrategy;
    }

}