~ubuntu-branches/ubuntu/wily/389-ds-console/wily-proposed

« back to all changes in this revision

Viewing changes to src/com/netscape/admin/dirserv/panel/MonitorBasePanel.java

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2012-03-15 19:58:37 UTC
  • Revision ID: package-import@ubuntu.com-20120315195837-296zyft51thld8q7
Tags: upstream-1.2.6
ImportĀ upstreamĀ versionĀ 1.2.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** BEGIN COPYRIGHT BLOCK
 
2
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 
3
 * Copyright (C) 2005 Red Hat, Inc.
 
4
 * All rights reserved.
 
5
 * 
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation version 2 of the License.
 
9
 * 
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 * 
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 * END COPYRIGHT BLOCK **/
 
19
 
 
20
package com.netscape.admin.dirserv.panel;
 
21
 
 
22
import java.awt.*;
 
23
import java.awt.event.*;
 
24
import java.util.*;
 
25
import javax.swing.*;
 
26
import javax.swing.table.*;
 
27
import com.netscape.management.nmclf.SuiTableHeaderRenderer;
 
28
import com.netscape.management.client.*;
 
29
import com.netscape.management.client.util.*;
 
30
import com.netscape.admin.dirserv.*;
 
31
import netscape.ldap.*;
 
32
 
 
33
/**
 
34
 * Panel for Directory Server resource page
 
35
 *
 
36
 * @author  rweltman
 
37
 * @version %I%, %G%
 
38
 * @date                9/15/97
 
39
 * @see     com.netscape.admin.dirserv
 
40
 */
 
