Skip to content

Package: ECPStringLineWrapper

ECPStringLineWrapper

nameinstructionbranchcomplexitylinemethod
ECPStringLineWrapper()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getPriority()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
modifyString(String, EStructuralFeature.Setting)
M: 0 C: 37
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
wrapLine(String)
M: 1 C: 72
99%
M: 2 C: 8
80%
M: 2 C: 4
67%
M: 1 C: 17
94%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2014 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Eugen - initial API and implementation
13: * Johannes Faltermeier - Bug 470478, Bug 459998
14: ******************************************************************************/
15: package org.eclipse.emf.ecp.internal.ui.view.linewrapper;
16:
17: import org.eclipse.emf.ecore.EStructuralFeature.Setting;
18: import org.eclipse.emf.ecp.view.spi.provider.ECPStringModifier;
19:
20: /**
21: * An {@link ECPStringModifier} which wraps texts automatically after 80 chars.
22: *
23: * @author Eugen Neufeld
24: * @author Johannes Faltermeier
25: *
26: */
27: public class ECPStringLineWrapper implements ECPStringModifier {
28:
29:         private static final int MAX_LINE_LENGTH = 80;
30:
31:         /**
32:          * {@inheritDoc}
33:          *
34:          * @see org.eclipse.emf.ecp.view.spi.provider.ECPStringModifier#modifyString(java.lang.String,
35:          * org.eclipse.emf.ecore.EStructuralFeature.Setting)
36:          */
37:         @Override
38:         public String modifyString(String text, Setting setting) {
39:                 final String[] textLines = text.split("\\r?\\n"); //$NON-NLS-1$
40:
41:                 final StringBuilder allLines = new StringBuilder();
42:•                for (int j = 0; j < textLines.length; j++) {
43:                         final String line = textLines[j];
44:•                        if (j != 0) {
45:                                 allLines.append("\n"); //$NON-NLS-1$
46:                         }
47:                         final String wrappedLine = wrapLine(line);
48:                         allLines.append(wrappedLine);
49:                 }
50:
51:                 return allLines.toString();
52:         }
53:
54:         private String wrapLine(final String line) {
55:                 final StringBuilder sb = new StringBuilder(line);
56:                 int i = 0;
57:•                while (i + MAX_LINE_LENGTH < sb.length()) {
58:                         i = sb.lastIndexOf("\n", i + MAX_LINE_LENGTH); //$NON-NLS-1$
59:•                        if (i == -1) {
60:                                 i = 0;
61:                         }
62:                         i = sb.lastIndexOf(" ", i + MAX_LINE_LENGTH); //$NON-NLS-1$
63:                         int multiplicator = 2;
64:•                        while (i == -1) {
65:                                 i = sb.lastIndexOf(" ", i + multiplicator * MAX_LINE_LENGTH); //$NON-NLS-1$
66:                                 multiplicator++;
67:•                                if (multiplicator * MAX_LINE_LENGTH > sb.length()) {
68:                                         break;
69:                                 }
70:                         }
71:•                        if (i == -1) {
72:                                 break;
73:                         }
74:                         sb.replace(i, i + 1, "\n"); //$NON-NLS-1$
75:                 }
76:                 final String wrappedLine = sb.toString();
77:                 return wrappedLine;
78:         }
79:
80:         /**
81:          * {@inheritDoc}
82:          *
83:          * @see org.eclipse.emf.ecp.view.spi.provider.ECPStringModifier#getPriority()
84:          */
85:         @Override
86:         public double getPriority() {
87:                 return 0;
88:         }
89:
90: }