Skip to content

Commit

Permalink
Added code preview for “find usages” and related refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
troizet committed Aug 25, 2024
1 parent 3502830 commit 6f8b485
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ HINT_physicalView=Show Physical View
HINT_nextMatch=Next Occurence - Ctrl+Period
HINT_prevMatch=Previous Occurence - Ctrl+Comma
HINT_stop=Stop Search
HINT_preview=Show code preview

ACSD_usagesPanel=N/A
ACSD_usagesTree=N/A
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ public void mouseClicked(MouseEvent e) {
o = ((TreeElement) o).getUserObject();
if (o instanceof RefactoringElement) {
openDiff(node);
}
}
}
}
} else {
Rectangle chRect = CheckRenderer.getCheckBoxRectangle();
Rectangle rowRect = tree.getPathBounds(path);
Expand Down Expand Up @@ -357,9 +357,21 @@ static void selectNextPrev(final boolean next, boolean isQuery, JTree tree) {
tree.setSelectionRow(newRow);
tree.scrollRowToVisible(newRow);
if (isQuery) {
CheckNodeListener.findInSource(node);
RefactoringPanel current = RefactoringPanel.getCurrentRefactoringPanel();
if (current != null && current.isSelectedPreview()) {
CheckNodeListener.openDiff(node);
} else {
CheckNodeListener.findInSource(node);
}
} else {
CheckNodeListener.openDiff(node);
}
}

static void selectCurrent(boolean isQuery, JTree tree) {
int row = tree.getRowForPath(tree.getSelectionPath());
TreePath path = tree.getPathForRow(row);
CheckNode node = (CheckNode) path.getLastPathComponent();
CheckNodeListener.openDiff(node);
}
} // end CheckNodeListener
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.netbeans.modules.refactoring.spi.impl;

import java.awt.Component;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.Reader;
Expand All @@ -27,12 +28,18 @@
import java.util.WeakHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.EditorKit;
import javax.swing.text.StyledDocument;
import org.netbeans.api.diff.DiffController;
import org.netbeans.api.diff.Difference;
import org.netbeans.api.diff.StreamSource;
import org.netbeans.api.editor.mimelookup.MimeLookup;
import org.netbeans.editor.EditorUI;
import org.netbeans.editor.Utilities;
import org.netbeans.modules.refactoring.api.impl.SPIAccessor;
import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImplementation;
import org.netbeans.modules.refactoring.spi.ui.UI;
Expand Down Expand Up @@ -106,6 +113,47 @@ private Pair getPair(SimpleRefactoringElementImplementation element) {
}

public void refresh(SimpleRefactoringElementImplementation element) {
RefactoringPanel current = RefactoringPanel.getCurrentRefactoringPanel();
if (current != null && current.isQuery() && current.isSelectedPreview()) {
showQueryPreview(element);
} else {
showDiffView(element);
}
}

