Skip to content

Commit 9e65149

Browse files
committed
- implement thema autocompletion
1 parent 2659162 commit 9e65149

File tree

3 files changed

+122
-8
lines changed

3 files changed

+122
-8
lines changed

src/main/java/mediathek/gui/tabs/tab_film/filter/SwingFilterDialog.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import mediathek.gui.tabs.tab_film.filter_selection.FilterSelectionComboBoxModel;
4040
import mediathek.mainwindow.MediathekGui;
4141
import mediathek.tool.*;
42+
import mediathek.tool.swing.AutoCompletionComboBox2;
4243
import net.engio.mbassy.listener.Handler;
4344
import net.miginfocom.layout.AC;
4445
import net.miginfocom.layout.CC;
@@ -226,6 +227,9 @@ private void updateThemaComboBox() {
226227
}
227228

228229
private void setupThemaComboBox() {
230+
jcbThema.setNoActionOnKeyNavigation(true);
231+
jcbThema.setStrict(true);
232+
jcbThema.setStrictCompletion(true);
229233
var model = GlazedListsSwing.eventComboBoxModel(new EventListWithEmptyFirstEntry(sourceThemaList));
230234
jcbThema.setModel(model);
231235
//otherwise stored filter will not be accepted as entry may not be in list
@@ -686,7 +690,7 @@ private void initComponents() {
686690
var separator5 = new JSeparator();
687691
var pnlThema = new JPanel();
688692
label4 = new JLabel();
689-
jcbThema = new JComboBox<>();
693+
jcbThema = new AutoCompletionComboBox2();
690694
btnResetThema = new JButton();
691695
var separator6 = new JSeparator();
692696
var pnlFlimlength = new JPanel();
@@ -873,7 +877,6 @@ private void initComponents() {
873877
pnlThema.add(label4, new CC().cell(0, 0));
874878

875879
//---- jcbThema ----
876-
jcbThema.setMaximumRowCount(6);
877880
jcbThema.setMinimumSize(new Dimension(50, 10));
878881
jcbThema.setPreferredSize(null);
879882
jcbThema.setMaximumSize(null);
@@ -969,7 +972,7 @@ private void initComponents() {
969972
private JLabel label3;
970973
public CheckBoxList senderList;
971974
private JLabel label4;
972-
private JComboBox<String> jcbThema;
975+
private AutoCompletionComboBox2 jcbThema;
973976
private JButton btnResetThema;
974977
private JLabel label5;
975978
private JLabel lblMinFilmLengthValue;

src/main/java/mediathek/gui/tabs/tab_film/filter/SwingFilterDialog.jfd

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,11 @@ new FormModel {
211211
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
212212
"value": "cell 0 0"
213213
} )
214-
add( new FormComponent( "javax.swing.JComboBox" ) {
214+
add( new FormComponent( "mediathek.tool.swing.AutoCompletionComboBox2" ) {
215215
name: "jcbThema"
216-
"maximumRowCount": 6
217216
"minimumSize": new java.awt.Dimension( 50, 10 )
218217
"preferredSize": sfield com.jformdesigner.model.FormObject NULL_VALUE
219218
"maximumSize": sfield com.jformdesigner.model.FormObject NULL_VALUE
220-
auxiliary() {
221-
"JavaCodeGenerator.typeParameters": "String"
222-
}
223219
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
224220
"value": "cell 1 0,growx,wmax 300"
225221
} )
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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

Comments
 (0)