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 2009-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2015 ForgeRock AS.
026 */
027package org.opends.guitools.controlpanel.ui;
028
029import static org.opends.messages.AdminToolMessages.*;
030
031import java.awt.Component;
032import java.awt.GridBagConstraints;
033import java.awt.event.ActionEvent;
034import java.awt.event.ActionListener;
035import java.util.HashSet;
036import java.util.LinkedHashSet;
037import java.util.Set;
038import java.util.SortedSet;
039import java.util.TreeSet;
040
041import javax.swing.Box;
042import javax.swing.JButton;
043import javax.swing.JLabel;
044import javax.swing.JScrollPane;
045import javax.swing.JTable;
046import javax.swing.SwingConstants;
047import javax.swing.table.DefaultTableCellRenderer;
048
049import org.opends.guitools.controlpanel.datamodel.BackendDescriptor;
050import org.opends.guitools.controlpanel.datamodel.
051 DBEnvironmentMonitoringTableModel;
052import org.opends.guitools.controlpanel.datamodel.ServerDescriptor;
053import org.opends.guitools.controlpanel.util.Utilities;
054import org.opends.server.util.ServerConstants;
055
056/**
057 * The panel displaying the database environment monitor panel.
058 */
059public class DBEnvironmentMonitoringPanel extends GeneralMonitoringPanel
060{
061  private static final long serialVersionUID = 9031734563723229830L;
062
063  private JTable table;
064  private DBEnvironmentMonitoringTableModel tableModel;
065  private JScrollPane scroll;
066  private JLabel noDBsFound;
067  private JLabel noMonitoringFound;
068  private JButton showOperations;
069
070  private LinkedHashSet<String> attributes = new LinkedHashSet<>();
071  private LinkedHashSet<String> allAttributes = new LinkedHashSet<>();
072
073  private MonitoringAttributesViewPanel<String> operationViewPanel;
074  private GenericDialog operationViewDlg;
075
076  /**
077   * Default constructor.
078   */
079  public DBEnvironmentMonitoringPanel()
080  {
081    super();
082    createLayout();
083  }
084
085  /** {@inheritDoc} */
086  public Component getPreferredFocusComponent()
087  {
088    return table;
089  }
090
091  /**
092   * Creates the layout of the panel (but the contents are not populated here).
093   */
094  private void createLayout()
095  {
096    GridBagConstraints gbc = new GridBagConstraints();
097    JLabel lTitle = Utilities.createTitleLabel(
098        INFO_CTRL_PANEL_DB_ENVIRONMENT.get());
099    gbc.fill = GridBagConstraints.NONE;
100    gbc.anchor = GridBagConstraints.WEST;
101    gbc.gridwidth = 2;
102    gbc.gridx = 0;
103    gbc.gridy = 0;
104    gbc.insets.top = 5;
105    gbc.insets.bottom = 7;
106    add(lTitle, gbc);
107
108    gbc.insets.bottom = 0;
109    gbc.insets.top = 10;
110    gbc.gridy ++;
111    gbc.anchor = GridBagConstraints.WEST;
112    gbc.gridwidth = 1;
113    showOperations =
114      Utilities.createButton(INFO_CTRL_PANEL_OPERATIONS_VIEW.get());
115    showOperations.addActionListener(new ActionListener()
116    {
117      public void actionPerformed(ActionEvent ev)
118      {
119        operationViewClicked();
120      }
121    });
122    showOperations.setVisible(false);
123    gbc.gridx = 0;
124    gbc.weightx = 1.0;
125    gbc.fill = GridBagConstraints.HORIZONTAL;
126    add(Box.createHorizontalGlue(), gbc);
127    gbc.gridx ++;
128    gbc.weightx = 0.0;
129    add(showOperations, gbc);
130
131    gbc.gridx = 0;
132    gbc.gridy ++;
133    gbc.gridwidth = 2;
134    tableModel = new DBEnvironmentMonitoringTableModel();
135    tableModel.setAttributes(attributes);
136    table = Utilities.createSortableTable(tableModel,
137        new DefaultTableCellRenderer());
138    scroll = Utilities.createScrollPane(table);
139    updateTableSize();
140    gbc.fill = GridBagConstraints.BOTH;
141    gbc.weightx = 1.0;
142    gbc.weighty = 1.0;
143    add(scroll, gbc);
144    noDBsFound = Utilities.createDefaultLabel(
145        INFO_CTRL_PANEL_NO_DBS_FOUND.get());
146    noDBsFound.setHorizontalAlignment(SwingConstants.CENTER);
147    add(noDBsFound, gbc);
148    noMonitoringFound = Utilities.createDefaultLabel(
149        INFO_CTRL_PANEL_NO_DB_MONITORING_FOUND.get());
150    noMonitoringFound.setHorizontalAlignment(SwingConstants.CENTER);
151    add(noMonitoringFound, gbc);
152
153    setBorder(PANEL_BORDER);
154  }
155
156  /**
157   * Updates the contents of the panel.  The code assumes that this is being
158   * called from the event thread.
159   *
160   */
161  public void updateContents()
162  {
163    boolean backendsFound = false;
164    ServerDescriptor server = null;
165    if (getInfo() != null)
166    {
167      server = getInfo().getServerDescriptor();
168    }
169    Set<BackendDescriptor> dbBackends = new HashSet<>();
170    boolean updateAttributes = allAttributes.isEmpty();
171    SortedSet<String> sortedAttrNames = new TreeSet<>();
172    if (server != null)
173    {
174      for (BackendDescriptor backend : server.getBackends())
175      {
176        if (backend.getType() == BackendDescriptor.Type.LOCAL_DB
177            || backend.getType() == BackendDescriptor.Type.PLUGGABLE)
178        {
179          dbBackends.add(backend);
180          if (updateAttributes)
181          {
182            sortedAttrNames.addAll(getMonitoringAttributes(backend));
183          }
184        }
185      }
186      backendsFound = !dbBackends.isEmpty();
187    }
188    if (updateAttributes)
189    {
190      allAttributes.addAll(sortedAttrNames);
191      for (String attrName : allAttributes)
192      {
193        attributes.add(attrName);
194        if (attributes.size() == 5)
195        {
196          break;
197        }
198      }
199      if (!attributes.isEmpty())
200      {
201        setOperationsToDisplay(attributes);
202        updateTableSize();
203      }
204    }
205    tableModel.setData(dbBackends);
206    showOperations.setVisible(backendsFound);
207    scroll.setVisible(backendsFound && !allAttributes.isEmpty());
208    noDBsFound.setVisible(!backendsFound);
209    noMonitoringFound.setVisible(backendsFound && allAttributes.isEmpty());
210    showOperations.setVisible(!allAttributes.isEmpty());
211  }
212
213
214  private void updateTableSize()
215  {
216    Utilities.updateTableSizes(table, 8);
217    Utilities.updateScrollMode(scroll, table);
218  }
219
220  /**
221   * Displays a dialog allowing the user to select which operations to display.
222   *
223   */
224  private void operationViewClicked()
225  {
226    if (operationViewDlg == null)
227    {
228      operationViewPanel = MonitoringAttributesViewPanel.createStringInstance(
229          allAttributes);
230      operationViewDlg = new GenericDialog(Utilities.getFrame(this),
231          operationViewPanel);
232      operationViewDlg.setModal(true);
233      Utilities.centerGoldenMean(operationViewDlg,
234          Utilities.getParentDialog(this));
235    }
236    operationViewPanel.setSelectedAttributes(attributes);
237    operationViewDlg.setVisible(true);
238    if (!operationViewPanel.isCanceled())
239    {
240      attributes = operationViewPanel.getAttributes();
241      setOperationsToDisplay(attributes);
242      updateTableSize();
243    }
244  }
245
246  private void setOperationsToDisplay(
247      LinkedHashSet<String> attributes)
248  {
249    this.attributes = attributes;
250    tableModel.setAttributes(attributes);
251    tableModel.forceDataStructureChange();
252  }
253
254  private Set<String> getMonitoringAttributes(BackendDescriptor backend)
255  {
256    Set<String> attrNames = new HashSet<>();
257    if (backend.getMonitoringEntry() != null)
258    {
259      Set<String> allNames = backend.getMonitoringEntry().getAttributeNames();
260      for (String attrName : allNames)
261      {
262        if (!attrName.equalsIgnoreCase(
263            ServerConstants.OBJECTCLASS_ATTRIBUTE_TYPE_NAME) &&
264            !attrName.equalsIgnoreCase(ServerConstants.ATTR_COMMON_NAME))
265        {
266          attrNames.add(attrName);
267        }
268      }
269    }
270    return attrNames;
271  }
272}