Skip to main content
summaryrefslogtreecommitdiffstats
blob: a157fba1e655bb579e9fee41452ee54b94ce6cc5 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 * Copyright (c) 2013 Eike Stepper (Berlin, Germany) and others.
 * 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.emf.cdo.releng.internal.setup.ui;

import org.eclipse.emf.cdo.releng.internal.setup.Activator;
import org.eclipse.emf.cdo.releng.internal.setup.SetupTaskPerformer;
import org.eclipse.emf.cdo.releng.setup.util.log.ProgressLog;
import org.eclipse.emf.cdo.releng.setup.util.log.ProgressLogProvider;
import org.eclipse.emf.cdo.releng.setup.util.log.ProgressLogRunnable;

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

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.progress.ProgressManager;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ProgressLogDialog extends TitleAreaDialog implements ProgressLog
{
  public static final String TITLE = "Setup Development Environment";

  private static final SimpleDateFormat TIME = new SimpleDateFormat("HH:mm:ss");

  private static final SimpleDateFormat DATE_TIME = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

  private PrintStream logStream;

  private Text text;

  private Button okButton;

  private Button cancelButton;

  private boolean cancelled;

  private String lastLine;

  private ProgressLogDialog(Shell parentShell, File logFile)
  {
    super(parentShell);
    if (logFile != null)
    {
      try
      {
        logFile.getParentFile().mkdirs();
        logStream = new PrintStream(new FileOutputStream(logFile, true));
      }
      catch (FileNotFoundException ex)
      {
        throw new RuntimeException(ex);
      }
    }

    setHelpAvailable(false);
    setShellStyle(SWT.BORDER | SWT.MAX | SWT.RESIZE | SWT.TITLE | SWT.APPLICATION_MODAL);
  }

  @Override
  protected Control createDialogArea(Composite parent)
  {
    setMessage("Please wait until the setup process is finished and the OK button is enabled...");
    setTitleImage(ResourceManager.getPluginImage("org.eclipse.emf.cdo.releng.setup", "icons/install_wiz.gif"));
    getShell().setText(TITLE);
    setTitle(TITLE);

    Composite area = (Composite)super.createDialogArea(parent);
    Composite container = new Composite(area, SWT.NONE);
    GridLayout gl_container = new GridLayout(1, false);
    gl_container.marginWidth = 10;
    gl_container.marginHeight = 10;
    container.setLayout(gl_container);
    container.setLayoutData(new GridData(GridData.FILL_BOTH));

    text = new Text(container, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
    text.setFont(SWTResourceManager.getFont("Courier New", 10, SWT.NORMAL));
    text.setEditable(false);
    text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    text.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));

    return area;
  }

  @Override
  protected void createButtonsForButtonBar(Composite parent)
  {
    okButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
    okButton.setEnabled(false);

    cancelButton = createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
    cancelButton.addSelectionListener(new SelectionAdapter()
    {
      @Override
      public void widgetSelected(SelectionEvent e)
      {
        cancelled = true;
        setFinished();
      }
    });
  }

  @Override
  public synchronized void create()
  {
    super.create();
    SetupTaskPerformer.setProgress(this);
    ProgressManager oldProgressProvider = ProgressManager.getInstance();
    ProgressLogProvider newProgressLogProvider = new ProgressLogProvider(this, oldProgressProvider);
    Job.getJobManager().setProgressProvider(newProgressLogProvider);
  }

  @Override
  public boolean close()
  {
    SetupTaskPerformer.setProgress(null);

    if (logStream != null)
    {
      logStream.println();
      logStream.println();
      logStream.println();
      logStream.println();
      IOUtil.closeSilent(logStream);
    }

    return super.close();
  }

  /**
   * Return the initial size of the dialog.
   */
  @Override
  protected Point getInitialSize()
  {
    return new Point(1000, 600);
  }

  public boolean isCancelled()
  {
    return cancelled;
  }

  public void log(String line)
  {
    if (isCancelled())
    {
      throw new OperationCanceledException();
    }

    if (line == null || line.length() == 0 || Character.isLowerCase(line.charAt(0)) || line.equals("Updating")
        || line.startsWith("Scanning Git") || line.startsWith("Re-indexing") || line.endsWith(" remaining.")
        || line.startsWith("Calculating Decorations") || line.startsWith("Decorating") || line.startsWith("http://")
        || line.startsWith("The user operation is waiting") || line.startsWith("Git repository changed")
        || line.startsWith("Refreshing ") || line.startsWith("Opening ") || line.startsWith("Connecting project ")
        || line.startsWith("Searching for associated repositories.") || line.startsWith("Preparing type ")
        || line.startsWith("Loading project description") || line.startsWith("Generating cspec from PDE artifacts")
        || line.startsWith("Reporting encoding changes") || line.startsWith("Saving")
        || line.startsWith("Downloading software") || line.startsWith("Java indexing..."))
    {
      return;
    }

    if (line.endsWith("/s)"))
    {
      int index = line.lastIndexOf(" (");
      if (index != -1)
      {
        line = line.substring(0, index);
      }
    }

    if (line.equals(lastLine))
    {
      return;
    }

    lastLine = line;

    final String message = line + "\n";
    final Date date = new Date();

    if (logStream != null)
    {
      try
      {
        logStream.print("[" + DATE_TIME.format(date) + "] " + message);
        logStream.flush();
      }
      catch (Exception ex)
      {
        Activator.log(ex);
      }
    }

    asyncExec(new Runnable()
    {
      public void run()
      {
        try
        {
          text.append("[" + TIME.format(date) + "] " + message);
        }
        catch (Exception ex)
        {
          //$FALL-THROUGH$
        }
      }
    });
  }

  public void setFinished()
  {
    Job.getJobManager().setProgressProvider(ProgressManager.getInstance());

    asyncExec(new Runnable()
    {
      public void run()
      {
        try
        {
          okButton.setEnabled(true);
          cancelButton.setEnabled(false);
        }
        catch (Exception ex)
        {
          //$FALL-THROUGH$
        }
      }
    });
  }

  private void asyncExec(Runnable runnable)
  {
    try
    {
      getShell().getDisplay().asyncExec(runnable);
    }
    catch (NullPointerException ex)
    {
      //$FALL-THROUGH$
    }
    catch (SWTException ex)
    {
      if (ex.code != SWT.ERROR_WIDGET_DISPOSED)
      {
        throw ex;
      }

      //$FALL-THROUGH$
    }
  }

  public static void run(Shell shell, File logFile, final String jobName, final ProgressLogRunnable runnable)
  {
    try
    {
      final boolean[] restart = { false };
      final ProgressLogDialog dialog = new ProgressLogDialog(shell, logFile);
      Runnable jobRunnable = new Runnable()
      {
        public void run()
        {
          Job job = new Job(jobName)
          {
            @Override
            protected IStatus run(IProgressMonitor monitor)
            {
              long start = System.currentTimeMillis();

              try
              {
                dialog.log(jobName);
                restart[0] = runnable.run(dialog);
              }
              catch (Exception ex)
              {
                Activator.log(ex);
                dialog.log("An error occured: " + ex.getMessage());
                dialog.log("The Error Log contains more infos...");
              }
              finally
              {
                long seconds = (System.currentTimeMillis() - start) / 1000;
                dialog.log("Took " + seconds + " seconds.");
                dialog.log("Press OK to close the dialog" + (restart[0] ? " and restart Eclipse" : "") + "...");
                dialog.setFinished();
              }

              return Status.OK_STATUS;
            }
          };

          job.schedule();

          if (dialog.open() == ProgressLogDialog.OK && restart[0])
          {
            PlatformUI.getWorkbench().restart();
          }
        }
      };

      if (Display.getCurrent() == shell.getDisplay())
      {
        jobRunnable.run();
      }
      else
      {
        shell.getDisplay().asyncExec(jobRunnable);
      }
    }
    catch (Exception ex)
    {
      Activator.log(ex);
    }
  }
}

Back to the top