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

de.invation.code.toval.graphic.dialog.ValueChooserDialog Maven / Gradle / Ivy

Go to download

TOVAL comprises a set of java classes for common programming issues. It includes utils for arrays, lists, sets and collections for convenient handling and modification, but also support for mathematic definitions concerning logic (clauses + resolution) together with some algorithms for permutations, powersets and resolution. Additionally it contains a number of types for multisets, matrices with object keys and much more.

The newest version!
package de.invation.code.toval.graphic.dialog;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Window;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.Border;

import de.invation.code.toval.graphic.renderer.AlternatingRowColorListCellRenderer;
import de.invation.code.toval.validate.ParameterException;
import de.invation.code.toval.validate.Validate;


public class ValueChooserDialog extends AbstractDialog> {
	
	private static final long serialVersionUID = 2306027725394345926L;
	
	public static final Border DEFAULT_BORDER = BorderFactory.createEmptyBorder(5, 5, 5, 5);
	public static final int DEFAULT_SELECTION_MODE = ListSelectionModel.SINGLE_SELECTION;

	private DefaultListModel stringListModel = new DefaultListModel();
	private Collection possibleValues;
	private int selectionMode;
	private JList stringList;
	
	protected ValueChooserDialog(Window owner, String title, Collection possibleValues) throws Exception {
		super(owner, title);
		setPossibleValues(possibleValues);
	}
	
	protected ValueChooserDialog(Window owner, String title, Collection possibleValues, int selectionMode) {
		super(owner, title);
		setPossibleValues(possibleValues);
		this.selectionMode = selectionMode;
	}
	
	private void setPossibleValues(Collection possibleValues) throws ParameterException{
		Validate.notNull(possibleValues);
		Validate.notEmpty(possibleValues);
		Validate.noNullElements(possibleValues);
		
		this.possibleValues = possibleValues;
	}

	@Override
	protected void addComponents() throws Exception {
		mainPanel().setLayout(new BorderLayout());
		
		JScrollPane scrollPane = new JScrollPane(getValueList());
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		mainPanel().add(scrollPane);
	}

	@Override
	protected void okProcedure() {
		if(!stringListModel.isEmpty()){
			List values = new ArrayList();
			for(Object o: stringList.getSelectedValues())
				values.add((String) o);
			setDialogObject(values);
			super.okProcedure();
		} else {
			JOptionPane.showMessageDialog(ValueChooserDialog.this, "Value list is empty.", "Invalid Parameter", JOptionPane.ERROR_MESSAGE);
		}
	}

	@Override
	protected void setTitle() {}
	
	private JList getValueList(){
		if(stringList == null){
			stringList = new JList(stringListModel);
			stringList.setCellRenderer(new AlternatingRowColorListCellRenderer());
			stringList.setFixedCellHeight(20);
			stringList.setVisibleRowCount(10);
			stringList.setPreferredSize(new Dimension(200,100));
			stringList.getSelectionModel().setSelectionMode(selectionMode);
			stringList.setBorder(null);
			for(String possibleValue: possibleValues){
				stringListModel.addElement(possibleValue);
			}
		}
		return stringList;
	}
	
	public static List showDialog(Window owner, String title, Collection values) throws Exception{
		ValueChooserDialog dialog = new ValueChooserDialog(owner, title, values);
		dialog.setUpGUI();
		return dialog.getDialogObject();
	}
	
	public static List showDialog(Window owner, String title, Collection values, int selectionMode) throws Exception{
		ValueChooserDialog dialog = new ValueChooserDialog(owner, title, values, selectionMode);
		dialog.setUpGUI();
		return dialog.getDialogObject();
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy