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 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2015 ForgeRock AS.
016 */
017package org.opends.guitools.controlpanel.ui;
018
019import java.awt.Component;
020import java.awt.GridBagConstraints;
021import java.util.ArrayList;
022import java.util.List;
023
024import javax.swing.Box;
025import javax.swing.JLabel;
026
027import org.opends.guitools.controlpanel.datamodel.CustomSearchResult;
028import org.opends.guitools.controlpanel.datamodel.MonitoringAttributes;
029import org.opends.guitools.controlpanel.datamodel.ServerDescriptor;
030import org.opends.guitools.controlpanel.util.Utilities;
031import org.opends.server.util.CollectionUtils;
032
033import static org.opends.guitools.controlpanel.datamodel.BasicMonitoringAttributes.*;
034import static org.opends.guitools.controlpanel.util.Utilities.*;
035import static org.opends.messages.AdminToolMessages.*;
036/**
037 * The panel displaying the entry caches monitor panel.
038 */
039public class EntryCachesMonitoringPanel extends GeneralMonitoringPanel
040{
041  private static final long serialVersionUID = 9031734563700069830L;
042  private static List<MonitoringAttributes> ngOperations = CollectionUtils.<MonitoringAttributes> newArrayList(
043      ENTRY_CACHE_TRIES, ENTRY_CACHE_HITS, ENTRY_CACHE_HIT_RATIO, CURRENT_ENTRY_CACHE_SIZE, MAX_ENTRY_CACHE_SIZE,
044      CURRENT_ENTRY_CACHE_COUNT, MAX_ENTRY_CACHE_COUNT);
045
046  private ArrayList<JLabel> monitoringLabels = new ArrayList<>();
047  {
048    for (int i=0; i<ngOperations.size(); i++)
049    {
050      monitoringLabels.add(Utilities.createDefaultLabel());
051    }
052  }
053  private ArrayList<JLabel> labels = new ArrayList<>();
054  {
055    for (int i=0; i<ngOperations.size(); i++)
056    {
057      labels.add(Utilities.createPrimaryLabel(getLabel(ngOperations.get(i))));
058    }
059  }
060
061  /**
062   * Default constructor.
063   */
064  public EntryCachesMonitoringPanel()
065  {
066    super();
067    createLayout();
068  }
069
070  /** {@inheritDoc} */
071  @Override
072  public Component getPreferredFocusComponent()
073  {
074    return monitoringLabels.get(0);
075  }
076
077  /**
078   * Creates the layout of the panel (but the contents are not populated here).
079   */
080  private void createLayout()
081  {
082    GridBagConstraints gbc = new GridBagConstraints();
083    JLabel lTitle = Utilities.createTitleLabel(
084        INFO_CTRL_PANEL_ENTRY_CACHES.get());
085    gbc.fill = GridBagConstraints.NONE;
086    gbc.anchor = GridBagConstraints.WEST;
087    gbc.gridwidth = 2;
088    gbc.gridx = 0;
089    gbc.gridy = 0;
090    gbc.insets.top = 5;
091    gbc.insets.bottom = 7;
092    add(lTitle, gbc);
093
094    gbc.insets.bottom = 0;
095    gbc.insets.top = 10;
096    gbc.gridy ++;
097    gbc.anchor = GridBagConstraints.WEST;
098    gbc.gridwidth = 1;
099    for (int i=0; i<ngOperations.size(); i++)
100    {
101      gbc.gridy ++;
102      gbc.insets.left = 0;
103      gbc.gridx = 0;
104      gbc.weightx = 0.0;
105      gbc.gridwidth = 1;
106      add(labels.get(i), gbc);
107      gbc.insets.left = 10;
108      gbc.gridx = 1;
109      gbc.gridwidth = 2;
110      add(monitoringLabels.get(i), gbc);
111    }
112
113    gbc.gridx = 0;
114    gbc.gridy ++;
115    gbc.fill = GridBagConstraints.BOTH;
116    gbc.weightx = 1.0;
117    gbc.weighty = 1.0;
118    gbc.gridwidth = 3;
119    add(Box.createGlue(), gbc);
120
121    setBorder(PANEL_BORDER);
122  }
123
124  /**
125   * Updates the contents of the panel.
126   *
127   */
128  public void updateContents()
129  {
130    ServerDescriptor server = null;
131    if (getInfo() != null)
132    {
133      server = getInfo().getServerDescriptor();
134    }
135    CustomSearchResult csr = null;
136    if (server != null)
137    {
138      csr = server.getEntryCachesMonitor();
139    }
140    if (csr != null)
141    {
142      updateMonitoringInfo(ngOperations, monitoringLabels, csr);
143      int index = 0;
144      for (MonitoringAttributes attr : ngOperations)
145      {
146        if (getFirstValueAsString(csr, attr.getAttributeName()) == null)
147        {
148          monitoringLabels.get(index).setVisible(false);
149          labels.get(index).setVisible(false);
150        }
151        index ++;
152      }
153      revalidate();
154      repaint();
155    }
156    else
157    {
158      for (JLabel l : monitoringLabels)
159      {
160        l.setText(NO_VALUE_SET.toString());
161      }
162    }
163  }
164}