From f19b098c98cd9f6dfecdbdccfbff494658a9fde6 Mon Sep 17 00:00:00 2001 From: deathmarine Date: Thu, 25 Aug 2016 08:21:20 -0400 Subject: [PATCH] FindAll acts as a dialog box, restores focus. Closes #40 Interrupt FindAll search thread in progress address #52 --- src/us/deathmarine/luyten/FindAllBox.java | 176 +++++++++++----------- src/us/deathmarine/luyten/MainWindow.java | 4 +- src/us/deathmarine/luyten/Model.java | 6 +- 3 files changed, 92 insertions(+), 94 deletions(-) diff --git a/src/us/deathmarine/luyten/FindAllBox.java b/src/us/deathmarine/luyten/FindAllBox.java index 46343d2..2596e98 100644 --- a/src/us/deathmarine/luyten/FindAllBox.java +++ b/src/us/deathmarine/luyten/FindAllBox.java @@ -30,20 +30,31 @@ public class FindAllBox extends JDialog { private static final long serialVersionUID = -4125409760166690462L; - private boolean cancel; + private boolean searching; + private JButton findButton; private JTextField textField; - JProgressBar progressBar; - private DefaultListModel classesList = new DefaultListModel(); + private JProgressBar progressBar; private JLabel statusLabel = new JLabel(""); - public FindAllBox() { + private DefaultListModel classesList = new DefaultListModel(); + + private Thread tmp_thread; + private MainWindow mainWindow; + + public FindAllBox(MainWindow mainWindow) { + this.mainWindow = mainWindow; + this.setDefaultCloseOperation(HIDE_ON_CLOSE); + this.setHideOnEscapeButton(); + progressBar = new JProgressBar(0, 100); + JLabel label = new JLabel("Find What:"); textField = new JTextField(); findButton = new JButton("Find"); findButton.addActionListener(new FindButton()); + this.getRootPane().setDefaultButton(findButton); JList list = new JList(classesList); @@ -65,15 +76,6 @@ public FindAllBox() { layout.setAutoCreateGaps(true); layout.setAutoCreateContainerGaps(true); - JButton cancelButton = new JButton("Cancel"); - cancelButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - if (isSearching()) - setCancel(true); - } - }); - layout.setHorizontalGroup(layout .createSequentialGroup() .addComponent(label) @@ -92,8 +94,7 @@ public void actionPerformed(ActionEvent e) { progressBar)))) .addGroup( layout.createParallelGroup(Alignment.LEADING) - .addComponent(findButton) - .addComponent(cancelButton)) + .addComponent(findButton)) ); @@ -109,15 +110,12 @@ public void actionPerformed(ActionEvent e) { layout.createSequentialGroup().addGroup( layout.createParallelGroup( Alignment.BASELINE) - .addComponent(listScroller) - .addComponent(cancelButton)))) + .addComponent(listScroller)))) .addGroup(layout.createParallelGroup(Alignment.LEADING)) .addComponent(statusLabel).addComponent(progressBar)); - this.setDefaultCloseOperation(HIDE_ON_CLOSE); - this.setHideOnEscapeButton(); this.adjustWindowPositionBySavedState(); this.setSaveWindowPositionOnClosing(); - this.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); + //this.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); this.setName("Find All"); this.setTitle("Find All"); @@ -128,71 +126,77 @@ private class FindButton extends AbstractAction { @Override public void actionPerformed(ActionEvent event) { - Thread tmp_thread = new Thread() { + tmp_thread = new Thread() { public void run() { - setSearching(true); - classesList.clear(); - ConfigSaver configSaver = ConfigSaver.getLoadedInstance(); - DecompilerSettings settings = configSaver - .getDecompilerSettings(); - File inFile = MainWindow.model.getOpenedFile(); - try { - JarFile jfile = new JarFile(inFile); - Enumeration entLength = jfile.entries(); - initProgressBar(Collections.list(entLength).size()); - Enumeration ent = jfile.entries(); - while (ent.hasMoreElements() && !isCancel()) { - JarEntry entry = ent.nextElement(); - setStatus(entry.getName()); - if (entry.getName().endsWith(".class")) { - synchronized (settings) { - String internalName = StringUtilities - .removeRight(entry.getName(), - ".class"); - TypeReference type = Model.metadataSystem - .lookupType(internalName); - TypeDefinition resolvedType = null; - if (type == null - || ((resolvedType = type.resolve()) == null)) { - throw new Exception( - "Unable to resolve type."); - } - StringWriter stringwriter = new StringWriter(); - DecompilationOptions decompilationOptions; - decompilationOptions = new DecompilationOptions(); - decompilationOptions.setSettings(settings); - decompilationOptions - .setFullDecompilation(true); - PlainTextOutput plainTextOutput = - new PlainTextOutput(stringwriter); - plainTextOutput.setUnicodeOutputEnabled( - decompilationOptions.getSettings() - .isUnicodeOutputEnabled()); - settings.getLanguage().decompileType( - resolvedType, - plainTextOutput, - decompilationOptions); - String decompiledSource = stringwriter - .toString().toLowerCase(); - if (decompiledSource.contains(textField - .getText().toLowerCase())) { - addClassName(entry.getName()); + if(findButton.getText().equals("Stop")){ + if (tmp_thread != null) + tmp_thread.interrupt(); + setStatus("Stopped."); + findButton.setText("Find"); + } else { + findButton.setText("Stop"); + classesList.clear(); + ConfigSaver configSaver = ConfigSaver.getLoadedInstance(); + DecompilerSettings settings = configSaver + .getDecompilerSettings(); + File inFile = MainWindow.model.getOpenedFile(); + try { + JarFile jfile = new JarFile(inFile); + Enumeration entLength = jfile.entries(); + initProgressBar(Collections.list(entLength).size()); + Enumeration ent = jfile.entries(); + while (ent.hasMoreElements() && findButton.getText().equals("Stop")) { + JarEntry entry = ent.nextElement(); + setStatus(entry.getName()); + if (entry.getName().endsWith(".class")) { + synchronized (settings) { + String internalName = StringUtilities + .removeRight(entry.getName(), + ".class"); + TypeReference type = Model.metadataSystem + .lookupType(internalName); + TypeDefinition resolvedType = null; + if (type == null + || ((resolvedType = type.resolve()) == null)) { + throw new Exception( + "Unable to resolve type."); + } + StringWriter stringwriter = new StringWriter(); + DecompilationOptions decompilationOptions; + decompilationOptions = new DecompilationOptions(); + decompilationOptions.setSettings(settings); + decompilationOptions + .setFullDecompilation(true); + PlainTextOutput plainTextOutput = + new PlainTextOutput(stringwriter); + plainTextOutput.setUnicodeOutputEnabled( + decompilationOptions.getSettings() + .isUnicodeOutputEnabled()); + settings.getLanguage().decompileType( + resolvedType, + plainTextOutput, + decompilationOptions); + String decompiledSource = stringwriter + .toString().toLowerCase(); + if (decompiledSource.contains(textField + .getText().toLowerCase())) { + addClassName(entry.getName()); + } } } } + setSearching(false); + if (findButton.getText().equals("Stop")){ + setStatus("Done."); + findButton.setText("Find"); + } + jfile.close(); + } catch (IOException e1) { + e1.printStackTrace(); + } catch (Exception e1) { + e1.printStackTrace(); } - setSearching(false); - if (isCancel()) { - setCancel(false); - setStatus("Cancelled."); - } else { - setStatus("Done."); - } - jfile.close(); - } catch (IOException e1) { - e1.printStackTrace(); - } catch (Exception e1) { - e1.printStackTrace(); + } } }; @@ -236,8 +240,6 @@ public void windowDeactivated(WindowEvent e) { WindowPosition windowPosition = ConfigSaver.getLoadedInstance() .getFindWindowPosition(); windowPosition.readPositionFromDialog(FindAllBox.this); - if (isSearching()) - setCancel(true); } }); } @@ -271,15 +273,7 @@ public void initProgressBar(Integer length) { progressBar.setValue(0); progressBar.setStringPainted(true); } - - public boolean isCancel() { - return cancel; - } - - public void setCancel(boolean cancel) { - this.cancel = cancel; - } - + public boolean isSearching() { return searching; } diff --git a/src/us/deathmarine/luyten/MainWindow.java b/src/us/deathmarine/luyten/MainWindow.java index e46e288..b06d1d9 100644 --- a/src/us/deathmarine/luyten/MainWindow.java +++ b/src/us/deathmarine/luyten/MainWindow.java @@ -218,7 +218,7 @@ public void onFindMenu() { public void onFindAllMenu() { try { if (findAllBox == null) - findAllBox = new FindAllBox(); + findAllBox = new FindAllBox(this); findAllBox.showFindBox(); } catch (Exception e) { @@ -394,7 +394,7 @@ private void setShowFindAllBoxOnMainWindowFocus() { @Override public void windowGainedFocus(WindowEvent e) { if (findAllBox != null && findAllBox.isVisible()) { - findAllBox.requestFocus(); + findAllBox.setVisible(false); } } }); diff --git a/src/us/deathmarine/luyten/Model.java b/src/us/deathmarine/luyten/Model.java index 6bb72e3..ff1df58 100644 --- a/src/us/deathmarine/luyten/Model.java +++ b/src/us/deathmarine/luyten/Model.java @@ -292,6 +292,7 @@ private void openEntryByTreePath(TreePath trp) { } } } + label.setText("Complete"); } catch (FileEntryNotFoundException e) { label.setText("File not found: " + name); @@ -432,7 +433,10 @@ public void stateChanged(ChangeEvent e) { } } } - + + public void openFileFromPath(){ + + } public void updateOpenClasses() { // invalidate all open classes (update will hapen at tab change) for (OpenFile open : hmap) {