Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Filter Mechanism for Target in Move Refactoring#18 #981

Closed
wants to merge 10 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
Expand All @@ -33,6 +34,8 @@
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;

import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
Expand All @@ -41,6 +44,10 @@
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;

import org.eclipse.jdt.internal.core.JavaProject;
import org.eclipse.jdt.internal.core.PackageFragment;
import org.eclipse.jdt.internal.core.PackageFragmentRoot;
import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgDestinationValidator;
import org.eclipse.jdt.internal.corext.util.Messages;

Expand All @@ -49,16 +56,18 @@
import org.eclipse.jdt.ui.JavaElementLabels;

import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;


abstract class ReorgUserInputPage extends UserInputWizardPage{
abstract class ReorgUserInputPage extends UserInputWizardPage {
private static final long LABEL_FLAGS= JavaElementLabels.ALL_DEFAULT
| JavaElementLabels.M_PRE_RETURNTYPE | JavaElementLabels.M_PARAMETER_NAMES | JavaElementLabels.F_PRE_TYPE_SIGNATURE;

private TreeViewer fViewer;

public ReorgUserInputPage(String pageName) {
super(pageName);
}

@Override
public void createControl(Composite parent) {
initializeDialogUnits(parent);
Expand Down Expand Up @@ -109,7 +118,7 @@ private void viewerSelectionChanged(SelectionChangedEvent event) {
ISelection selection= event.getSelection();
if (!(selection instanceof IStructuredSelection))
return;
IStructuredSelection ss= (IStructuredSelection)selection;
IStructuredSelection ss= (IStructuredSelection) selection;
verifyDestination(ss.getFirstElement(), false);
}

Expand All @@ -125,6 +134,7 @@ private void viewerSelectionChanged(SelectionChangedEvent event) {
protected abstract RefactoringStatus verifyDestination(Object selected) throws JavaModelException;

protected abstract IResource[] getResources();

protected abstract IJavaElement[] getJavaElements();

protected abstract IReorgDestinationValidator getDestinationValidator();
Expand All @@ -142,7 +152,18 @@ private final void verifyDestination(Object selected, boolean initialVerificatio
}
}

// Initialize variables for debounce
Runnable debounceRunnable = null;
long lastModifyTime = 0;
int DEBOUNCE_DELAY = 1000;

private TreeViewer createViewer(Composite parent) {
// Create Search Text
Text searchText= new Text(parent, SWT.BORDER | SWT.SEARCH | SWT.CANCEL | SWT.ICON_SEARCH);
GridData searchStyle = new GridData(GridData.FILL_HORIZONTAL);
searchText.setMessage("Search"); //$NON-NLS-1$
searchText.setLayoutData(searchStyle);

TreeViewer treeViewer= new TreeViewer(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
GridData gd= new GridData(GridData.FILL_BOTH);
gd.widthHint= convertWidthInCharsToPixels(40);
Expand All @@ -151,10 +172,38 @@ private TreeViewer createViewer(Composite parent) {
treeViewer.setLabelProvider(new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_SMALL_ICONS));
treeViewer.setContentProvider(new DestinationContentProvider(getDestinationValidator()));
treeViewer.setComparator(new JavaElementComparator());

TreeViewerFilter viewerFilter = new TreeViewerFilter();
treeViewer.addFilter(viewerFilter);

searchText.addModifyListener(e -> {
long currentTime = System.currentTimeMillis();
if (currentTime - lastModifyTime > DEBOUNCE_DELAY) {
// If debounce time has elapsed, execute search immediately
executeSearch(searchText, viewerFilter, treeViewer);
} else {
// Schedule the search to execute after debounce delay
if (debounceRunnable != null) {
parent.getDisplay().timerExec(-1, debounceRunnable); // Cancel previous debounce
}
debounceRunnable = () -> executeSearch(searchText, viewerFilter, treeViewer);
parent.getDisplay().timerExec((int) (DEBOUNCE_DELAY - (currentTime - lastModifyTime)), debounceRunnable);
}
lastModifyTime = currentTime;
});

treeViewer.setInput(JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()));
return treeViewer;
}

private void executeSearch(Text searchText, TreeViewerFilter viewerFilter, TreeViewer treeViewer) {
String searchString= searchText.getText().trim();
viewerFilter.setSearchText(searchString);
treeViewer.refresh();
setPageComplete(false);
treeViewer.expandAll();
}

protected TreeViewer getTreeViewer() {
return fViewer;
}
Expand All @@ -167,8 +216,7 @@ public void doubleClick(DoubleClickEvent event) {
if (fViewer.isExpandable(element)) {
if (fViewer.getExpandedState(element)) {
fViewer.collapseToLevel(element, 1);
}
else {
} else {
ITreeContentProvider contentProvider= (ITreeContentProvider) fViewer.getContentProvider();
Object[] children= contentProvider.getChildren(element);
if (children.length > 0) {
Expand All @@ -179,3 +227,45 @@ public void doubleClick(DoubleClickEvent event) {
}
}
}
class TreeViewerFilter extends ViewerFilter {

private String searchText;

public void setSearchText(String searchText) {
this.searchText = searchText.toLowerCase();
}

@Override
public boolean select(Viewer viewer, Object parentElement, Object element) {
if (searchText == null || searchText.isEmpty()) {
return true; // If search text is empty, show all elements
}

if (element instanceof JavaProject) {
JavaProject treeElement = (JavaProject) element;
Object[] children;
try {
children= treeElement.getChildren();

for (Object child : children) {
if(child.getClass().equals(PackageFragmentRoot.class) ) { // Added this check so we don't try to find anything in JarFragmentRoot
PackageFragmentRoot packFrag= (PackageFragmentRoot) child;
if(packFrag.toString().toLowerCase().contains(searchText)) {
return true; // short circuit the operation if even one child is matching (to keep the project in the tree view), child filtering is done in else block
}
}
}
} catch (JavaModelException e) {
e.printStackTrace();
}
return false; // in case project name and its content doesn't match remove it entirely
} else {
if(element instanceof PackageFragment) {
PackageFragment packageFrag = (PackageFragment) element;
return packageFrag.toString().toLowerCase().contains(searchText);
}
}
return true;
}
}