1 | package com.framsticks.framclipse.ui.contentassist; |
---|
2 | |
---|
3 | import java.util.Map; |
---|
4 | import java.util.Map.Entry; |
---|
5 | import java.util.Set; |
---|
6 | |
---|
7 | import org.eclipse.jface.viewers.StyledString; |
---|
8 | import org.eclipse.jface.viewers.StyledString.Styler; |
---|
9 | import org.eclipse.swt.SWT; |
---|
10 | import org.eclipse.swt.graphics.TextStyle; |
---|
11 | import org.eclipse.swt.widgets.Display; |
---|
12 | |
---|
13 | public class HTMLDescriptorProvider implements DescriptorProvider { |
---|
14 | |
---|
15 | private static final Styler LEFT_STYLER = new ColorStyler(SWT.COLOR_BLACK); |
---|
16 | private static final Styler RIGHT_STYLER = new ColorStyler(SWT.COLOR_DARK_GRAY); |
---|
17 | private static final String INDENT = "<pre> </pre>"; |
---|
18 | |
---|
19 | @Override |
---|
20 | public StyledString display(String string) { |
---|
21 | int pointcut = string.indexOf(':'); |
---|
22 | if (pointcut < 0) { |
---|
23 | return new StyledString(string, LEFT_STYLER); |
---|
24 | } else { |
---|
25 | StyledString display = new StyledString(string.substring(0, pointcut), LEFT_STYLER); |
---|
26 | display.append(string.substring(pointcut, string.length()), RIGHT_STYLER); |
---|
27 | return display; |
---|
28 | } |
---|
29 | } |
---|
30 | |
---|
31 | @Override |
---|
32 | public String description(Map<String, String> description) { |
---|
33 | StringBuilder builder = new StringBuilder(); |
---|
34 | Set<Entry<String, String>> set = description.entrySet(); |
---|
35 | for (Entry<String, String> entry : set) { |
---|
36 | builder.append(wrap(entry.getKey(), entry.getValue())); |
---|
37 | } |
---|
38 | return builder.toString(); |
---|
39 | } |
---|
40 | |
---|
41 | private String wrap(String name, String content) { |
---|
42 | if (name != null && content != null && content.length() > 0) { |
---|
43 | String[] split = content.split("\n"); |
---|
44 | return "<b>" + name + ":</b><br/>" + indent(split); |
---|
45 | } else { |
---|
46 | return ""; |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | private String indent(String... content) { |
---|
51 | StringBuilder builder = new StringBuilder(); |
---|
52 | for (String string : content) { |
---|
53 | builder.append(INDENT).append(string).append("<br/>"); |
---|
54 | } |
---|
55 | return builder.toString(); |
---|
56 | } |
---|
57 | |
---|
58 | private static class ColorStyler extends Styler { |
---|
59 | |
---|
60 | private final int color; |
---|
61 | |
---|
62 | public ColorStyler(int color) { |
---|
63 | this.color = color; |
---|
64 | } |
---|
65 | |
---|
66 | @Override |
---|
67 | public void applyStyles(TextStyle textStyle) { |
---|
68 | textStyle.foreground = Display.getCurrent().getSystemColor(color); |
---|
69 | } |
---|
70 | |
---|
71 | } |
---|
72 | |
---|
73 | } |
---|