Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2024fe915efa53f9217bc4ae5443c0096ea7aa36 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*******************************************************************************
 * Copyright (c) 2012, 2014 Obeo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.compare.provider.utils;

import org.eclipse.emf.common.util.URI;

/**
 * Utility class that mimics the JFace's StyledString.
 * 
 * @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
 */
public interface IStyledString {

	/**
	 * Returns the Java string for this styled string.
	 * 
	 * @return the Java string for this styled string.
	 */
	String getString();

	/**
	 * Returns the style associated with this string.
	 * 
	 * @return the style associated with this string.
	 */
	Style getStyle();

	/**
	 * An iterable of {@link IStyledString}. It is appendable.
	 * 
	 * @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
	 */
	interface IComposedStyledString extends Iterable<IStyledString> {

		/**
		 * Append the given string with no style.
		 * 
		 * @param str
		 *            the string to append.
		 * @return this instance to let you chain the calls.
		 */
		IComposedStyledString append(String str);

		/**
		 * Append the given string with the given style.
		 * 
		 * @param str
		 *            the string to append.
		 * @param style
		 *            the style of the appended string.
		 * @return this instance to let you chain the calls.
		 */
		IComposedStyledString append(String str, Style style);

		/**
		 * Appends the given composed styled string to this.
		 * 
		 * @param composedStyledString
		 *            the styled string to append.
		 * @return this instance.
		 * @since 4.0
		 */
		IComposedStyledString append(IComposedStyledString composedStyledString);

		/**
		 * Returns the Java string for this composed styled string.
		 * 
		 * @return the Java string for this composed styled string.
		 */
		String getString();
	}

	/**
	 * A style class for {@link IStyledString}.
	 * 
	 * @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
	 */
	public static final class Style {

		/** Instance that represent a default unstyled string. */
		public static final Style NO_STYLE = new Style(null, null, null, false, null, null, null, null, null);

		/** Constant that will eventually map to org.eclipse.jface.viewers.StyledString.QUALIFIER_STYLER. */
		public static final Style QUALIFIER_STYLER = new Style(null, null, null, false, null, null, null,
				null, null);

		/** Constant that will eventually map to org.eclipse.jface.viewers.StyledString.COUNTER_STYLER. */
		public static final Style COUNTER_STYLER = new Style(null, null, null, false, null, null, null, null,
				null);

		/** Constant that will eventually map to org.eclipse.jface.viewers.StyledString.DECORATIONS_STYLER. */
		public static final Style DECORATIONS_STYLER = new Style(null, null, null, false, null, null, null,
				null, null);

		/** The font URI. */
		private final URI font;

		/** The background color URI. */
		private final URI backgroundColor;

		/** The foreground color URI. */
		private final URI foregroundColor;

		/** Is the string strikedout. */
		private final boolean isStrikedout;

		/** The strikeout color URI. */
		private final URI strikeoutColor;

		/** The style of the underline. */
		private final UnderLineStyle underlineStyle;

		/** The underline color URI. */
		private final URI underlineColor;

		/** The border style. */
		private final BorderStyle borderStyle;

		/** The border color URI. */
		private final URI borderColor;

		/**
		 * Creates a new instance with the given values for each component of this style.
		 * 
		 * @param font
		 *            the font
		 * @param backgroundColor
		 *            the background color
		 * @param foregroundColor
		 *            the foreground color
		 * @param isStrikedout
		 *            whether to strikedout the text
		 * @param strikeoutColor
		 *            the color of the strikedout
		 * @param underlineStyle
		 *            the style of the underline
		 * @param underlineColor
		 *            the color of the underline
		 * @param borderStyle
		 *            the style of the border
		 * @param borderColor
		 *            the color of the border
		 */
		// CHECKSTYLE:OFF private method
		private Style(URI font, URI backgroundColor, URI foregroundColor, boolean isStrikedout,
				URI strikeoutColor, UnderLineStyle underlineStyle, URI underlineColor,
				BorderStyle borderStyle, URI borderColor) {
			// CHECKSTYLE:ON
			this.font = font;
			this.backgroundColor = backgroundColor;
			this.foregroundColor = foregroundColor;
			this.isStrikedout = isStrikedout;
			this.strikeoutColor = strikeoutColor;
			this.underlineStyle = underlineStyle;
			this.underlineColor = underlineColor;
			this.borderStyle = borderStyle;
			this.borderColor = borderColor;
		}

		public URI getFont() {
			return font;
		}

		public URI getBackgoundColor() {
			return backgroundColor;
		}

		public URI getForegroundColor() {
			return foregroundColor;
		}

		public boolean isStrikedout() {
			return isStrikedout;
		}

		public URI getStrikeoutColor() {
			return strikeoutColor;
		}

		public UnderLineStyle getUnderlineStyle() {
			return underlineStyle;
		}

		public URI getUnderlineColor() {
			return underlineColor;
		}

		public BorderStyle getBorderStyle() {
			return borderStyle;
		}

		public URI getBorderColor() {
			return borderColor;
		}

