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

jvergara
07.43.2009 b471bc6b9f2a087e8bb8e3adc6e5d1b32e80db8d
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
/*
 * 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
 *
 *
 *      Copyright 2009 Sun Microsystems, Inc.
 */
 
package org.opends.guitools.controlpanel.ui;
 
import static org.opends.messages.AdminToolMessages.*;
 
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
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.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.text.JTextComponent;
 
import org.opends.guitools.controlpanel.ui.GenericDialog.ButtonType;
import org.opends.guitools.controlpanel.util.Utilities;
import org.opends.server.util.DynamicConstants;
 
/**
 * The generic frame of the Control Panel.  It contains a StatusGenericPanel.
 *
 */
public class GenericFrame extends JFrame
{
  private static final long serialVersionUID = -2643144936460484112L;
  private final static Color buttonPanelBackground =
    ColorAndFontConstants.greyBackground;
  private JButton okButton;
 
  /**
   * The close button.
   */
  protected JButton closeButton;
  private JButton cancelButton;
  //private JPanel contentPanel;
  /**
   * The panel contained in the frame.
   */
  protected StatusGenericPanel panel;
  //private ProgressPanel progressPanel;
  //private boolean displayInputInNextVisible;
  private Component lastComponentWithFocus;
 
  /**
   * Constructor of the frame.
   * @param panel the panel contained in this frame.
   */
  public GenericFrame(StatusGenericPanel panel)
  {
    super();
    this.panel = panel;
    if (panel.requiresBorder())
    {
      setDefaultBorder(panel);
    }
    JMenuBar menu = panel.getMenuBar();
    if (menu != null)
    {
      setJMenuBar(menu);
    }
    setResizable(true);
    JScrollPane scroll = Utilities.createScrollPane(panel);
    JPanel inputPanel = new JPanel(new GridBagLayout());
    setContentPane(inputPanel);
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.weightx = 1.0;
    gbc.weighty = 1.0;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.BOTH;
    if (panel.requiresScroll())
    {
      inputPanel.add(scroll, gbc);
    }
    else
    {
      inputPanel.add(panel, gbc);
    }
    if (panel.getButtonType() != ButtonType.NO_BUTTON)
    {
      gbc.gridy ++;
      gbc.weighty = 0.0;
      inputPanel.add(createButtonsPanel(panel), gbc);
    }
 
    KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
    ActionListener actionListener = new ActionListener()
    {
      /**
       * {@inheritDoc}
       */
      public void actionPerformed(ActionEvent ev)
      {
        setVisible(false);
      }
    };
    getRootPane().registerKeyboardAction(actionListener, stroke,
        JComponent.WHEN_IN_FOCUSED_WINDOW);
 
    FocusListener focusListener = new FocusAdapter()
    {
      /**
       * {@inheritDoc}
       */
      public void focusGained(FocusEvent ev)
      {
        lastComponentWithFocus = ev.getComponent();
      }
    };
    addFocusListener(focusListener, panel);
 
    addWindowListener(new WindowAdapter() {
      /**
       * {@inheritDoc}
       */
      public void windowClosing(WindowEvent e) {
        GenericFrame.this.panel.closeClicked();
      }
    });
 
    org.opends.quicksetup.ui.Utilities.setFrameIcon(this);
    pack();
    if (!SwingUtilities.isEventDispatchThread())
    {
      Thread.dumpStack();
    }
  }
 
  /**
   * Method used to add a focus listeners to all the components in the panel.
   * This is done to recover the focus on an item when the frame is closed
   * and then opened again.
   * @param focusListener the focus listener.
   * @param container the container where the components are layed out.
   */
  private void addFocusListener(FocusListener focusListener,
      Container container)
  {
    for (int i=0; i < container.getComponentCount(); i++)
    {
      Component comp = container.getComponent(i);
      if ((comp instanceof AbstractButton) ||
          (comp instanceof JTextComponent) ||
          (comp instanceof JList) ||
          (comp instanceof JComboBox) ||
          (comp instanceof JTable))
      {
        comp.addFocusListener(focusListener);
      }
      else if ((comp instanceof JPanel) || (comp instanceof JScrollPane)
          || (comp instanceof JViewport))
      {
        addFocusListener(focusListener, (Container)comp);
      }
    }
  }
 
  /**
   * {@inheritDoc}
   */
  public void setVisible(boolean visible)
  {
    if (lastComponentWithFocus == null)
    {
      lastComponentWithFocus = panel.getPreferredFocusComponent();
    }
    if (visible && (lastComponentWithFocus != null))
    {
      lastComponentWithFocus.requestFocusInWindow();
    }
    updateDefaultButton(panel);
    panel.toBeDisplayed(visible);
    updateTitle();
    super.setVisible(visible);
  }
 