private void showQueryPreview(SimpleRefactoringElementImplementation element) {
try {
FileObject fileObject = element.getParentFile();
DataObject dataObject = DataObject.find(fileObject);
EditorCookie editorCookie = dataObject != null ? dataObject.getLookup().lookup(org.openide.cookies.EditorCookie.class) : null;
if (editorCookie != null) {
StyledDocument document = editorCookie.openDocument();
if (document != null) {
String mimeType = (String) document.getProperty("mimeType"); //NOI18N
if (mimeType != null) {
JEditorPane pane = new JEditorPane();
EditorKit editorKit = MimeLookup.getLookup(mimeType).lookup(EditorKit.class);
pane.setEditorKit(editorKit);
pane.setDocument(document);
pane.setEditable(false);
Component editorComponent;
EditorUI editorUI = Utilities.getEditorUI(pane);
if (editorUI != null) {
editorComponent = editorUI.getExtComponent();
} else {
editorComponent = new JScrollPane(pane);
}
pane.setCaretPosition(element.getPosition().getBegin().getOffset());
UI.setComponentForRefactoringPreview(editorComponent);
}
}
}
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}

private void showDiffView(SimpleRefactoringElementImplementation element) {
try {
String newText = SPIAccessor.DEFAULT.getNewFileContent(element);
if (newText==null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import javax.swing.tree.ExpandVetoException;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import org.netbeans.api.annotations.common.CheckForNull;
import org.netbeans.api.progress.ProgressHandle;
import org.netbeans.api.progress.ProgressHandleFactory;
import org.netbeans.api.project.*;
Expand Down Expand Up @@ -92,6 +93,7 @@ public class RefactoringPanel extends JPanel implements FiltersManagerImpl.Filte
private transient JButton cancelButton = null;
private transient ButtonL buttonListener = null;
private transient JButton rerunButton = null;
private transient JToggleButton previewButton = null;

private final RefactoringUI ui;
private final boolean isQuery;
Expand Down Expand Up @@ -176,10 +178,8 @@ private void initialize() {
left.setLayout(new BorderLayout());
setLayout(new BorderLayout());
add(splitPane, BorderLayout.CENTER);
if (!isQuery) {
if (!isQuery || (isQuery && isPreview())) {
splitPane.setRightComponent(new JLabel(org.openide.util.NbBundle.getMessage(RefactoringPanel.class, "LBL_Preview_not_Available"), SwingConstants.CENTER));
} else {
splitPane.setDividerSize(0);
}
splitPane.setBorder(null);
// add panel with buttons
Expand Down Expand Up @@ -377,6 +377,22 @@ private JToolBar getToolBar() {
toolbar.add(expandButton);
toolbar.add(logicalViewButton);
toolbar.add(physicalViewButton);

if (isQuery) {
previewButton = new JToggleButton(
ImageUtilities.loadImageIcon("org/netbeans/modules/refactoring/api/resources/findusages.png", false)
);
previewButton.setMaximumSize(dim);
previewButton.setMinimumSize(dim);
previewButton.setPreferredSize(dim);
previewButton.setToolTipText(
NbBundle.getMessage(RefactoringPanel.class, "HINT_preview") // NOI18N
);
previewButton.setSelected(getPrefPreview());
previewButton.addActionListener(getButtonListener());
toolbar.add(previewButton);
}

if (ui instanceof RefactoringCustomUI) {
toolbar.add(customViewButton);
}
Expand Down Expand Up @@ -418,8 +434,11 @@ private JButton[] getButtons() {
private static final byte LOGICAL = 0;
private static final byte PHYSICAL = 1;
private static final byte GRAPHICAL = 2;


private static final boolean PREVIEW = false;

private static final String PREF_VIEW_TYPE = "PREF_VIEW_TYPE";
private static final String PREF_PREVIEW = "PREF_PREVIEW";
private byte currentView = getPrefViewType();

void switchToLogicalView() {
Expand Down Expand Up @@ -469,7 +488,19 @@ void switchToCustomView() {
expandButton.setEnabled(false);
refresh(false);
}


void switchToPreview() {
checkEventThread();
if (!previewButton.isSelected()) {
splitPane.setRightComponent(null);
} else {
splitPane.setRightComponent(new JLabel(org.openide.util.NbBundle.getMessage(RefactoringPanel.class, "LBL_Preview_not_Available"), SwingConstants.CENTER));
splitPane.setDividerLocation(0.3);
CheckNodeListener.selectCurrent(isQuery, tree);
}
storePrefPreview();
}

private CheckNode createNode(TreeElement representedObject, Map<Object, CheckNode> nodes, CheckNode root) {
//checkEventThread();
boolean isLogical = currentView == LOGICAL;
Expand Down Expand Up @@ -784,6 +815,29 @@ public void run() {
root.setNodeLabel(description + getErrorDesc(0, occurrences, hiddenOccurrences, isQuery && sizeIsApproximate.get()));
if (last) {
tree.repaint();
if (previewButton.isSelected()) {
if (showParametersPanel) {
splitPane.setDividerLocation(0.3);
if (size.get() < MAX_ROWS) {
expandAll();
if (isQuery) {
selectNextUsage();
}
} else {
expandButton.setSelected(false);
}
} else {
splitPane.setDividerLocation(0.3);
if (expandButton.isSelected()) {
expandAll();
if (isQuery) {
selectNextUsage();
}
} else {
expandButton.setSelected(false);
}
}
}
}
}
}
Expand Down Expand Up @@ -910,6 +964,7 @@ public void run() {
expandButton.setSelected(false);
}
} else {
splitPane.setDividerLocation(0.3);
if (expandButton.isSelected()) {
expandAll();
if (!isQuery) {
Expand Down Expand Up @@ -1013,6 +1068,7 @@ private void setupInstantTree(final CheckNode root, final boolean showParameters
public void run() {
createTree(root);
tree.setSelectionRow(0);
splitPane.setDividerLocation(0.3);
if (refactorButton != null) {
refactorButton.requestFocusInWindow();
} else if (tree != null) {
Expand Down Expand Up @@ -1066,6 +1122,7 @@ private void disableComponents() {
disableComponent(refreshButton);
disableComponent(rerunButton);
disableComponent(stopButton);
disableComponent(previewButton);
disableComponent(tree);
}

Expand All @@ -1090,17 +1147,45 @@ public void restoreDeviderLocation() {
}
}

public boolean isPreview()
{
return getPrefPreview();
}

public boolean isSelectedPreview()
{
if (previewButton != null) {
return previewButton.isSelected();
}
return false;
}

public boolean isQuery()
{
return isQuery;
}

private byte getPrefViewType() {
Preferences prefs = NbPreferences.forModule(RefactoringPanel.class);
return (byte) prefs.getInt(PREF_VIEW_TYPE, PHYSICAL);
}

private boolean getPrefPreview() {
Preferences prefs = NbPreferences.forModule(RefactoringPanel.class);
return prefs.getBoolean(PREF_PREVIEW, PREVIEW);
}

private void storePrefViewType() {
assert currentView!=GRAPHICAL;
Preferences prefs = NbPreferences.forModule(RefactoringPanel.class);
prefs.putInt(PREF_VIEW_TYPE, currentView);
}

private void storePrefPreview() {
Preferences prefs = NbPreferences.forModule(RefactoringPanel.class);
prefs.putBoolean(PREF_PREVIEW, previewButton.isSelected());
}

private void updateFilters(boolean reset) {
if(!ui.isQuery() || callback != null) {
if(filterBar != null) {
Expand Down Expand Up @@ -1133,6 +1218,25 @@ public void filterStateChanged(ChangeEvent e) {
refresh(false);
}

@CheckForNull
public static RefactoringPanel getCurrentRefactoringPanel()
{
TopComponent activated = TopComponent.getRegistry().getActivated();
RefactoringPanel refactoringPanel = null;
if (activated instanceof RefactoringPanelContainer) {
RefactoringPanelContainer panel = (RefactoringPanelContainer) activated;
refactoringPanel = panel.getCurrentPanel();
}
if (refactoringPanel == null) {
refactoringPanel = RefactoringPanelContainer.getRefactoringComponent().getCurrentPanel();
}
if (refactoringPanel == null) {
refactoringPanel = RefactoringPanelContainer.getUsagesComponent().getCurrentPanel();
}

return refactoringPanel;
}

////////////////////////////////////////////////////////////////////////////
// INNER CLASSES
////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1176,6 +1280,8 @@ else if (o == expandButton && tree != null) {
selectPrevUsage();
} else if (o == stopButton) {
stopSearch();
} else if (o == previewButton) {
switchToPreview();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,21 @@ public static void openRefactoringUI(RefactoringUI ui, RefactoringSession caller
* @return component was successfuly set
*/
public static boolean setComponentForRefactoringPreview(Component component) {
TopComponent activated = TopComponent.getRegistry().getActivated();
RefactoringPanel refactoringPanel = null;
if (activated instanceof RefactoringPanelContainer) {
RefactoringPanelContainer panel = (RefactoringPanelContainer) activated;
refactoringPanel = panel.getCurrentPanel();
}
if (refactoringPanel == null) {
refactoringPanel = RefactoringPanelContainer.getRefactoringComponent().getCurrentPanel();
}
if (refactoringPanel == null) {
refactoringPanel = RefactoringPanelContainer.getUsagesComponent().getCurrentPanel();
}
RefactoringPanel refactoringPanel = RefactoringPanel.getCurrentRefactoringPanel();
if (refactoringPanel == null)
return false;
if (component == null) {
if (refactoringPanel.splitPane.getRightComponent() == null)
return false;
}
refactoringPanel.storeDividerLocation();
refactoringPanel.splitPane.setRightComponent(component);
refactoringPanel.restoreDeviderLocation();

if (!refactoringPanel.isQuery()
|| (refactoringPanel.isQuery() && refactoringPanel.isSelectedPreview())) {
refactoringPanel.storeDividerLocation();
refactoringPanel.splitPane.setRightComponent(component);
refactoringPanel.restoreDeviderLocation();
}

return true;

}
Expand Down

0 comments on commit 6f8b485

Please sign in to comment.