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