gdx-studio

#libgdx#java#desktop

git clone https://git.pyrossh.dev/gdx-studio

An IDE for creating Games using libgdx and Java supported on all platforms Android, iOS, Desktop


src/gdxstudio/panel/BaseList.java
a9b2a64 1
package gdxstudio.panel;
a9b2a64 2
import gdxstudio.Style;
a9b2a64 3
a62d533 4
import java.awt.Dimension;
a62d533 5
import java.awt.event.ActionEvent;
a62d533 6
import java.awt.event.ActionListener;
a62d533 7
import java.util.Arrays;
a62d533 8
import java.util.Collection;
a62d533 9
import java.util.Iterator;
a62d533 10
import java.util.SortedSet;
a62d533 11
import java.util.TreeSet;
a62d533 12
a62d533 13
import javax.swing.AbstractListModel;
7c84877 14
import javax.swing.BorderFactory;
a62d533 15
import javax.swing.DefaultListModel;
a62d533 16
import javax.swing.JList;
a62d533 17
import javax.swing.JPanel;
a62d533 18
import javax.swing.JScrollPane;
a62d533 19
import javax.swing.ListSelectionModel;
a62d533 20
import javax.swing.event.ListSelectionListener;
a62d533 21
a62d533 22
import web.laf.lite.layout.VerticalFlowLayout;
a62d533 23
import web.laf.lite.utils.UIUtils;
a62d533 24
7c84877 25
public abstract class BaseList extends JPanel implements ListSelectionListener, ActionListener {
a62d533 26
	private static final long serialVersionUID = 1L;
a9b2a64 27
	public JList<String> list;
a62d533 28
	protected DefaultListModel<String> listModel;
a62d533 29
	protected SortedListModel listModel2;
a62d533 30
	protected JScrollPane scrollPane;
a9b2a64 31
	protected final Style.TitleButton header;
a62d533 32
	
a62d533 33
	BaseList(String title){
a62d533 34
		super(new VerticalFlowLayout());
a62d533 35
		UIUtils.setShadeWidth(this, 0);
a62d533 36
		UIUtils.setRound(this, 0);
a62d533 37
		listModel = new DefaultListModel<String>();
a62d533 38
		list = new JList<String>(listModel);
a62d533 39
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
a62d533 40
		list.addListSelectionListener(this);
7c84877 41
		UIUtils.setDecorateSelection(list, false);
a9b2a64 42
		header = new Style.TitleButton(title, this);
a62d533 43
		add(header);
a62d533 44
		scrollPane = new JScrollPane(list);
a62d533 45
		scrollPane.setPreferredSize(new Dimension(200, 175));
a62d533 46
		UIUtils.setDrawBorder(scrollPane, false);
a62d533 47
		add(scrollPane);
7c84877 48
		setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Style.border));
a62d533 49
	}
a62d533 50
	
a62d533 51
	BaseList(String title, boolean dontAddScroll){
a62d533 52
		super(new VerticalFlowLayout());
a62d533 53
		UIUtils.setShadeWidth(this, 0);
a62d533 54
		UIUtils.setRound(this, 0);
a62d533 55
		listModel = new DefaultListModel<String>();
a62d533 56
		list = new JList<String>(listModel);
a62d533 57
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
a62d533 58
		list.addListSelectionListener(this);
a9b2a64 59
		header = new Style.TitleButton(title, this);
a62d533 60
		add(header);
a62d533 61
		scrollPane = new JScrollPane(list);
a62d533 62
		scrollPane.setPreferredSize(new Dimension(200, 175));
a62d533 63
		UIUtils.setDrawBorder(scrollPane, false);
7c84877 64
		setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Style.border));
a62d533 65
	}
a62d533 66
	
a62d533 67
	BaseList(String title, String... items){
a62d533 68
		super(new VerticalFlowLayout());
a62d533 69
		UIUtils.setShadeWidth(this, 0);
a62d533 70
		UIUtils.setRound(this, 0);
a62d533 71
		//listModel = new DefaultListModel<String>();
a62d533 72
		list = new JList<String>(items);
a62d533 73
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
a62d533 74
		list.addListSelectionListener(this);
a9b2a64 75
		header = new Style.TitleButton(title, this);
a62d533 76
		add(header);
a62d533 77
		scrollPane = new JScrollPane(list);
a62d533 78
		scrollPane.setPreferredSize(new Dimension(200, 175));
a62d533 79
		UIUtils.setDrawBorder(scrollPane, false);
a62d533 80
		add(scrollPane);
7c84877 81
		setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Style.border));
