|
| 1 | +/* |
| 2 | + * RomRaider Open-Source Tuning, Logging and Reflashing |
| 3 | + * Copyright (C) 2006-2025 RomRaider.com |
| 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 2 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 along |
| 16 | + * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + */ |
| 19 | + |
| 20 | +package com.romraider.swing; |
| 21 | + |
| 22 | +import javax.swing.BorderFactory; |
| 23 | +import javax.swing.JLabel; |
| 24 | +import javax.swing.JPanel; |
| 25 | +import javax.swing.JTextField; |
| 26 | +import javax.swing.event.DocumentEvent; |
| 27 | +import javax.swing.event.DocumentListener; |
| 28 | +import javax.swing.tree.DefaultMutableTreeNode; |
| 29 | +import javax.swing.tree.DefaultTreeModel; |
| 30 | +import javax.swing.tree.TreePath; |
| 31 | + |
| 32 | +import java.awt.*; |
| 33 | +import java.util.Enumeration; |
| 34 | +import java.util.List; |
| 35 | +import java.util.ResourceBundle; |
| 36 | + |
| 37 | +import com.romraider.maps.Rom; |
| 38 | +import com.romraider.swing.RomTree; |
| 39 | +import com.romraider.util.ResourceUtil; |
| 40 | + |
| 41 | +public class RomFilterPanel extends JPanel { |
| 42 | + |
| 43 | + private static final long serialVersionUID = 1L; |
| 44 | + |
| 45 | + private static final ResourceBundle rb = new ResourceUtil().getBundle( |
| 46 | + RomFilterPanel.class.getName()); |
| 47 | + |
| 48 | + public RomFilterPanel(final DefaultMutableTreeNode imageRoot, final RomTree imageList) { |
| 49 | + super(new BorderLayout()); |
| 50 | + |
| 51 | + final JTextField filterField; |
| 52 | + |
| 53 | + JLabel label = new JLabel(rb.getString("LBLFILTER")); |
| 54 | + label.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0)); |
| 55 | + filterField = new JTextField(20); |
| 56 | + filterField.setToolTipText(rb.getString("LBLTOOLTIP")); |
| 57 | + |
| 58 | + add(label, BorderLayout.WEST); |
| 59 | + add(filterField, BorderLayout.CENTER); |
| 60 | + |
| 61 | + filterField.getDocument().addDocumentListener(new DocumentListener() { |
| 62 | + @Override |
| 63 | + public void insertUpdate(DocumentEvent e) { filter(); } |
| 64 | + @Override |
| 65 | + public void removeUpdate(DocumentEvent e) { filter(); } |
| 66 | + @Override |
| 67 | + public void changedUpdate(DocumentEvent e) { filter(); } |
| 68 | + |
| 69 | + private void filter() { |
| 70 | + String text = filterField.getText().trim(); |
| 71 | + |
| 72 | + final Enumeration<?> children = imageRoot.children(); |
| 73 | + while (children.hasMoreElements()) { |
| 74 | + Object child = children.nextElement(); |
| 75 | + if (child instanceof Rom) { |
| 76 | + Rom rom = (Rom) child; |
| 77 | + List<TreePath> pathsToExpand = rom.refreshDisplayedTables(text); |
| 78 | + |
| 79 | + DefaultTreeModel model = (DefaultTreeModel) imageList.getModel(); |
| 80 | + model.reload(rom); |
| 81 | + |
| 82 | + for (TreePath path : pathsToExpand) { |
| 83 | + imageList.expandPath(path); |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + imageList.repaint(); |
| 90 | + } |
| 91 | + }); |
| 92 | + } |
| 93 | +} |
0 commit comments