Skip to content

Commit

Permalink
Refactor BasicSearch and BasicReplaceResultsPanels
Browse files Browse the repository at this point in the history
 - BasicReplaceResultsPanel extends BasicSearchResultsPanel
 - less code duplication
  • Loading branch information
mbien committed Dec 26, 2024
1 parent 45b609a commit 6635544
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.AbstractButton;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import org.netbeans.modules.search.BasicComposition;
import org.netbeans.modules.search.ContextView;
import org.netbeans.modules.search.FindDialogMemory;
import org.netbeans.modules.search.Manager;
import org.netbeans.modules.search.ReplaceTask;
import org.netbeans.modules.search.ResultModel;
Expand All @@ -43,55 +40,44 @@
import org.openide.nodes.Node;
import org.openide.util.Mutex;
import org.openide.util.NbBundle;
import org.openide.util.RequestProcessor;

/**
*
* @author jhavlin
*/
public class BasicReplaceResultsPanel extends BasicAbstractResultsPanel {
public class BasicReplaceResultsPanel extends BasicSearchResultsPanel {

private static final RequestProcessor RP =
new RequestProcessor(BasicReplaceResultsPanel.class.getName());
private final RequestProcessor.Task SAVE_TASK = RP.create(new SaveTask());
private JButton replaceButton;
private JSplitPane splitPane;

public BasicReplaceResultsPanel(ResultModel resultModel,
BasicComposition composition, Node infoNode) {
super(resultModel, composition, true,
new ResultsOutlineSupport(true, true, resultModel, composition,
infoNode));
init();

public BasicReplaceResultsPanel(ResultModel resultModel, BasicComposition composition, Node infoNode) {
super(resultModel, composition, true,
new ResultsOutlineSupport(true, true, resultModel, composition, infoNode));
}

private void init() {
JPanel leftPanel = new JPanel();
@Override
protected JComponent createLeftComponent() {
replaceButton = new JButton();
replaceButton.addActionListener((ActionEvent e) -> replace());
replaceButton.setMaximumSize(replaceButton.getPreferredSize());
replaceButton.getAccessibleContext().setAccessibleDescription(
NbBundle.getMessage(ResultView.class,
"ACS_TEXT_BUTTON_REPLACE")); //NOI18N
updateReplaceButton();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));

JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.LEADING, 2, 1));
buttonPanel.add(replaceButton);
replaceButton.setMaximumSize(replaceButton.getPreferredSize());
buttonPanel.setMaximumSize(new Dimension( // #225246
(int) buttonPanel.getMaximumSize().getWidth(),
(int) buttonPanel.getPreferredSize().getHeight()));

JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
leftPanel.add(resultsOutlineSupport.getOutlineView());
leftPanel.add(buttonPanel);

this.splitPane = new JSplitPane();
splitPane.setLeftComponent(leftPanel);
splitPane.setRightComponent(new ContextView(resultModel,
getExplorerManager()));
initSplitDividerLocationHandling();

getContentPanel().add(splitPane);

initResultModelListener();
replaceButton.getAccessibleContext().setAccessibleDescription(
NbBundle.getMessage(ResultView.class,
"ACS_TEXT_BUTTON_REPLACE")); //NOI18N
return leftPanel;
}

private void replace() {
Expand All @@ -107,17 +93,6 @@ private void initResultModelListener() {
resultModel.addPropertyChangeListener(new ModelListener());
}

private void initSplitDividerLocationHandling() {
int location = FindDialogMemory.getDefault().getReplaceResultsDivider();
splitPane.setDividerLocation(Math.max(location, 250));
splitPane.addPropertyChangeListener((PropertyChangeEvent evt) -> {
String pn = evt.getPropertyName();
if (pn.equals(JSplitPane.DIVIDER_LOCATION_PROPERTY)) {
SAVE_TASK.schedule(1000);
}
});
}

@Override
public void searchFinished() {
super.searchFinished();
Expand Down Expand Up @@ -224,14 +199,4 @@ public void showFinishedInfo() {
});
}

private class SaveTask implements Runnable {

@Override
public void run() {
if (splitPane != null) {
FindDialogMemory.getDefault().setReplaceResultsDivider(
splitPane.getDividerLocation());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
*/
package org.netbeans.modules.search.ui;

import java.beans.PropertyChangeEvent;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JComponent;
import javax.swing.JSplitPane;
import org.netbeans.modules.search.BasicComposition;
import org.netbeans.modules.search.ContextView;
Expand All @@ -34,44 +32,37 @@
* @author jhavlin
*/
public class BasicSearchResultsPanel extends BasicAbstractResultsPanel {
private final RequestProcessor.Task SAVE_TASK = RequestProcessor.getDefault().create(new BasicSearchResultsPanel.SaveTask());
private JSplitPane splitPane;

public BasicSearchResultsPanel(ResultModel resultModel,
BasicComposition composition, boolean details, Node infoNode) {
super(resultModel, composition, details,
new ResultsOutlineSupport(false, details, resultModel,
composition, infoNode));
init();
public BasicSearchResultsPanel(ResultModel resultModel, BasicComposition composition, boolean details, Node infoNode) {
this(resultModel, composition, details,
new ResultsOutlineSupport(false, details, resultModel, composition, infoNode));
}

private void init() {
splitPane = new JSplitPane();
splitPane.setLeftComponent(resultsOutlineSupport.getOutlineView());
splitPane.setRightComponent(new ContextView(resultModel, getExplorerManager()));
initSplitDividerLocationHandling();
BasicSearchResultsPanel(ResultModel resultModel, BasicComposition composition, boolean details, ResultsOutlineSupport resultsOutlineSupport) {
super(resultModel, composition, details, resultsOutlineSupport);

JSplitPane splitPane = new JSplitPane();
splitPane.setLeftComponent(createLeftComponent());
splitPane.setRightComponent(createRightComponent());
getContentPanel().add(splitPane);
}

private void initSplitDividerLocationHandling() {
// divider persistance
RequestProcessor.Task dividerSaveTask = RequestProcessor.getDefault().create(() ->
FindDialogMemory.getDefault().setReplaceResultsDivider(splitPane.getDividerLocation())
);
int location = FindDialogMemory.getDefault().getReplaceResultsDivider();
splitPane.setDividerLocation(Math.max(location, 250));
splitPane.addPropertyChangeListener(evt -> {
String pn = evt.getPropertyName();
if (pn.equals(JSplitPane.DIVIDER_LOCATION_PROPERTY)) {
SAVE_TASK.schedule(1000);
}
});
splitPane.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY, e ->
dividerSaveTask.schedule(1000)
);
}

private class SaveTask implements Runnable {
protected JComponent createLeftComponent() {
return resultsOutlineSupport.getOutlineView();
}

@Override
public void run() {
if (splitPane != null) {
FindDialogMemory.getDefault().setReplaceResultsDivider(
splitPane.getDividerLocation());
}
}
protected JComponent createRightComponent() {
return new ContextView(resultModel, getExplorerManager());
}

}

0 comments on commit 6635544

Please sign in to comment.