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