41
public class MonitorBasePanel extends RefreshablePanel {
 
42
 
 
43
        public MonitorBasePanel(IDSModel model, String title) {
 
44
                super( model, title, true );
 
45
        }
 
46
 
 
47
 
 
48
        /* Various methods used by subclasses */
 
49
 
 
50
    /**
 
51
      * Called when the panel is selected.
 
52
      */
 
53
    public void select(IResourceObject parent, IPage viewInstance) {
 
54
        if (!_isInitialized) { // first time called
 
55
            init();
 
56
            _isInitialized = true;
 
57
                }               
 
58
                _isSelected = true;
 
59
                if ( (_refreshCheckbox != null) &&
 
60
                         _refreshCheckbox.isSelected() ) {
 
61
                                startUpdater();
 
62
                }
 
63
        }
 
64
 
 
65
    /**
 
66
      * Called when the panel is deselected.
 
67
      */
 
68
    public void unselect(IResourceObject parent, IPage viewInstance) {
 
69
                //Debug.println( "MonitorBasePanel: unselect" );
 
70
                _isSelected = false;
 
71
                stopUpdater();
 
72
        }
 
73
 
 
74
        /**
 
75
         * This method is overridden in subclasses.
 
76
         */
 
77
    public boolean refresh() {
 
78
                /* Have the data models notify listeners that they need updating */
 
79
                Enumeration en = _tables.elements();
 
80
                while( en.hasMoreElements() ) {
 
81
                        JTable table = (JTable)en.nextElement();
 
82
//                      ((PerfTableModel)table.getModel()).notifyChanges();
 
83
                        table.repaint();
 
84
                }
 
85
                return true;
 
86
        }
 
87
 
 
88
    protected String getAttrValue( String attrName ) {
 
89
                return DSUtil.getAttrValue( _entry, attrName );
 
90
        }
 
91
 
 
92
    protected void setColumnWidths( JTable table, int[] widths ) {
 
93
                Enumeration en = table.getColumnModel().getColumns();
 
94
                int i = 0;
 
95
                while( en.hasMoreElements() ) {
 
96
                        TableColumn col = (TableColumn)en.nextElement();
 
97
                        col.setPreferredWidth( widths[i] );                     
 
98
                        col.setResizable( true );
 
99
                        i++;
 
100
                }
 
101
        }
 
102
 
 
103
    protected void setColumnHeaders( JTable table, String[] headers ) {
 
104
                Enumeration en = table.getColumnModel().getColumns();
 
105
                int i = 0;
 
106
                while( en.hasMoreElements() ) {
 
107
                        TableColumn col = (TableColumn)en.nextElement();
 
108
                        col.setHeaderValue( headers[i] );
 
109
                        i++;
 
110
                }
 
111
        }
 
112
 
 
113
    protected int getTotalColumnWidth( JTable table ) {
 
114
                Enumeration en = table.getColumnModel().getColumns();
 
115
                int width = 0;
 
116
                while( en.hasMoreElements() ) {
 
117
                        TableColumn col = (TableColumn)en.nextElement();
 
118
                        width += col.getWidth();
 
119
                }
 
120
                return width;
 
121
        }
 
122
 
 
123
    private Dimension getOptimalDimension( JTable table,
 
124
                                                                                   JComponent container ) {
 
125
                /* What is the initial total width of all columns? */
 
126
                int width = getTotalColumnWidth( table );
 
127
                /* Make the minimum and the current size of the table fit all
 
128
                   columns */
 
129
        Dimension d =
 
130
                        new Dimension( width,
 
131
                                                   (table.getRowMargin() + table.getRowHeight()) * table.getRowCount());
 
132
                Insets ins = container.getInsets();
 
133
                width = container.getWidth() - ins.left - ins.right;
 
134
                //Debug.println( "getOptimalDimensionTable: " + d + ", container: " + width );
 
135
                d.width = Math.max( d.width, width );
 
136
                return d;
 
137
        }
 
138
 
 
139
    private void setupTable( JTable table,
 
140
                                                         JComponent container ) {
 
141
                Dimension d = getOptimalDimension( table, container );
 
142
                table.setMinimumSize( d );
 
143
                table.setSize( d );
 
144
                table.getTableHeader().setReorderingAllowed(false);
 
145
                table.setRowSelectionAllowed(false);
 
146
                table.setColumnSelectionAllowed(false);
 
147
                table.setAutoResizeMode( table.AUTO_RESIZE_NEXT_COLUMN );
 
148
                table.setRequestFocusEnabled(false);
 
149
                /* If necessary, adjust column widths */
 
150
                table.sizeColumnsToFit( true );
 
151
        }
 
152
 
 
153
        protected void addTable( JTable table, JComponent myContainer,
 
154
                                                   String key ) {
 
155
                /* The column headers only show up if I put this in a scrollpane
 
156
                   in Swing 0.7 or 1.0... */
 
157
//              addTableInGroupPane( table, myContainer, key );
 
158
                addTableInScrollPane( table, myContainer, key );
 
159
        }
 
160
 
 
161
        protected void addTableInScrollPane( JTable table,
 
162
                                                                                 JComponent container,
 
163
                                                                                 String titleString ) {
 
164
                JScrollPane scrollpane = new JScrollPane( table );              
 
165
                JPanel outer = new GroupPanel(titleString);             
 
166
        scrollpane.setHorizontalScrollBarPolicy(
 
167
                        scrollpane.HORIZONTAL_SCROLLBAR_NEVER);
 
168
        scrollpane.setVerticalScrollBarPolicy(
 
169
                        scrollpane.VERTICAL_SCROLLBAR_NEVER);
 
170
        _gbc.gridwidth = _gbc.REMAINDER;
 
171
        _gbc.fill = GridBagConstraints.BOTH;
 
172
//        ((Container)container).add( scrollpane, _gbc );
 
173
        outer.add( scrollpane, _gbc );
 
174
        _gbc.fill = GridBagConstraints.HORIZONTAL;
 
175
        ((Container)container).add( outer, _gbc );
 
176
//        ((Container)container).add( scrollpane, _gbc );
 
177
                setupTable( table, container );
 
178
                Dimension d = getOptimalDimension( table, container );          
 
179
        table.setPreferredScrollableViewportSize(new Dimension(10, d.height));
 
180
        }
 
181
 
 
182
 
 
183
 
 
184
        protected void addTableInGroupPane( JTable table,
 
185
                                                                            JComponent container,
 
186
                                                                                String titleString ) {
 
187
                JPanel panel = new GroupPanel( titleString);
 
188
                panel.add( table, _gbc );
 
189
                setupTable( table, container );
 
190
                _gbc.insets = new Insets(10,0,2,2);
 
191
        _gbc.gridwidth = _gbc.REMAINDER;
 
192
        _gbc.fill = GridBagConstraints.HORIZONTAL;
 
193
                ((Container)container).add( panel, _gbc );
 
194
        }
 
195
 
 
196
        protected void rightAlignColumns( JTable table,
 
197
                                          int first, int last ) {
 
198
                for( int i = first; i <= last; i++ ) {
 
199
                        table.getColumnModel().getColumn(i).setCellRenderer(
 
200
                                new RightAlignedCellRenderer() );
 
201
                        SuiTableHeaderRenderer renderer = (SuiTableHeaderRenderer)
 
202
                                table.getColumnModel().getColumn(i).getHeaderRenderer();
 
203
                        renderer.setHorizontalAlignment(SwingConstants.RIGHT);
 
204
                        renderer.setHorizontalTextPosition(SwingConstants.RIGHT);
 
205
                }
 
206
        }
 
207
 
 
208
        class RightAlignedCellRenderer extends DefaultTableCellRenderer {
 
209
                public Component getTableCellRendererComponent(
 
210
                        JTable table, Object value,
 
211
                        boolean isSelected, boolean hasFocus, int row, int column) {
 
212
                        setHorizontalAlignment(SwingConstants.RIGHT);
 
213
                        return super.getTableCellRendererComponent(
 
214
                                table, value, isSelected, hasFocus, row, column );
 
215
                }
 
216
        }
 
217
 
 
218
    /**
 
219
         * Table model to provide data from the cached directory entry.
 
220
         */
 
221
    class PerfTableModel extends AbstractTableModel {
 
222
        
 
223
        PerfTableModel( String[] labels, String[] headers,
 
224
                        String[] dataNames ) {
 
225
            _labels = labels;
 
226
            _headers = headers;
 
227
            _dataNames = dataNames;
 
228
            _monitorEntry = _entry;
 
229
        }
 
230
 
 
231
        PerfTableModel( String[] labels, String[] headers,
 
232
                        String[] dataNames, LDAPEntry monitorEntry ) {
 
233
            _labels = labels;
 
234
            _headers = headers;
 
235
            _dataNames = dataNames;
 
236
            _monitorEntry = monitorEntry;
 
237
        }
 
238
 
 
239
        public void updEntry( LDAPEntry updEntry ) {
 
240
            updEntry( updEntry, true );
 
241
        }
 
242
 
 
243
 
 
244
        public void updEntry( LDAPEntry updEntry, boolean notifChanges ) {
 
245
            _monitorEntry = updEntry;
 
246
            if ( notifChanges) fireTableDataChanged();
 
247
        }
 
248
 
 
249
        public void updDataNames( String[] dataNames, boolean notifChanges ) {
 
250
            _dataNames = dataNames;
 
251
            if( notifChanges) fireTableDataChanged();
 
252
        }    
 
253
 
 
254
        public void notifyChanges() {
 
255
            fireTableDataChanged();
 
256
        }
 
257
        public int getColumnCount() {
 
258
            return _headers.length;
 
259
        }
 
260
        public int getRowCount() {
 
261
                        return _labels.length;
 
262
        }
 
263
        public Object getValueAt(int row, int col) {
 
264
            String val;
 
265
            if ( col == 0 ) {
 
266
                val = _labels[row];
 
267
            } else {
 
268
                val = DSUtil.getAttrValue( _monitorEntry, _dataNames[row] );
 
269
                if ( (val == null) || (val.length() < 1) ) {
 
270
                    return "";
 
271
                }
 
272
            }
 
273
            // Debug.println( " table value " + row + "," +
 
274
            //                          col + ": " + val );
 
275
            return val;
 
276
        }
 
277
        public String getColumnName(int columnIndex) {
 
278
            return _headers[columnIndex];
 
279
        }
 
280
        
 
281
        protected String[] _labels; // Row labels
 
282
        protected String[] _headers; // Column labels
 
283
        protected String[] _dataNames; // Attributes to provide data 
 
284
        protected LDAPEntry _monitorEntry = null;
 
285
    }
 
286
    
 
287
    protected LDAPEntry _entry;
 
288
    protected LDAPConnection  _ldc;
 
289
    protected String _entryName = "cn=monitor";
 
290
        protected Vector _tables = new Vector();
 
291
    private boolean _isSelected = false;
 
292
    protected static final int PREFERRED_WIDTH = 500;
 
293
    
 
294
        static final ResourceSet _resource = DSUtil._resource;
 
295
}
 
296