bcv-vf/src/main/java/the/bytecode/club/bytecodeviewer/gui/resourcesearch/SearchBoxPane.java

194 lines
7.4 KiB
Java
Raw Normal View History

package the.bytecode.club.bytecodeviewer.gui.resourcesearch;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ItemListener;
2021-04-12 22:31:22 +00:00
import java.util.Objects;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import org.objectweb.asm.tree.ClassNode;
2021-04-12 20:19:12 +00:00
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.gui.components.VisibleComponent;
import the.bytecode.club.bytecodeviewer.gui.resourceviewer.viewer.ResourceViewer;
2021-04-12 20:19:12 +00:00
import the.bytecode.club.bytecodeviewer.searching.BackgroundSearchThread;
import the.bytecode.club.bytecodeviewer.searching.SearchResultNotifier;
2021-06-30 22:27:23 +00:00
import the.bytecode.club.bytecodeviewer.translation.Translation;
import the.bytecode.club.bytecodeviewer.translation.components.TranslatedJCheckBox;
2019-04-17 06:45:15 +00:00
import the.bytecode.club.bytecodeviewer.util.FileContainer;
/***************************************************************************
* Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
* Copyright (C) 2014 Kalen 'Konloch' Kinloch - http://bytecodeviewer.com *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
/**
* A pane dedicating to searching the loaded files.
2018-01-31 15:41:24 +00:00
*
* @author Konloch
* @author WaterWolf
2021-07-04 07:03:40 +00:00
* @since 09/29/2011
*/
@SuppressWarnings("rawtypes")
public class SearchBoxPane extends VisibleComponent
{
2021-04-12 22:31:22 +00:00
public static final SearchRadius[] SEARCH_RADII = SearchRadius.values();
public static final SearchType[] SEARCH_TYPES = SearchType.values();
2021-06-30 22:27:23 +00:00
public final JCheckBox exact = new TranslatedJCheckBox("Exact", Translation.EXACT);
2021-06-22 18:05:25 +00:00
public final DefaultMutableTreeNode treeRoot = new DefaultMutableTreeNode("Results");
public final JTree tree;
public final JComboBox typeBox;
public SearchType searchType = null;
public final JComboBox searchRadiusBox;
2018-01-31 15:41:24 +00:00
public JButton search = new JButton("Search");
2021-07-05 02:06:15 +00:00
public BackgroundSearchThread performSearchThread;
2018-01-31 15:41:24 +00:00
@SuppressWarnings("unchecked")
2021-07-05 02:06:15 +00:00
public SearchBoxPane()
{
2018-01-31 15:41:24 +00:00
super("Search");
2018-01-31 15:41:24 +00:00
final JPanel optionPanel = new JPanel(new BorderLayout());
final JPanel searchRadiusOpt = new JPanel(new BorderLayout());
final JPanel searchOpts = new JPanel(new GridLayout(2, 1));
searchRadiusOpt.add(new JLabel("Search from "), BorderLayout.WEST);
DefaultComboBoxModel model = new DefaultComboBoxModel();
2021-07-05 02:06:15 +00:00
for (final SearchRadius st : SEARCH_RADII)
2018-01-31 15:41:24 +00:00
model.addElement(st);
searchRadiusBox = new JComboBox(model);
searchRadiusOpt.add(searchRadiusBox, BorderLayout.CENTER);
searchOpts.add(searchRadiusOpt);
model = new DefaultComboBoxModel();
2021-06-22 18:05:25 +00:00
for (final SearchType st : SEARCH_TYPES)
2018-01-31 15:41:24 +00:00
model.addElement(st);
2019-04-17 06:45:15 +00:00
typeBox = new JComboBox(model);
2018-01-31 15:41:24 +00:00
final JPanel searchOptPanel = new JPanel();
2021-04-12 22:31:22 +00:00
final ItemListener il = arg0 -> {
searchOptPanel.removeAll();
searchType = (SearchType) typeBox.getSelectedItem();
searchOptPanel.add(Objects.requireNonNull(searchType).details.getPanel());
2018-01-31 15:41:24 +00:00
2021-04-12 22:31:22 +00:00
searchOptPanel.revalidate();
searchOptPanel.repaint();
2018-01-31 15:41:24 +00:00
};
typeBox.addItemListener(il);
2019-04-17 06:45:15 +00:00
typeBox.setSelectedItem(SearchType.Strings);
2018-01-31 15:41:24 +00:00
il.itemStateChanged(null);
searchOpts.add(typeBox);
optionPanel.add(searchOpts, BorderLayout.NORTH);
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout());
p2.add(searchOptPanel, BorderLayout.NORTH);
p2.add(exact, BorderLayout.SOUTH);
optionPanel.add(p2, BorderLayout.CENTER);
2021-04-12 22:31:22 +00:00
search.addActionListener(arg0 -> search());
2018-01-31 15:41:24 +00:00
optionPanel.add(search, BorderLayout.SOUTH);
this.tree = new JTree(treeRoot);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(new JScrollPane(optionPanel), BorderLayout.NORTH);
getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER);
2021-07-05 02:06:15 +00:00
this.tree.addTreeSelectionListener(selectionEvent ->
{
if (selectionEvent.getPath().getPathComponent(0).equals("Results"))
2021-04-12 22:31:22 +00:00
return;
2019-04-17 06:45:15 +00:00
2021-07-05 02:06:15 +00:00
selectionEvent.getPath().getPathComponent(1);
2019-04-17 06:45:15 +00:00
2021-07-05 02:06:15 +00:00
String path = selectionEvent.getPath().getPathComponent(1).toString();
2018-01-31 15:41:24 +00:00
2021-04-12 22:31:22 +00:00
String containerName = path.split(">", 2)[0];
String className = path.split(">", 2)[1].split("\\.")[0];
FileContainer container = BytecodeViewer.getFileContainer(containerName);
2019-04-17 06:45:15 +00:00
2021-04-12 22:31:22 +00:00
final ClassNode fN = Objects.requireNonNull(container).getClassNode(className);
2019-04-17 06:45:15 +00:00
2021-07-05 02:06:15 +00:00
if (fN != null)
2021-06-22 18:05:25 +00:00
BytecodeViewer.viewer.openClassFile(container, className + ".class", fN);
2018-01-31 15:41:24 +00:00
});
this.setVisible(true);
}
2021-07-05 02:06:15 +00:00
public void search()
{
2019-04-17 06:45:15 +00:00
treeRoot.removeAllChildren();
searchType = (SearchType) typeBox.getSelectedItem();
2021-07-05 02:06:15 +00:00
final SearchRadius radius = (SearchRadius) searchRadiusBox.getSelectedItem();
2021-04-12 22:31:22 +00:00
final SearchResultNotifier srn = debug -> treeRoot.add(new DefaultMutableTreeNode(debug));
2021-07-05 02:06:15 +00:00
if (radius == SearchRadius.All_Classes)
{
if (performSearchThread == null || performSearchThread.finished)
{
BytecodeViewer.viewer.searchBoxPane.search.setEnabled(false);
BytecodeViewer.viewer.searchBoxPane.search.setText("Searching, please wait..");
performSearchThread = new PerformSearch(this, srn);
performSearchThread.start();
}
else
{ // this should really never be called.
2021-06-21 23:37:55 +00:00
BytecodeViewer.showMessage("You currently have a search performing in the background, please wait for that to finish.");
2019-04-17 06:45:15 +00:00
}
2021-07-05 02:06:15 +00:00
}
else if (radius == SearchRadius.Current_Class)
{
2021-07-07 03:05:35 +00:00
final ResourceViewer cv = BytecodeViewer.getActiveResource();
2021-07-05 02:06:15 +00:00
if (cv != null)
2019-04-17 06:45:15 +00:00
searchType.details.search(cv.container, cv.cn, srn, exact.isSelected());
}
}
2021-07-05 02:06:15 +00:00
public void resetWorkspace()
{
2018-01-31 15:41:24 +00:00
treeRoot.removeAllChildren();
tree.updateUI();
}
2021-07-05 02:06:15 +00:00
private static final long serialVersionUID = -1098524689236993932L;
}