		/**
		 * The possible styles of the underline. These are those supported by JFace 3.8. Other versions or
		 * other widgets toolkit may not support all of these or may support more than these.
		 * 
		 * @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
		 */
		public static enum UnderLineStyle {
			/** No underline. */
			NONE,
			/** Single line. */
			SINGLE,
			/** Double line. */
			DOUBLE,
			/** Squiggle line. */
			SQUIGGLE,
			/** Error line (often dash line). */
			ERROR,
			/** Hyperlink. */
			LINK;
		}

		/**
		 * The possible styles of the border. These are those supported by JFace 3.8. Other versions or other
		 * widgets toolkit may not support all of these or may support more than these.
		 * 
		 * @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
		 */
		public static enum BorderStyle {
			/** No border. */
			NONE,
			/** Solid thin border. */
			SOLID,
			/** Dot thin border. */
			DOT,
			/** Dash thin border. */
			DASH;
		}

		/**
		 * Returns a new builder for the style.
		 * 
		 * @return a new builder for the style.
		 */
		public static StyleBuilder builder() {
			return new StyleBuilder();
		}

		/**
		 * A {@link Style} builder.
		 * 
		 * @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
		 */
		public static final class StyleBuilder {

			/** The black color URI constant. */
			private static final URI BLACK = URI.createURI("color://rgb/0/0/0"); //$NON-NLS-1$

			/** The font. */
			private URI font;

			/** The background color. */
			private URI backgroundColor;

			/** The foreground color. */
			private URI foregroundColor;

			/** Is the text strikedout. */
			private boolean isStrikedout;

			/** The strikedout color. */
			private URI strikeoutColor;

			/** The underline style. */
			private UnderLineStyle underlineStyle = UnderLineStyle.NONE;

			/** The underline color. */
			private URI underlineColor;

			/** The border style. */
			private BorderStyle borderStyle = BorderStyle.NONE;

			/** The border color. */
			private URI borderColor;

			/**
			 * Set the font.
			 * 
			 * @param pFont
			 *            the font to set.
			 * @return this.
			 */
			public StyleBuilder setFont(URI pFont) {
				this.font = pFont;
				return this;
			}

			/**
			 * Set the background color.
			 * 
			 * @param pBackgroundColor
			 *            the backgroundColor to set
			 * @return this.
			 */
			public StyleBuilder setBackgroundColor(URI pBackgroundColor) {
				this.backgroundColor = pBackgroundColor;
				return this;
			}

			/**
			 * Set the foreground color.
			 * 
			 * @param pForegroundColor
			 *            the forregroundColor to set
			 * @return this
			 */
			public StyleBuilder setForegroundColor(URI pForegroundColor) {
				this.foregroundColor = pForegroundColor;
				return this;
			}

			/**
			 * Set the strikedout.
			 * 
			 * @param pIsStrikedout
			 *            the isStrikedout to set
			 * @return this.
			 */
			public StyleBuilder setStrikedout(boolean pIsStrikedout) {
				this.isStrikedout = pIsStrikedout;
				if (strikeoutColor == null) {
					strikeoutColor = BLACK;
				}
				return this;
			}

			/**
			 * Set the strikeout color.
			 * 
			 * @param pStrikeoutColor
			 *            the strikeoutColor to set
			 * @return this.
			 */
			public StyleBuilder setStrikeoutColor(URI pStrikeoutColor) {
				this.strikeoutColor = pStrikeoutColor;
				isStrikedout = true;
				return this;
			}

			/**
			 * Set the border color.
			 * 
			 * @param pBorderColor
			 *            the borderColor to set
			 * @return this.
			 */
			public StyleBuilder setBorderColor(URI pBorderColor) {
				this.borderColor = pBorderColor;
				if (borderStyle == BorderStyle.NONE) {
					borderStyle = BorderStyle.SOLID;
				}
				return this;
			}

			/**
			 * Set the border color.
			 * 
			 * @param pBorderStyle
			 *            the borderStyle to set
			 * @return this.
			 */
			public StyleBuilder setBorderStyle(BorderStyle pBorderStyle) {
				this.borderStyle = pBorderStyle;
				if (borderColor == null) {
					borderColor = BLACK;
				}
				return this;
			}

			/**
			 * Set the underline color.
			 * 
			 * @param pUnderlineColor
			 *            the underlineColor to set
			 * @return this.
			 */
			public StyleBuilder setUnderlineColor(URI pUnderlineColor) {
				this.underlineColor = pUnderlineColor;
				if (underlineStyle == UnderLineStyle.NONE) {
					underlineStyle = UnderLineStyle.SINGLE;
				}
				return this;
			}

			/**
			 * Set the underline style.
			 * 
			 * @param pUnderlineStyle
			 *            the underlineStyle to set
			 * @return this.
			 */
			public StyleBuilder setUnderlineStyle(UnderLineStyle pUnderlineStyle) {
				this.underlineStyle = pUnderlineStyle;
				if (pUnderlineStyle == null) {
					underlineColor = BLACK;
				}
				return this;
			}

			/**
			 * Builds and returns a new Style instance regarding the values that have been given beforehands.
			 * 
			 * @return a new Style instance regarding the values that have been given beforehands.
			 */
			public Style build() {
				return new Style(font, backgroundColor, foregroundColor, isStrikedout, strikeoutColor,
						underlineStyle, underlineColor, borderStyle, borderColor);
			}
		}
	}
}

Back to the top