changedUpdate exception
[fedora-idea.git] / platform / platform-impl / src / com / intellij / ui / SplittingUtil.java
blob2a8ac3eaea768ff8545d3c8733e297d0a280ad5b
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.ui;
18 import com.intellij.util.ArrayUtil;
20 import java.awt.*;
21 import java.util.ArrayList;
23 public class SplittingUtil {
24 public static String[] splitText(String text, FontMetrics fontMetrics, int widthLimit, char separator){
25 ArrayList<String> lines = new ArrayList<String>();
26 String currentLine = "";
27 StringBuffer currentAtom = new StringBuffer();
29 for (int i=0; i < text.length(); i++) {
30 char ch = text.charAt(i);
31 currentAtom.append(ch);
33 if (ch == separator) {
34 currentLine += currentAtom.toString();
35 currentAtom.setLength(0);
38 String s = currentLine + currentAtom.toString();
39 int width = fontMetrics.stringWidth(s);
41 if (width >= widthLimit - fontMetrics.charWidth('w')) {
42 if (currentLine.length() > 0) {
43 lines.add(currentLine);
44 currentLine = "";
46 else {
47 lines.add(currentAtom.toString());
48 currentAtom.setLength(0);
53 String s = currentLine + currentAtom.toString();
54 if (s.length() > 0) {
55 lines.add(s);
58 return ArrayUtil.toStringArray(lines);