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

Jean-Noël Rouvignac
05.39.2016 609077ed606e3b094e303f298e8dca10567bc3e2
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
/*
 * 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-2009 Sun Microsystems, Inc.
 * Portions Copyright 2013-2016 ForgeRock AS.
 */
package org.opends.guitools.controlpanel.ui;
 
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
 
import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
 
import org.forgerock.i18n.LocalizableMessage;
import org.opends.guitools.controlpanel.ui.nodes.BasicNode;
import org.opends.guitools.controlpanel.util.Utilities;
 
/**
 * A basic panel that contains a browser.  It is used in general in panels that
 * require to provide some DNs of existing entries: we allow the user to launch
 * a browser to select entries.
 */
public class LDAPEntrySelectionPanel extends AbstractBrowseEntriesPanel
{
  private LocalizableMessage title;
  private Filter f;
  private String[] dns;
 
  /**
   * The values of the filters that will be used when opening the dialog where
   * this panel is contained.  For instance if the filter is set to Filter.USERS
   * the panel will display only users when the dialog appears.
   */
  public enum Filter
  {
    /** Display users. */
    USERS,
    /** Display groups. */
    GROUPS,
    /** Display Dynamic Groups. */
    DYNAMIC_GROUPS,
    /** Display Static Groups. */
    STATIC_GROUPS,
    /** Default filter (all entries). */
    DEFAULT
  }
 
  private static final long serialVersionUID = -8140540064410029902L;
 
  /** Default constructor. */
  public LDAPEntrySelectionPanel()
  {
    super();
  }
 
  /**
   * Updates the tree selection model to allow multiple selection or not.
   * @param multiple whether the tree should allow multiple selection or not.
   */
  public void setMultipleSelection(boolean multiple)
  {
    treePane.getTree().getSelectionModel().setSelectionMode(multiple ?
        TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION :
          TreeSelectionModel.SINGLE_TREE_SELECTION);
  }
 
  @Override
  public void toBeDisplayed(boolean visible)
  {
    super.toBeDisplayed(visible);
    if (visible)
    {
      dns = new String[0];
    }
  }
 
  @Override
  public LocalizableMessage getTitle()
  {
    return title;
  }
 
  @Override
  public void okClicked()
  {
    dns = retrieveDNs();
    super.closeClicked();
  }
 
  /**
   * Returns the selected DN's in an array of Strings.  The array is never
   * <CODE>null</CODE> but might be empty.
   * @return the selected DN's.
   */
  public String[] getDNs()
  {
    return dns;
  }
 
  private String[] retrieveDNs()
  {
    String[] dns;
    TreePath[] paths = treePane.getTree().getSelectionPaths();
    if (paths != null)
    {
      dns = new String[paths.length];
      for (int i=0; i<paths.length; i++)
      {
        dns[i] = ((BasicNode) paths[i].getLastPathComponent()).getDN().toString();
      }
    }
    else
    {
      dns = new String[0];
    }
    return dns;
  }
 
  @Override
  public GenericDialog.ButtonType getBrowseButtonType()
  {
    return GenericDialog.ButtonType.OK_CANCEL;
  }
 
  @Override
  protected Component createMainPanel()
  {
    JComponent p = createTreePane();
 
    final JTree tree = treePane.getTree();
    tree.getSelectionModel().addTreeSelectionListener(
    new TreeSelectionListener()
    {
      @Override
      public void valueChanged(TreeSelectionEvent ev)
      {
        TreePath[] paths = tree.getSelectionPaths();
        setEnabledOK(paths != null && paths.length > 0);
      }
    });
    MouseListener mouseListener = new MouseAdapter() {
      @Override
      public void mousePressed(MouseEvent e) {
        int selRow = tree.getRowForLocation(e.getX(), e.getY());
        if (selRow != -1 && e.getClickCount() == 2)
        {
          okClicked();
        }
      }
    };
    tree.addMouseListener(mouseListener);
 
    JScrollPane treeScroll = Utilities.createScrollPane(p);
    treeScroll.setPreferredSize(
        new Dimension(treeScroll.getPreferredSize().width + 30,
            4 * treeScroll.getPreferredSize().height));
 
    return treeScroll;
  }
 
  /**
   * Returns the last filter set with the setFilter method.
   * @return the last filter set with the setFilter method.
   */
  public Filter getFilter()
  {
    return f;
  }
 
  /**
   * Sets the filter to be used when the panel is displayed.
   * @param filter the filter.
   */
  public void setFilter(Filter filter)
  {
    f = filter;
    switch (f)
    {
    case USERS:
      filterAttribute.setSelectedItem(USER_FILTER);
      super.filter.setText("*");
      break;
    case GROUPS:
      filterAttribute.setSelectedItem(GROUP_FILTER);
      super.filter.setText("*");
      break;
    case DYNAMIC_GROUPS:
      filterAttribute.setSelectedItem(LDAP_FILTER);
      super.filter.setText("objectClass=groupOfURLs");
      break;
    case STATIC_GROUPS:
      filterAttribute.setSelectedItem(LDAP_FILTER);
      super.filter.setText("(|(objectClass=groupOfNames)" +
        "(objectClass=groupOfEntries)(objectClass=groupOfUniqueNames))");
      break;
    case DEFAULT:
      Object o = filterAttribute.getItemAt(0);
      filterAttribute.setSelectedItem(o);
      super.filter.setText("");
      break;
    }
    if (controller != null)
    {
      applyButtonClicked();
    }
  }
 
  /**
   * Sets the title that will be displayed in the dialog containing this panel.
   * @param title the title.
   */
  public void setTitle(LocalizableMessage title)
  {
    this.title = title;
    Window w = Utilities.getParentDialog(this);
    if (w instanceof GenericDialog)
    {
      ((GenericDialog)w).updateTitle();
    }
    else if (w instanceof GenericFrame)
    {
      ((GenericFrame)w).updateTitle();
    }
  }
}