1
+ /*
2
+ * Copyright (c) 2025 derreisende77.
3
+ * This code was developed as part of the MediathekView project https://github.com/mediathekview/MediathekView
4
+ *
5
+ * This program is free software: you can redistribute it and/or modify
6
+ * it under the terms of the GNU General Public License as published by
7
+ * the Free Software Foundation, either version 3 of the License, or
8
+ * (at your option) any later version.
9
+ *
10
+ * This program is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ * GNU General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU General Public License
16
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+
19
+ package mediathek .tool .swing ;
20
+
21
+ import com .jidesoft .swing .AutoCompletion ;
22
+ import com .jidesoft .swing .ComboBoxSearchable ;
23
+
24
+ import javax .swing .*;
25
+
26
+ /**
27
+ * An auto completion combobox.
28
+ * <p/>
29
+ * Since auto-complete has to listen to the key user types, it has to be editable. If you want to limit user to the list
30
+ * available in the combobox model, you can call {@link #setStrict(boolean)} and set it to true.
31
+ * This class is a rewrite of {@link com.jidesoft.swing.AutoCompletionComboBox} and does not send action events when keys
32
+ * are pressed if {@link #setNoActionOnKeyNavigation(boolean)} is set to true.
33
+ */
34
+ public class AutoCompletionComboBox2 extends JComboBox <String > {
35
+ protected AutoCompletion _autoCompletion ;
36
+ boolean _noActionOnKeyNavigation ;
37
+ private boolean _preventActionEvent ;
38
+
39
+ public AutoCompletionComboBox2 () {
40
+ initComponents ();
41
+ }
42
+
43
+ protected void initComponents () {
44
+ setEditable (true );
45
+ _autoCompletion = createAutoCompletion ();
46
+ }
47
+
48
+ public void setNoActionOnKeyNavigation (boolean _noActionOnKeyNavigation ) {
49
+ this ._noActionOnKeyNavigation = _noActionOnKeyNavigation ;
50
+ }
51
+
52
+ /**
53
+ * Creates the <code>AutoCompletion</code>.
54
+ *
55
+ * @return the <code>AutoCompletion</code>.
56
+ */
57
+ protected AutoCompletion createAutoCompletion () {
58
+ return new AutoCompletion (this , new ComboBoxSearchable (this ) {
59
+ @ Override
60
+ protected void setSelectedIndex (int index , boolean incremental ) {
61
+ Object propTableCellEditor = AutoCompletionComboBox2 .this .getClientProperty ("JComboBox.isTableCellEditor" );
62
+ Object propNoActionOnKeyNavigation = UIManager .get ("ComboBox.noActionOnKeyNavigation" );
63
+ if ((propTableCellEditor instanceof Boolean && (Boolean ) propTableCellEditor ) ||
64
+ (propNoActionOnKeyNavigation instanceof Boolean && (Boolean ) propNoActionOnKeyNavigation ) ||
65
+ _noActionOnKeyNavigation ) {
66
+ _preventActionEvent = true ;
67
+ }
68
+ try {
69
+ super .setSelectedIndex (index , incremental );
70
+ } finally {
71
+ _preventActionEvent = false ;
72
+ }
73
+ }
74
+ });
75
+ }
76
+
77
+ /**
78
+ * Sets the strict property. If true, it will not allow user to type in anything that is not in the known item list.
79
+ * If false, user can type in whatever he/she wants. If the text can match with a item in the known item list, it
80
+ * will still auto-complete.
81
+ *
82
+ * @param strict true or false.
83
+ */
84
+ public void setStrict (boolean strict ) {
85
+ getAutoCompletion ().setStrict (strict );
86
+ }
87
+
88
+ /**
89
+ * Sets the strict completion property. If true, in case insensitive searching, it will always use the exact item in
90
+ * the Searchable to replace whatever user types. For example, when Searchable has an item "Arial" and user types in
91
+ * "AR", if this flag is true, it will auto-completed as "Arial". If false, it will be auto-completed as "ARial". Of
92
+ * course, this flag will only make a difference if Searchable is case insensitive.
93
+ *
94
+ * @param strictCompletion true or false.
95
+ */
96
+ public void setStrictCompletion (boolean strictCompletion ) {
97
+ getAutoCompletion ().setStrictCompletion (strictCompletion );
98
+ }
99
+
100
+ /**
101
+ * Gets the underlying AutoCompletion class.
102
+ *
103
+ * @return the underlying AutoCompletion.
104
+ */
105
+ public AutoCompletion getAutoCompletion () {
106
+ return _autoCompletion ;
107
+ }
108
+
109
+ @ Override
110
+ protected void fireActionEvent () {
111
+ if (!_preventActionEvent ) {
112
+ super .fireActionEvent ();
113
+ }
114
+ }
115
+ }
0 commit comments