added filter function for media browse

save
Berat Kocak 2024-11-14 19:04:48 +01:00
parent af5961609d
commit 12c332d77e
2 changed files with 62 additions and 4 deletions

View File

@ -678,7 +678,7 @@ public class Bibliothek {
} }
} }
public static ArrayList<JLabel> findMatches(String searchTerm) { public static ArrayList<JLabel> findMatchesFiltered(String searchTerm) {
ArrayList<JLabel> matches = new ArrayList<>(); ArrayList<JLabel> matches = new ArrayList<>();
for(Buch b: bücherListe) { for(Buch b: bücherListe) {
String titel = b.getTitel(); String titel = b.getTitel();
@ -716,9 +716,28 @@ public class Bibliothek {
matches.add(j); matches.add(j);
} }
} }
System.out.println("Filtered: " + matches.size());
return matches; return matches;
} }
public static ArrayList<JLabel> findMatchesUnfiltered(String searchTerm) {
ArrayList<JLabel> matches = new ArrayList<>();
for(Medium m: katalog) {
String titel = m.getTitel();
if(titel.contains(searchTerm)) {
String text = m.getClass().toString();
int lastDotIndex = text.lastIndexOf(".");
String substring = text.substring(lastDotIndex + 1);
int kennnummer = m.getKennnummer();
JLabel j = new JLabel(substring + " namens: " + titel + " mit der Kennnummer: " + kennnummer);
matches.add(j);
}
}
System.out.println("Unfiltered: " + matches.size());
return matches;
}
} }

View File

@ -344,22 +344,37 @@ public class BibliothekGUI extends JFrame implements ActionListener{
private static void searchMenu(Kunde k) { private static void searchMenu(Kunde k) {
JPanel mainSearch = new JPanel(new BorderLayout()); JPanel mainSearch = new JPanel(new BorderLayout());
//NORTH: Search bar //NORTH: Search bar
JPanel enterSearch = new JPanel(new GridLayout(1,3,10,10)); JPanel enterSearch = new JPanel(new GridLayout(1,4,10,10));
JLabel text0 = new JLabel("Geben Sie einen Suchbegriff ein:"); JLabel text0 = new JLabel("Geben Sie einen Suchbegriff ein:");
JTextField enterSearchTerm = new JTextField(20); JTextField enterSearchTerm = new JTextField(20);
JButton b0 = new JButton("Suchen"); JButton b0 = new JButton("Suchen");
JCheckBox box0 = new JCheckBox("Nur ausleihbare Medien anzeigen");
enterSearch.add(text0); enterSearch.add(text0);
enterSearch.add(enterSearchTerm); enterSearch.add(enterSearchTerm);
enterSearch.add(b0); enterSearch.add(b0);
enterSearch.add(box0);
mainSearch.add(enterSearch, BorderLayout.NORTH); mainSearch.add(enterSearch, BorderLayout.NORTH);
//CENTER: Matched media //CENTER: Matched media
mainSearch.revalidate();
int[] doThis = {0};
box0.addActionListener(e -> {
if(box0.isSelected()) {
doThis[0] = 0;
} else {
doThis[0] = 1;
}
});
if(doThis[0] == 0) {
b0.addActionListener(e -> { b0.addActionListener(e -> {
String searchTerm = enterSearchTerm.getText().toString(); String searchTerm = enterSearchTerm.getText().toString();
ArrayList<JLabel> matchedMedia = Bibliothek.findMatches(searchTerm); ArrayList<JLabel> matchedMedia = Bibliothek.findMatchesFiltered(searchTerm);
int i = matchedMedia.size(); int i = matchedMedia.size();
if(i == 0) { if(i == 0) {
JLabel text1 = new JLabel("Nichts passendes zu dem Suchbegriff " + searchTerm + " gefunden."); JLabel text1 = new JLabel("Nichts passendes zu dem Suchbegriff " + searchTerm + " gefunden.");
mainSearch.add(text0, BorderLayout.CENTER); mainSearch.revalidate();
mainSearch.add(text1, BorderLayout.CENTER);
mainSearch.revalidate();
System.out.println("FIL");
} else if (i > 0){ } else if (i > 0){
JPanel ergebnisPanel = new JPanel(new GridLayout(i, 1, 10,10)); JPanel ergebnisPanel = new JPanel(new GridLayout(i, 1, 10,10));
for(JLabel j: matchedMedia) { for(JLabel j: matchedMedia) {
@ -370,6 +385,30 @@ public class BibliothekGUI extends JFrame implements ActionListener{
mainSearch.revalidate(); mainSearch.revalidate();
} }
}); });
}
if(doThis[0] == 1) {
b0.addActionListener(e -> {
String searchTerm = enterSearchTerm.getText().toString();
ArrayList<JLabel> matchedMedia = Bibliothek.findMatchesUnfiltered(searchTerm);
int i = matchedMedia.size();
if(i == 0) {
JLabel text1 = new JLabel("Nichts passendes zu dem Suchbegriff " + searchTerm + " gefunden.");
mainSearch.revalidate();
mainSearch.add(text1, BorderLayout.CENTER);
mainSearch.revalidate();
System.out.println("UNFIL");
} else if (i > 0){
JPanel ergebnisPanel = new JPanel(new GridLayout(i, 1, 10,10));
for(JLabel j: matchedMedia) {
ergebnisPanel.add(j);
}
mainSearch.revalidate();
mainSearch.add(ergebnisPanel, BorderLayout.CENTER);
mainSearch.revalidate();
}
mainSearch.revalidate();
});
}
//SOUTH: OK Button to return //SOUTH: OK Button to return
JButton b1 = new JButton("OK"); JButton b1 = new JButton("OK");
b1.addActionListener(e -> cl.show(mainPanel, "suchenMenu")); b1.addActionListener(e -> cl.show(mainPanel, "suchenMenu"));