001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2008-2009 Sun Microsystems, Inc.
015 * Portions Copyright 2013-2016 ForgeRock AS.
016 */
017package org.opends.guitools.controlpanel.ui;
018
019import java.awt.Component;
020import java.awt.Dimension;
021import java.awt.Window;
022import java.awt.event.MouseAdapter;
023import java.awt.event.MouseEvent;
024import java.awt.event.MouseListener;
025
026import javax.swing.JComponent;
027import javax.swing.JScrollPane;
028import javax.swing.JTree;
029import javax.swing.event.TreeSelectionEvent;
030import javax.swing.event.TreeSelectionListener;
031import javax.swing.tree.TreePath;
032import javax.swing.tree.TreeSelectionModel;
033
034import org.forgerock.i18n.LocalizableMessage;
035import org.opends.guitools.controlpanel.ui.nodes.BasicNode;
036import org.opends.guitools.controlpanel.util.Utilities;
037
038/**
039 * A basic panel that contains a browser.  It is used in general in panels that
040 * require to provide some DNs of existing entries: we allow the user to launch
041 * a browser to select entries.
042 */
043public class LDAPEntrySelectionPanel extends AbstractBrowseEntriesPanel
044{
045  private LocalizableMessage title;
046  private Filter f;
047  private String[] dns;
048
049  /**
050   * The values of the filters that will be used when opening the dialog where
051   * this panel is contained.  For instance if the filter is set to Filter.USERS
052   * the panel will display only users when the dialog appears.
053   */
054  public enum Filter
055  {
056    /** Display users. */
057    USERS,
058    /** Display groups. */
059    GROUPS,
060    /** Display Dynamic Groups. */
061    DYNAMIC_GROUPS,
062    /** Display Static Groups. */
063    STATIC_GROUPS,
064    /** Default filter (all entries). */
065    DEFAULT
066  }
067
068  private static final long serialVersionUID = -8140540064410029902L;
069
070  /** Default constructor. */
071  public LDAPEntrySelectionPanel()
072  {
073    super();
074  }
075
076  /**
077   * Updates the tree selection model to allow multiple selection or not.
078   * @param multiple whether the tree should allow multiple selection or not.
079   */
080  public void setMultipleSelection(boolean multiple)
081  {
082    treePane.getTree().getSelectionModel().setSelectionMode(multiple ?
083        TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION :
084          TreeSelectionModel.SINGLE_TREE_SELECTION);
085  }
086
087  @Override
088  public void toBeDisplayed(boolean visible)
089  {
090    super.toBeDisplayed(visible);
091    if (visible)
092    {
093      dns = new String[0];
094    }
095  }
096
097  @Override
098  public LocalizableMessage getTitle()
099  {
100    return title;
101  }
102
103  @Override
104  public void okClicked()
105  {
106    dns = retrieveDNs();
107    super.closeClicked();
108  }
109
110  /**
111   * Returns the selected DN's in an array of Strings.  The array is never
112   * <CODE>null</CODE> but might be empty.
113   * @return the selected DN's.
114   */
115  public String[] getDNs()
116  {
117    return dns;
118  }
119
120  private String[] retrieveDNs()
121  {
122    String[] dns;
123    TreePath[] paths = treePane.getTree().getSelectionPaths();
124    if (paths != null)
125    {
126      dns = new String[paths.length];
127      for (int i=0; i<paths.length; i++)
128      {
129        dns[i] = ((BasicNode)paths[i].getLastPathComponent()).getDN();
130      }
131    }
132    else
133    {
134      dns = new String[0];
135    }
136    return dns;
137  }
138
139  @Override
140  public GenericDialog.ButtonType getBrowseButtonType()
141  {
142    return GenericDialog.ButtonType.OK_CANCEL;
143  }
144
145  @Override
146  protected Component createMainPanel()
147  {
148    JComponent p = createTreePane();
149
150    final JTree tree = treePane.getTree();
151    tree.getSelectionModel().addTreeSelectionListener(
152    new TreeSelectionListener()
153    {
154      @Override
155      public void valueChanged(TreeSelectionEvent ev)
156      {
157        TreePath[] paths = tree.getSelectionPaths();
158        setEnabledOK(paths != null && paths.length > 0);
159      }
160    });
161    MouseListener mouseListener = new MouseAdapter() {
162      @Override
163      public void mousePressed(MouseEvent e) {
164        int selRow = tree.getRowForLocation(e.getX(), e.getY());
165        if (selRow != -1 && e.getClickCount() == 2)
166        {
167          okClicked();
168        }
169      }
170    };
171    tree.addMouseListener(mouseListener);
172
173    JScrollPane treeScroll = Utilities.createScrollPane(p);
174    treeScroll.setPreferredSize(
175        new Dimension(treeScroll.getPreferredSize().width + 30,
176            4 * treeScroll.getPreferredSize().height));
177
178    return treeScroll;
179  }
180
181  /**
182   * Returns the last filter set with the setFilter method.
183   * @return the last filter set with the setFilter method.
184   */
185  public Filter getFilter()
186  {
187    return f;
188  }
189
190  /**
191   * Sets the filter to be used when the panel is displayed.
192   * @param filter the filter.
193   */
194  public void setFilter(Filter filter)
195  {
196    f = filter;
197    switch (f)
198    {
199    case USERS:
200      filterAttribute.setSelectedItem(USER_FILTER);
201      super.filter.setText("*");
202      break;
203    case GROUPS:
204      filterAttribute.setSelectedItem(GROUP_FILTER);
205      super.filter.setText("*");
206      break;
207    case DYNAMIC_GROUPS:
208      filterAttribute.setSelectedItem(LDAP_FILTER);
209      super.filter.setText("objectClass=groupOfURLs");
210      break;
211    case STATIC_GROUPS:
212      filterAttribute.setSelectedItem(LDAP_FILTER);
213      super.filter.setText("(|(objectClass=groupOfNames)" +
214        "(objectClass=groupOfEntries)(objectClass=groupOfUniqueNames))");
215      break;
216    case DEFAULT:
217      Object o = filterAttribute.getItemAt(0);
218      filterAttribute.setSelectedItem(o);
219      super.filter.setText("");
220      break;
221    }
222    if (controller != null)
223    {
224      applyButtonClicked();
225    }
226  }
227
228  /**
229   * Sets the title that will be displayed in the dialog containing this panel.
230   * @param title the title.
231   */
232  public void setTitle(LocalizableMessage title)
233  {
234    this.title = title;
235    Window w = Utilities.getParentDialog(this);
236    if (w instanceof GenericDialog)
237    {
238      ((GenericDialog)w).updateTitle();
239    }
240    else if (w instanceof GenericFrame)
241    {
242      ((GenericFrame)w).updateTitle();
243    }
244  }
245}
246