/*******************************************************************************
* Copyright (c) 2008, 2010 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0, which accompanies this distribution
* and is available at http://www.eclipse.org/legal/epl-v10.html.
*
* Contributors:
* Oracle - initial API and implementation
******************************************************************************/
package org.eclipse.jpt.ui.internal.widgets;
import com.ibm.icu.text.Collator;
import java.util.Arrays;
import java.util.Comparator;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jpt.ui.WidgetFactory;
import org.eclipse.jpt.ui.internal.JptUiMessages;
import org.eclipse.jpt.ui.internal.util.SWTUtil;
import org.eclipse.jpt.utility.model.Model;
import org.eclipse.jpt.utility.model.value.PropertyValueModel;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.Composite;
/**
* This pane simply shows a combo where its data is populating through
* {@link #choices()} and a default value can also be added.
*
*
* @version 2.0
* @since 1.0
*/
@SuppressWarnings("nls")
abstract class EnumComboViewer extends Pane
{
/**
* The main widget of this pane.
*/
private ComboViewer comboViewer;
/**
* A constant used to represent the null value.
*/
public static final String NULL_VALUE = "null";
/**
* Creates a new EnumComboViewer.
*
* @param parentPane The parent container of this one
* @param parent The parent container
* @param widgetFactory The factory used to create various widgets
*/
EnumComboViewer(Pane extends T> parentPane,
Composite parent) {
super(parentPane, parent);
}
/**
* Creates a new EnumComboViewer.
*
* @param parentPane The parent container of this one
* @param parent The parent container
* @param widgetFactory The factory used to create various widgets
*/
EnumComboViewer(Pane> parentPane,
PropertyValueModel extends T> subjectHolder,
Composite parent) {
super(parentPane, subjectHolder, parent);
}
/**
* Creates a new EnumComboViewer.
*
* @param subjectHolder The holder of this pane's subject
* @param parent The parent container
* @param widgetFactory The factory used to create various widgets
*/
EnumComboViewer(PropertyValueModel extends T> subjectHolder,
Composite parent,
WidgetFactory widgetFactory) {
super(subjectHolder, parent, widgetFactory);
}
/**
* Creates the list of choices and add an extra element that represents the
* default value.
*
* @return The combo's choices including the default value
*/
private Object[] buildChoices() {
V[] choices = getChoices();
if (sortChoices()) {
Arrays.sort(choices, buildComparator());
}
Object[] extendedChoices = new Object[choices.length + 1];
System.arraycopy(choices, 0, extendedChoices, 1, choices.length);
extendedChoices[0] = NULL_VALUE;
return extendedChoices;
}
/**
* Return true to sort the choices in alphabetical order
* @return
*/
protected boolean sortChoices() {
return true;
}
/**
* Creates the ComboViewer with the right combo widgets.
*
* @param container The container of the combo
* @return A new ComboViewer containing the right combo widget
*/
protected ComboViewer addComboViewer(Composite container) {
return addComboViewer(container, buildLabelProvider());
}
private Comparator