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

kenneth_suter
19.28.2007 efde227b9d0180122362133a750c6b322601c883
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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Portions Copyright 2007 Sun Microsystems, Inc.
 */
 
package org.opends.quicksetup.ui;
 
import org.opends.quicksetup.UserInteraction;
import org.opends.quicksetup.Constants;
import org.opends.quicksetup.i18n.ResourceProvider;
import org.opends.quicksetup.util.Utils;
 
import javax.swing.*;
import javax.swing.plaf.basic.BasicOptionPaneUI;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.List;
 
/**
 * This class supports user interactions for a graphical application.
 */
public class GuiUserInteraction implements UserInteraction {
 
  static private final int MAX_CHARS_PER_LINE = 100;
 
  private Component parent = null;
 
  /**
   * Creates an instance.
   * @param parent Component acting as parent to dialogs supporting interaction.
   */
  public GuiUserInteraction(Component parent) {
    this.parent = parent;
  }
 
  /**
   * {@inheritDoc}
   */
  public Object confirm(String summary, String details,
                        String title, MessageType type, String[] options,
                        String def) {
    return confirm(summary, details, null, title, type, options, def, null);
  }
 
  /**
   * {@inheritDoc}
   */
  public Object confirm(String summary, String details, String fineDetails,
                        String title, MessageType type, String[] options,
                        String def, String viewDetailsOption) {
    int optionType;
    if (options != null) {
      if (options.length == 2) {
        optionType = JOptionPane.YES_NO_OPTION;
      } else if (options.length == 3) {
        optionType = JOptionPane.YES_NO_CANCEL_OPTION;
      } else {
        throw new IllegalArgumentException(
                "unsupported number of options: " + options.length);
      }
    } else {
      throw new NullPointerException("options cannot be null");
    }
    int msgType;
    switch(type) {
        case PLAIN: msgType = JOptionPane.PLAIN_MESSAGE; break;
        case ERROR: msgType = JOptionPane.ERROR_MESSAGE; break;
        case INFORMATION: msgType = JOptionPane.INFORMATION_MESSAGE; break;
        case WARNING: msgType = JOptionPane.WARNING_MESSAGE; break;
        case QUESTION: msgType = JOptionPane.QUESTION_MESSAGE; break;
        default: throw new IllegalArgumentException("unsupported MessageType");
    }
    JOptionPane op;
    if (fineDetails != null) {
      op = new DetailsOptionPane(MAX_CHARS_PER_LINE, fineDetails);
    } else {
      op = new MaxCharactersPerLineOptionPane(MAX_CHARS_PER_LINE);
    }
 
    // Create the main message using HTML formatting.  The max
    // characters per line functionality of the extends options
    // pane does not affect message that are components so we
    // have to format this ourselves.
    StringBuilder sb = new StringBuilder(Constants.HTML_BOLD_OPEN);
    sb.append(Utils.breakHtmlString(summary, MAX_CHARS_PER_LINE));
    sb.append(Constants.HTML_BOLD_CLOSE);
    sb.append(Constants.HTML_LINE_BREAK);
    sb.append(Constants.HTML_LINE_BREAK);
    sb.append(Utils.breakHtmlString(details, MAX_CHARS_PER_LINE));
    JEditorPane ep = UIFactory.makeHtmlPane(
            sb.toString(),
            UIFactory.INSTRUCTIONS_FONT);
    ep.setBorder(BorderFactory.createEmptyBorder(0, 0, 20, 0));
    op.setMessage(ep);
    op.setOptionType(optionType);
    op.setMessageType(msgType);
    op.setOptions(options);
    op.setInitialValue(def);
    JDialog dlg = op.createDialog(parent, title);
    dlg.setVisible(true);
    return op.getValue();
  }
 
  /**
   * {@inheritDoc}
   */
  public String createUnorderedList(List list) {
    StringBuilder sb = new StringBuilder();
    if (list != null) {
      sb.append(Constants.HTML_UNORDERED_LIST_OPEN);
      for (Object o : list) {
        sb.append(Constants.HTML_LIST_ITEM_OPEN);
        sb.append(o.toString());
        sb.append(Constants.HTML_LIST_ITEM_CLOSE);
      }
      sb.append(Constants.HTML_UNORDERED_LIST_CLOSE);
    }
    return sb.toString();
  }
 
  /**
   * JOptionPane that controls the number of characters that are allowed
   * to appear on a single line in the input area of the dialog.
   */
  private class MaxCharactersPerLineOptionPane extends JOptionPane {
 
    /** Implements serializable. */
    static final long serialVersionUID = 8984664928623358120L;
 
    private int maxCharactersPerLineCount;
 
    /**
     * Creates an instance.
     * @param maxCharactersPerLine the maximum number of characters that
     *        are allowed on a single line in the dialog.
     */
    public MaxCharactersPerLineOptionPane(int maxCharactersPerLine) {
      this.maxCharactersPerLineCount = maxCharactersPerLine;
    }
 
