/*
 * #%L
 * SciJava UI components for Java Swing.
 * %%
 * Copyright (C) 2010 - 2020 SciJava developers.
 * %%
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * #L%
 */

package org.scijava.ui.swing.widget;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JPanel;

import org.scijava.Priority;
import org.scijava.plugin.Plugin;
import org.scijava.widget.ChoiceWidget;
import org.scijava.widget.InputWidget;
import org.scijava.widget.WidgetModel;

/**
 * Swing implementation of multiple choice selector widget using a
 * {@link JComboBox}.
 * 
 * @author Curtis Rueden
 */
@Plugin(type = InputWidget.class, priority = SwingChoiceWidget.PRIORITY)
public class SwingChoiceWidget extends SwingInputWidget<String> implements
	ActionListener, ChoiceWidget<JPanel>
{

	public static final double PRIORITY = Priority.NORMAL;

	private JComboBox<String> comboBox;

	// -- ActionListener methods --

	@Override
	public void actionPerformed(final ActionEvent e) {
		updateModel();
	}

	// -- InputWidget methods --

	@Override
	public String getValue() {
		return comboBox.getSelectedItem().toString();
	}

	// -- WrapperPlugin methods --

	@Override
	public void set(final WidgetModel model) {
		super.set(model);

		final String[] items = model.getChoices();

		comboBox = new JComboBox<>(items);
		setToolTip(comboBox);
		getComponent().add(comboBox);
		comboBox.addActionListener(this);

		refreshWidget();
	}

	// -- Typed methods --

	@Override
	public boolean supports(final WidgetModel model) {
		return super.supports(model) && model.isText() && model.isMultipleChoice();
	}

	// -- AbstractUIInputWidget methods ---

	@Override
	public void doRefresh() {
		final Object value = get().getValue();
		if (value.equals(comboBox.getSelectedItem())) return; // no change
		comboBox.setSelectedItem(value);
	}
}

@@ -2368,24 +2368,60 @@
 etValue() %7B%0A
+%09%09if (comboBox.getItemCount() %3E 0)%0A%09
 %09%09return com
@@ -2452,24 +2452,47 @@
 toString();%0A
+%09%09else%0A%09%09%09return null;%0A
 %09%7D%0A%0A%09// -- W
@@ -3041,16 +3041,233 @@
 esh() %7B%0A
+%09%09final String%5B%5D choices = get().getChoices();%0A%09%09%0A%09%09if (!areListsEqual(choices, comboBoxItems())) %7B%0A%09%09%09comboBox.removeAllItems();%09%0A%09%09%09for (int i=0; i%3Cchoices.length; i++)%0A%09%09%09%09comboBox.addItem(choices%5Bi%5D);%0A%09%09%7D else %7B%0A%09
 %09%09final 
@@ -3299,16 +3299,17 @@
 alue();%0A
+%09
 %09%09if (va
@@ -3359,24 +3359,360 @@
 urn;
- // no change%0A%09%09
+%0A%09%09%09comboBox.setSelectedItem(value);%0A%09%09%7D%0A%09%7D%0A%09%0A%09private boolean areListsEqual(String%5B%5D list1, String%5B%5D list2) %7B%0A%09%09if (list1.length != list2.length)%0A%09%09%09return false;%0A%09%09%0A%09%09for (int i=0; i%3C list1.length; i++)%0A%09%09%09if (!list1%5Bi%5D.equals(list2%5Bi%5D))%0A%09%09%09%09return false;%0A%09%09%0A%09%09return true;%0A%09%7D%0A%09%0A%09private String%5B%5D comboBoxItems() %7B%0A%09%09String%5B%5D comboItems = new String%5B
 comb
@@ -3716,37 +3716,145 @@
 omboBox.
-setSelectedItem(value)
+getItemCount()%5D;%0A%09%09for (int i=0; i %3CcomboBox.getItemCount(); i++)%0A%09%09%09comboItems%5Bi%5D = comboBox.getItemAt(i);%0A%09%09%0A%09%09return comboItems
 ;%0A%09%7D%0A%7D%0A

Update JComboBox choices during SwingChoiceWidget refresh