Skip to content

Commit

Permalink
Prevent multiple "Press F2 for focus" in Generic Editor
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelistria committed May 4, 2023
1 parent 783721b commit e93878e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public abstract class AbstractInformationControl implements IInformationControl,
* @param statusFieldText the text to be used in the status field or <code>null</code> to hide the status field
*/
public AbstractInformationControl(Shell parentShell, String statusFieldText) {
this(parentShell, SWT.TOOL | SWT.ON_TOP, statusFieldText, null);
this(parentShell, SWT.TOOL | SWT.ON_TOP | SWT.RESIZE, statusFieldText, null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.editors.text.EditorsUI;
import org.eclipse.ui.internal.genericeditor.GenericEditorPlugin;

public class CompositeInformationControl extends AbstractInformationControl implements IInformationControlExtension2 {

final LinkedHashMap<ITextHover, IInformationControlCreator> creators;
LinkedHashMap<ITextHover, IInformationControl> controls;
LinkedHashMap<ITextHover, AbstractInformationControl> controls;

public CompositeInformationControl(Shell parentShell, LinkedHashMap<ITextHover, IInformationControlCreator> creators) {
super(parentShell, true); // TODO check best constructor
super(parentShell, EditorsUI.getTooltipAffordanceString()); // TODO check best constructor
Assert.isLegal(creators.size() > 1, "Do not compose a unique hover"); //$NON-NLS-1$
this.creators = creators;
create();
Expand All @@ -66,21 +67,31 @@ public boolean hasContents() {

@Override
public void setInput(Object input) {
int withContent = 0;
@SuppressWarnings("unchecked")
Map<ITextHover, Object> inputs = (Map<ITextHover, Object>)input;
for (Entry<ITextHover, Object> entry : inputs.entrySet()) {
IInformationControl informationControl = controls.get(entry.getKey());
AbstractInformationControl informationControl = controls.get(entry.getKey());
if (informationControl != null) {
if (informationControl instanceof IInformationControlExtension2) {
((IInformationControlExtension2)informationControl).setInput(entry.getValue());
continue;
} else {
String information = entry.getValue().toString();
if(!information.isEmpty()){
informationControl.setInformation(information);
}
}
String information = entry.getValue().toString();
if(!information.isEmpty()){
informationControl.setInformation(information);
if (informationControl.hasContents()) {
withContent++;
}
}
}
if (withContent > 1) {
controls.values().forEach(control -> control.setStatusText(null));
setStatusText(EditorsUI.getTooltipAffordanceString());
} else {
setStatusText(null);
}
}

@Override
Expand All @@ -90,16 +101,16 @@ public void createContent(Composite parent) {
parent.setLayout(layout);
for (Entry<ITextHover, IInformationControlCreator> hoverControlCreator : this.creators.entrySet()) {
IInformationControl informationControl = hoverControlCreator.getValue().createInformationControl(parent.getShell());
if (informationControl instanceof AbstractInformationControl) {
List<Control> children = Arrays.asList(((AbstractInformationControl)informationControl).getShell().getChildren());
if (informationControl instanceof AbstractInformationControl abstractInformationControl) {
List<Control> children = Arrays.asList(abstractInformationControl.getShell().getChildren());
children.remove(parent);
if (children.isEmpty() ) {
continue;
}
for (Control control : children) {
control.setParent(parent);
}
controls.put(hoverControlCreator.getKey(), informationControl);
controls.put(hoverControlCreator.getKey(), abstractInformationControl);
} else {
GenericEditorPlugin.getDefault().getLog().log(new Status(IStatus.WARNING, GenericEditorPlugin.BUNDLE_ID,
"Only text hovers producing an AbstractInformationControl can be aggregated; got a " + informationControl.getClass().getSimpleName())); //$NON-NLS-1$
Expand Down Expand Up @@ -127,7 +138,7 @@ public IInformationControlCreator getInformationPresenterControlCreator() {
} else {
LinkedHashMap<ITextHover, IInformationControlCreator> presenterCreators = new LinkedHashMap<>();
boolean allNull = true;
for (Entry<ITextHover, IInformationControl> hover : this.controls.entrySet()) {
for (Entry<ITextHover, AbstractInformationControl> hover : this.controls.entrySet()) {
IInformationControlCreator creator = null;
if (hover.getValue() instanceof IInformationControlExtension5)
creator = ((IInformationControlExtension5)hover.getValue()).getInformationPresenterControlCreator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.List;
import java.util.Map.Entry;

import org.eclipse.jface.text.AbstractInformationControl;
import org.eclipse.jface.text.AbstractReusableInformationControlCreator;
import org.eclipse.jface.text.DefaultInformationControl;
import org.eclipse.jface.text.IInformationControl;
Expand Down Expand Up @@ -70,10 +71,10 @@ public boolean canReuse(IInformationControl control) {
return false;
}
Iterator<Entry<ITextHover, IInformationControlCreator>> thisIterator = this.creators.entrySet().iterator();
Iterator<Entry<ITextHover, IInformationControl>> otherIterator = other.controls.entrySet().iterator();
Iterator<Entry<ITextHover, AbstractInformationControl>> otherIterator = other.controls.entrySet().iterator();
do {
Entry<ITextHover, IInformationControlCreator> thisEntry = thisIterator.next();
Entry<ITextHover, IInformationControl> otherEntry = otherIterator.next();
Entry<ITextHover, AbstractInformationControl> otherEntry = otherIterator.next();
if (!thisEntry.getKey().equals(otherEntry.getKey())) {
return false;
}
Expand Down Expand Up @@ -117,7 +118,9 @@ public boolean canReplace(IInformationControlCreator creator) {

@Override
public IInformationControl createInformationControl(Shell parent) {
return new CompositeInformationControl(parent, this.creators);
return creators.size() == 1 ? //
creators.values().iterator().next().createInformationControl(parent) : //
new CompositeInformationControl(parent, this.creators);
}

}

0 comments on commit e93878e

Please sign in to comment.