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 2009-2010 Sun Microsystems, Inc. 015 * Portions Copyright 2014-2015 ForgeRock AS. 016 */ 017package org.opends.guitools.controlpanel.ui; 018 019import static org.opends.messages.AdminToolMessages.*; 020import static org.opends.server.util.CollectionUtils.*; 021 022import java.awt.Component; 023import java.awt.Dimension; 024import java.awt.GridBagConstraints; 025import java.awt.GridBagLayout; 026import java.awt.Insets; 027import java.awt.event.ActionEvent; 028import java.awt.event.ActionListener; 029import java.util.Collection; 030import java.util.LinkedHashSet; 031 032import javax.swing.Box; 033import javax.swing.JButton; 034import javax.swing.JCheckBox; 035import javax.swing.JPanel; 036import javax.swing.JScrollPane; 037 038import org.opends.guitools.controlpanel.datamodel.MonitoringAttributes; 039import org.opends.guitools.controlpanel.event.ConfigurationChangeEvent; 040import org.opends.guitools.controlpanel.event.ScrollPaneBorderListener; 041import org.opends.guitools.controlpanel.util.Utilities; 042import org.forgerock.i18n.LocalizableMessage; 043 044/** 045* The panel that allows the user to select which attributes must be displayed 046* in the traffic monitoring tables. 047* 048* @param <T> the type of the objects that this panel manages. For now it only 049* manages String and MonitoringAttribute objects. 050*/ 051public class MonitoringAttributesViewPanel<T> extends StatusGenericPanel 052{ 053 private static final long serialVersionUID = 6462932163745559L; 054 055 private LinkedHashSet<T> selectedAttributes = new LinkedHashSet<>(); 056 private LinkedHashSet<T> monitoringAttributes; 057 private boolean isCanceled = true; 058 059 /** 060 * Note: the order of the checkboxes and the elements in the Attributes 061 * enumeration will be the same. 062 */ 063 private JCheckBox[] checkboxes = {}; 064 065 private JButton selectAll; 066 private JButton selectNone; 067 068 /** 069 * Creates an instance of this panel that uses String as attributes. 070 * @param attributes the list of possible attributes. 071 * @return an instance of this panel that uses String as attributes. 072 */ 073 public static MonitoringAttributesViewPanel<String> createStringInstance(LinkedHashSet<String> attributes) 074 { 075 return new MonitoringAttributesViewPanel<>(attributes); 076 } 077 078 /** 079 * Creates an instance of this panel that uses MonitoringAttributes as 080 * attributes. 081 * @param attributes the list of possible attributes. 082 * @return an instance of this panel that uses MonitoringAttributes as 083 * attributes. 084 */ 085 public static MonitoringAttributesViewPanel<MonitoringAttributes> 086 createMonitoringAttributesInstance(LinkedHashSet<MonitoringAttributes> attributes) 087 { 088 return new MonitoringAttributesViewPanel<>(attributes); 089 } 090 091 /** 092 * Creates an instance of this panel that uses LocalizableMessage as 093 * attributes. 094 * @param attributes the list of possible attributes. 095 * @return an instance of this panel that uses LocalizableMessage as attributes. 096 */ 097 public static MonitoringAttributesViewPanel<LocalizableMessage> 098 createMessageInstance(LinkedHashSet<LocalizableMessage> attributes) 099 { 100 return new MonitoringAttributesViewPanel<>(attributes); 101 } 102 103 /** {@inheritDoc} */ 104 @Override 105 public boolean requiresScroll() 106 { 107 return false; 108 } 109 110 /** 111 * Default constructor. 112 * @param attributes the attributes that will be proposed to the user. 113 * 114 */ 115 protected MonitoringAttributesViewPanel(LinkedHashSet<T> attributes) 116 { 117 monitoringAttributes = new LinkedHashSet<>(attributes); 118 createLayout(); 119 } 120 121 /** 122 * Sets the attributes that must be selected in this dialog. 123 * @param selectedAttributes the selected attributes. 124 */ 125 public void setSelectedAttributes( 126 Collection<T> selectedAttributes) 127 { 128 int i = 0; 129 for (T attribute : monitoringAttributes) 130 { 131 checkboxes[i].setSelected(selectedAttributes.contains(attribute)); 132 i++; 133 } 134 } 135 136 /** 137 * Creates the layout of the panel (but the contents are not populated here). 138 */ 139 private void createLayout() 140 { 141 GridBagConstraints gbc = new GridBagConstraints(); 142 gbc.fill = GridBagConstraints.HORIZONTAL; 143 gbc.gridy = 0; 144 145 gbc.gridwidth = 2; 146 gbc.gridx = 0; 147 add(Utilities.createPrimaryLabel(INFO_CTRL_PANEL_OPERATION_VIEW_LABEL.get()), gbc); 148 gbc.gridy ++; 149 gbc.gridwidth = 1; 150 gbc.insets.top = 10; 151 152 JPanel checkBoxPanel = new JPanel(new GridBagLayout()); 153 checkBoxPanel.setOpaque(false); 154 JScrollPane scroll = Utilities.createBorderLessScrollBar(checkBoxPanel); 155 ScrollPaneBorderListener.createFullBorderListener(scroll); 156 157 checkboxes = new JCheckBox[monitoringAttributes.size()]; 158 159 int i = 0; 160 for (T attribute : monitoringAttributes) 161 { 162 LocalizableMessage m = getMessage(attribute); 163 checkboxes[i] = Utilities.createCheckBox(m); 164 i++; 165 } 166 selectAll = Utilities.createButton(INFO_CTRL_PANEL_SELECT_ALL_BUTTON.get()); 167 selectAll.addActionListener(new ActionListener() 168 { 169 public void actionPerformed(ActionEvent ev) 170 { 171 for (JCheckBox cb : checkboxes) 172 { 173 cb.setSelected(true); 174 } 175 } 176 }); 177 178 selectNone = Utilities.createButton(INFO_CTRL_PANEL_CLEAR_SELECTION_BUTTON.get()); 179 selectNone.addActionListener(new ActionListener() 180 { 181 public void actionPerformed(ActionEvent ev) 182 { 183 for (JCheckBox cb : checkboxes) 184 { 185 cb.setSelected(false); 186 } 187 } 188 }); 189 190 gbc.weightx = 1.0; 191 gbc.weighty = 1.0; 192 gbc.gridheight = 3; 193 gbc.fill = GridBagConstraints.BOTH; 194 add(scroll, gbc); 195 196 gbc.gridx = 1; 197 gbc.weightx = 0.0; 198 gbc.weighty = 0.0; 199 gbc.insets.left = 10; 200 gbc.gridheight = 1; 201 add(selectAll, gbc); 202 gbc.gridy ++; 203 gbc.insets.top = 10; 204 add(selectNone, gbc); 205 gbc.gridy ++; 206 gbc.weighty = 1.0; 207 add(Box.createVerticalGlue(), gbc); 208 209 gbc = new GridBagConstraints(); 210 gbc.gridy = 0; 211 gbc.gridwidth = 1; 212 int preferredViewHeight = -1; 213 for (JCheckBox cb : checkboxes) 214 { 215 gbc.gridx = 0; 216 gbc.weightx = 0.0; 217 gbc.anchor = GridBagConstraints.WEST; 218 gbc.fill = GridBagConstraints.NONE; 219 checkBoxPanel.add(cb, gbc); 220 gbc.gridx = 1; 221 gbc.weightx = 1.0; 222 gbc.fill = GridBagConstraints.HORIZONTAL; 223 checkBoxPanel.add(Box.createHorizontalGlue(), gbc); 224 gbc.insets.top = 10; 225 gbc.gridy ++; 226 if (gbc.gridy == 15) 227 { 228 preferredViewHeight = checkBoxPanel.getPreferredSize().height; 229 } 230 } 231 if (preferredViewHeight < 0) 232 { 233 preferredViewHeight = checkBoxPanel.getPreferredSize().height; 234 } 235 gbc.insets = new Insets(0, 0, 0, 0); 236 gbc.gridx = 0; 237 gbc.gridwidth = 2; 238 gbc.fill = GridBagConstraints.VERTICAL; 239 gbc.weighty = 1.0; 240 checkBoxPanel.add(Box.createVerticalGlue(), gbc); 241 scroll.getViewport().setPreferredSize( 242 new Dimension(checkBoxPanel.getPreferredSize().width + 15, preferredViewHeight)); 243 } 244 245 /** {@inheritDoc} */ 246 public LocalizableMessage getTitle() 247 { 248 return INFO_CTRL_PANEL_ATTRIBUTE_VIEW_OPTIONS_TITLE.get(); 249 } 250 251 /** {@inheritDoc} */ 252 public void configurationChanged(ConfigurationChangeEvent ev) 253 { 254 } 255 256 /** {@inheritDoc} */ 257 public Component getPreferredFocusComponent() 258 { 259 return checkboxes[0]; 260 } 261 262 /** {@inheritDoc} */ 263 public void toBeDisplayed(boolean visible) 264 { 265 if (visible) 266 { 267 isCanceled = true; 268 } 269 } 270 271 /** {@inheritDoc} */ 272 public void okClicked() 273 { 274 // Check that at least one checkbox is selected. 275 selectedAttributes.clear(); 276 int i = 0; 277 for (T attribute : monitoringAttributes) 278 { 279 if (checkboxes[i].isSelected()) 280 { 281 selectedAttributes.add(attribute); 282 } 283 i++; 284 } 285 if (selectedAttributes.isEmpty()) 286 { 287 super.displayErrorDialog(newArrayList(INFO_CTRL_PANEL_NO_OPERATION_SELECTED.get())); 288 } 289 else 290 { 291 isCanceled = false; 292 super.closeClicked(); 293 } 294 } 295 296 /** {@inheritDoc} */ 297 public GenericDialog.ButtonType getButtonType() 298 { 299 return GenericDialog.ButtonType.OK_CANCEL; 300 } 301 302 /** 303 * Returns <CODE>true</CODE> if the user closed the dialog by cancelling it 304 * and <CODE>false</CODE> otherwise. 305 * @return <CODE>true</CODE> if the user closed the dialog by cancelling it 306 * and <CODE>false</CODE> otherwise. 307 */ 308 public boolean isCanceled() 309 { 310 return isCanceled; 311 } 312 313 /** 314 * Returns the list of attributes that the user selected. 315 * @return the list of attributes that the user selected. 316 */ 317 public LinkedHashSet<T> getAttributes() 318 { 319 return selectedAttributes; 320 } 321 322 /** 323 * Returns the message for the provided attribute. 324 * @param attribute the attribute. 325 * @return the message for the provided attribute. 326 */ 327 protected LocalizableMessage getMessage(T attribute) 328 { 329 LocalizableMessage m; 330 if (attribute instanceof MonitoringAttributes) 331 { 332 m = ((MonitoringAttributes)attribute).getMessage(); 333 } 334 else if (attribute instanceof LocalizableMessage) 335 { 336 m = (LocalizableMessage)attribute; 337 } 338 else 339 { 340 m = LocalizableMessage.raw(attribute.toString()); 341 } 342 return m; 343 } 344}