Skip to content

Commit

Permalink
JBIDE-21379 Enhance emulated link
Browse files Browse the repository at this point in the history
  • Loading branch information
scabanovich authored and fbricon committed Dec 22, 2015
1 parent 0dec17a commit e7dde41
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 111 deletions.
Expand Up @@ -37,11 +37,13 @@
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
Expand Down Expand Up @@ -123,7 +125,7 @@ protected void doCreateControls(final Composite parent, DataBindingContext dbc)
GridDataFactory.fillDefaults()
.align(SWT.LEFT, SWT.CENTER).span(3, 1).applyTo(signupLink);;
showHideSignupLink();
signupLink.addListener(SWT.MouseDown, onSignupLinkClicked());
StyledTextUtils.emulateLinkAction(signupLink, r->onSignupLinkClicked());
IObservableValue signupUrlObservable = BeanProperties.value(ConnectionWizardPageModel.PROPERTY_SIGNUPURL).observe(pageModel);
signupUrlObservable.addValueChangeListener(new IValueChangeListener() {

Expand All @@ -140,7 +142,7 @@ public void handleValueChange(ValueChangeEvent event) {
showHideUserdocLink();
IObservableValue userdocUrlObservable = BeanProperties
.value(ConnectionWizardPageModel.PROPERTY_USERDOCURL).observe(pageModel);
userdocLink.addSelectionListener(onUserdocLinkClicked(userdocUrlObservable));
StyledTextUtils.emulateLinkAction(userdocLink, r->onUserdocLinkClicked(userdocUrlObservable));
userdocUrlObservable.addValueChangeListener(new IValueChangeListener() {

@Override
Expand Down Expand Up @@ -336,39 +338,28 @@ public void focusLost(FocusEvent e) {
};
}

protected Listener onSignupLinkClicked() {
return new Listener() {
@Override
public void handleEvent(Event event) {
String signupUrl = pageModel.getSignupUrl();
if (StringUtils.isEmpty(signupUrl)) {
return;
}
new BrowserUtility().checkedCreateInternalBrowser(
signupUrl,
signupUrl,
OpenShiftCommonUIActivator.PLUGIN_ID,
OpenShiftCommonUIActivator.getDefault().getLog());
WizardUtils.close(getWizard());
}
};
protected void onSignupLinkClicked() {
String signupUrl = pageModel.getSignupUrl();
if (StringUtils.isEmpty(signupUrl)) {
return;
}
new BrowserUtility().checkedCreateInternalBrowser(
signupUrl,
signupUrl,
OpenShiftCommonUIActivator.PLUGIN_ID,
OpenShiftCommonUIActivator.getDefault().getLog());
WizardUtils.close(getWizard());
}

protected SelectionAdapter onUserdocLinkClicked(final IObservableValue userdocUrlObservable) {
return new SelectionAdapter() {

@Override
public void widgetSelected(SelectionEvent e) {
String userdocUrl = (String) userdocUrlObservable.getValue();
if (StringUtils.isEmpty(userdocUrl)) {
return;
}
new BrowserUtility().checkedCreateExternalBrowser(
userdocUrl,
OpenShiftCommonUIActivator.PLUGIN_ID,
OpenShiftCommonUIActivator.getDefault().getLog());
}
};
protected void onUserdocLinkClicked(final IObservableValue userdocUrlObservable) {
String userdocUrl = (String) userdocUrlObservable.getValue();
if (StringUtils.isEmpty(userdocUrl)) {
return;
}
new BrowserUtility().checkedCreateExternalBrowser(
userdocUrl,
OpenShiftCommonUIActivator.PLUGIN_ID,
OpenShiftCommonUIActivator.getDefault().getLog());
}

@Override
Expand Down
Expand Up @@ -20,7 +20,10 @@
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.jboss.tools.openshift.common.core.utils.StringUtils;
import org.jboss.tools.openshift.internal.common.ui.OpenShiftCommonImages;
Expand Down Expand Up @@ -53,6 +56,51 @@ public static StyledText emulateLinkWidget(String text, StyledText styledText) {
return styledText;
}

public static void emulateLinkAction(final StyledText styledText, EmulatedLinkClickListener listener) {
Listener mouseListener = new Listener() {
@Override
public void handleEvent(Event event) {
int offset = getOffsetAtEvent(styledText, event);
if(offset < 0) {
return;
}
StyleRange r = styledText.getStyleRangeAtOffset(offset);
if(event.type == SWT.MouseUp) {
if(r != null) {
listener.handleClick(r);
}
} else if(event.type == SWT.MouseMove) {
if(r != null) {
styledText.setCursor(styledText.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
} else {
styledText.setCursor(styledText.getDisplay().getSystemCursor(SWT.CURSOR_ARROW));
}
}

}
};
styledText.addListener(SWT.MouseMove, mouseListener);
styledText.addListener(SWT.MouseUp, mouseListener);
}

private static int getOffsetAtEvent(StyledText styledText, Event event) {
Point p = new Point(event.x, event.y);
int offset = -1;
try {
offset = styledText.getOffsetAtLocation(p);
} catch (IllegalArgumentException e) {
//ignore
}
if(offset < 0 || offset >= styledText.getCharCount()) {
return -1;
}
return offset;
}

public static interface EmulatedLinkClickListener {
public void handleClick(StyleRange range);
}

/**
* Sets a given text (with link markers <a></a>) to a given styled text.
* Applies a link-styled alike style range to the text within the markers
Expand Down
Expand Up @@ -125,7 +125,7 @@ public Composite createControls(Composite parent, Object context, DataBindingCon
if(authDetails != null) {
authDetails.getRequestTokenLink();
}
tokenRequestLink.addListener(SWT.MouseDown, onRetrieveLinkClicked(tokenRequestLink.getShell()));
StyledTextUtils.emulateLinkAction(tokenRequestLink, r->onRetrieveLinkClicked(tokenRequestLink.getShell()));
tokenRequestLink.setCursor(new Cursor(tokenRequestLink.getShell().getDisplay(), SWT.CURSOR_HAND));

//token
Expand Down Expand Up @@ -196,11 +196,7 @@ public String toString() {
return IAuthorizationContext.AUTHSCHEME_OAUTH;
}

private Listener onRetrieveLinkClicked(final Shell shell) {
return new Listener() {

@Override
public void handleEvent(Event event) {
private void onRetrieveLinkClicked(final Shell shell) {
if (StringUtils.isBlank(pageModel.getHost())) {
return;
}
Expand Down Expand Up @@ -247,8 +243,6 @@ public void run() {
} catch (InvocationTargetException | InterruptedException ex) {
showErrorDialog(shell,ex);
}
}
};
}

private void showErrorDialog(final Shell shell, final Throwable e) {
Expand Down
Expand Up @@ -322,14 +322,10 @@ public IStatus validate(Object value) {
GridDataFactory.fillDefaults()
.align(SWT.LEFT, SWT.CENTER).indent(8, 0)
.applyTo(manageProjectsLink);
manageProjectsLink.addListener(SWT.MouseDown, onManageProjectsClicked());
StyledTextUtils.emulateLinkAction(manageProjectsLink, r->onManageProjectsClicked());
}

private Listener onManageProjectsClicked() {
return new Listener() {

@Override
public void handleEvent(Event event) {
private void onManageProjectsClicked() {
try {
// run in job to enforce busy cursor which doesnt work otherwise
WizardUtils.runInWizard(new UIUpdatingJob("Opening projects wizard...") {
Expand Down Expand Up @@ -359,7 +355,5 @@ protected IStatus updateUI(IProgressMonitor monitor) {
} catch (InvocationTargetException | InterruptedException e) {
// swallow intentionnally
}
}
};
}
}

0 comments on commit e7dde41

Please sign in to comment.