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
/*
 * 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 2006-2010 Sun Microsystems, Inc.
 * Portions Copyright 2014-2016 ForgeRock AS.
 */
package org.opends.quicksetup.ui;
 
import static com.forgerock.opendj.util.OperatingSystem.isMacOS;
 
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.Window;
import java.util.StringTokenizer;
 
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.text.JTextComponent;
 
import org.forgerock.i18n.LocalizableMessage;
 
/**
 * A set of utilities specific to GUI QuickSetup applications.
 */
public class Utilities {
 
  /**
   * Creates a panel with a field and a browse button.
   * @param lbl JLabel for the field
   * @param tf JTextField for holding the browsed data
   * @param but JButton for invoking browse action
   * @return the created panel.
   */
  public static JPanel createBrowseButtonPanel(JLabel lbl,
                                         JTextComponent tf,
                                         JButton but)
  {
    GridBagConstraints gbc = new GridBagConstraints();
 
    JPanel panel = UIFactory.makeJPanel();
    panel.setLayout(new GridBagLayout());
 
    gbc.insets = UIFactory.getEmptyInsets();
    gbc.gridwidth = 4;
    gbc.weightx = 0.0;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    panel.add(lbl, gbc);
 
    gbc.insets.left = UIFactory.LEFT_INSET_SECONDARY_FIELD;
    gbc.gridwidth--;
    gbc.weightx = 0.1;
    panel.add(tf, gbc);
 
    gbc.insets.left = UIFactory.LEFT_INSET_BROWSE;
    gbc.gridwidth = GridBagConstraints.RELATIVE;
    panel.add(but, gbc);
 
    gbc.weightx = 1.0;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    panel.add(Box.createHorizontalGlue(), gbc);
 
    return panel;
  }
 
  /**
   * Sets a frames image icon to the standard OpenDS icon appropriate
   * for the running platform.
   *
   * @param frame for which the icon will be set
   */
  public static void setFrameIcon(JFrame frame)
  {
    UIFactory.IconType ic;
    if (isMacOS()) {
      ic = UIFactory.IconType.MINIMIZED_MAC;
    } else {
      ic = UIFactory.IconType.MINIMIZED;
    }
    frame.setIconImage(UIFactory.getImageIcon(ic).getImage());
  }
 
  /**
   * Center the component location based on its preferred size. The code
   * considers the particular case of 2 screens and puts the component on the
   * center of the left screen
   *
   * @param comp the component to be centered.
   */
  public static void centerOnScreen(Component comp)
  {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
 
    int width = (int) comp.getPreferredSize().getWidth();
    int height = (int) comp.getPreferredSize().getHeight();
 
    boolean multipleScreen = screenSize.width / screenSize.height >= 2;
 
    if (multipleScreen)
    {
      comp.setLocation(screenSize.width / 4 - width / 2,
          (screenSize.height - height) / 2);
    } else
    {
      comp.setLocation((screenSize.width - width) / 2,
          (screenSize.height - height) / 2);
    }
  }
 
  /**
   * Center the component location of the ref component.
   *
   * @param comp the component to be centered.
   * @param ref the component to be used as reference.
   *
   */
  public static void centerOnComponent(Window comp, Component ref)
  {
    comp.setLocationRelativeTo(ref);
  }
 
  /**
   * Displays a confirmation message dialog.
  *
  * @param parent
   *          the parent frame of the confirmation dialog.
   * @param msg
  *          the confirmation message.
  * @param title
  *          the title of the dialog.
  * @return <CODE>true</CODE> if the user confirms the message, or
  * <CODE>false</CODE> if not.
  */
 public static boolean displayConfirmation(Component parent, LocalizableMessage msg,
     LocalizableMessage title)
 {
   return JOptionPane.YES_OPTION == JOptionPane.showOptionDialog(
           parent, wrapMsg(String.valueOf(msg), 100), String.valueOf(title),
           JOptionPane.YES_NO_OPTION,
           JOptionPane.QUESTION_MESSAGE,
           null, // don't use a custom Icon
           null, // the titles of buttons
           null); // default button title
 }
 
