Skip to content

Commit

Permalink
Merge pull request #7694 from troizet/find_usages_preview
Browse files Browse the repository at this point in the history
Added code preview for “find usages” and related refactorings
  • Loading branch information
mbien authored Oct 1, 2024
2 parents 5f47772 + 18c2f55 commit 22de49f
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 46 deletions.
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 @@ -356,10 +356,7 @@ static void selectNextPrev(final boolean next, boolean isQuery, JTree tree) {
} while (!node.isLeaf());
tree.setSelectionRow(newRow);
tree.scrollRowToVisible(newRow);
if (isQuery) {
CheckNodeListener.findInSource(node);
} else {
CheckNodeListener.openDiff(node);
}
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()) {
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 @@ -176,11 +177,7 @@ private void initialize() {
left.setLayout(new BorderLayout());
setLayout(new BorderLayout());
add(splitPane, BorderLayout.CENTER);
if (!isQuery) {
splitPane.setRightComponent(new JLabel(org.openide.util.NbBundle.getMessage(RefactoringPanel.class, "LBL_Preview_not_Available"), SwingConstants.CENTER));
} else {
splitPane.setDividerSize(0);
}
splitPane.setRightComponent(new JLabel(org.openide.util.NbBundle.getMessage(RefactoringPanel.class, "LBL_Preview_not_Available"), SwingConstants.CENTER));
splitPane.setBorder(null);
// add panel with buttons
JButton[] buttons = getButtons();
Expand Down Expand Up @@ -418,7 +415,7 @@ private JButton[] getButtons() {
private static final byte LOGICAL = 0;
private static final byte PHYSICAL = 1;
private static final byte GRAPHICAL = 2;

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

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

private CheckNode createNode(TreeElement representedObject, Map<Object, CheckNode> nodes, CheckNode root) {
//checkEventThread();
boolean isLogical = currentView == LOGICAL;
Expand Down Expand Up @@ -829,8 +826,10 @@ public void run() {
}

if (!(isQuery && showParametersPanel)) {
root.setNodeLabel(description + getErrorDesc(errorsNum, isQuery ? size.get() : elements.size(), hidden, isQuery && sizeIsApproximate.get()).toString());
root.setNodeLabel(description + getErrorDesc(errorsNum, elements.size(), hidden, false).toString());
setupTree(root, showParametersPanel, elements.size());
} else if (isQuery && showParametersPanel) {
SwingUtilities.invokeLater(() -> expandTreeIfNeeded(showParametersPanel, size.get()));
}

}
Expand Down Expand Up @@ -899,26 +898,8 @@ private void setupTree(final CheckNode root, final boolean showParametersPanel,
public void run() {
createTree(root);

if (showParametersPanel) {
splitPane.setDividerLocation(0.3);
if (size < MAX_ROWS) {
expandAll();
if (!isQuery) {
selectNextUsage();
}
} else {
expandButton.setSelected(false);
}
} else {
if (expandButton.isSelected()) {
expandAll();
if (!isQuery) {
selectNextUsage();
}
} else {
expandButton.setSelected(false);
}
}
splitPane.setDividerLocation(0.3);
expandTreeIfNeeded(showParametersPanel, size);

tree.setSelectionRow(0);
setRefactoringEnabled(true, true);
Expand All @@ -928,6 +909,24 @@ public void run() {
}
});
}

private void expandTreeIfNeeded(boolean showParametersPanel, int size) {
if (showParametersPanel) {
if (size < MAX_ROWS) {
expandAll();
selectNextUsage();
} else {
expandButton.setSelected(false);
}
} else {
if (expandButton.isSelected()) {
expandAll();
selectNextUsage();
} else {
expandButton.setSelected(false);
}
}
}

private Map<FileObject, Long> timeStamps = new HashMap<FileObject, Long>();

Expand Down Expand Up @@ -1013,6 +1012,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 @@ -1090,6 +1090,10 @@ public void restoreDeviderLocation() {
}
}

public boolean isQuery() {
return isQuery;
}

private byte getPrefViewType() {
Preferences prefs = NbPreferences.forModule(RefactoringPanel.class);
return (byte) prefs.getInt(PREF_VIEW_TYPE, PHYSICAL);
Expand Down Expand Up @@ -1133,6 +1137,24 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,7 @@ 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) {
Expand Down

0 comments on commit 22de49f

Please sign in to comment.