Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3fd33e513682007310e0753cd028cc74aad0e709 (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
/*******************************************************************************
 * 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 static com.google.common.collect.Lists.newArrayList;

import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.compare.provider.utils.IStyledString.IComposedStyledString;
import org.eclipse.emf.compare.provider.utils.IStyledString.Style;

/**
 * Composed styled string implementation backed by an {@link java.util.ArrayList}.
 * 
 * @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
 */
public class ComposedStyledString implements IComposedStyledString {

	/** the backing list. */
	private final List<IStyledString> content;

	/** Creates a new empty instance. */
	public ComposedStyledString() {
		content = newArrayList();
	}

	/**
	 * Creates a new instance with the given text without style.
	 * 
	 * @param text
	 *            the text.
	 */
	public ComposedStyledString(String text) {
		this(text, Style.NO_STYLE);
	}

	/**
	 * Creates a new instance with the given text and the given style.
	 * 
	 * @param text
	 *            the text.
	 * @param style
	 *            the style of the text.
	 */
	public ComposedStyledString(String text, Style style) {
		this();
		append(text, style);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.provider.utils.IStyledString.IComposedStyledString#getString()
	 */
	public String getString() {
		StringBuilder sb = new StringBuilder();
		for (IStyledString styledString : this) {
			sb.append(styledString.getString());
		}
		return sb.toString();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.ide.ui.internal.util.IComposedStyledString#append(java.lang.String)
	 */
	public IComposedStyledString append(String str) {
		content.add(new StyledString(str, IStyledString.Style.NO_STYLE));
		return this;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.ide.ui.internal.util.IComposedStyledString#append(java.lang.String,
	 *      org.eclipse.emf.compare.ide.ui.internal.util.IComposedStyledString.Style)
	 */
	public IComposedStyledString append(String str, Style style) {
		content.add(new StyledString(str, style));
		return this;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.provider.utils.IStyledString.IComposedStyledString#append(org.eclipse.emf.compare.provider.utils.IStyledString.IComposedStyledString)
	 * @since 4.0
	 */
	public IComposedStyledString append(IComposedStyledString composedStyledString) {
		for (IStyledString styledString : composedStyledString) {
			content.add(styledString);
		}
		return this;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.lang.Iterable#iterator()
	 */
	public Iterator<IStyledString> iterator() {
		return content.iterator();
	}

	/**
	 * Private implementation of {@link IStyledString} backed by the tuple String and Style.
	 * 
	 * @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
	 */
	private static final class StyledString implements IStyledString {
		/** The string. */
		private final String str;

		/** The style. */
		private final Style style;

		/**
		 * Creates a new styled string.
		 * 
		 * @param str
		 *            the text value
		 * @param style
		 *            the style.
		 */
		StyledString(String str, Style style) {
			this.str = str;
			this.style = style;
		}

		/**
		 * Returns the string value.
		 * 
		 * @return the string value.
		 */
		public String getString() {
			return str;
		}

		/**
		 * Returns the style.
		 * 
		 * @return the style.
		 */
		public Style getStyle() {
			return style;
		}
	}
}

Back to the top