  /**
   * Sets the enable state of the OK button.
   * @param enable whether the OK button must be enabled or not.
   */
  public void setEnabledOK(boolean enable)
  {
    okButton.setEnabled(enable);
  }
 
  /**
   * Sets the enable state of the Cancel button.
   * @param enable whether the Cancel button must be enabled or not.
   */
  public void setEnabledCancel(boolean enable)
  {
    cancelButton.setEnabled(enable);
  }
 
  /**
   * Sets the enable state of the Close button.
   * @param enable whether the Close button must be enabled or not.
   */
  public void setEnabledClose(boolean enable)
  {
    closeButton.setEnabled(enable);
  }
 
  /**
   * Updates the title of the frame using the title of the panel.
   *
   */
  public void updateTitle()
  {
    if (panel.getTitle() != null)
    {
      setTitle(INFO_CTRL_PANEL_GENERIC_TITLE.get(
              DynamicConstants.PRODUCT_NAME,
              panel.getTitle().toString()).toString());
    }
  }
 
  private void setDefaultBorder(JComponent comp)
  {
    Utilities.setBorder(comp, new EmptyBorder(20, 20, 20, 20));
  }
 
 
  private JPanel createButtonsPanel(final StatusGenericPanel panel)
  {
    JPanel buttonsPanel = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    ButtonType buttonType = panel.getButtonType();
    gbc.gridx = 0;
    gbc.weightx = 1.0;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    buttonsPanel.add(Box.createHorizontalGlue(), gbc);
    buttonsPanel.setOpaque(true);
    buttonsPanel.setBackground(buttonPanelBackground);
    gbc.insets = new Insets(10, 0, 10, 0);
    gbc.insets.left = 5;
 
    if (buttonType == ButtonType.OK_CANCEL)
    {
      gbc.gridx ++;
      gbc.weightx = 0.0;
      okButton = Utilities.createButton(
          INFO_CTRL_PANEL_OK_BUTTON_LABEL.get());
      okButton.setOpaque(false);
      buttonsPanel.add(okButton, gbc);
      okButton.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent ev)
        {
          panel.okClicked();
        }
      });
      okButton.setEnabled(panel.isEnableOK());
 
      gbc.gridx ++;
      cancelButton = Utilities.createButton(
          INFO_CTRL_PANEL_CANCEL_BUTTON_LABEL.get());
      cancelButton.setOpaque(false);
      cancelButton.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent ev)
        {
          panel.cancelClicked();
        }
      });
      cancelButton.setEnabled(panel.isEnableCancel());
      gbc.insets.right = 10;
      buttonsPanel.add(cancelButton, gbc);
    }
 
    if (buttonType == ButtonType.OK)
    {
      gbc.gridx ++;
      gbc.weightx = 0.0;
      okButton = Utilities.createButton(
          INFO_CTRL_PANEL_OK_BUTTON_LABEL.get());
      okButton.setOpaque(false);
      gbc.insets.right = 10;
      buttonsPanel.add(okButton, gbc);
      okButton.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent ev)
        {
          panel.okClicked();
        }
      });
      okButton.setEnabled(panel.isEnableOK());
    }
 
    if (buttonType == ButtonType.CLOSE)
    {
      gbc.gridx ++;
      gbc.weightx = 0.0;
      closeButton = Utilities.createButton(
          INFO_CTRL_PANEL_CLOSE_BUTTON_LABEL.get());
      closeButton.setOpaque(false);
      gbc.insets.right = 10;
      buttonsPanel.add(closeButton, gbc);
      closeButton.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent ev)
        {
          panel.closeClicked();
        }
      });
      closeButton.setEnabled(panel.isEnableClose());
    }
 
 
 
    buttonsPanel.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0,
        ColorAndFontConstants.defaultBorderColor));
    return buttonsPanel;
  }
 
  /**
   * Updates the default button of the frame, depending on the type of
   * generic panel that it contains.
   * @param panel the generic panel contained in this frame.
   */
  private void updateDefaultButton(StatusGenericPanel panel)
  {
    ButtonType buttonType = panel.getButtonType();
 
    if (buttonType == ButtonType.OK_CANCEL)
    {
      getRootPane().setDefaultButton(okButton);
    }
    else if (buttonType == ButtonType.OK)
    {
      getRootPane().setDefaultButton(okButton);
    }
    else if (buttonType == ButtonType.CLOSE)
    {
      getRootPane().setDefaultButton(closeButton);
    }
  }
}