Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8ef43bf6659e4c69318bb72fecdf16ae87bcb060 (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
/***************************************************************************
 * Copyright (c) 2004 - 2007 Eike Stepper, Germany.
 * 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:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/
package org.eclipse.net4j.ui.widgets;

import org.eclipse.net4j.util.io.IORuntimeException;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.TextStyle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Eike Stepper
 */
public class LogDialog extends BaseDialog
{
  private StringBuilder log = new StringBuilder();

  private StyledText text;

  private TextStyle textStyle;

  private List<StyleRange> styleRanges = new ArrayList();

  private StyleRange currentStyleRange = new StyleRange();

  private StyleRange lastStyleRange;

  private Font font;

  public LogDialog(Shell parentShell, int shellStyle, String title, String message, IDialogSettings settings)
  {
    super(parentShell, shellStyle, title, message, settings);
  }

  public LogDialog(Shell parentShell, String title, String message, IDialogSettings settings)
  {
    this(parentShell, DEFAULT_SHELL_STYLE, title, message, settings);
  }

  public TextStyle getTextStyle()
  {
    return textStyle;
  }

  public void setTextStyle(TextStyle textStyle)
  {
    if (textStyle == null)
    {
      throw new IllegalArgumentException("textStyle == null");
    }

    if (textStyle.equals(this.textStyle))
    {
      return;
    }

    lastStyleRange = currentStyleRange;
    this.textStyle = textStyle;

    currentStyleRange = new StyleRange();
    currentStyleRange.start = log.length();
    currentStyleRange.rise = textStyle.rise;
    currentStyleRange.background = textStyle.background;
    currentStyleRange.font = textStyle.font;
    currentStyleRange.foreground = textStyle.foreground;
    currentStyleRange.metrics = textStyle.metrics;
    currentStyleRange.strikeout = textStyle.strikeout;
    currentStyleRange.underline = textStyle.underline;
  }

  public void append(String text)
  {
    checkStyleRange();
    log.append(text);
    currentStyleRange.length += text.length();
  }

  public void append(Throwable t)
  {
    try
    {
      checkStyleRange();
      ByteArrayOutputStream bytes = new ByteArrayOutputStream();
      String message = t.getMessage() + "\n";
      bytes.write(message.getBytes());
      t.printStackTrace(new PrintStream(bytes));

      String text = bytes.toString();
      log.append(text);
      currentStyleRange.length += text.length();
    }
    catch (IOException ex)
    {
      throw new IORuntimeException(ex);
    }
  }

  @Override
  public boolean close()
  {
    font.dispose();
    return super.close();
  }

  @Override
  protected void createUI(Composite parent)
  {
    GridLayout grid = new GridLayout();
    grid.marginTop = 6;
    grid.marginLeft = 6;
    grid.marginRight = 6;
    grid.marginBottom = 6;

    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(grid);
    composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    font = new Font(getShell().getDisplay(), "Courier New", 9, SWT.NORMAL);
    checkStyleRange();
    lastStyleRange = currentStyleRange;
    checkStyleRange();

    text = new StyledText(composite, SWT.MULTI | SWT.READ_ONLY | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    text.setText(log.toString());
    text.setStyleRanges(styleRanges.toArray(new StyleRange[styleRanges.size()]));
    text.setBackground(getShell().getDisplay().getSystemColor(SWT.COLOR_WHITE));
    text.setFont(font);
  }

  @Override
  protected void createButtonsForButtonBar(Composite parent)
  {
    createButton(parent, IDialogConstants.OK_ID, IDialogConstants.CLOSE_LABEL, true);
  }

  private void checkStyleRange()
  {
    if (lastStyleRange != null)
    {
      styleRanges.add(lastStyleRange);
      lastStyleRange = null;
    }
  }
}

Back to the top