| 1 | 0 | |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
package org.eclipse.swtbot.forms.finder.widgets; |
| 12 | |
|
| 13 | |
import java.util.regex.Matcher; |
| 14 | |
import java.util.regex.Pattern; |
| 15 | |
|
| 16 | |
import org.eclipse.swt.SWT; |
| 17 | |
import org.eclipse.swt.graphics.Image; |
| 18 | |
import org.eclipse.swt.widgets.Event; |
| 19 | |
import org.eclipse.swtbot.swt.finder.ReferenceBy; |
| 20 | |
import org.eclipse.swtbot.swt.finder.SWTBotWidget; |
| 21 | |
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; |
| 22 | |
import org.eclipse.swtbot.swt.finder.results.Result; |
| 23 | |
import org.eclipse.swtbot.swt.finder.utils.MessageFormat; |
| 24 | |
import org.eclipse.swtbot.swt.finder.utils.SWTUtils; |
| 25 | |
import org.eclipse.swtbot.swt.finder.utils.internal.Assert; |
| 26 | |
import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot; |
| 27 | |
import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBotControl; |
| 28 | |
import org.eclipse.ui.forms.widgets.ImageHyperlink; |
| 29 | |
import org.hamcrest.SelfDescribing; |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
@SWTBotWidget(clasz = ImageHyperlink.class, preferredName = "imageHyperlink", referenceBy = { ReferenceBy.MNEMONIC }) |
| 38 | |
public class SWTBotImageHyperlink extends AbstractSWTBotControl<ImageHyperlink> { |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
public SWTBotImageHyperlink(ImageHyperlink w) throws WidgetNotFoundException { |
| 47 | 2 | super(w); |
| 48 | 2 | } |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
public SWTBotImageHyperlink(ImageHyperlink w, SelfDescribing description) throws WidgetNotFoundException { |
| 58 | 2 | super(w, description); |
| 59 | 2 | } |
| 60 | |
|
| 61 | |
public AbstractSWTBot<ImageHyperlink> click() { |
| 62 | 0 | return click(true); |
| 63 | |
} |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
public Image image() { |
| 71 | 0 | return syncExec(new Result<Image>() { |
| 72 | |
public Image run() { |
| 73 | 0 | return widget.getImage(); |
| 74 | |
} |
| 75 | |
}); |
| 76 | |
} |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
public Image hoverImage() { |
| 84 | 0 | return syncExec(new Result<Image>() { |
| 85 | |
public Image run() { |
| 86 | 0 | return widget.getHoverImage(); |
| 87 | |
} |
| 88 | |
}); |
| 89 | |
} |
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
public Image activeImage() { |
| 97 | 0 | return syncExec(new Result<Image>() { |
| 98 | |
public Image run() { |
| 99 | 0 | return widget.getActiveImage(); |
| 100 | |
} |
| 101 | |
}); |
| 102 | |
} |
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
public AbstractSWTBot<ImageHyperlink> click(String hyperlinkText) { |
| 111 | 0 | log.debug(MessageFormat.format("Clicked on {0}", SWTUtils.getText(widget))); |
| 112 | 0 | String text = getText(); |
| 113 | 0 | boolean isText = text.contains(">" + hyperlinkText + "<"); |
| 114 | 0 | Assert.isLegal(isText, "Link does not contain text (" + hyperlinkText + "). It contains (" + text + ")"); |
| 115 | |
|
| 116 | 0 | hyperlinkText = extractHyperlinkTextOrHREF(hyperlinkText, text); |
| 117 | 0 | notify(SWT.Selection, createHyperlinkEvent(hyperlinkText)); |
| 118 | |
|
| 119 | 0 | log.debug(MessageFormat.format("Clicked on {0}", SWTUtils.getText(widget))); |
| 120 | 0 | return click(true); |
| 121 | |
} |
| 122 | |
|
| 123 | |
private String extractHyperlinkTextOrHREF(String hyperlinkText, String text) { |
| 124 | 0 | Pattern pattern = Pattern.compile(".*<[aA] [hH][rR][eE][fF]\\s*=\\s*['\"](.*)['\"]>" + hyperlinkText + "</[aA]>.*"); |
| 125 | 0 | Matcher matcher = pattern.matcher(text); |
| 126 | 0 | return matcher.find() ? matcher.group(1) : hyperlinkText; |
| 127 | |
} |
| 128 | |
|
| 129 | |
private Event createHyperlinkEvent(String hyperlinkText) { |
| 130 | 0 | Event e = createEvent(); |
| 131 | 0 | e.text = hyperlinkText; |
| 132 | 0 | return e; |
| 133 | |
} |
| 134 | |
} |