Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7215c6a387cab70cd7ff6f3a0e6ec43449b08b04 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*******************************************************************************
 * Copyright (c) 2007 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.utility.internal.swing;

import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.ListModel;

/**
 * This implementation of ListChooser.Browser uses a
 * JOptionPane to prompt the user for the selection. Subclasses 
 * can change the dialog's title, message, and/or icon.
 */
public class SimpleListBrowser
	implements ListChooser.ListBrowser 
{
	/** Default constructor */
	protected SimpleListBrowser() {
		super();
	}
	
	/**
	 * Prompt the user using a JOptionPane.
	 */
	public void browse(ListChooser chooser) {
		Object selection = 
			JOptionPane.showInputDialog(
				chooser, 
				this.message(chooser), 
				this.title(chooser), 
				this.messageType(chooser), 
				this.icon(chooser), 
				this.selectionValues(chooser), 
				this.initialSelectionValue(chooser)
			);
		
		if (selection != null) {
			chooser.getModel().setSelectedItem(selection);
		}
	}
	
	protected Object message(JComboBox comboBox) {
		return null;
	}
	
	protected String title(JComboBox comboBox) {
		return null;
	}
	
	protected int messageType(JComboBox comboBox) {
		return JOptionPane.QUESTION_MESSAGE;
	}
	
	protected Icon icon(JComboBox comboBox) {
		return null;
	}
	
	protected Object[] selectionValues(JComboBox comboBox) {
		return this.convertToArray(comboBox.getModel());
	}
	
	protected Object initialSelectionValue(JComboBox comboBox) {
		return comboBox.getModel().getSelectedItem();
	}
	
	/**
	 * Convert the list of objects in the specified list model
	 * into an array.
	 */
	protected Object[] convertToArray(ListModel model) {
		int size = model.getSize();
		Object[] result = new Object[size];
		for (int i = 0; i < size; i++) {
			result[i] = model.getElementAt(i);
		}
		return result;
	}
}

Back to the top