/*
 * #%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.plugin.Plugin;
import org.scijava.widget.InputWidget;
import org.scijava.widget.ObjectWidget;
import org.scijava.widget.WidgetModel;

/**
 * Swing implementation of object selector widget.
 * 
 * @author Curtis Rueden
 */
@Plugin(type = InputWidget.class)
public class SwingObjectWidget extends SwingInputWidget<Object> implements
	ActionListener, ObjectWidget<JPanel>
{

	private JComboBox<Object> comboBox;

	// -- ActionListener methods --

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

	// -- InputWidget methods --

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

	// -- WrapperPlugin methods --

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

		comboBox = new JComboBox<>(model.getObjectPool().toArray());
		setToolTip(comboBox);
		getComponent().add(comboBox);
		comboBox.addActionListener(this);

		refreshWidget();
	}

	// -- Typed methods --

	@Override
	public boolean supports(final WidgetModel model) {
		return super.supports(model) && model.getObjectPool().size() > 0;
	}

	// -- AbstractUIInputWidget methods ---

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

}

@@ -1460,16 +1460,43 @@
 idget;%0A%0A
+import java.awt.Component;%0A
 import j
@@ -1584,46 +1584,221 @@
 ing.
-JComboBox;%0Aimport javax.swing.JPanel;%0A
+DefaultListCellRenderer;%0Aimport javax.swing.JComboBox;%0Aimport javax.swing.JLabel;%0Aimport javax.swing.JList;%0Aimport javax.swing.JPanel;%0Aimport javax.swing.ListCellRenderer;%0A%0Aimport org.scijava.object.ObjectService;
 %0Aimp
@@ -2715,16 +2715,71 @@
 r(this);
+%0A%09%09comboBox.setRenderer(new NamedObjectCellRenderer());
 %0A%0A%09%09refr
@@ -3179,10 +3179,593 @@
 e);%0A%09%7D%0A%0A
+%09private class NamedObjectCellRenderer implements ListCellRenderer%3CObject%3E %7B%0A%09%09%0A%09%09private DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();%0A%0A%09%09@Override%0A%09%09public Component getListCellRendererComponent(JList%3C?%3E list, Object value, int index, boolean isSelected,%0A%09%09%09%09boolean cellHasFocus) %7B%0A%09%09%09JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);%0A%09%09%09renderer.setText(context().service(ObjectService.class).getName(value));%0A%09%09%09renderer.setToolTipText(value.toString());%0A%09%09%09return renderer;%0A%09%09%7D%0A%09%7D%0A
 %7D%0A

Support named objects in (Swing)ObjectWidget