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