    /**
     * {@inheritDoc}
     */
    public int getMaxCharactersPerLineCount() {
      return maxCharactersPerLineCount;
    }
 
  }
 
  /**
   * JOptionPane that controls the number of characters that are allowed
   * to appear on a single line in the input area of the dialog.
   */
  private class DetailsOptionPane extends MaxCharactersPerLineOptionPane {
 
    static final long serialVersionUID = -7813059467702205272L;
 
    private static final int MAX_DETAILS_COMPONENT_HEIGHT = 200;
 
    private boolean detailsShowing = false;
 
    private Component detailsComponent;
 
    private JDialog dialog;
 
    /**
     * Creates an instance.
     * @param maxCharactersPerLine the maximum number of characters that
     *        are allowed on a single line in the dialog.
     * @param details String of HTML representing the details section of the
     *        dialog.
     */
    public DetailsOptionPane(int maxCharactersPerLine,
                                         String details) {
      super(maxCharactersPerLine);
      detailsComponent = createDetailsComponent(details);
    }
 
    /**
     * {@inheritDoc}
     */
    public Component add(Component comp) {
      if ("OptionPane.buttonArea".equals(comp.getName())) {
        JPanel detailsButtonsPanel = new JPanel();
        detailsButtonsPanel.setLayout(
                new BoxLayout(detailsButtonsPanel,
                              BoxLayout.LINE_AXIS));
        final String showDetailsLabel = getMsg("show-details-button-label");
        final String hideDetailsLabel = getMsg("hide-details-button-label");
        final JButton btnDetails = new JButton(showDetailsLabel);
        btnDetails.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            Dimension current = dialog.getSize();
            if (!detailsShowing) {
              // detailsComponent.setVisible(true);
              dialog.setSize(current.width,
                      current.height + getExpansionHeight());
              btnDetails.setText(hideDetailsLabel);
            } else {
              // detailsComponent.setVisible(false);
              dialog.setSize(current.width,
                      current.height - getExpansionHeight());
              btnDetails.setText(showDetailsLabel);
            }
            detailsShowing = !detailsShowing;
          }
        });
 
        JPanel detailsBottom = new JPanel();
        Border border = UIManager.getBorder("OptionPane.buttonAreaBorder");
        if (border != null) {
          detailsBottom.setBorder(border);
        }
        detailsBottom.setLayout(
                new BasicOptionPaneUI.ButtonAreaLayout(
                        UIManager.getBoolean("OptionPane.sameSizeButtons"),
                        UIManager.getInt("OptionPane.buttonPadding")));
        detailsBottom.add(btnDetails);
        detailsButtonsPanel.add(detailsBottom);
        detailsButtonsPanel.add(Box.createHorizontalGlue());
        detailsButtonsPanel.add(comp);
        super.add(detailsButtonsPanel);
      } else {
        super.add(comp);
      }
      return comp;
    }
 
    /**
     * {@inheritDoc}
     */
    public JDialog createDialog(Component parentComponent, String title)
            throws HeadlessException
    {
      this.dialog = super.createDialog(parentComponent, title);
      Dimension d = dialog.getSize();
      add(detailsComponent);
      this.dialog.pack();
      dialog.setSize(d);
      return dialog;
    }
 
    private Component createDetailsComponent(String details) {
      JPanel detailsPanel = new JPanel();
      detailsPanel.setLayout(new GridBagLayout());
      GridBagConstraints gbc = new GridBagConstraints();
 
      gbc.insets = new Insets(15, 0, 0, 0);
      gbc.fill = GridBagConstraints.HORIZONTAL;
      detailsPanel.add(UIFactory.makeJLabel(null,
              getMsg("details-label"),
              UIFactory.TextStyle.PRIMARY_FIELD_VALID), gbc);
 
      gbc.insets.top = UIFactory.TOP_INSET_PRIMARY_FIELD;
      gbc.gridx++;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      gbc.fill = GridBagConstraints.BOTH;
 
      JEditorPane ep;
      if (Utils.containsHtml(details)) {
        ep = UIFactory.makeHtmlPane(details, UIFactory.INSTRUCTIONS_FONT);
      } else {
        ep = UIFactory.makeTextPane(details, UIFactory.TextStyle.INSTRUCTIONS);
      }
      ep.setOpaque(false);
 
      detailsPanel.add(new JScrollPane(ep), gbc);
      return detailsPanel;
    }
 
    private int getExpansionHeight() {
      return (int) Math.min(detailsComponent.getPreferredSize().getHeight(),
              MAX_DETAILS_COMPONENT_HEIGHT);
    }
 
  }
 
  private static String getMsg(String key) {
    return ResourceProvider.getInstance().getMsg(key);
  }
 
//  public static void main(String[] args) {
//    new GuiUserInteraction(null).confirm(
//            "Summary",
//            "Details",
//            "Title",
//            MessageType.ERROR,
//            new String[]{"Yes","No"},"No");
//  }
 
}