Java tutorial
/*---------------- 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.rrm.internal.simulations; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.ObjectUtils; import org.eclipse.jface.action.Action; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.viewers.ArrayContentProvider; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.window.Window; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.dialogs.ListDialog; import org.kalypso.gmlschema.property.IPropertyType; import org.kalypso.model.hydrology.binding.control.NAControl; import org.kalypso.ogc.gml.command.ChangeFeatureCommand; import org.kalypso.ui.rrm.internal.i18n.Messages; /** * @author Holger Albert */ public class InitialValueAction extends Action { private static final String RESET_CONSTANT = Messages.getString("InitialValueAction.0"); //$NON-NLS-1$ private final InitialValueFeatureControl m_control; public InitialValueAction(final InitialValueFeatureControl control) { m_control = control; } @Override public void runWithEvent(final Event event) { /* Get the shell. */ final Display display = event.widget.getDisplay(); final Shell shell = display.getActiveShell(); /* Longterm simulations may not have start conditions. */ final NAControl simulation = (NAControl) m_control.getFeature(); if (SimulationUtilities.isLongterm(simulation)) { MessageDialog.open(MessageDialog.INFORMATION, shell, getText(), Messages.getString("InitialValueAction.1"), SWT.NONE); //$NON-NLS-1$ return; } /* Get the old initial value. */ final String oldInitialValue = simulation.getInitialValueSource(); /* Create the dialog. */ final ListDialog dialog = new ListDialog(shell); dialog.setTitle(getText()); dialog.setMessage(Messages.getString("InitialValueAction.2")); //$NON-NLS-1$ dialog.setLabelProvider(new LabelProvider()); dialog.setContentProvider(new ArrayContentProvider()); /* Create the input. */ final List<String> input = new ArrayList<>(); input.add(RESET_CONSTANT); /* Add all longterm simulations. */ final String[] longtermSimulations = SimulationUtilities.getLongtermSimulations(simulation); for (final String longtermSimulation : longtermSimulations) input.add(longtermSimulation); /* Set the input. */ dialog.setInput(input); /* Set the old initial value as selection. */ if (oldInitialValue != null && input.contains(oldInitialValue)) dialog.setInitialSelections(new String[] { oldInitialValue }); else dialog.setInitialSelections(new String[] { RESET_CONSTANT }); /* Open the dialog. */ final int open = dialog.open(); if (open != Window.OK) return; /* Get the result. */ final Object[] result = dialog.getResult(); if (result == null || result.length == 0) return; /* Get the new initial value. */ String newInitialValue = (String) result[0]; /* HINT: It may be a the reset constant. */ if (RESET_CONSTANT.equals(newInitialValue)) newInitialValue = ""; //$NON-NLS-1$ /* Only change, if the old initial value is different from the new initial value. */ if (!ObjectUtils.equals(oldInitialValue, newInitialValue)) { final IPropertyType pt = m_control.getFeatureTypeProperty(); m_control.fireFeatureChanges(new ChangeFeatureCommand(simulation, pt, newInitialValue)); } } @Override public String getText() { return Messages.getString("InitialValueAction.4"); //$NON-NLS-1$ } }