summaryrefslogtreecommitdiffstats
path: root/pki/base/console/src/com/netscape/admin/certsrv/config/CMSTabPanel.java
blob: 63899892f76c2ed30c2768cc470051188ddf3b47 (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
// --- 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.config;

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

/**
 * Base Class for Tabbed right hand pane
 *
 * @author Jack Pan-Chen
 * @version $Revision$, $Date$
 */
public class CMSTabPanel extends CMSBaseConfigPanel
    implements IResourceSelectionListener, ChangeListener, IRefreshTabPanel
{
    /*==========================================================
     * variables
     *==========================================================*/
    private static String PANEL_NAME = "CMSTABPANEL";

    protected JTabbedPane mTabbedPane;          //tabbed panel
    protected JButton mbOK, mbReset, mbHelp;    //buttons
    CMSBaseResourceModel mModel;          //resource model
    private ResourceObject mParent;           //tree node parent

	/*==========================================================
     * constructors
     *==========================================================*/
    public CMSTabPanel(CMSBaseResourceModel model, ResourceObject parent) {
        this(model, parent, true);
    }

    public CMSTabPanel(CMSBaseResourceModel model, ResourceObject parent, boolean showButton) {
        super(PANEL_NAME);
        mModel = model;
        mParent = parent;

        setLayout(new BorderLayout());
        //mTabbedPane = new JTabbedPane();

        // Look and Feel
        mTabbedPane = new NSTabbedPane();
        add("Center", mTabbedPane);

		if (showButton)
		    add("South", createButtonPanel());
		mbOK.setEnabled(false);
		mbReset.setEnabled(false);

        mModel.addIResourceSelectionListener(this);
        //mTabbedPane.addChangeListener(this);
    }

    /*==========================================================
	 * public methods
     *==========================================================*/
    public void init() {}

    /**
     * Retrieve Resource Model
     */
    public CMSBaseResourceModel getResourceModel() {
        return mModel;
    }

    public ResourceObject getResourceObject() {
        return mParent;
    }

    /**
     * Set the Tab associated with this component dirty
     */
    public void setDirtyTab(CMSBaseTab component) {
        int index = mTabbedPane.indexOfComponent(component);
        if (index == -1) {
            Debug.println("CMSTabPanel: setDirtyTab() - component not part of this panel");
            return;
        }
        if ((mTabbedPane != null) && (mTabbedPane.getIconAt(index)== null) ) {
            mTabbedPane.setIconAt(index,
                        CMSAdminUtil.getImage(CMSAdminResources.IMAGE_DIRTY_TAB));
            mTabbedPane.repaint();
        }
        mbOK.setEnabled(true);
        mbReset.setEnabled(true);
    }

    /**
     * Clear the Tab associated with this component
     */
    public void clearDirtyTab(CMSBaseTab component) {
        int index = mTabbedPane.indexOfComponent(component);
        if (index == -1) {
            Debug.println("CMSTabPanel: clearDirtyTab() - component not part of this panel");
            return;
        }
        if ((mTabbedPane != null) && (mTabbedPane.getIconAt(index)!= null) ) {
            mTabbedPane.setIconAt(index, null);
            mTabbedPane.repaint();
        }
        setOKCancel();
    }

    //=== Callback methods ====
    public boolean applyCallback(){
        int currentTab = mTabbedPane.getSelectedIndex();
        int nTabs = mTabbedPane.getTabCount();
        boolean failed = false;

        //we will go through each tab and apply
        for (int i= 0; i < nTabs; ++i) {
            CMSBaseTab p = (CMSBaseTab)mTabbedPane.getComponentAt(i);
            if (p.isDirty()) {
                mTabbedPane.setSelectedIndex(i);
                if (!p.applyCallback()) {
                    failed = true;
                }
            }
        }

        //one or more panel falied
        if (failed) {
            //get first dirty tab
            for (int i= 0; i < nTabs; ++i) {
                CMSBaseTab p = (CMSBaseTab)mTabbedPane.getComponentAt(i);
                if (p.isDirty()) {
                    mTabbedPane.setSelectedIndex(i);
                    break;
                }
            }
            return false;
        }

        //everything ok
        if (currentTab >=0 )
            mTabbedPane.setSelectedIndex(currentTab);
        mbOK.setEnabled(false);
        mbReset.setEnabled(false);
        mbHelp.requestFocusInWindow();
        return true;
    }

    public boolean resetCallback() {
        int nTabs = mTabbedPane.getTabCount();
        for (int i= 0; i < nTabs; ++i) {
            CMSBaseTab p = (CMSBaseTab)mTabbedPane.getComponentAt(i);
            if (p.isDirty()) {
                p.resetCallback();
            }
        }
        mbOK.setEnabled(false);
        mbReset.setEnabled(false);
        return true;
    }

    public void setOKCancel() {
        int nTabs = mTabbedPane.getTabCount();
        for (int i= 0; i < nTabs; ++i) {
            CMSBaseTab p = (CMSBaseTab)mTabbedPane.getComponentAt(i);
            if (p.isDirty()) {
                return;
            }
        }
        mbOK.setEnabled(false);
        mbReset.setEnabled(false);
    }

    public void helpCallback() {
        CMSBaseTab p = (CMSBaseTab)mTabbedPane.getSelectedComponent();
        if (p != null)
            p.helpCallback();
        Debug.println("CMSTabPanel: helpCallback()");
    }

    public CMSBasePanel getSelectedTab() {
        return (CMSBasePanel)mTabbedPane.getSelectedComponent();
    }

    /*==========================================================
	 * EVNET HANDLER METHODS
     *==========================================================*/
    //== ACTIONLISTENER =====
    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(mbOK)) {
            applyCallback();
        }
        if (e.getSource().equals(mbReset)) {
            resetCallback();
        }
        if (e.getSource().equals(mbHelp)) {
            helpCallback();
        }
    }

    //== IResourceListener ===
    public void select(IResourceObject parent, Object viewInstance) {
        //System.out.println("CMSTabPanel: select() "+ parent);
        if (parent == mParent) {
            try {
                mTabbedPane.addChangeListener(this);
                mTabbedPane.setSelectedIndex(0);
                CMSBaseConfigPanel selectedPanel = (CMSBaseConfigPanel) mTabbedPane.getComponentAt(0);
                if ( selectedPanel != null )
                    selectedPanel.initialize();
                mTabbedPane.invalidate();
                mTabbedPane.validate();
            } catch (ArrayIndexOutOfBoundsException e) {
                //NO TAB SO IGNORE
            }
        }
    }

    public boolean unselect(IResourceObject parent, Object viewInstance) {
        //System.out.println("CMSTabPanel: unselect() "+ parent);

        // XXX NOT SAVED MODEL IS MOVED TO HIGHER LEVEL

        //check if any tab is dirty
        boolean dirty = false;
        int nTabs = mTabbedPane.getTabCount();
        for (int i= 0; i < nTabs; ++i) {
            CMSBaseTab p = (CMSBaseTab)mTabbedPane.getComponentAt(i);
            if (p.isDirty()) {
                dirty = true;
            }
        }
        if (!dirty)
            return true;

        //return to previous node
        TreePath thisPath = new TreePath(((ResourceObject)parent).getPath());
        TreePath selectedPath =
                ((CMSResourcePage)viewInstance).getTree().getSelectionPath();
        if (!thisPath.equals(selectedPath))
        	    ((CMSResourcePage)viewInstance).getTree().setSelectionPath(thisPath);

        //popup dialog for user to set changes
        int result = showConfirmDialog("NOTSAVED");
        switch (result) {
            case JOptionPane.YES_OPTION:
                if (applyCallback()) {
                    ((CMSResourcePage)viewInstance).getTree().setSelectionPath(selectedPath);
                    return true;
                }
                break;
            case JOptionPane.NO_OPTION:
                resetCallback();
                ((CMSResourcePage)viewInstance).getTree().setSelectionPath(selectedPath);
                return true;
            default:
                break;
        }

        //can not be unselected
        return false;
    }

    //== ChangeListener ==
	public void stateChanged(ChangeEvent e) {
	    //Debug.println("CMSTabPanel: stateChanged()");
		CMSBaseConfigPanel selectedPanel = (CMSBaseConfigPanel)mTabbedPane.getSelectedComponent();
        if ( selectedPanel != null )
            selectedPanel.initialize();
        mTabbedPane.invalidate();
        mTabbedPane.validate();
        mTabbedPane.repaint(1);
	}

    /**
     * Add Panels to the Tab Panel. ChangeListener is
     * added automatically.
     *
     * @param p CMS Panel to be added
     */
    public void addTab(CMSBaseTab p) {
		mTabbedPane.addTab(p.getTitle(),  p);
	}

    /*==========================================================
	 * protected methods
     *==========================================================*/

    /**
     * create button panel using the factory method
     */
    protected JPanel createButtonPanel() {
        mbOK = makeJButton("APPLY");
		mbReset = makeJButton("RESET");
		mbHelp = makeJButton("HELP");

		//JButton[] buttons = { mbOK, mbReset, mbHelp };
		JButton[] buttons = { mbOK, mbReset };
		return makeJButtonPanel(buttons, true, true);
    }

    class NSTabbedPane extends JTabbedPane {
/*
		public String getUIClassID() {
			return "SecondaryTabbedPaneUI";
		}
*/
	}

    //=== 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);
    }


}