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-2015 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 /** {@inheritDoc} */ 088 @Override 089 public void toBeDisplayed(boolean visible) 090 { 091 super.toBeDisplayed(visible); 092 if (visible) 093 { 094 dns = new String[0]; 095 } 096 } 097 098 /** {@inheritDoc} */ 099 @Override 100 public LocalizableMessage getTitle() 101 { 102 return title; 103 } 104 105 /** {@inheritDoc} */ 106 @Override 107 public void okClicked() 108 { 109 dns = retrieveDNs(); 110 super.closeClicked(); 111 } 112 113 /** 114 * Returns the selected DN's in an array of Strings. The array is never 115 * <CODE>null</CODE> but might be empty. 116 * @return the selected DN's. 117 */ 118 public String[] getDNs() 119 { 120 return dns; 121 } 122 123 private String[] retrieveDNs() 124 { 125 String[] dns; 126 TreePath[] paths = treePane.getTree().getSelectionPaths(); 127 if (paths != null) 128 { 129 dns = new String[paths.length]; 130 for (int i=0; i<paths.length; i++) 131 { 132 dns[i] = ((BasicNode)paths[i].getLastPathComponent()).getDN(); 133 } 134 } 135 else 136 { 137 dns = new String[0]; 138 } 139 return dns; 140 } 141 142 /** {@inheritDoc} */ 143 @Override 144 public GenericDialog.ButtonType getBrowseButtonType() 145 { 146 return GenericDialog.ButtonType.OK_CANCEL; 147 } 148 149 /** {@inheritDoc} */ 150 @Override 151 protected Component createMainPanel() 152 { 153 JComponent p = createTreePane(); 154 155 final JTree tree = treePane.getTree(); 156 tree.getSelectionModel().addTreeSelectionListener( 157 new TreeSelectionListener() 158 { 159 /** {@inheritDoc} */ 160 @Override 161 public void valueChanged(TreeSelectionEvent ev) 162 { 163 TreePath[] paths = tree.getSelectionPaths(); 164 setEnabledOK(paths != null && paths.length > 0); 165 } 166 }); 167 MouseListener mouseListener = new MouseAdapter() { 168 /** {@inheritDoc} */ 169 @Override 170 public void mousePressed(MouseEvent e) { 171 int selRow = tree.getRowForLocation(e.getX(), e.getY()); 172 if (selRow != -1 && e.getClickCount() == 2) 173 { 174 okClicked(); 175 } 176 } 177 }; 178 tree.addMouseListener(mouseListener); 179 180 JScrollPane treeScroll = Utilities.createScrollPane(p); 181 treeScroll.setPreferredSize( 182 new Dimension(treeScroll.getPreferredSize().width + 30, 183 4 * treeScroll.getPreferredSize().height)); 184 185 return treeScroll; 186 } 187 188 /** 189 * Returns the last filter set with the setFilter method. 190 * @return the last filter set with the setFilter method. 191 */ 192 public Filter getFilter() 193 { 194 return f; 195 } 196 197 /** 198 * Sets the filter to be used when the panel is displayed. 199 * @param filter the filter. 200 */ 201 public void setFilter(Filter filter) 202 { 203 f = filter; 204 switch (f) 205 { 206 case USERS: 207 filterAttribute.setSelectedItem(USER_FILTER); 208 super.filter.setText("*"); 209 break; 210 case GROUPS: 211 filterAttribute.setSelectedItem(GROUP_FILTER); 212 super.filter.setText("*"); 213 break; 214 case DYNAMIC_GROUPS: 215 filterAttribute.setSelectedItem(LDAP_FILTER); 216 super.filter.setText("objectClass=groupOfURLs"); 217 break; 218 case STATIC_GROUPS: 219 filterAttribute.setSelectedItem(LDAP_FILTER); 220 super.filter.setText("(|(objectClass=groupOfNames)" + 221 "(objectClass=groupOfEntries)(objectClass=groupOfUniqueNames))"); 222 break; 223 case DEFAULT: 224 Object o = filterAttribute.getItemAt(0); 225 filterAttribute.setSelectedItem(o); 226 super.filter.setText(""); 227 break; 228 } 229 if (controller != null) 230 { 231 applyButtonClicked(); 232 } 233 } 234 235 /** 236 * Sets the title that will be displayed in the dialog containing this panel. 237 * @param title the title. 238 */ 239 public void setTitle(LocalizableMessage title) 240 { 241 this.title = title; 242 Window w = Utilities.getParentDialog(this); 243 if (w instanceof GenericDialog) 244 { 245 ((GenericDialog)w).updateTitle(); 246 } 247 else if (w instanceof GenericFrame) 248 { 249 ((GenericFrame)w).updateTitle(); 250 } 251 } 252} 253