1 | |
|
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.widgets.Event; |
18 | |
import org.eclipse.swtbot.swt.finder.ReferenceBy; |
19 | |
import org.eclipse.swtbot.swt.finder.SWTBotWidget; |
20 | |
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; |
21 | |
import org.eclipse.swtbot.swt.finder.utils.MessageFormat; |
22 | |
import org.eclipse.swtbot.swt.finder.utils.SWTUtils; |
23 | |
import org.eclipse.swtbot.swt.finder.utils.internal.Assert; |
24 | |
import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot; |
25 | |
import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBotControl; |
26 | |
import org.eclipse.ui.forms.widgets.Hyperlink; |
27 | |
import org.hamcrest.SelfDescribing; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
@SWTBotWidget(clasz = Hyperlink.class, preferredName = "hyperlink", referenceBy = { ReferenceBy.MNEMONIC }) |
36 | |
public class SWTBotHyperlink extends AbstractSWTBotControl<Hyperlink> { |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public SWTBotHyperlink(Hyperlink w) throws WidgetNotFoundException { |
45 | 1 | super(w); |
46 | 1 | } |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public SWTBotHyperlink(Hyperlink w, SelfDescribing description) throws WidgetNotFoundException { |
56 | 1 | super(w, description); |
57 | 1 | } |
58 | |
|
59 | |
public AbstractSWTBot<Hyperlink> click() { |
60 | 0 | return click(true); |
61 | |
} |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
public AbstractSWTBot<Hyperlink> click(String hyperlinkText) { |
70 | 0 | log.debug(MessageFormat.format("Clicking on {0}", SWTUtils.getText(widget))); |
71 | 0 | String text = getText(); |
72 | 0 | boolean isText = text.contains(">" + hyperlinkText + "<"); |
73 | 0 | Assert.isLegal(isText, "Link does not contain text (" + hyperlinkText + "). It contains (" + text + ")"); |
74 | |
|
75 | 0 | hyperlinkText = extractHyperlinkTextOrHREF(hyperlinkText, text); |
76 | 0 | notify(SWT.Selection, createHyperlinkEvent(hyperlinkText)); |
77 | |
|
78 | 0 | log.debug(MessageFormat.format("Clicked on {0}", SWTUtils.getText(widget))); |
79 | 0 | return click(true); |
80 | |
} |
81 | |
|
82 | |
private String extractHyperlinkTextOrHREF(String hyperlinkText, String text) { |
83 | 0 | Pattern pattern = Pattern.compile(".*<[aA] [hH][rR][eE][fF]\\s*=\\s*['\"](.*)['\"]>" + hyperlinkText + "</[aA]>.*"); |
84 | 0 | Matcher matcher = pattern.matcher(text); |
85 | 0 | return matcher.find() ? matcher.group(1) : hyperlinkText; |
86 | |
} |
87 | |
|
88 | |
private Event createHyperlinkEvent(String hyperlinkText) { |
89 | 0 | Event e = createEvent(); |
90 | 0 | e.text = hyperlinkText; |
91 | 0 | return e; |
92 | |
} |
93 | |
} |