Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6030d34924af4c3c8103a48ed00b5ee4c735eba6 (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
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * 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:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.ui.swt;

import java.util.ArrayList;
import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

/**
 * @author Roberto E. Escobar
 */
public class FormattedText extends Composite {

   private final List<StyleRange> styleArray;
   private StyledText textArea;
   private final int height;
   private final int width;
   private final boolean editable;

   public FormattedText(Composite parent, int style) {
      this(parent, style, 300, 300, false);
   }

   public FormattedText(Composite parent, int style, boolean editable) {
      this(parent, style, 300, 300, editable);
   }

   public FormattedText(Composite parent, int style, int height, int width) {
      this(parent, style, height, width, false);
   }

   public FormattedText(Composite parent, int style, int height, int width, boolean editable) {
      super(parent, style);
      this.editable = editable;
      this.height = height;
      this.width = width;
      this.styleArray = new ArrayList<>();
      createTextArea();
   }

   private void createTextArea() {
      this.setLayout(new GridLayout());
      this.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

      textArea = new StyledText(this, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
      GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
      gd.heightHint = height;
      gd.widthHint = width;
      textArea.setLayoutData(gd);
      textArea.setEditable(editable);
      textArea.setBackground(Displays.getSystemColor(SWT.COLOR_WHITE));
      textArea.setBackground(Displays.getSystemColor(SWT.COLOR_GRAY));
      textArea.setText("");
   }

   public String[] getCmdList() {
      return textArea.getText().split("\n");
   }

   public void setTextAreaBackground(final int swtColor) {
      textArea.setBackground(Displays.getSystemColor(swtColor));
   }

   public StyledText getStyledText() {
      return textArea;
   }

   public void clearTextArea() {
      textArea.setText("");
      styleArray.clear();
   }

   public void addText(String textToAdd) {
      addText(textToAdd, SWT.NORMAL, SWT.COLOR_BLACK);
   }

   public void addText(String textToAdd, int swtFontStyle, int swtColor) {
      addText(textToAdd, swtFontStyle, swtColor, false);
   }

   public void addText(String textToAdd, int swtFontStyle, int swtColor, boolean underline) {
      if (textToAdd != null) {
         String temp = textArea.getText();
         int startIndex = temp.length();
         temp += textToAdd;
         textArea.setText(temp);
         StyleRange tempStyle = new StyleRange();
         styleArray.add(tempStyle);
         tempStyle.fontStyle = swtFontStyle;
         tempStyle.start = startIndex;
         tempStyle.length = textToAdd.length();
         tempStyle.underline = underline;
         tempStyle.foreground = Displays.getSystemColor(swtColor);
         textArea.setStyleRanges(styleArray.toArray(new StyleRange[styleArray.size()]));
         textArea.redraw();
      }
   }

   @Override
   public void dispose() {
      super.dispose();
      textArea.dispose();
   }
}

Back to the top