summaryrefslogtreecommitdiffstats
path: root/pki/base/console/src/com/netscape/admin/certsrv/status/CMSLogPanel.java
blob: bd19a467f5cc07b36b36bbb900b7373f86733cd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.admin.certsrv.status;

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.border.*;
import com.netscape.admin.certsrv.connection.*;
import com.netscape.admin.certsrv.*;
import javax.swing.event.*;
import java.awt.event.*;
import com.netscape.management.client.*;
import com.netscape.management.client.util.*;

/**
 * Log Panel to be displayed at the right hand side
 * <pre>
 * Top Panel with filter input:
 *          Number of entries: default 25
 *          Source: default all
 *          Log Level: default warning
 * </pre>
 * @author Jack Pan-Chen
 * @version $Revision$, $Date$
 * @see com.netscape.admin.certsrv.status
 */
public abstract class CMSLogPanel extends CMSBasePanel
    implements IResourceSelectionListener, IRefreshTab, IRefreshTabPanel, MouseListener
{

    /*==========================================================
     * variables
     *==========================================================*/
    private static String PANELNAME = "LOGCONTENT";

    public static int DEFAULT_LOG_ENTRY = 25;

    protected JPanel mFilterPanel, mListPanel, mActionPanel;  //panels
    protected boolean mInit = false;    // true if this panel is initialized
    protected CMSBaseResourceModel mModel;
    protected LogDataModel mDataModel;  //table data model

    protected JScrollPane mScrollPane;
    protected JTable mTable;            //table
    protected JButton mView, mRefresh, mHelp;  //action buttons
    protected JTextField mNoRecord;
    protected JComboBox mSource, mLevel, mFile;
    protected String mHelpToken;
    protected LogEntryViewDialog mViewer;

    /*==========================================================
     * constructors
     *==========================================================*/
    public CMSLogPanel( CMSBaseResourceModel model,LogDataModel dataModel) {
        super(PANELNAME);
        model.addIResourceSelectionListener(this);
        mModel = model;
        mDataModel = dataModel;
    }

    /*==========================================================
	 * public methods
     *==========================================================*/

    /**
     * Actual construction of the panel
     */
    public void init() {
        setLayout(new BorderLayout());

        //======== filter panel ======================
 		mFilterPanel = createFilterPanel();
		add("North",mFilterPanel);

        //======== list panel ========================
		mListPanel = createListPanel();
		mListPanel.setBorder(new EmptyBorder(SEPARATED_COMPONENT_SPACE,
		        DIFFERENT_COMPONENT_SPACE - COMPONENT_SPACE,
		        COMPONENT_SPACE,
		        DIFFERENT_COMPONENT_SPACE - COMPONENT_SPACE));
		add("Center",mListPanel);

		//====== action panel ===========================
		mActionPanel = createActionPanel();
		add("South",mActionPanel);
		updateArchive();
		refresh();
    }

    //== IResourceListener ===

    public void select(IResourceObject parent, Object viewInstance) {
        if (!mInit) {
            init();
            mInit = true;
        }

        //refresh the screen
        invalidate();
        validate();
        repaint(1);
    }

    public boolean unselect(IResourceObject parent, Object viewInstance) {
        return true;
    }

    /*==========================================================
	 * EVNET HANDLER METHODS
     *==========================================================*/

    //=== ACTIONLISTENER =====================
    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(mRefresh)) {
            Debug.println("AccessLogPanel: Refresh Log");
            refresh();
        }else if (e.getSource().equals(mView)) { 
            if (mDataModel.getRowCount() == 0) {
                refresh();
            } else {
                viewDetail();
            }
        }else if (e.getSource().equals(mHelp)) {
            CMSAdminUtil.help(mHelpToken);
        } else if (e.getSource().equals(mLevel) || e.getSource().equals(mSource)
                  || e.getSource().equals(mFile)) {
            Debug.println("AccessLogPanel: Changed Log Level or Source or File");
            refresh(); 
        }
    }

    public CMSBasePanel getSelectedTab() {
        return this;
    }

    //==== MOUSELISTENER ======================
    public void mouseClicked(MouseEvent e) {
        //Debug.println("CertRepositoryPanel: mouseClicked() -"+e.toString());
        
        //we track the double click action on the table entry - View op
        if(e.getClickCount() == 2) {
            //Debug.println("View Detail");
            viewDetail();
        }
    }
    
    public void mousePressed(MouseEvent e) { }
    public void mouseReleased(MouseEvent e) { }
    public void mouseEntered(MouseEvent e) { }
    public void mouseExited(MouseEvent e) { }
    
    /**
     * refresh the table data
     */
    public void refresh() {
        mDataModel.removeAllRows();
        Debug.println("CMSLogPanel: refresh()");

        update();

        mTable.invalidate();
        mTable.validate();
        mTable.repaint(1);
		mScrollPane.invalidate();
		mScrollPane.validate();
		mScrollPane.repaint(1);
        if (mDataModel.getRowCount() > 0) {
            mTable.setRowSelectionInterval(0,0);
        }
    }

    /*==========================================================
	 * protected methods
     *==========================================================*/
    
    /**
     * View the log entry in a dialog box
     * (no assumption of validity)
     */
    protected void viewDetail() {
        //check item selected
        if (mTable.getSelectedRow()>= 0) {
            if (mViewer == null) 
                mViewer = new LogEntryViewDialog(mModel.getFrame());
            mViewer.showDialog(
                (String)mDataModel.getValueAt(mTable.getSelectedRow(),0),
                (String)mDataModel.getValueAt(mTable.getSelectedRow(),1),
                (String)mDataModel.getValueAt(mTable.getSelectedRow(),2),
                (String)mDataModel.getValueAt(mTable.getSelectedRow(),3),
                ((JLabel)mDataModel.getValueAt(mTable.getSelectedRow(),4)).getText());
        }
    }


    /**
     * create action button panel
     */
    protected JPanel createActionPanel() {
        //actionlister to this object
        mView = makeJButton("VIEW");
        mRefresh = makeJButton("REFRESH");
        mHelp = makeJButton("HELP");
		JButton[] buttons = { mView,mRefresh,mHelp };
		return makeJButtonPanel(buttons,true,true);
    }

    /**
     * create log listing panel
     */
    protected JPanel createListPanel() {
		mListPanel = new JPanel();
		mListPanel.setLayout(new BorderLayout());

		//center table
		mTable = new JTable(mDataModel);
		mScrollPane = JTable.createScrollPaneForTable(mTable);
		mScrollPane.setHorizontalScrollBarPolicy(mScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		mScrollPane.setVerticalScrollBarPolicy(mScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		mTable.setAutoscrolls(true);
		mTable.addMouseListener(this);
		//setColumnWidth(mTable);
		mTable.setAutoResizeMode(mTable.AUTO_RESIZE_OFF);
		setColumnWidth(mTable);
		mTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		mListPanel.add("Center",mScrollPane);
        setLabelCellRenderer(mTable,4);
        mScrollPane.setBackground(Color.white);
		return mListPanel;
    }


    protected void setColumnWidth(JTable table) {
        int i = table.getColumnModel().getColumnCount();
		for (int x=0; x< i-1; x++) {
			TableColumn col = table.getColumnModel().getColumn(x);
			col.setMinWidth(50);
			col.setResizable( true );
		}
		TableColumn col = table.getColumnModel().getColumn(i-1);
		col.setMinWidth(400);
		col.setResizable( true );
	}
	
    /**
     * create filter criteria panel
     */
    protected JPanel createFilterPanel() {
        JPanel panel = new JPanel();
        GridBagLayout gb = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        panel.setBorder(makeTitledBorder("OPTIONS"));
        panel.setLayout(gb);

        //entry
        CMSAdminUtil.resetGBC(gbc);
        JLabel noRec = makeJLabel("NUMBERREC");
		mNoRecord = makeJTextField(10);
		mNoRecord.setText(Integer.toString(DEFAULT_LOG_ENTRY));
		addEntryField(panel, noRec, mNoRecord, gbc);

		//source and level
		CMSAdminUtil.resetGBC(gbc);
		JLabel label1 = makeJLabel("SOURCE");
		mSource = makeJComboBox("SOURCE");
		JLabel label2 = makeJLabel("LOGLEVEL");
		mLevel = makeJComboBox("LOGLEVEL");
		CMSAdminUtil.addEntryField(panel, label1, mSource, label2, mLevel, gbc);
                mLevel.addActionListener(this);
                mSource.addActionListener(this);
                

		//file
		CMSAdminUtil.resetGBC(gbc);
		gbc.gridheight = gbc.REMAINDER;
		JLabel label3 = makeJLabel("FILE");
		mFile = new JComboBox();
        CMSAdminUtil.addEntryField(panel, label3, mFile, gbc);
                mFile.addActionListener(this);

        return panel;
    }

    /**
     * retrieve log entries from the server side and
     * populate the data model.
     */
    protected abstract void update();

    /**
     * retrieve archieve log file listing from the server
     * side and poupulate the combobox
     */
    protected abstract void updateArchive();

  	//Set the first column's cellrender as label cell
	protected void setLabelCellRenderer(JTable table, int index) {
	    table.getColumnModel().getColumn(index).setCellRenderer(new LabelCellRenderer(new JLabel()));
	}

    //=== OVERWRITE DIALOG MESSAGE =====================

    protected void showMessageDialog(String keyword, int messageType ) {
        CMSAdminUtil.showMessageDialog(mModel.getFrame(), mResource, mPanelName, keyword, messageType);
    }

    protected void showMessageDialog(String keyword) {
        showMessageDialog(keyword, ERROR_MESSAGE);
    }

    protected int showConfirmDialog(String keyword, int messageType ) {
        return CMSAdminUtil.showConfirmDialog(mModel.getFrame(), mResource, mPanelName, keyword, messageType);
    }

    protected int showConfirmDialog(String keyword) {
        return showConfirmDialog(keyword, WARNING_MESSAGE);
    }

    protected void showErrorDialog(String message) {
        CMSAdminUtil.showErrorDialog(mModel.getFrame(), mResource, message, ERROR_MESSAGE);
    }


    private static void addEntryField(JPanel panel, JComponent label,
      JComponent field, GridBagConstraints gbc) {
        gbc.fill = gbc.NONE;
        gbc.weightx = 0.0;
        gbc.gridwidth = 1;
        gbc.gridx = 0;
        gbc.anchor = gbc.EAST;
        gbc.insets = new Insets(COMPONENT_SPACE,DIFFERENT_COMPONENT_SPACE,0,0);
        panel.add( label, gbc );

        gbc.gridx++;
        gbc.anchor = gbc.WEST;
        gbc.fill = gbc.NONE;
        gbc.weightx = 1.0;
        gbc.gridwidth = gbc.REMAINDER;
        gbc.insets = new Insets(COMPONENT_SPACE,COMPONENT_SPACE,
      		                            0,DIFFERENT_COMPONENT_SPACE);
        panel.add( field, gbc );
    }
}