001    /*
002     *  Copyright (C) 2003 Adam Olsen
003     *  This program is free software; you can redistribute it and/or modify
004     *  it under the terms of the GNU General Public License as published by
005     *  the Free Software Foundation; either version 1, or (at your option)
006     *  any later version.
007     *  This program is distributed in the hope that it will be useful,
008     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
009     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
010     *  GNU General Public License for more details.
011     *  You should have received a copy of the GNU General Public License
012     *  along with this program; if not, write to the Free Software
013     *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
014     */
015    package com.valhalla.jbother;
016    
017    import java.awt.event.ActionEvent;
018    import java.awt.event.ActionListener;
019    import java.awt.event.MouseAdapter;
020    import java.awt.event.MouseEvent;
021    import java.awt.event.WindowAdapter;
022    import java.awt.event.WindowEvent;
023    import java.util.ArrayList;
024    import java.util.Hashtable;
025    import java.util.Locale;
026    import java.util.ResourceBundle;
027    
028    import javax.swing.BorderFactory;
029    import javax.swing.Box;
030    import javax.swing.BoxLayout;
031    import javax.swing.JButton;
032    import javax.swing.JDialog;
033    import javax.swing.JFrame;
034    import javax.swing.JList;
035    import javax.swing.JPanel;
036    import javax.swing.JScrollPane;
037    import javax.swing.ListSelectionModel;
038    
039    import com.valhalla.gui.Standard;
040    import com.valhalla.misc.GnuPG;
041    
042    /**
043     * Allows a user to edit a profile
044     *
045     * @author Andrey Zakirov
046     * @created February, 2004
047     */
048    public class KeySelectDialog extends JDialog {
049        private ResourceBundle resources = ResourceBundle.getBundle(
050                "JBotherBundle", Locale.getDefault());
051    
052        private JPanel main;
053    
054        private JButton okButton = new JButton(resources.getString("okButton"));
055    
056        private JButton cancelButton = new JButton(resources
057                .getString("cancelButton"));
058    
059        private String name = null;
060    
061        private String id = null;
062    
063        private String type;
064    
065        private String[] entries;
066    
067        private ArrayList entries2 = new ArrayList();
068    
069        private Hashtable keys = new Hashtable();
070    
071        private JList sampleJList = new JList();
072    
073        /**
074         * Contructs the ProfileEditorDialog
075         *
076         * @param dialog
077         *            the ProfileManager dialog that's calling this editor, or <tt>
078         *      null</tt>
079         *            if nothing is calling it
080         * @param type
081         *            Description of the Parameter
082         */
083        public KeySelectDialog(JDialog dialog, String type) {
084            super(dialog, "", true);
085            this.type = type;
086        }
087    
088        /**
089         * Constructor for the KeySelectDialog object
090         *
091         * @param type
092         *            Description of the Parameter
093         */
094        public KeySelectDialog(String type) {
095            super((JFrame) null, "", true);
096            this.type = type;
097        }
098    
099        /**
100         * Description of the Method
101         */
102        public void showDialog() {
103            setTitle(resources.getString("gnupgKeySelector"));
104            GnuPG gnupg = new GnuPG();
105            boolean res = false;
106            if (type.equals("sec")) {
107                res = gnupg.listSecretKeys("");
108            } else {
109                res = gnupg.listKeys("");
110            }
111    
112            if (!res) {
113                Standard.warningMessage(null, "Error running GnuPG",
114                        "Error running GnuPG: " + gnupg.getErrorString());
115                return;
116            }
117    
118            entries = gnupg.getResult().split("\n");
119            for (int i = 0; i < entries.length; i++) {
120                id = entries[i]
121                        .replaceAll(
122                                "^"
123                                        + type
124                                        + ":[^:]*:[^:]*:[^:]*:([^:]*):[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*$",
125                                "$1");
126                name = entries[i]
127                        .replaceAll(
128                                "^"
129                                        + type
130                                        + ":[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:([^:]*):[^:]*:[^:]*:[^:]*$",
131                                "$1");
132                if ((!name.equals(entries[i])) && (!id.equals(entries[i]))) {
133                    entries2.add(name);
134                    keys.put(name, id);
135                }
136            }
137            name = null;
138            id = null;
139            sampleJList.setListData(entries2.toArray());
140            sampleJList.setSelectedIndex(0);
141            sampleJList.setVisibleRowCount(4);
142            sampleJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
143            sampleJList.addMouseListener(new MouseAdapter() {
144                public void mouseClicked(MouseEvent e) {
145                    if (e.getClickCount() == 2) {
146                        okHandler();
147                    }
148                }
149            });
150    
151            JScrollPane listPane = new JScrollPane(sampleJList);
152            main = (JPanel) getContentPane();
153    
154            JPanel listPanel = new JPanel();
155            JPanel buttonPanel = new JPanel();
156            buttonPanel.setBorder(BorderFactory.createEmptyBorder(2, 5, 2, 5));
157            buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
158            buttonPanel.add(Box.createHorizontalGlue());
159            buttonPanel.add(okButton);
160            buttonPanel.add(cancelButton);
161            listPanel.setBorder(BorderFactory.createEmptyBorder(2, 5, 2, 5));
162            listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.Y_AXIS));
163            listPanel.add(Box.createHorizontalGlue());
164            listPanel.add(listPane);
165            listPanel.add(buttonPanel);
166            main.add(listPanel);
167            addListeners();
168            pack();
169            setLocationRelativeTo(null);
170            addWindowListener(new WindowAdapter() {
171                public void windowClosing(WindowEvent e) {
172                    okHandler();
173                }
174            });
175            this.setVisible(true);
176    
177        }
178    
179        /**
180         * Adds the event listeners to the buttons
181         */
182        private void addListeners() {
183            PEDialogListener listener = new PEDialogListener();
184            okButton.addActionListener(listener);
185            cancelButton.addActionListener(listener);
186        }
187    
188        /**
189         * Handles events
190         *
191         * h *@author synic
192         *
193         * @created November 30, 2004
194         */
195        class PEDialogListener implements ActionListener {
196            /**
197             * Description of the Method
198             *
199             * @param e
200             *            Description of the Parameter
201             */
202            public void actionPerformed(ActionEvent e) {
203                if (e.getSource() == cancelButton) {
204                    cancelHandler();
205                } else {
206                    okHandler();
207                }
208    
209            }
210        }
211    
212        /**
213         * Cancels the dialog, and quits if exitOnClose is set to true
214         */
215        private void okHandler() {
216            if (sampleJList.getSelectedValue() != null) {
217                this.name = sampleJList.getSelectedValue().toString();
218                this.id = keys.get(sampleJList.getSelectedValue()).toString();
219            }
220            dispose();
221        }
222    
223        /**
224         * Description of the Method
225         */
226        private void cancelHandler() {
227            dispose();
228        }
229    
230        /**
231         * Gets the name attribute of the KeySelectDialog object
232         *
233         * @return The name value
234         */
235        public String getName() {
236            return this.name;
237        }
238    
239        /**
240         * Gets the iD attribute of the KeySelectDialog object
241         *
242         * @return The iD value
243         */
244        public String getID() {
245            return this.id;
246        }
247    
248    }
249