summaryrefslogtreecommitdiffstats
path: root/pki/base/console/src/com/netscape/admin/certsrv/security/CertRequestSelectTokenPane.java
blob: cab38e8d368501cc034ebc7c7114693fc8e696f9 (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
// --- 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.security;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import com.netscape.management.client.util.*;
import com.netscape.management.nmclf.*;

/**
 *
 * Prompt user to see which token they want to use, and weather or not
 * certificate has been installed or not.
 *
 * @version    1.0    98/07/10
 * @author     <A HREF="mailto:shihcm@netscape.com">shihcm@netscape.com</A>
 *
 */
class CertRequestSelectTokenPane extends JPanel implements SuiConstants,
IKeyCertPage {

    JComboBox tokenSelection = new JComboBox();
    JRadioButton _no;
    JRadioButton _yes;
    JRadioButton _noneed;

    String _defaultToken;
    String _internal;

    /**
     * Determain whether a cgi need to be call again
     */
    boolean modified = true;


    /**
     * Get the panel that is going to be displayed
     * @return a panel to be displayed by the key & cert wizard
     */
    public JPanel getPanel() {
        return this;
    }

    /**
      * Checks if this panel can be shown
      * @return true if this page can be shown
      */
    public boolean pageShow(WizardObservable observable) {
        //might have to call cgi that loadmodule...
        observable.put("sie",
                KeyCertUtility.createTokenName(
                observable.getConsoleInfo()));

        if (tokenSelection.getItemCount() == 0) {
            observable.put("createTrust" , new Boolean(true));

            KeyCertTaskInfo taskInfo = observable.getTaskInfo();
            taskInfo.put("sie", observable.get("sie"));

            try {
                taskInfo.exec(taskInfo.SEC_LSTOKEN);
            } catch (Exception e) {
                SuiOptionPane.showMessageDialog(
                        UtilConsoleGlobals.getActivatedFrame(),
                        e.getMessage());
                return true;
            }

            Vector cipherList = taskInfo.getResponse().getFamilyList();
            for (int i = 0; i < cipherList.size(); i++) {
                CipherEntry cipher = (CipherEntry)(cipherList.elementAt(i));
                JComboBox tokenNames = cipher.getTokenComboBox();
                for (int j = 0; j < cipher.getTokenCount(); j++) {
                    tokenSelection.addItem(tokenNames.getItemAt(j));
                }
                if (tokenSelection.getItemCount() > 0) {
                    observable.put("createTrust" , new Boolean(false));
                }
            }


            boolean noDefaultToken = true;
            for (int i = tokenSelection.getItemCount() - 1; i >= 0; i--) {
                if (tokenSelection.getItemAt(i).equals(_defaultToken)) {
                    noDefaultToken = false;
                }
            }
            if (noDefaultToken) {
                tokenSelection.addItem(_defaultToken);
                observable.put("createTrust" , new Boolean(true));
            }

            try {
                tokenSelection.setSelectedIndex(0);
            } catch (Exception e) {}
        }

        return true;
    }


    /**
      * Checks if this panel can be hidden
      * @return true if this page can be hide
      */
    public boolean pageHide(WizardObservable observable) {
        /*observable.put("isInternal"   , ((TOGGLEPANEeditor)(questionPane.getCtrlByName("isInternal"))).getValue());*/

        try {
            observable.put("isInternal" , new Boolean(true));
            if (!(((String)(tokenSelection.getSelectedItem())).
                    toLowerCase()).startsWith(_internal.toLowerCase())) {
                observable.put("isInternal" , new Boolean(false));
            }
        } catch (Exception e) {}
        if (modified) {
            observable.put("CertReqModified", new Boolean(true));
            observable.put("tokenName" , tokenSelection.getSelectedItem());
            observable.put("requestCert" , new Boolean(_no.isSelected()));
            observable.put("installCert" ,
                    new Boolean(!(_noneed.isSelected())));
            observable.put("noneed" , new Boolean(_noneed.isSelected()));
            modified = false;
        }

        return true;
    }

    /**
      * Listen to changes to determain if cgi need to be called again
      *
      */
    class ModifiedActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            modified = true;
        }
    }



    /**
      *
      * Convinent method for create a numbered component:
      * [panel]
      *    1. bla bla bla
      *    2. bla bla bla
      * [panel]
      *
      */
    private void addNumberedComponent(JPanel p, int count, Component c,
            Vector components) {
        //JPanel entry = new JPanel();
        //entry.setLayout(new GridBagLayout());
        GridBagUtil.constrain(p,
                Box.createRigidArea(
                new Dimension(SEPARATED_COMPONENT_SPACE, 0)), 0,
                count - 1, 1, 1, 0.0, 0.0, GridBagConstraints.NORTH,
                GridBagConstraints.BOTH, 0, 0, 0, 0);
        GridBagUtil.constrain(p,
                new JLabel(Integer.toString(count) + ".  "), 1,
                count - 1, 1, 1, 0.0, 0.0, GridBagConstraints.NORTH,
                GridBagConstraints.NONE, 0, 0, 0, 0);
        GridBagUtil.constrain(p, c, 2, count - 1, 1, 1, 0.0, 0.0,
                GridBagConstraints.NORTH,
                GridBagConstraints.HORIZONTAL, 0, 0,
                DIFFERENT_COMPONENT_SPACE, 0);


        for (int i = 0; i < components.size(); i++) {
            GridBagUtil.constrain(p,
                    (Component)(components.elementAt(i)), 2,
                    count + i, 1, 1, 1.0, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.NONE,
                    0, 0, 0, 0);
        }

        //p.add(entry);
    }

    /**
      *
      * Create a token selection panel for Key & Cert wizard.
      *
      */
    public CertRequestSelectTokenPane() {
        super();
        setLayout(new GridBagLayout());

        ResourceSet resource = KeyCertUtility.getKeyCertWizardResourceSet();

        _internal = resource.getString("SelectToken", "internal");
        _defaultToken = resource.getString("SelectToken", "defaultToken");

        _no = new JRadioButton(resource.getString("SelectToken", "no"),
                true);
        _yes = new JRadioButton(resource.getString("SelectToken", "yes"),
                false);
        _noneed =
                new JRadioButton(resource.getString("SelectToken", "noNeed"),
                false);

        JLabel useExt_noneed =
                new JLabel(resource.getString("SelectToken", "noNeed_ext"));
        Insets b = _noneed.getMargin();
        useExt_noneed.setBorder( new EmptyBorder( new Insets(0,
                12 + b.right + _noneed.getHorizontalTextPosition(),
                b.bottom, b.right)));



        ModifiedActionListener listener = new ModifiedActionListener();
        _no.addActionListener(listener);
        _yes.addActionListener(listener);
        _noneed.addActionListener(listener);
        tokenSelection.addActionListener(listener);


        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(_no);
        buttonGroup.add(_yes);
        buttonGroup.add(_noneed);


        int y = 0;

        setBorder( new TitledBorder( new CompoundBorder(new EtchedBorder(),
                new EmptyBorder(COMPONENT_SPACE, COMPONENT_SPACE,
                COMPONENT_SPACE, COMPONENT_SPACE)),
                resource.getString("SelectToken", "title")));


        JPanel tokenSelectPane = new JPanel();
        //tokenSelectPane.setLayout(new BoxLayout(tokenSelectPane, BoxLayout.Y_AXIS));
        tokenSelectPane.setLayout(new GridBagLayout());

        JLabel _pickToken =
                new JLabel(resource.getString("SelectToken", "pickToken"));
        Vector components = new Vector();
        components.addElement(tokenSelection);
        addNumberedComponent(tokenSelectPane, ++y, _pickToken, components);
        GridBagUtil.constrain(this, tokenSelectPane, 0, y, 1, 1, 0.0,
                0.0, GridBagConstraints.NORTH,
                GridBagConstraints.BOTH, 0, 0,
                DIFFERENT_COMPONENT_SPACE, 0);


        JPanel certInstPane = new JPanel();
        //certInstPane.setLayout(new BoxLayout(certInstPane, BoxLayout.Y_AXIS));
        certInstPane.setLayout(new GridBagLayout());

        components = new Vector();
        components.addElement(_no);
        components.addElement(_yes);
        //need a radio button that can wrap the string.
        components.addElement(_noneed);
        components.addElement(useExt_noneed);
        addNumberedComponent(certInstPane, ++y,
                new MultilineLabel(
                resource.getString("SelectToken", "certReadyForInst")),
                components);
        GridBagUtil.constrain(this, certInstPane, 0, y, 1, 1, 0.0, 0.0,
                GridBagConstraints.NORTH, GridBagConstraints.BOTH, 0,
                0, DIFFERENT_COMPONENT_SPACE, 0);

        GridBagUtil.constrain(this, Box.createVerticalGlue(), 0, ++y,
                1, 1, 1.0, 1.0, GridBagConstraints.NORTH,
                GridBagConstraints.BOTH, 0, 0, 0, 0);

        GridBagUtil.constrain(this,
                new JLabel(
                resource.getString(null, "clickNextToContinue")), 0,
                ++y, 1, 1, 1.0, 0.0, GridBagConstraints.NORTH,
                GridBagConstraints.BOTH, 0, 0, 0, 0);
    }

    /*public static void main(String arg[]) {
     JFrame f = new JFrame();
     CertRequestSelectTokenPane c = new CertRequestSelectTokenPane();
     f.getContentPane().add("North",c );
     f.setSize(400,400);
     f.show();
     }*/

}