mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noël Rouvignac
25.17.2016 01bf9b6a5d324d45355659581e9ebbd1280834fe
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2008-2010 Sun Microsystems, Inc.
 * Portions Copyright 2015-2016 ForgeRock AS.
 */
package org.opends.guitools.controlpanel.ui;
 
import static org.opends.messages.AdminToolMessages.*;
import static org.opends.messages.QuickSetupMessages.INFO_CLOSE_BUTTON_LABEL;
 
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.text.html.HTMLDocument;
 
import org.forgerock.i18n.LocalizableMessage;
import org.opends.guitools.controlpanel.datamodel.ControlPanelInfo;
import org.opends.guitools.controlpanel.event.ConfigurationChangeEvent;
import org.opends.guitools.controlpanel.event.PrintStreamListener;
import org.opends.guitools.controlpanel.ui.components.BasicExpander;
import org.opends.guitools.controlpanel.util.ApplicationPrintStream;
import org.opends.guitools.controlpanel.util.Utilities;
 
/** The dialog that is used to display progress in a task. */
public class ProgressDialog extends GenericDialog
{
  private static final long serialVersionUID = -6462866257463062629L;
  private ProgressPanel progressPanel;
 
  /**
   * Constructor of the dialog.
   * @param parentFrame the parent frame.
   * @param relativeTo the component to use as reference to set the position
   * of this dialog.
   * @param title the title of the dialog.
   * @param info the control panel information.
   */
  public ProgressDialog(JFrame parentFrame, Component relativeTo,
      LocalizableMessage title, ControlPanelInfo info)
  {
    super(parentFrame, getPanel(info));
    Utilities.centerGoldenMean(this, relativeTo);
    setTitle(title.toString());
    progressPanel = (ProgressPanel)panel;
    getRootPane().setDefaultButton(progressPanel.closeButton);
  }
 
  /**
   * Creates the panel that will be contained in the dialog.
   * @param info the control panel information.
   * @return the panel that will be contained in the dialog.
   */
  private static StatusGenericPanel getPanel(ControlPanelInfo info)
  {
    ProgressPanel panel = new ProgressPanel();
    panel.setInfo(info);
    return panel;
  }
 
  /**
   * Adds two print stream listeners.
   * @param outPrintStream the output stream listener.
   * @param errorPrintStream the error stream listener.
   */
  public void addPrintStreamListeners(ApplicationPrintStream outPrintStream,
      ApplicationPrintStream errorPrintStream)
  {
    errorPrintStream.addListener(new PrintStreamListener()
    {
      @Override
      public void newLine(final String msg)
      {
        SwingUtilities.invokeLater(new Runnable()
        {
          @Override
          public void run()
          {
            progressPanel.appendErrorLine(msg);
          }
        });
      }
    });
    outPrintStream.addListener(new PrintStreamListener()
    {
      @Override
      public void newLine(final String msg)
      {
        SwingUtilities.invokeLater(new Runnable()
        {
          @Override
          public void run()
          {
            progressPanel.appendOutputLine(msg);
          }
        });
      }
    });
  }
 
  /**
   * Returns the progress bar of the dialog.
   * @return the progress bar of the dialog.
   */
  public JProgressBar getProgressBar()
  {
    return progressPanel.getProgressBar();
  }
 
  /**
   * Appends some text in HTML format to the 'Details' section of the dialog.
   * @param text the text in HTML format to be appended.
   */
  public void appendProgressHtml(String text)
  {
    progressPanel.appendHtml(text);
  }
 
  /** Resets the contents of the 'Details' section of the dialog. */
  public void resetProgressLogs()
  {
    progressPanel.resetLogs();
  }
 
  /**
   * Sets the text to be displayed in the summary area of the progress
   * dialog.
   * @param text the text to be displayed.
   */
  public void setSummary(LocalizableMessage text)
  {
    progressPanel.setSummary(text);
  }
 
  @Override
  public void setEnabledClose(boolean enable)
  {
    progressPanel.closeButton.setEnabled(enable);
  }
 
  /**
   * Note: this will make the dialog to be closed asynchronously.  So that
   * sequential calls to setTaskIsOver(true) and setTaskIsOver(false) on the
   * event thread are guaranteed not to close the dialog.
   * @param taskIsOver whether the task is finished or not.
   */
  public void setTaskIsOver(boolean taskIsOver)
  {
    progressPanel.taskIsOver = taskIsOver;
    progressPanel.closeWhenOverClicked();
  }
 
  /** The panel contained in the progress dialog. */
  private static class ProgressPanel extends StatusGenericPanel
  {
    private static final long serialVersionUID = -364496083928260306L;
    private BasicExpander details;
    private JEditorPane logs;
    private JScrollPane scroll;
    private JCheckBox closeWhenOver;
    private final String LASTID = "lastid";
    private final String INIT_TEXT = "<span id=\""+LASTID+
    "\" style=\"bold\">&nbsp;</span>";
    private JProgressBar progressBar;
    private Component extraStrut;
    private JButton closeButton;
    private static final String FAKE_PROGRESS_TEXT =
      "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"+
      "<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>"+
      "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
    private int heightDiff;
    private int lastCollapsedHeight = -1;
    private int lastExpandedHeight = -1;
 
    private static boolean lastShowDetails;
    private static boolean lastCloseWhenOver;
 
    private boolean taskIsOver;
 
    /** Default constructor. */
    public ProgressPanel()
    {
      super();
      createLayout();
    }
 
    @Override
    public LocalizableMessage getTitle()
    {
      return null;
    }
 
    @Override
    public boolean requiresScroll()
    {
      return false;
    }
 
    @Override
    public boolean requiresBorder()
    {
      return false;
    }
 
    @Override
    public boolean isDisposeOnClose()
    {
      return true;
    }
 
    /**
     * Appends a line to the logs (Details are) section of the panel.  The text
     * will have a new-line char at the end (is similar to println()).
     * @param msg the HTML formatted text to be appended.
     */
    private void appendErrorLine(String msg)
    {
      msg = filterForBugID4988885(msg+"<br>");
      msg = Utilities.applyFont(msg, ColorAndFontConstants.progressFont);
      appendHtml(msg);
    }
 
    /**
     * Sets the text to be displayed in the summary area of the progress
     * dialog.
     * @param msg the text to be displayed.
     */
    public void setSummary(LocalizableMessage msg)
    {
      errorPane.setText(msg.toString());
 
      if (!details.isSelected() && isVisible())
      {
        LocalizableMessage wrappedText = Utilities.wrapHTML(msg, 70);
        JEditorPane pane = new JEditorPane();
        pane.setContentType("text/html");
        pane.setText(wrappedText.toString());
        ProgressDialog dlg = (ProgressDialog)Utilities.getParentDialog(this);
        int width = Math.max(pane.getPreferredSize().width + 40,
        dlg.getWidth());
        int height = Math.max(pane.getPreferredSize().height + 40 +
            extraStrut.getHeight() + details.getPreferredSize().height,
        dlg.getHeight());
        // We might want to resize things.
        if (width > dlg.getWidth() || height > dlg.getHeight())
        {
          Dimension newDim = new Dimension(width, height);
          dlg.setSize(newDim);
        }
      }
    }
 
    /**
     * Appends a line to the logs (Details are) section of the panel.  The text
     * will be preceded by a new line (is similar to println()).
     * @param msg the HTML formatted text to be appended.
     */
    private void appendOutputLine(String msg)
    {
      appendErrorLine(msg);
    }
 
    /**
     * Appends text to the logs (Details are) section of the panel.  The text
     * will be appended as it is (is similar to print()).
     * @param msg the HTML formatted text to be appended.
     */
    private void appendHtml(String msg)
    {
      HTMLDocument doc = (HTMLDocument)logs.getDocument();
 
      try
      {
        msg = filterForBugID4988885(msg);
        doc.insertBeforeStart(doc.getElement(LASTID), msg);
      }
      catch (Throwable t)
      {
        // Bug
        t.printStackTrace();
      }
    }
 
    /** Resets the contents of the logs (Details) section. */
    private void resetLogs()
    {
      logs.setText(INIT_TEXT);
    }
 
    /** Creates the layout of the panel (but the contents are not populated here). */
    private void createLayout()
    {
      GridBagConstraints gbc = new GridBagConstraints();
      addErrorPane(gbc);
 
      errorPane.setVisible(true);
      errorPane.setText(Utilities.applyFont(
              INFO_CTRL_PANEL_PLEASE_WAIT_SUMMARY.get(),
              ColorAndFontConstants.defaultFont));
 
      gbc.anchor = GridBagConstraints.WEST;
      gbc.gridwidth = 1;
      gbc.gridx = 0;
      gbc.gridy = 1;
 
      progressBar = new JProgressBar();
      progressBar.setMaximum(100);
      gbc.weightx = 1.0;
      gbc.fill = GridBagConstraints.HORIZONTAL;
      gbc.insets = new Insets(10, 20, 0, 30);
      add(progressBar, gbc);
 
      gbc.insets.top = 10;
      gbc.insets.bottom = 5;
      details =
        new BasicExpander(INFO_CTRL_PANEL_PROGRESS_DIALOG_DETAILS_LABEL.get());
      gbc.gridy ++;
      add(details, gbc);
 
      logs = Utilities.makeHtmlPane(FAKE_PROGRESS_TEXT,
          ColorAndFontConstants.progressFont);
      gbc.gridy ++;
      gbc.weighty = 1.0;
      gbc.fill = GridBagConstraints.BOTH;
      gbc.insets.top = 5;
      gbc.insets.right = 20;
      gbc.insets.bottom = 5;
      scroll = Utilities.createScrollPane(logs);
      scroll.setOpaque(false);
      scroll.getViewport().setOpaque(false);
      add(scroll, gbc);
      Dimension scrollDim = scroll.getPreferredSize();
 
      gbc.weighty = 1.0;
      extraStrut = Box.createRigidArea(new Dimension(scrollDim.width, 50));
      add(extraStrut, gbc);
      gbc.gridy ++;
      gbc.weighty = 0.0;
      add(Box.createHorizontalStrut(scrollDim.width), gbc);
 
      heightDiff = scrollDim.height - extraStrut.getHeight();
 
      logs.setText(INIT_TEXT);
 
      scroll.setPreferredSize(scrollDim);
 
      updateVisibility(lastShowDetails);
      details.addActionListener(new ActionListener()
      {
        @Override
        public void actionPerformed(ActionEvent ev)
        {
          lastShowDetails = details.isSelected();
          updateVisibility(lastShowDetails);
        }
      });
 
      // The button panel
      gbc.gridy ++;
      gbc.weighty = 0.0;
      gbc.insets = new Insets(0, 0, 0, 0);
      add(createButtonsPanel(), gbc);
    }
 
    private JPanel createButtonsPanel()
    {
      JPanel buttonsPanel = new JPanel(new GridBagLayout());
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = 0;
      gbc.gridy = 0;
      gbc.anchor = GridBagConstraints.WEST;
      gbc.fill = GridBagConstraints.HORIZONTAL;
      gbc.gridwidth = 1;
      gbc.gridy = 0;
      closeWhenOver = Utilities.createCheckBox(
          INFO_CTRL_PANEL_CLOSE_WINDOW_WHEN_OPERATION_COMPLETES_LABEL.get());
      closeWhenOver.setOpaque(false);
      closeWhenOver.addActionListener(new ActionListener()
      {
        @Override
        public void actionPerformed(ActionEvent ev)
        {
          closeWhenOverClicked();
        }
      });
      closeWhenOver.setSelected(lastCloseWhenOver);
      gbc.insets = new Insets(10, 10, 10, 10);
      buttonsPanel.add(closeWhenOver, gbc);
      gbc.weightx = 1.0;
      gbc.gridx ++;
      buttonsPanel.add(Box.createHorizontalStrut(150));
      buttonsPanel.add(Box.createHorizontalGlue(), gbc);
      buttonsPanel.setOpaque(true);
      buttonsPanel.setBackground(ColorAndFontConstants.greyBackground);
      gbc.gridx ++;
      gbc.weightx = 0.0;
      buttonsPanel.add(Box.createHorizontalStrut(100));
      gbc.gridx ++;
      closeButton = Utilities.createButton(INFO_CLOSE_BUTTON_LABEL.get());
      closeButton.setOpaque(false);
      gbc.gridx ++;
      gbc.insets.left = 5;
      gbc.insets.right = 10;
      buttonsPanel.add(closeButton, gbc);
      closeButton.addActionListener(new ActionListener()
      {
        @Override
        public void actionPerformed(ActionEvent ev)
        {
          closeClicked();
        }
      });
 
      buttonsPanel.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0,
          ColorAndFontConstants.defaultBorderColor));
 
      return buttonsPanel;
    }
 
    private void updateVisibility(boolean showDetails)
    {
      scroll.setVisible(showDetails);
      extraStrut.setVisible(!showDetails);
      details.setSelected(showDetails);
 
      final Window dialog = Utilities.getParentDialog(this);
      if (dialog != null)
      {
        final Runnable repaint = new Runnable()
        {
          @Override
          public void run()
          {
            invalidate();
            dialog.invalidate();
            dialog.repaint();
          }
        };
 
        final Dimension dialogSize = dialog.getSize();
        if (showDetails)
        {
          lastCollapsedHeight = dialogSize.height;
          if (lastExpandedHeight == -1)
          {
            dialog.setSize(new Dimension(dialogSize.width, dialogSize.height + heightDiff));
          }
          else
          {
            dialog.setSize(new Dimension(dialogSize.width, lastExpandedHeight));
          }
          SwingUtilities.invokeLater(repaint);
        }
        else
        {
          lastExpandedHeight = dialogSize.height;
          if (lastCollapsedHeight == -1)
          {
            packParentDialog();
          }
          else
          {
            dialog.setSize(new Dimension(dialogSize.width, lastCollapsedHeight));
            SwingUtilities.invokeLater(repaint);
          }
        }
      }
    }
 
    @Override
    public GenericDialog.ButtonType getButtonType()
    {
      return GenericDialog.ButtonType.NO_BUTTON;
    }
 
    @Override
    public void configurationChanged(ConfigurationChangeEvent ev)
    {
    }
 
    @Override
    public Component getPreferredFocusComponent()
    {
      return details;
    }
 
    @Override
    public void okClicked()
    {
      Utilities.getParentDialog(this).setVisible(false);
    }
 
    /**
     * Returns the progress bar of the dialog.
     * @return the progress bar of the dialog.
     */
    public JProgressBar getProgressBar()
    {
      return progressBar;
    }
 
    /**
     * Checks if the 'Close when over' check box is selected and if it is the
     * case, closes the dialog after waiting for 2 seconds (so that the user
     * can see the result, or cancel the automatic closing of the dialog).
     */
    private void closeWhenOverClicked()
    {
      lastCloseWhenOver = closeWhenOver.isSelected();
      if (lastCloseWhenOver && taskIsOver)
      {
        Thread t = new Thread(new Runnable()
        {
          @Override
          public void run()
          {
            try
            {
              Thread.sleep(2000);
              SwingUtilities.invokeLater(new Runnable()
              {
                @Override
                public void run()
                {
                  if (closeWhenOver.isSelected() && taskIsOver)
                  {
                    closeClicked();
                  }
                }
              });
            }
            catch (Throwable t)
            {
            }
          }
        });
        t.start();
      }
    }
  }
 
  /**
   * This is necessary because of bug 4988885.
   * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4988885
   * @param msg the message.
   * @return the message filtered.
   */
  private static String filterForBugID4988885(String msg)
  {
    return msg.replaceAll("<br>", "&#10;<br>");
  }
}