001/*
002 * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
003 * Copyright (C) 2000-2011 The Gridarta Developers.
004 *
005 * This program is free software; you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation; either version 2 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License along
016 * with this program; if not, write to the Free Software Foundation, Inc.,
017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
018 */
019
020package net.sf.gridarta.gui.dialog.plugin.parameter;
021
022import java.awt.Dimension;
023import javax.swing.JComboBox;
024import javax.swing.ListCellRenderer;
025import net.sf.gridarta.gui.panel.gameobjectattributes.GameObjectAttributesModel;
026import net.sf.gridarta.gui.panel.objectchooser.ObjectChooser;
027import net.sf.gridarta.model.archetype.Archetype;
028import net.sf.gridarta.model.archetypeset.ArchetypeSet;
029import net.sf.gridarta.model.face.FaceObjectProviders;
030import net.sf.gridarta.model.gameobject.GameObject;
031import net.sf.gridarta.model.maparchobject.MapArchObject;
032import org.apache.log4j.Category;
033import org.apache.log4j.Logger;
034import org.jetbrains.annotations.NotNull;
035
036public class ArchComboBox<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends JComboBox {
037
038    /**
039     * The Logger for printing log messages.
040     */
041    private static final Category log = Logger.getLogger(ArchComboBox.class);
042
043    private static final long serialVersionUID = 1L;
044
045    private final ArchComboBoxEditor<G, A, R> archComboBoxEditor;
046
047    private final ArchComboBoxModel<G, A, R> archComboBoxModel;
048
049    /**
050     * Creates a new instance.
051     * @param faceObjectProviders the face object providers for looking up
052     * faces
053     */
054    public ArchComboBox(@NotNull final GameObjectAttributesModel<G, A, R> gameObjectAttributesModel, @NotNull final ArchetypeSet<G, A, R> archetypeSet, @NotNull final ObjectChooser<G, A, R> objectChooser, @NotNull final FaceObjectProviders faceObjectProviders) {
055        setMaximumRowCount(4);
056        archComboBoxModel = new ArchComboBoxModel<G, A, R>(archetypeSet);
057        archComboBoxEditor = new ArchComboBoxEditor<G, A, R>(this, archComboBoxModel, objectChooser, gameObjectAttributesModel, faceObjectProviders);
058        final ListCellRenderer cellRenderer = new ArchComboBoxCellRenderer(archComboBoxEditor, faceObjectProviders);
059        //setPrototypeDisplayValue(cellRenderer.sizeTester);
060        setRenderer(cellRenderer);
061        setModel(archComboBoxModel);
062        setEditable(true);
063        setEditor(archComboBoxEditor);
064        final Dimension d = getPreferredSize();
065        if (log.isDebugEnabled()) {
066            log.debug("Preferred size: " + d);
067        }
068        setPreferredSize(new Dimension(d.width, archComboBoxEditor.getEditorComponent().getPreferredSize().height));
069        if (log.isDebugEnabled()) {
070            log.debug("NEW Preferred size: " + getPreferredSize());
071        }
072    }
073
074    public void editorEntryChange() {
075        archComboBoxEditor.lockEditor();
076        final Archetype<G, A, R> nearestMatch = archComboBoxModel.getNearestMatch(archComboBoxEditor.getEditor().getText());
077        setSelectedItem(nearestMatch);
078        archComboBoxEditor.setItem(nearestMatch);
079        archComboBoxEditor.unlockEditor();
080    }
081
082}