  /**
   * Displays an error message dialog.
   *
   * @param parent
   *          the parent component of the error dialog.
   * @param msg
 *          the error message.
   * @param title
   *          the title for the dialog.
   */
  public static void displayError(Component parent, LocalizableMessage msg, LocalizableMessage title)
  {
    JOptionPane.showMessageDialog(parent,
            wrapMsg(String.valueOf(msg), 100),
            String.valueOf(title), JOptionPane.ERROR_MESSAGE);
  }
 
  /**
   * Displays an information message dialog.
   *
   * @param parent
   *          the parent frame of the information dialog.
   * @param msg
 *          the error message.
   * @param title
   *          the title for the dialog.
   */
  public static void displayInformationMessage(JFrame parent, LocalizableMessage msg,
      LocalizableMessage title)
  {
    JOptionPane.showMessageDialog(parent,
            wrapMsg(String.valueOf(msg), 100), String.valueOf(title),
            JOptionPane.INFORMATION_MESSAGE);
  }
 
  /**
   * Private method used to wrap the messages that are displayed in dialogs
   * of type JOptionPane.
   * @param msg the message.
   * @param width the maximum width of the column.
   * @return the wrapped message.
   */
  private static String wrapMsg(String msg, int width)
  {
    StringBuilder   buffer        = new StringBuilder();
    StringTokenizer lineTokenizer = new StringTokenizer(msg, "\n", true);
    while (lineTokenizer.hasMoreTokens())
    {
      String line = lineTokenizer.nextToken();
      if (line.equals("\n"))
      {
        // It's an end-of-line character, so append it as-is.
        buffer.append(line);
      }
      else if (line.length() < width)
      {
        // The line fits in the specified width, so append it as-is.
        buffer.append(line);
      }
      else
      {
        // The line doesn't fit in the specified width, so it needs to be
        // wrapped.  Do so at space boundaries.
        StringBuilder   lineBuffer    = new StringBuilder();
        StringBuilder   delimBuffer   = new StringBuilder();
        StringTokenizer wordTokenizer = new StringTokenizer(line, " ", true);
        while (wordTokenizer.hasMoreTokens())
        {
          String word = wordTokenizer.nextToken();
          if (word.equals(" "))
          {
            // It's a space, so add it to the delim buffer only if the line
            // buffer is not empty.
            if (lineBuffer.length() > 0)
            {
              delimBuffer.append(word);
            }
          }
          else if (word.length() > width)
          {
            // This is a long word that can't be wrapped, so we'll just have to
            // make do.
            if (lineBuffer.length() > 0)
            {
              buffer.append(lineBuffer);
              buffer.append("\n");
              lineBuffer = new StringBuilder();
            }
            buffer.append(word);
 
            if (wordTokenizer.hasMoreTokens())
            {
              // The next token must be a space, so remove it.  If there are
              // still more tokens after that, then append an EOL.
              wordTokenizer.nextToken();
              if (wordTokenizer.hasMoreTokens())
              {
                buffer.append("\n");
              }
            }
 
            if (delimBuffer.length() > 0)
            {
              delimBuffer = new StringBuilder();
            }
          }
          else
          {
            // It's not a space, so see if we can fit it on the current line.
            int newLineLength = lineBuffer.length() + delimBuffer.length() +
            word.length();
            if (newLineLength < width)
            {
              // It does fit on the line, so add it.
              lineBuffer.append(delimBuffer).append(word);
            }
            else
            {
              // It doesn't fit on the line, so end the current line and start
              // a new one.
              buffer.append(lineBuffer);
              buffer.append("\n");
 
              lineBuffer = new StringBuilder();
              lineBuffer.append(word);
            }
 
            if (delimBuffer.length() > 0)
            {
              delimBuffer = new StringBuilder();
            }
          }
        }
 
        // If there's anything left in the line buffer, then add it to the
        // final buffer.
        buffer.append(lineBuffer);
      }
    }
    return buffer.toString();
  }
}