a62d533 82
	}
a62d533 83
	
a62d533 84
	@SuppressWarnings("unchecked")
a62d533 85
	BaseList(String title, String useSort){
a62d533 86
		super(new VerticalFlowLayout());
a62d533 87
		UIUtils.setShadeWidth(this, 0);
a62d533 88
		UIUtils.setRound(this, 0);
a62d533 89
		listModel2 = new  SortedListModel();
a62d533 90
		list = new JList<String>(listModel2);
a62d533 91
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
a62d533 92
		list.addListSelectionListener(this);
a9b2a64 93
		header = new Style.TitleButton(title, this);
a62d533 94
		add(header);
a62d533 95
		scrollPane = new JScrollPane(list);
a62d533 96
		scrollPane.setPreferredSize(new Dimension(200, 175));
a62d533 97
		UIUtils.setDrawBorder(scrollPane, false);
7c84877 98
		setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Style.border));
a62d533 99
	}
a62d533 100
	
a62d533 101
	public void enable(){
a62d533 102
		list.setEnabled(true);
a62d533 103
	}
a62d533 104
	
a62d533 105
	public void disable(){
a62d533 106
		list.setEnabled(false);
a62d533 107
	}
a62d533 108
	
a62d533 109
	public void lock(){
a62d533 110
		list.removeListSelectionListener(this);
a62d533 111
	}
a62d533 112
	
a62d533 113
	public void unlock(){
a62d533 114
		list.addListSelectionListener(this);
a62d533 115
	}
a62d533 116
	
a62d533 117
	public void clear(){
a62d533 118
		lock();
a62d533 119
		listModel.clear();
a62d533 120
		unlock();
a62d533 121
	}
a62d533 122
	
a62d533 123
	@Override
a62d533 124
	public void actionPerformed(ActionEvent arg0) {
a9b2a64 125
		setVisible(false);
a62d533 126
		validate();
a62d533 127
		repaint();
a62d533 128
	}
a62d533 129
}
a62d533 130
a62d533 131
@SuppressWarnings("rawtypes")
a62d533 132
class SortedListModel extends AbstractListModel {
a62d533 133
	private static final long serialVersionUID = 1L;
a62d533 134
	SortedSet<Object> model;
a62d533 135
a62d533 136
	public SortedListModel() {
a62d533 137
		model = new TreeSet<Object>();
a62d533 138
	}
a62d533 139
a62d533 140
	public int getSize() {
a62d533 141
		return model.size();
a62d533 142
	}
a62d533 143
a62d533 144
	public Object getElementAt(int index) {
a62d533 145
		return model.toArray()[index];
a62d533 146
	}
a62d533 147
	
a62d533 148
	public int indexOf(Object element){
a62d533 149
		int index = 0;
a62d533 150
		for(Object e: model.toArray()){
a62d533 151
			if(e.equals(element))
a62d533 152
				break;
a62d533 153
			index++;
a62d533 154
		}
a62d533 155
		return index;
a62d533 156
	}
a62d533 157
	
a62d533 158
a62d533 159
	public void addElement(Object element) {
a62d533 160
		if (model.add(element)) {
a62d533 161
			fireContentsChanged(this, 0, getSize());
a62d533 162
		}
a62d533 163
	}
a62d533 164
	public void addAll(Object elements[]) {
a62d533 165
		Collection<Object> c = Arrays.asList(elements);
a62d533 166
		model.addAll(c);
a62d533 167
		fireContentsChanged(this, 0, getSize());
a62d533 168
	}
a62d533 169
a62d533 170
	public void clear() {
a62d533 171
		model.clear();
a62d533 172
		fireContentsChanged(this, 0, getSize());
a62d533 173
	}
a62d533 174
a62d533 175
	public boolean contains(Object element) {
a62d533 176
		return model.contains(element);
a62d533 177
	}
a62d533 178
a62d533 179
	public Object firstElement() {
a62d533 180
		return model.first();
a62d533 181
	}
a62d533 182
a62d533 183
	public Iterator iterator() {
a62d533 184
		return model.iterator();
a62d533 185
	}
a62d533 186
a62d533 187
	public Object lastElement() {
a62d533 188
		return model.last();
a62d533 189
	}
a62d533 190
a62d533 191
	public boolean removeElement(Object element) {
a62d533 192
		boolean removed = model.remove(element);
a62d533 193
		if (removed) {
a62d533 194
			fireContentsChanged(this, 0, getSize());
a62d533 195
		}
a62d533 196
		return removed;
a62d533 197
	}
7c84877 198
}