All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mmb.menu.world.CreativeItemList Maven / Gradle / Ivy

Go to download

Dependency for the MultiMachineBuilder, a voxel game about building an industrial empire in a finite world. THIS RELEASE IS NOT PLAYABLE. To play the game, donwload from >ITCH.IO LINK HERE< or >GH releases link here<

The newest version!
/**
 * 
 */
package mmb.menu.world;

import java.awt.Component;
import java.awt.event.MouseEvent;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

import mmb.NN;
import mmb.engine.item.ItemType;
import mmb.engine.item.Items;
import monniasza.collects.Collects;

/**
 * @author oskar
 *
 */
public class CreativeItemList extends JList {
	private static final long serialVersionUID = -5883411113161667818L;
	public CreativeItemList() {
		setModel(model);
		setToolTipText("");
	}
	@NN public static final DefaultListModel model = new DefaultListModel<>();
	@NN public static final List list = Collects.toWritableList(model);
	static {
		list.addAll(Items.items);
		Collections.sort(list, (l, r) -> l.title().compareTo(r.title()));
	}
	public static void resort(Comparator sort, DefaultListModel model) {
		Collections.sort(Collects.toWritableList(model), sort);
	}
	@Override
	public ListCellRenderer getCellRenderer() {
		return cellrender;
	}

	private final ListCellRenderer cellrender = new ItemListCellRenderer();
	public static class ItemListCellRenderer extends JLabel implements ListCellRenderer<@NN ItemType>{
		private static final long serialVersionUID = -3535344904857285958L;
		public ItemListCellRenderer() {
			setOpaque(true);
		}
		@Override
		public Component getListCellRendererComponent(@SuppressWarnings("null") JList list, ItemType itemType, int index,
		boolean isSelected, boolean cellHasFocus) {
			setIcon(itemType.getIcon());
			setText(itemType.title());
			
			if (isSelected) {
			    setBackground(list.getSelectionBackground());
			    setForeground(list.getSelectionForeground());
			} else {
			    setBackground(list.getBackground());
			    setForeground(list.getForeground());
			}
			return this;
		}
	}
	@Override
	public String getToolTipText(@SuppressWarnings("null") MouseEvent event) {
		int index = locationToIndex(event.getPoint());
		if(index > -1) {
			String description = model.get(index).description();
			return description;
		}
		return null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy