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

Jean-Noël Rouvignac
20.36.2016 2a3158aad80fc910b83336485b3e545dea50066c
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
/*
 * 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 2010 Sun Microsystems, Inc.
 * Portions Copyright 2011-2016 ForgeRock AS.
 */
 
package org.opends.quicksetup.installer.ui;
 
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Collection;
 
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.text.JTextComponent;
 
import org.opends.quicksetup.JavaArguments;
import org.opends.quicksetup.event.MinimumSizeComponentListener;
import org.opends.quicksetup.ui.UIFactory;
import org.opends.quicksetup.ui.Utilities;
import org.opends.quicksetup.util.BackgroundTask;
import org.opends.quicksetup.util.Utils;
import org.opends.server.util.SetupUtils;
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.LocalizableMessageBuilder;
 
import static org.opends.messages.QuickSetupMessages.*;
import static com.forgerock.opendj.cli.Utils.getThrowableMsg;
 
/**
 * This class is a dialog that appears when the user wants to configure
 * java parameters in the runtime settings panel.
 */
public class JavaArgumentsDialog extends JDialog
{
  private static final long serialVersionUID = -7950773258109643264L;
  private JLabel lInitialMemory;
  private JLabel lMaxMemory;
  private JLabel lOtherArguments;
 
  private JTextField tfInitialMemory;
  private JTextField tfMaxMemory;
  private JTextField tfOtherArguments;
 
  private JButton cancelButton;
  private JButton okButton;
 
  private boolean isCanceled = true;
 
  private LocalizableMessage message;
 
  private JavaArguments javaArguments;
 
  private JPanel inputContainer;
 
  private static final String INPUT_PANEL = "input";
  private static final String CHECKING_PANEL = "checking";
 
  private boolean isCheckingVisible;
 
  /**
   * Constructor of the JavaArgumentsDialog.
   * @param parent the parent frame for this dialog.
   * @param javaArguments the java arguments used to populate this dialog.
   * @param title the title of the dialog.
   * @param message the message to be displayed in top.
   * @throws IllegalArgumentException if options is null.
   */
  public JavaArgumentsDialog(JFrame parent, JavaArguments javaArguments,
      LocalizableMessage title, LocalizableMessage message)
  throws IllegalArgumentException
  {
    super(parent);
    if (javaArguments == null)
    {
      throw new IllegalArgumentException("javaArguments cannot be null.");
    }
    if (title == null)
    {
      throw new IllegalArgumentException("title cannot be null.");
    }
    if (message == null)
    {
      throw new IllegalArgumentException("message cannot be null.");
    }
    setTitle(title.toString());
    this.message = message;
    this.javaArguments = javaArguments;
    getContentPane().add(createPanel());
    pack();
 
    updateContents();
 
    int minWidth = (int) getPreferredSize().getWidth();
    int minHeight = (int) getPreferredSize().getHeight();
    addComponentListener(new MinimumSizeComponentListener(this, minWidth,
        minHeight));
    getRootPane().setDefaultButton(okButton);
 
    addWindowListener(new WindowAdapter()
    {
      @Override
      public void windowClosing(WindowEvent e)
      {
        cancelClicked();
      }
    });
    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
 
    Utilities.centerOnComponent(this, parent);
  }
 
  /**
   * Returns <CODE>true</CODE> if the user clicked on cancel and
   * <CODE>false</CODE> otherwise.
   * @return <CODE>true</CODE> if the user clicked on cancel and
   * <CODE>false</CODE> otherwise.
   */
  public boolean isCanceled()
  {
    return isCanceled;
  }
 
  /**
   * Returns the java arguments object representing the input of the user
   * in this panel.  The method assumes that the values in the panel are
   * valid.
   * @return the java arguments object representing the input of the user
   * in this panel.
   */
  public JavaArguments getJavaArguments()
  {
    JavaArguments javaArguments = new JavaArguments();
 
    String sMaxMemory = tfMaxMemory.getText().trim();
    if (sMaxMemory.length() > 0)
    {
      javaArguments.setMaxMemory(Integer.parseInt(sMaxMemory));
    }
    String sInitialMemory = tfInitialMemory.getText().trim();
    if (sInitialMemory.length() > 0)
    {
      javaArguments.setInitialMemory(Integer.parseInt(sInitialMemory));
    }
    String[] args = getOtherArguments();
    if (args.length > 0)
    {
      javaArguments.setAdditionalArguments(args);
    }
    return javaArguments;
  }
 
  private String[] getOtherArguments()
  {
    String sArgs = this.tfOtherArguments.getText().trim();
    if (sArgs.length() <= 0)
    {
      return new String[]{};
    }
 
    String[] args = sArgs.split(" ");
    ArrayList<String> array = new ArrayList<>();
    for (String arg : args)
    {
      if (arg.length() > 0)
      {
        array.add(arg);
      }
    }
    return array.toArray(new String[array.size()]);
  }
 
  /**
   * Creates and returns the panel of the dialog.
   * @return the panel of the dialog.
   */
  private JPanel createPanel()
  {
    GridBagConstraints gbc = new GridBagConstraints();
 
    JPanel contentPanel = new JPanel(new GridBagLayout());
    contentPanel.setBackground(UIFactory.DEFAULT_BACKGROUND);
 
    JPanel topPanel = new JPanel(new GridBagLayout());
    topPanel.setBorder(UIFactory.DIALOG_PANEL_BORDER);
    topPanel.setBackground(UIFactory.CURRENT_STEP_PANEL_BACKGROUND);
    Insets insets = UIFactory.getCurrentStepPanelInsets();
    insets.bottom = 0;
    gbc.insets = insets;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 1.0;
    gbc.weighty = 0.0;
    gbc.gridwidth = 3;
    gbc.gridx = 0;
    gbc.gridy = 0;
    LocalizableMessage title = INFO_JAVA_RUNTIME_SETTINGS_TITLE.get();
    JLabel l =
        UIFactory.makeJLabel(UIFactory.IconType.NO_ICON, title,
            UIFactory.TextStyle.TITLE);
    l.setOpaque(false);
    topPanel.add(l, gbc);
 
    JTextComponent instructionsPane =
      UIFactory.makeHtmlPane(message, UIFactory.INSTRUCTIONS_FONT);
    instructionsPane.setOpaque(false);
    instructionsPane.setEditable(false);
 
    gbc.gridy ++;
    gbc.insets.top = UIFactory.TOP_INSET_INPUT_SUBPANEL;
    topPanel.add(instructionsPane, gbc);
 
    gbc.gridy ++;
    gbc.insets.top = UIFactory.TOP_INSET_INPUT_SUBPANEL;
    gbc.insets.bottom = UIFactory.TOP_INSET_INPUT_SUBPANEL;
 
    inputContainer = new JPanel(new CardLayout());
    inputContainer.setOpaque(false);
    inputContainer.add(createInputPanel(), INPUT_PANEL);
    JPanel checkingPanel = UIFactory.makeJPanel();
    checkingPanel.setLayout(new GridBagLayout());
    checkingPanel.add(UIFactory.makeJLabel(UIFactory.IconType.WAIT,
        INFO_GENERAL_CHECKING_DATA.get(),
        UIFactory.TextStyle.PRIMARY_FIELD_VALID),
        new GridBagConstraints());
    inputContainer.add(checkingPanel, CHECKING_PANEL);
 
    topPanel.add(inputContainer, gbc);
    gbc.weighty = 1.0;
    gbc.gridy ++;
    gbc.insets = UIFactory.getEmptyInsets();
    topPanel.add(Box.createVerticalGlue(), gbc);
 
    gbc.gridx = 0;
    gbc.gridy = 0;
    contentPanel.add(topPanel, gbc);
    gbc.weighty = 0.0;
    gbc.gridy ++;
    gbc.insets = UIFactory.getButtonsPanelInsets();
    contentPanel.add(createButtonsPanel(), gbc);
 
    return contentPanel;
  }
 
  /**
   * Creates and returns the input sub panel: the panel with all the widgets
   * that are used to define the security options.
   * @return the input sub panel.
   */
  private Component createInputPanel()
  {
    JPanel inputPanel = new JPanel(new GridBagLayout());
    inputPanel.setOpaque(false);
 
    lInitialMemory = UIFactory.makeJLabel(UIFactory.IconType.NO_ICON,
        INFO_INITIAL_MEMORY_LABEL.get(),
        UIFactory.TextStyle.PRIMARY_FIELD_VALID);
    lInitialMemory.setOpaque(false);
    tfInitialMemory = UIFactory.makeJTextField(LocalizableMessage.EMPTY,
        INFO_INITIAL_MEMORY_TOOLTIP.get(), 10, UIFactory.TextStyle.TEXTFIELD);
    lInitialMemory.setLabelFor(tfInitialMemory);
 
    lMaxMemory = UIFactory.makeJLabel(UIFactory.IconType.NO_ICON,
        INFO_MAX_MEMORY_LABEL.get(),
        UIFactory.TextStyle.PRIMARY_FIELD_VALID);
    lMaxMemory.setOpaque(false);
    tfMaxMemory = UIFactory.makeJTextField(LocalizableMessage.EMPTY,
        INFO_MAX_MEMORY_TOOLTIP.get(), 10, UIFactory.TextStyle.TEXTFIELD);
    lMaxMemory.setLabelFor(tfMaxMemory);
 
    lOtherArguments = UIFactory.makeJLabel(UIFactory.IconType.NO_ICON,
        INFO_OTHER_JAVA_ARGUMENTS_LABEL.get(),
        UIFactory.TextStyle.PRIMARY_FIELD_VALID);
    lOtherArguments.setOpaque(false);
    tfOtherArguments = UIFactory.makeJTextField(LocalizableMessage.EMPTY,
        INFO_OTHER_JAVA_ARGUMENTS_TOOLTIP.get(), 30,
        UIFactory.TextStyle.TEXTFIELD);
    lOtherArguments.setLabelFor(tfOtherArguments);
 
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.weightx = 0.0;
    inputPanel.add(lInitialMemory, gbc);
    gbc.gridx = 1;
    gbc.weightx = 1.0;
    gbc.insets.left = UIFactory.LEFT_INSET_PRIMARY_FIELD;
    inputPanel.add(tfInitialMemory, gbc);
    gbc.weightx = 0.0;
    gbc.gridx = 2;
    gbc.insets.left = UIFactory.LEFT_INSET_SECONDARY_FIELD;
    JLabel lMb = UIFactory.makeJLabel(UIFactory.IconType.NO_ICON,
        INFO_MEGABYTE_LABEL.get(),
        UIFactory.TextStyle.SECONDARY_FIELD_VALID);
    lMb.setOpaque(false);
    inputPanel.add(lMb, gbc);
    gbc.gridx = 1;
    gbc.gridy ++;
    gbc.gridwidth = 2;
    gbc.insets.top = 3;
    gbc.insets.left = UIFactory.LEFT_INSET_PRIMARY_FIELD;
    inputPanel.add(UIFactory.makeJLabel(UIFactory.IconType.NO_ICON,
        INFO_JAVA_ARGUMENTS_LEAVE_EMPTY.get(),
        UIFactory.TextStyle.INLINE_HELP), gbc);
 
    gbc.gridy ++;
    gbc.gridwidth = 1;
    gbc.gridx = 0;
    gbc.weightx = 0.0;
    gbc.insets.left = 0;
    gbc.insets.top = UIFactory.TOP_INSET_PRIMARY_FIELD;
    inputPanel.add(lMaxMemory, gbc);
    gbc.gridx = 1;
    gbc.weightx = 1.0;
    gbc.insets.left = UIFactory.LEFT_INSET_PRIMARY_FIELD;
    inputPanel.add(tfMaxMemory, gbc);
    gbc.weightx = 0.0;
    gbc.gridx = 2;
    gbc.insets.left = UIFactory.LEFT_INSET_SECONDARY_FIELD;
    lMb = UIFactory.makeJLabel(UIFactory.IconType.NO_ICON,
        INFO_MEGABYTE_LABEL.get(),
        UIFactory.TextStyle.SECONDARY_FIELD_VALID);
    lMb.setOpaque(false);
    inputPanel.add(lMb, gbc);
    gbc.gridx = 1;
    gbc.gridy ++;
    gbc.gridwidth = 2;
    gbc.insets.top = 3;
    gbc.insets.left = UIFactory.LEFT_INSET_PRIMARY_FIELD;
    inputPanel.add(UIFactory.makeJLabel(UIFactory.IconType.NO_ICON,
        INFO_JAVA_ARGUMENTS_LEAVE_EMPTY.get(),
        UIFactory.TextStyle.INLINE_HELP), gbc);
 
    gbc.gridy ++;
    gbc.gridwidth = 1;
    gbc.gridx = 0;
    gbc.weightx = 0.0;
    gbc.insets.left = 0;
    gbc.insets.top = UIFactory.TOP_INSET_PRIMARY_FIELD;
    inputPanel.add(lOtherArguments, gbc);
    gbc.gridx = 1;
    gbc.weightx = 1.0;
    gbc.gridwidth = 2;
    gbc.insets.left = UIFactory.LEFT_INSET_PRIMARY_FIELD;
    inputPanel.add(tfOtherArguments, gbc);
 
    gbc.gridy ++;
    gbc.gridx = 0;
    gbc.weighty = 1.0;
    gbc.insets = UIFactory.getEmptyInsets();
    inputPanel.add(Box.createVerticalGlue(), gbc);
 
    return inputPanel;
  }
 
  /**
   * Creates and returns the buttons OK/CANCEL sub panel.
   * @return the buttons OK/CANCEL sub panel.
   */
  private Component createButtonsPanel()
  {
    JPanel buttonsPanel = new JPanel(new GridBagLayout());
    buttonsPanel.setOpaque(false);
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridwidth = 4;
    gbc.insets = UIFactory.getEmptyInsets();
    gbc.insets.left = UIFactory.getCurrentStepPanelInsets().left;
    buttonsPanel.add(UIFactory.makeJLabel(UIFactory.IconType.NO_ICON,
        null, UIFactory.TextStyle.NO_STYLE), gbc);
    gbc.weightx = 1.0;
    gbc.gridwidth--;
    gbc.insets.left = 0;
    buttonsPanel.add(Box.createHorizontalGlue(), gbc);
    gbc.gridwidth = GridBagConstraints.RELATIVE;
    gbc.fill = GridBagConstraints.NONE;
    gbc.weightx = 0.0;
    okButton =
      UIFactory.makeJButton(INFO_OK_BUTTON_LABEL.get(),
          INFO_JAVA_ARGUMENTS_OK_BUTTON_TOOLTIP.get());
    buttonsPanel.add(okButton, gbc);
    okButton.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent ev)
      {
        okClicked();
      }
    });
 
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.insets.left = UIFactory.HORIZONTAL_INSET_BETWEEN_BUTTONS;
    cancelButton =
      UIFactory.makeJButton(INFO_CANCEL_BUTTON_LABEL.get(),
          INFO_JAVA_ARGUMENTS_CANCEL_BUTTON_TOOLTIP.get());
    buttonsPanel.add(cancelButton, gbc);
    cancelButton.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent ev)
      {
        cancelClicked();
      }
    });
 
    return buttonsPanel;
  }
 
  /** Method called when user clicks on cancel. */
  private void cancelClicked()
  {
    isCanceled = true;
    dispose();
  }
 
  /** Method called when user clicks on OK. */
  private void okClicked()
  {
    BackgroundTask<ArrayList<LocalizableMessage>> worker =
      new BackgroundTask<ArrayList<LocalizableMessage>>()
    {
      @Override
      public ArrayList<LocalizableMessage> processBackgroundTask()
      {
        setValidLater(lInitialMemory, true);
        setValidLater(lMaxMemory, true);
        setValidLater(lOtherArguments, true);
        int initialMemory = -1;
        int maxMemory = -1;
        ArrayList<LocalizableMessage> errorMsgs = new ArrayList<>();
        try
        {
          String sInitialMemory = tfInitialMemory.getText().trim();
          if (sInitialMemory.length() > 0)
          {
            initialMemory = Integer.parseInt(sInitialMemory);
            if (initialMemory <= 0)
            {
              initialMemory = -1;
              errorMsgs.add(ERR_INITIAL_MEMORY_VALUE.get());
              setValidLater(lInitialMemory, false);
            }
          }
        }
        catch (Throwable t)
        {
          errorMsgs.add(ERR_INITIAL_MEMORY_VALUE.get());
          setValidLater(lInitialMemory, false);
        }
        try
        {
          String sMaxMemory = tfMaxMemory.getText().trim();
          if (sMaxMemory.length() > 0)
          {
            maxMemory = Integer.parseInt(sMaxMemory);
            if (maxMemory <= 0)
            {
              maxMemory = -1;
              errorMsgs.add(ERR_MAX_MEMORY_VALUE.get());
              setValidLater(lMaxMemory, false);
            }
          }
        }
        catch (Throwable t)
        {
          errorMsgs.add(ERR_MAX_MEMORY_VALUE.get());
          setValidLater(lMaxMemory, false);
        }
        if (maxMemory != -1
            && initialMemory != -1
            && initialMemory > maxMemory)
        {
          errorMsgs.add(ERR_MAX_MEMORY_BIGGER_THAN_INITIAL_MEMORY.get());
          setValidLater(lMaxMemory, false);
          setValidLater(lInitialMemory, false);
        }
        if (errorMsgs.isEmpty())
        {
          // Try the options together, often there are interdependencies.
          ArrayList<LocalizableMessage> allErrors = new ArrayList<>();
          checkAllArgumentsTogether(initialMemory, maxMemory, allErrors);
 
          if (!allErrors.isEmpty())
          {
            ArrayList<LocalizableMessage> memoryErrors = new ArrayList<>();
            checkMemoryArguments(initialMemory, maxMemory, memoryErrors);
            ArrayList<LocalizableMessage> otherErrors = new ArrayList<>();
            checkOtherArguments(otherErrors);
 
            if (!memoryErrors.isEmpty())
            {
              errorMsgs.addAll(memoryErrors);
              if (!otherErrors.isEmpty())
              {
                errorMsgs.addAll(otherErrors);
              }
            }
            else
            {
              if (!otherErrors.isEmpty())
              {
                errorMsgs.addAll(otherErrors);
              }
              else
              {
                setValidLater(lInitialMemory, false);
                setValidLater(lMaxMemory, false);
                setValidLater(lOtherArguments, false);
                // It appears that the arguments are not compatible together.
                errorMsgs.add(
                    ERR_MEMORY_AND_OTHER_ARGUMENTS_NOT_COMPATIBLE.get());
              }
            }
          }
        }
        return errorMsgs;
      }
 
      @Override
      public void backgroundTaskCompleted(ArrayList<LocalizableMessage> returnValue,
          Throwable throwable)
      {
        setCheckingVisible(false);
        if (throwable != null)
        {
          // Bug
          throwable.printStackTrace();
          displayError(
              getThrowableMsg(INFO_BUG_MSG.get(), throwable),
              INFO_ERROR_TITLE.get());
          cancelButton.setEnabled(true);
          okButton.setEnabled(true);
        }
        else
        {
          cancelButton.setEnabled(true);
          okButton.setEnabled(true);
 
          if (!returnValue.isEmpty())
          {
            displayError(Utils.getMessageFromCollection(returnValue, "\n"),
                INFO_ERROR_TITLE.get());
          }
          else
          {
            isCanceled = false;
            dispose();
          }
        }
      }
    };
    setCheckingVisible(true);
    cancelButton.setEnabled(false);
    okButton.setEnabled(false);
    worker.startBackgroundTask();
  }
 
  /**
   * Displays an error message dialog.
   *
   * @param msg
   *          the error message.
   * @param title
   *          the title for the dialog.
   */
  private void displayError(LocalizableMessage msg, LocalizableMessage title)
  {
    Utilities.displayError(this, msg, title);
    toFront();
  }
 
  /** Updates the widgets on the dialog with the contents of the securityOptions object. */
  private void updateContents()
  {
    if (javaArguments.getInitialMemory() > 0)
    {
      tfInitialMemory.setText(String.valueOf(javaArguments.getInitialMemory()));
    }
    else
    {
      tfInitialMemory.setText("");
    }
    if (javaArguments.getMaxMemory() > 0)
    {
      tfMaxMemory.setText(String.valueOf(javaArguments.getMaxMemory()));
    }
    else
    {
      tfMaxMemory.setText("");
    }
    if (javaArguments.getAdditionalArguments() != null)
    {
      StringBuilder sb = new StringBuilder();
      for (String arg : javaArguments.getAdditionalArguments())
      {
        if (sb.length() > 0)
        {
          sb.append(" ");
        }
        sb.append(arg);
      }
      tfOtherArguments.setText(sb.toString());
    }
    else
    {
      tfOtherArguments.setText("");
    }
  }
 
  /**
   * Method that updates the text style of a provided component by calling
   * SwingUtilities.invokeLater.  This method is aimed to be called outside
   * the event thread (calling it from the event thread will also work though).
   * @param comp the component to be updated.
   * @param valid whether to use a TextStyle to mark the component as valid
   * or as invalid.
   */
  private void setValidLater(final JComponent comp, final boolean valid)
  {
    SwingUtilities.invokeLater(new Runnable()
    {
      @Override
      public void run()
      {
        UIFactory.setTextStyle(comp,
            valid ? UIFactory.TextStyle.PRIMARY_FIELD_VALID :
              UIFactory.TextStyle.PRIMARY_FIELD_INVALID);
      }
    });
  }
 
  /**
   * This method displays a working progress icon in the panel.
   * @param visible whether the icon must be displayed or not.
   */
  private void setCheckingVisible(boolean visible)
  {
    if (visible != isCheckingVisible && inputContainer != null)
    {
      CardLayout cl = (CardLayout) inputContainer.getLayout();
      if (visible)
      {
        cl.show(inputContainer, CHECKING_PANEL);
      }
      else
      {
        cl.show(inputContainer, INPUT_PANEL);
      }
      isCheckingVisible = visible;
    }
  }
 
  /**
   * Method written for testing purposes.
   * @param args the arguments to be passed to the test program.
   */
  public static void main(String[] args)
  {
    try
    {
      JavaArguments javaArgs = new JavaArguments();
      javaArgs.setInitialMemory(100);
      javaArgs.setMaxMemory(99);
      javaArgs.setAdditionalArguments(new String[]{"" , "-client", "-XX"});
      // UIFactory.initialize();
      JavaArgumentsDialog dlg = new JavaArgumentsDialog(new JFrame(), javaArgs,
          LocalizableMessage.raw("my title"),
          LocalizableMessage.raw("Set the java arguments for the test command-line."));
      dlg.pack();
      dlg.setVisible(true);
    } catch (Exception ex)
    {
      ex.printStackTrace();
    }
  }
 
  private static final String INSTALL_PATH =
    Utils.getInstallPathFromClasspath();
 
  private void checkOptions(String options, Collection<LocalizableMessage> errorMsgs,
      JLabel l,  LocalizableMessage errorMsg)
  {
    checkOptions(options, errorMsgs, new JLabel[]{l}, errorMsg);
  }
 
  private void checkOptions(
          String options, Collection<LocalizableMessage> errorMsgs, JLabel[] ls,  LocalizableMessage errorMsg)
  {
    String javaHome = System.getProperty("java.home");
    if (javaHome == null || javaHome.length() == 0)
    {
      javaHome = System.getenv(SetupUtils.OPENDJ_JAVA_HOME);
    }
    if (!Utils.supportsOption(options, javaHome, INSTALL_PATH))
    {
      for (JLabel l : ls)
      {
        setValidLater(l, false);
      }
      errorMsgs.add(errorMsg);
    }
  }
 
  private LocalizableMessage getMemoryErrorMessage(LocalizableMessage msg, int memValue)
  {
    // 2048 MB is acceptable max heap size on 32Bit OS
    if (memValue < 2048)
    {
      return msg;
    }
    else
    {
      LocalizableMessageBuilder mb = new LocalizableMessageBuilder();
      mb.append(msg);
      mb.append("  ");
      mb.append(ERR_MEMORY_32_BIT_LIMIT.get());
      return mb.toMessage();
    }
  }
 
  private void checkMemoryArguments(int initialMemory, int maxMemory,
      Collection<LocalizableMessage> errorMsgs)
  {
    setValidLater(lInitialMemory, true);
    setValidLater(lMaxMemory, true);
    if (initialMemory != -1)
    {
      if (maxMemory != -1)
      {
        LocalizableMessage msg = getMemoryErrorMessage(ERR_MEMORY_VALUE_EXTENDED.get(
              JavaArguments.getInitialMemoryGenericArgument(),
              JavaArguments.getMaxMemoryGenericArgument()), maxMemory);
        String sMemory =
          JavaArguments.getInitialMemoryArgument(initialMemory) + " "+
          JavaArguments.getMaxMemoryArgument(maxMemory);
        checkOptions(sMemory,
            errorMsgs,
            new JLabel[] {lInitialMemory, lMaxMemory},
            msg);
      }
      else
      {
        LocalizableMessage msg = getMemoryErrorMessage(
            ERR_INITIAL_MEMORY_VALUE_EXTENDED.get(
                JavaArguments.getInitialMemoryGenericArgument()),
                initialMemory);
        checkOptions(JavaArguments.getInitialMemoryArgument(initialMemory),
            errorMsgs,
            lInitialMemory,
            msg);
      }
    }
    else if (maxMemory != -1)
    {
      LocalizableMessage msg = getMemoryErrorMessage(
          ERR_MAX_MEMORY_VALUE_EXTENDED.get(
              JavaArguments.getMaxMemoryGenericArgument()), maxMemory);
      checkOptions(JavaArguments.getMaxMemoryArgument(maxMemory),
          errorMsgs,
          lMaxMemory,
          msg);
    }
  }
 
  private void checkAllArgumentsTogether(int initialMemory, int maxMemory,
      Collection<LocalizableMessage> errorMsgs)
  {
    setValidLater(lInitialMemory, true);
    setValidLater(lMaxMemory, true);
    setValidLater(lOtherArguments, true);
    ArrayList<JLabel> ls = new ArrayList<>();
    StringBuilder sb = new StringBuilder();
 
    if (initialMemory != -1)
    {
      if (maxMemory != -1)
      {
        String sMemory =
          JavaArguments.getInitialMemoryArgument(initialMemory) + " "+
          JavaArguments.getMaxMemoryArgument(maxMemory);
        sb.append(sMemory);
        ls.add(lInitialMemory);
        ls.add(lMaxMemory);
      }
      else
      {
        sb.append(JavaArguments.getInitialMemoryArgument(initialMemory));
        ls.add(lInitialMemory);
      }
    }
    else if (maxMemory != -1)
    {
      sb.append(JavaArguments.getMaxMemoryArgument(maxMemory));
      ls.add(lMaxMemory);
    }
 
    String[] otherArgs = getOtherArguments();
    if (otherArgs.length > 0)
    {
      ls.add(lOtherArguments);
      for (String arg : otherArgs)
      {
        if (sb.length() > 0)
        {
          sb.append(" ");
        }
        sb.append(arg);
      }
    }
    if (sb.length() > 0)
    {
      checkOptions(sb.toString(), errorMsgs,
          ls.toArray(new JLabel[ls.size()]),
          ERR_GENERIC_JAVA_ARGUMENT.get(sb));
    }
  }
 
  private void checkOtherArguments(Collection<LocalizableMessage> errorMsgs)
  {
    setValidLater(lOtherArguments, true);
    StringBuilder sb = new StringBuilder();
 
    String[] otherArgs = getOtherArguments();
    if (otherArgs.length > 0)
    {
      for (String arg : otherArgs)
      {
        if (sb.length() > 0)
        {
          sb.append(" ");
        }
        sb.append(arg);
      }
    }
    if (sb.length() > 0)
    {
      checkOptions(sb.toString(), errorMsgs, lOtherArguments,
          ERR_GENERIC_JAVA_ARGUMENT.get(sb));
    }
  }
}