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

Use styled label provider for selection popup #220

Merged
merged 1 commit into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,12 @@ public Consumer<MouseEvent> getAction()

TypeFacade typeFacade = TypeFacade.createFacade(((IMember) element).getCompilationUnit());

String testOrTested = typeFacade instanceof TestCaseTypeFacade ? "tested" : "test";
CorrespondingMemberRequest request = newCorrespondingMemberRequest() //
.withExpectedResultType(MemberType.TYPE_OR_METHOD) //
.withCurrentMethod(element instanceof IMethod method ? method : null) //
.methodSearchMode(searchMode) //
.promptText("Jump to " + testOrTested + " class...")
.build();

IMember memberToJump = typeFacade.getOneCorrespondingMember(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,25 @@ private Builder()

/**
* Will propose the creation of a type if no correspondence is found.
*
*
* @param promptText the prompt text to display in the dialog asking the
* user to choose for a member
*/
public Builder createClassIfNoResult(String promptText)
{
this.createClassIfNoResult = true;
return promptText(promptText);
}

/**
* @param promptText the prompt text to display in the dialog asking the
* user to choose for a member
*/
public Builder promptText(String promptText)
{
this.promptText = promptText;
return this;
}

/**
* How to search for corresponding methods (by call or/and by method
* name).
Expand Down
40 changes: 30 additions & 10 deletions org.moreunit.plugin/src/org/moreunit/ui/ChooseDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.ui.JavaElementLabelProvider;
import org.eclipse.jdt.ui.JavaElementLabels;
import org.eclipse.jface.dialogs.PopupDialog;
import org.eclipse.jface.viewers.AbstractTreeViewer;
import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
Expand All @@ -26,6 +29,7 @@
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.PlatformUI;
import org.moreunit.core.util.Strings;
import org.moreunit.log.LogHandler;

public class ChooseDialog<T> extends PopupDialog implements DisposeListener
Expand Down Expand Up @@ -209,7 +213,7 @@ protected TreeViewer createTreeViewer(Composite parent)
TreeViewer viewer = new TreeViewer(parent, SWT.NO_TRIM);
viewer.setAutoExpandLevel(AbstractTreeViewer.ALL_LEVELS);
viewer.setContentProvider(contentProvider);
viewer.setLabelProvider(new LabelProvider());
viewer.setLabelProvider(new DelegatingStyledCellLabelProvider(new LabelProvider()));
viewer.setInput(this);
return viewer;
}
Expand Down Expand Up @@ -251,29 +255,45 @@ private void runEventLoop(Shell loopShell)

private static class LabelProvider extends JavaElementLabelProvider
{
public LabelProvider()
{
super(JavaElementLabelProvider.SHOW_DEFAULT | JavaElementLabelProvider.SHOW_OVERLAY_ICONS);
}

@Override
public Image getImage(Object element)
{
if(element instanceof TreeActionElement)
if(element instanceof TreeActionElement action)
{
return ((TreeActionElement< ? >) element).getImage();
return action.getImage();
}
return super.getImage(element);
}

@Override
public String getText(Object element)
public StyledString getStyledText(Object element)
{
if(element instanceof TreeActionElement)
if(element instanceof TreeActionElement action)
{
return ((TreeActionElement< ? >) element).getText();
return styled(action.getText(), null);
}
else if(element instanceof IType)
else if(element instanceof IType type)
{
IType type = (IType) element;
return String.format("%s - %s", type.getElementName(), type.getPackageFragment().getElementName());
return styled(type.getElementName(), type.getPackageFragment().getElementName());
}
return super.getText(element);
return super.getStyledText(element);
}

private StyledString styled(String text, String decoration)
{
StyledString styled = new StyledString();
styled.append(text);
if(decoration != null && ! Strings.isBlank(decoration))
{
styled.append(JavaElementLabels.CONCAT_STRING + decoration, StyledString.DECORATIONS_STYLER);
}
return styled;
}
}

}
Loading