Skip to main content
summaryrefslogtreecommitdiffstats
blob: 68ee9aa0ec416e59d55e65000820ec6dd736da7d (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*******************************************************************************
 * Copyright (c) 2007, 2010 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.common.utility.internal.swing;

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

/**
 * This implementation of LongListComponent.Browser uses a
 * JOptionPane to prompt the user for the selection. The JOPtionPane
 * is passed a FilteringListPanel to assist the user in making
 * a selection.
 */
public class FilteringListBrowser<T> 
	implements ListChooser.ListBrowser 
{
	private FilteringListPanel<T> panel;

	/**
	 * Default constructor.
	 */
	public FilteringListBrowser() {
		super();
		this.panel = this.buildPanel();
	}

	protected FilteringListPanel<T> buildPanel() {
		return new LocalFilteringListPanel<T>();
	}

	/**
	 * Prompt the user using a JOptionPane with a filtering
	 * list panel.
	 */
	public void browse(ListChooser chooser) {	
		this.initializeCellRenderer(chooser);
		
		int option = 
			JOptionPane.showOptionDialog(
				chooser, 
				this.message(chooser), 
				this.title(chooser), 
				this.optionType(chooser), 
				this.messageType(chooser), 
				this.icon(chooser), 
				this.selectionValues(chooser), 
				this.initialSelectionValue(chooser)
		);
		
		if (option == JOptionPane.OK_OPTION) {
			chooser.getModel().setSelectedItem(this.panel.selection());
		}
		
		// clear the text field so the list box is re-filtered
		this.panel.textField().setText(""); //$NON-NLS-1$
	}
	
	protected void initializeCellRenderer(JComboBox comboBox) {
		// default behavior should be to use the cell renderer from the combobox.
		this.panel.listBox().setCellRenderer(comboBox.getRenderer());
	}

	/**
	 * the message can be anything - here we build a component
	 */
	protected Object message(JComboBox comboBox) {
		this.panel.setCompleteList(this.convertToArray(comboBox.getModel()));
		this.panel.setSelection(comboBox.getModel().getSelectedItem());
		return this.panel;
	}

	protected String title(@SuppressWarnings("unused") JComboBox comboBox) {
		return null;
	}

	protected int optionType(@SuppressWarnings("unused") JComboBox comboBox) {
		return JOptionPane.OK_CANCEL_OPTION;
	}

	protected int messageType(@SuppressWarnings("unused") JComboBox comboBox) {
		return JOptionPane.QUESTION_MESSAGE;
	}

	protected Icon icon(@SuppressWarnings("unused") JComboBox comboBox) {
		return null;
	}

	protected Object[] selectionValues(@SuppressWarnings("unused") JComboBox comboBox) {
		return null;
	}

	protected Object initialSelectionValue(@SuppressWarnings("unused") JComboBox comboBox) {
		return null;
	}

	/**
	 * 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;
	}
	
	
	// ********** custom panel **********
	
	protected static class LocalFilteringListPanel<S> extends FilteringListPanel<S> {
		protected static final Object[] EMPTY_ARRAY = new Object[0];

		protected LocalFilteringListPanel() {
			super(EMPTY_ARRAY, null);
		}
	
		/**
		 * Disable the performance tweak because JOptionPane
		 * will try open wide enough to disable the horizontal scroll bar;
		 * and it looks a bit clumsy.
		 */
		@Override
		protected String prototypeCellValue() {
			return null;
		}
	
	}

}

Back to the top