f68ec573df118c14ea445d02a48b0e93ea1c845c
[jSite.git] / src / de / todesbaum / jsite / gui / ProjectPage.java
1 /*
2  * jSite - a tool for uploading websites into Freenet
3  * Copyright (C) 2006 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package de.todesbaum.jsite.gui;
21
22 import java.awt.BorderLayout;
23 import java.awt.Dimension;
24 import java.awt.FlowLayout;
25 import java.awt.GridBagConstraints;
26 import java.awt.GridBagLayout;
27 import java.awt.Insets;
28 import java.awt.Toolkit;
29 import java.awt.datatransfer.Clipboard;
30 import java.awt.datatransfer.ClipboardOwner;
31 import java.awt.datatransfer.StringSelection;
32 import java.awt.datatransfer.Transferable;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.KeyEvent;
35 import java.io.IOException;
36 import java.text.MessageFormat;
37
38 import javax.swing.AbstractAction;
39 import javax.swing.Action;
40 import javax.swing.JButton;
41 import javax.swing.JComponent;
42 import javax.swing.JFileChooser;
43 import javax.swing.JLabel;
44 import javax.swing.JList;
45 import javax.swing.JOptionPane;
46 import javax.swing.JPanel;
47 import javax.swing.JScrollPane;
48 import javax.swing.JTextField;
49 import javax.swing.ListSelectionModel;
50 import javax.swing.border.EmptyBorder;
51 import javax.swing.event.DocumentEvent;
52 import javax.swing.event.DocumentListener;
53 import javax.swing.event.ListSelectionEvent;
54 import javax.swing.event.ListSelectionListener;
55 import javax.swing.text.AbstractDocument;
56 import javax.swing.text.AttributeSet;
57 import javax.swing.text.BadLocationException;
58 import javax.swing.text.Document;
59 import javax.swing.text.DocumentFilter;
60
61 import de.todesbaum.jsite.application.Freenet7Interface;
62 import de.todesbaum.jsite.application.Project;
63 import de.todesbaum.jsite.i18n.I18n;
64 import de.todesbaum.jsite.i18n.I18nContainer;
65 import de.todesbaum.util.swing.SortedListModel;
66 import de.todesbaum.util.swing.TLabel;
67 import de.todesbaum.util.swing.TWizard;
68 import de.todesbaum.util.swing.TWizardPage;
69
70 /**
71  * @author David Roden <droden@gmail.com>
72  * @version $Id$
73  */
74 public class ProjectPage extends TWizardPage implements ListSelectionListener, DocumentListener, ClipboardOwner {
75
76         private Freenet7Interface freenetInterface;
77
78         protected Action projectLocalPathBrowseAction;
79         protected Action projectAddAction;
80         protected Action projectDeleteAction;
81         protected Action projectCloneAction;
82         protected Action projectCopyURIAction;
83         protected Action projectGenerateKeyAction;
84
85         private JFileChooser pathChooser;
86         private SortedListModel projectListModel;
87         private JScrollPane projectScrollPane;
88         private JList projectList;
89         private JTextField projectNameTextField;
90         private JTextField projectDescriptionTextField;
91         private JTextField projectLocalPathTextField;
92         private JTextField projectPublicKeyTextField;
93         private JTextField projectPrivateKeyTextField;
94         private JTextField projectPathTextField;
95
96         public ProjectPage(final TWizard wizard) {
97                 super(wizard);
98                 setLayout(new BorderLayout(12, 12));
99                 dialogInit();
100                 setHeading(I18n.getMessage("jsite.project.heading"));
101                 setDescription(I18n.getMessage("jsite.project.description"));
102
103                 I18nContainer.getInstance().registerRunnable(new Runnable() {
104
105                         public void run() {
106                                 setHeading(I18n.getMessage("jsite.project.heading"));
107                                 setDescription(I18n.getMessage("jsite.project.description"));
108                         }
109                 });
110         }
111
112         protected void dialogInit() {
113                 createActions();
114
115                 pathChooser = new JFileChooser();
116                 projectListModel = new SortedListModel();
117                 projectList = new JList(projectListModel);
118                 projectList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
119                 projectList.addListSelectionListener(this);
120                 projectList.setPreferredSize(new Dimension(150, projectList.getPreferredSize().height));
121
122                 add(projectScrollPane = new JScrollPane(projectList), BorderLayout.LINE_START);
123                 add(createInformationPanel(), BorderLayout.CENTER);
124         }
125
126         /**
127          * {@inheritDoc}
128          */
129         @Override
130         public void pageAdded(TWizard wizard) {
131                 super.pageAdded(wizard);
132                 projectList.clearSelection();
133                 this.wizard.setPreviousName(I18n.getMessage("jsite.menu.nodes.manage-nodes"));
134                 this.wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
135                 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
136                 this.wizard.setNextEnabled(false);
137         }
138
139         /**
140          */
141         public void addListSelectionListener(ListSelectionListener listener) {
142                 projectList.addListSelectionListener(listener);
143         }
144
145         /**
146          */
147         public void removeListSelectionListener(ListSelectionListener listener) {
148                 projectList.removeListSelectionListener(listener);
149         }
150
151         private void createActions() {
152                 projectLocalPathBrowseAction = new AbstractAction(I18n.getMessage("jsite.project.action.browse")) {
153
154                         public void actionPerformed(ActionEvent actionEvent) {
155                                 actionLocalPathBrowse();
156                         }
157                 };
158                 projectLocalPathBrowseAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.browse.tooltip"));
159                 projectLocalPathBrowseAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_B);
160                 projectLocalPathBrowseAction.setEnabled(false);
161
162                 projectAddAction = new AbstractAction(I18n.getMessage("jsite.project.action.add-project")) {
163
164                         public void actionPerformed(ActionEvent actionEvent) {
165                                 actionAdd();
166                         }
167                 };
168                 projectAddAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.add-project.tooltip"));
169                 projectAddAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_A);
170
171                 projectDeleteAction = new AbstractAction(I18n.getMessage("jsite.project.action.delete-project")) {
172
173                         public void actionPerformed(ActionEvent actionEvent) {
174                                 actionDelete();
175                         }
176                 };
177                 projectDeleteAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.delete-project.tooltip"));
178                 projectDeleteAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_D);
179                 projectDeleteAction.setEnabled(false);
180
181                 projectCloneAction = new AbstractAction(I18n.getMessage("jsite.project.action.clone-project")) {
182
183                         public void actionPerformed(ActionEvent actionEvent) {
184                                 actionClone();
185                         }
186                 };
187                 projectCloneAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.clone-project.tooltip"));
188                 projectCloneAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_L);
189                 projectCloneAction.setEnabled(false);
190
191                 projectCopyURIAction = new AbstractAction(I18n.getMessage("jsite.project.action.copy-uri")) {
192
193                         public void actionPerformed(ActionEvent actionEvent) {
194                                 actionCopyURI();
195                         }
196                 };
197                 projectCopyURIAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.copy-uri.tooltip"));
198                 projectCopyURIAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_U);
199                 projectCopyURIAction.setEnabled(false);
200
201                 projectGenerateKeyAction = new AbstractAction(I18n.getMessage("jsite.project.action.generate-new-key")) {
202
203                         public void actionPerformed(ActionEvent actionEvent) {
204                                 actionGenerateNewKey();
205                         }
206                 };
207                 projectGenerateKeyAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.generate-new-key.tooltip"));
208                 projectGenerateKeyAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_G);
209                 projectGenerateKeyAction.setEnabled(false);
210
211                 I18nContainer.getInstance().registerRunnable(new Runnable() {
212
213                         @SuppressWarnings("synthetic-access")
214                         public void run() {
215                                 projectLocalPathBrowseAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.browse"));
216                                 projectLocalPathBrowseAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.browse.tooltip"));
217                                 projectAddAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.add-project"));
218                                 projectAddAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.add-project.tooltip"));
219                                 projectDeleteAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.delete-project"));
220                                 projectDeleteAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.delete-project.tooltip"));
221                                 projectCloneAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.clone-project"));
222                                 projectCloneAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.clone-project.tooltip"));
223                                 projectCopyURIAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.copy-uri"));
224                                 projectCopyURIAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.copy-uri.tooltip"));
225                                 projectGenerateKeyAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.generate-new-key"));
226                                 projectGenerateKeyAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.generate-new-key.tooltip"));
227                                 pathChooser.setApproveButtonText(I18n.getMessage("jsite.project.action.browse.choose"));
228                         }
229                 });
230         }
231
232         private JComponent createInformationPanel() {
233                 JPanel informationPanel = new JPanel(new BorderLayout(12, 12));
234
235                 JPanel informationTable = new JPanel(new GridBagLayout());
236
237                 JPanel functionButtons = new JPanel(new FlowLayout(FlowLayout.LEADING, 12, 12));
238                 functionButtons.setBorder(new EmptyBorder(-12, -12, -12, -12));
239                 functionButtons.add(new JButton(projectAddAction));
240                 functionButtons.add(new JButton(projectDeleteAction));
241                 functionButtons.add(new JButton(projectCloneAction));
242                 functionButtons.add(new JButton(projectCopyURIAction));
243
244                 informationPanel.add(functionButtons, BorderLayout.PAGE_START);
245                 informationPanel.add(informationTable, BorderLayout.CENTER);
246
247                 final JLabel projectInformationLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.project.project.information") + "</b></html>");
248                 informationTable.add(projectInformationLabel, new GridBagConstraints(0, 0, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
249
250                 projectNameTextField = new JTextField();
251                 projectNameTextField.getDocument().putProperty("name", "project.name");
252                 projectNameTextField.getDocument().addDocumentListener(this);
253                 projectNameTextField.setEnabled(false);
254
255                 final TLabel projectNameLabel = new TLabel(I18n.getMessage("jsite.project.project.name") + ":", KeyEvent.VK_N, projectNameTextField);
256                 informationTable.add(projectNameLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
257                 informationTable.add(projectNameTextField, new GridBagConstraints(1, 1, 2, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
258
259                 projectDescriptionTextField = new JTextField();
260                 projectDescriptionTextField.getDocument().putProperty("name", "project.description");
261                 projectDescriptionTextField.getDocument().addDocumentListener(this);
262                 projectDescriptionTextField.setEnabled(false);
263
264                 final TLabel projectDescriptionLabel = new TLabel(I18n.getMessage("jsite.project.project.description") + ":", KeyEvent.VK_D, projectDescriptionTextField);
265                 informationTable.add(projectDescriptionLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
266                 informationTable.add(projectDescriptionTextField, new GridBagConstraints(1, 2, 2, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
267
268                 projectLocalPathTextField = new JTextField();
269                 projectLocalPathTextField.getDocument().putProperty("name", "project.localpath");
270                 projectLocalPathTextField.getDocument().addDocumentListener(this);
271                 projectLocalPathTextField.setEnabled(false);
272
273                 final TLabel projectLocalPathLabel = new TLabel(I18n.getMessage("jsite.project.project.local-path") + ":", KeyEvent.VK_L, projectLocalPathTextField);
274                 informationTable.add(projectLocalPathLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
275                 informationTable.add(projectLocalPathTextField, new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
276                 informationTable.add(new JButton(projectLocalPathBrowseAction), new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
277
278                 final JLabel projectAddressLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.project.project.address") + "</b></html>");
279                 informationTable.add(projectAddressLabel, new GridBagConstraints(0, 4, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(12, 0, 0, 0), 0, 0));
280
281                 projectPublicKeyTextField = new JTextField(27);
282                 projectPublicKeyTextField.getDocument().putProperty("name", "project.publickey");
283                 projectPublicKeyTextField.getDocument().addDocumentListener(this);
284                 projectPublicKeyTextField.setEnabled(false);
285
286                 final TLabel projectPublicKeyLabel = new TLabel(I18n.getMessage("jsite.project.project.public-key") + ":", KeyEvent.VK_U, projectPublicKeyTextField);
287                 informationTable.add(projectPublicKeyLabel, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
288                 informationTable.add(projectPublicKeyTextField, new GridBagConstraints(1, 5, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
289                 informationTable.add(new JButton(projectGenerateKeyAction), new GridBagConstraints(2, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
290
291                 projectPrivateKeyTextField = new JTextField(27);
292                 projectPrivateKeyTextField.getDocument().putProperty("name", "project.privatekey");
293                 projectPrivateKeyTextField.getDocument().addDocumentListener(this);
294                 projectPrivateKeyTextField.setEnabled(false);
295
296                 final TLabel projectPrivateKeyLabel = new TLabel(I18n.getMessage("jsite.project.project.private-key") + ":", KeyEvent.VK_R, projectPrivateKeyTextField);
297                 informationTable.add(projectPrivateKeyLabel, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
298                 informationTable.add(projectPrivateKeyTextField, new GridBagConstraints(1, 6, 2, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
299
300                 projectPathTextField = new JTextField();
301                 projectPathTextField.getDocument().putProperty("name", "project.path");
302                 projectPathTextField.getDocument().addDocumentListener(this);
303                 ((AbstractDocument) projectPathTextField.getDocument()).setDocumentFilter(new DocumentFilter() {
304
305                         /**
306                          * {@inheritDoc}
307                          */
308                         @Override
309                         public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
310                                 super.insertString(fb, offset, string.replaceAll("/", ""), attr);
311                         }
312
313                         /**
314                          * {@inheritDoc}
315                          */
316                         @Override
317                         public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
318                                 super.replace(fb, offset, length, text.replaceAll("/", ""), attrs);
319                         }
320                 });
321                 projectPathTextField.setEnabled(false);
322
323                 final TLabel projectPathLabel = new TLabel(I18n.getMessage("jsite.project.project.path") + ":", KeyEvent.VK_P, projectPathTextField);
324                 I18nContainer.getInstance().registerRunnable(new Runnable() {
325
326                         public void run() {
327                                 projectInformationLabel.setText("<html><b>" + I18n.getMessage("jsite.project.project.information") + "</b></html>");
328                                 projectNameLabel.setText(I18n.getMessage("jsite.project.project.name") + ":");
329                                 projectDescriptionLabel.setText(I18n.getMessage("jsite.project.project.description") + ":");
330                                 projectLocalPathLabel.setText(I18n.getMessage("jsite.project.project.local-path") + ":");
331                                 projectAddressLabel.setText("<html><b>" + I18n.getMessage("jsite.project.project.address") + "</b></html>");
332                                 projectPublicKeyLabel.setText(I18n.getMessage("jsite.project.project.public-key") + ":");
333                                 projectPrivateKeyLabel.setText(I18n.getMessage("jsite.project.project.private-key") + ":");
334                                 projectPathLabel.setText(I18n.getMessage("jsite.project.project.path") + ":");
335                         }
336                 });
337                 informationTable.add(projectPathLabel, new GridBagConstraints(0, 7, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
338                 informationTable.add(projectPathTextField, new GridBagConstraints(1, 7, 2, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
339
340                 return informationPanel;
341         }
342
343         public void setProjects(Project[] projects) {
344                 projectListModel.clear();
345                 for (Project project: projects) {
346                         projectListModel.add(project);
347                 }
348         }
349
350         public Project[] getProjects() {
351                 return (Project[]) projectListModel.toArray(new Project[projectListModel.size()]);
352         }
353
354         /**
355          * @param freenetInterface
356          *            The freenetInterface to set.
357          */
358         public void setFreenetInterface(Freenet7Interface freenetInterface) {
359                 this.freenetInterface = freenetInterface;
360         }
361
362         public Project getSelectedProject() {
363                 return (Project) projectList.getSelectedValue();
364         }
365
366         private void setTextField(DocumentEvent documentEvent) {
367                 Document document = documentEvent.getDocument();
368                 String propertyName = (String) document.getProperty("name");
369                 Project project = (Project) projectList.getSelectedValue();
370                 if (project == null) {
371                         return;
372                 }
373                 try {
374                         String text = document.getText(0, document.getLength()).trim();
375                         if ("project.name".equals(propertyName)) {
376                                 project.setName(text);
377                                 projectList.repaint();
378                         } else if ("project.description".equals(propertyName)) {
379                                 project.setDescription(text);
380                         } else if ("project.localpath".equals(propertyName)) {
381                                 project.setLocalPath(text);
382                         } else if ("project.privatekey".equals(propertyName)) {
383                                 project.setInsertURI(text);
384                         } else if ("project.publickey".equals(propertyName)) {
385                                 project.setRequestURI(text);
386                         } else if ("project.path".equals(propertyName)) {
387                                 project.setPath(text);
388                         }
389                 } catch (BadLocationException e) {
390                 }
391         }
392
393         //
394         // ACTIONS
395         //
396
397         protected void actionLocalPathBrowse() {
398                 Project project = (Project) projectList.getSelectedValue();
399                 if (project == null) {
400                         return;
401                 }
402                 pathChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
403                 if (pathChooser.showDialog(this, I18n.getMessage("jsite.project.action.browse.choose")) == JFileChooser.APPROVE_OPTION) {
404                         projectLocalPathTextField.setText(pathChooser.getSelectedFile().getPath());
405                 }
406         }
407
408         protected void actionAdd() {
409                 String[] keyPair = null;
410                 if (!freenetInterface.hasNode()) {
411                         JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
412                         return;
413                 }
414                 try {
415                         keyPair = freenetInterface.generateKeyPair();
416                 } catch (IOException ioe1) {
417                         JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.project.keygen.io-error"), ioe1.getMessage()), null, JOptionPane.ERROR_MESSAGE);
418                         return;
419                 }
420                 Project newProject = new Project();
421                 newProject.setName(I18n.getMessage("jsite.project.new-project.name"));
422                 newProject.setInsertURI(keyPair[0]);
423                 newProject.setRequestURI(keyPair[1]);
424                 newProject.setEdition(0);
425                 projectListModel.add(newProject);
426                 projectScrollPane.revalidate();
427                 projectScrollPane.repaint();
428                 projectList.setSelectedIndex(projectListModel.size() - 1);
429         }
430
431         protected void actionDelete() {
432                 int selectedIndex = projectList.getSelectedIndex();
433                 if (selectedIndex > -1) {
434                         if (JOptionPane.showConfirmDialog(this, MessageFormat.format(I18n.getMessage("jsite.project.action.delete-project.confirm"), ((Project) projectList.getSelectedValue()).getName()), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
435                                 projectListModel.remove(selectedIndex);
436                                 projectList.clearSelection();
437                                 if (projectListModel.getSize() != 0) {
438                                         projectList.setSelectedIndex(Math.min(selectedIndex, projectListModel.getSize() - 1));
439                                 }
440                         }
441                 }
442         }
443
444         protected void actionClone() {
445                 int selectedIndex = projectList.getSelectedIndex();
446                 if (selectedIndex > -1) {
447                         Project newProject = new Project((Project) projectList.getSelectedValue());
448                         newProject.setName(MessageFormat.format(I18n.getMessage("jsite.project.action.clone-project.copy"), newProject.getName()));
449                         projectListModel.add(newProject);
450                         projectList.setSelectedIndex(projectListModel.indexOf(newProject));
451                 }
452         }
453
454         protected void actionCopyURI() {
455                 int selectedIndex = projectList.getSelectedIndex();
456                 if (selectedIndex > -1) {
457                         Project selectedProject = (Project) projectList.getSelectedValue();
458                         Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
459                         clipboard.setContents(new StringSelection(selectedProject.getFinalRequestURI(0)), this);
460                 }
461         }
462
463         protected void actionGenerateNewKey() {
464                 if (JOptionPane.showConfirmDialog(this, I18n.getMessage("jsite.project.warning.generate-new-key"), null, JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) {
465                         return;
466                 }
467                 int selectedIndex = projectList.getSelectedIndex();
468                 if (selectedIndex > -1) {
469                         Project selectedProject = (Project) projectList.getSelectedValue();
470                         String[] keyPair = null;
471                         try {
472                                 keyPair = freenetInterface.generateKeyPair();
473                         } catch (IOException ioe1) {
474                                 JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.project.keygen.io-error"), ioe1.getMessage()), null, JOptionPane.ERROR_MESSAGE);
475                                 return;
476                         }
477                         selectedProject.setInsertURI(keyPair[0]);
478                         selectedProject.setRequestURI(keyPair[1]);
479                         projectPublicKeyTextField.setText(selectedProject.getRequestURI());
480                         projectPrivateKeyTextField.setText(selectedProject.getInsertURI());
481                 }
482         }
483
484         //
485         // INTERFACE ListSelectionListener
486         //
487
488         /**
489          * {@inheritDoc}
490          */
491         public void valueChanged(ListSelectionEvent listSelectionEvent) {
492                 int selectedRow = projectList.getSelectedIndex();
493                 Project selectedProject = (Project) projectList.getSelectedValue();
494                 projectNameTextField.setEnabled(selectedRow > -1);
495                 projectDescriptionTextField.setEnabled(selectedRow > -1);
496                 projectLocalPathTextField.setEnabled(selectedRow > -1);
497                 projectPublicKeyTextField.setEnabled(selectedRow > -1);
498                 projectPrivateKeyTextField.setEnabled(selectedRow > -1);
499                 projectPathTextField.setEnabled(selectedRow > -1);
500                 projectLocalPathBrowseAction.setEnabled(selectedRow > -1);
501                 projectDeleteAction.setEnabled(selectedRow > -1);
502                 projectCloneAction.setEnabled(selectedRow > -1);
503                 projectCopyURIAction.setEnabled(selectedRow > -1);
504                 projectGenerateKeyAction.setEnabled(selectedRow > -1);
505                 if (selectedRow > -1) {
506                         projectNameTextField.setText(selectedProject.getName());
507                         projectDescriptionTextField.setText(selectedProject.getDescription());
508                         projectLocalPathTextField.setText(selectedProject.getLocalPath());
509                         projectPublicKeyTextField.setText(selectedProject.getRequestURI());
510                         projectPrivateKeyTextField.setText(selectedProject.getInsertURI());
511                         projectPathTextField.setText(selectedProject.getPath());
512                 } else {
513                         projectNameTextField.setText("");
514                         projectDescriptionTextField.setText("");
515                         projectLocalPathTextField.setText("");
516                         projectPublicKeyTextField.setText("");
517                         projectPrivateKeyTextField.setText("");
518                         projectPathTextField.setText("");
519                 }
520         }
521
522         //
523         // INTERFACE ChangeListener
524         //
525
526         //
527         // INTERFACE DocumentListener
528         //
529
530         /**
531          * {@inheritDoc}
532          */
533         public void insertUpdate(DocumentEvent documentEvent) {
534                 setTextField(documentEvent);
535         }
536
537         /**
538          * {@inheritDoc}
539          */
540         public void removeUpdate(DocumentEvent documentEvent) {
541                 setTextField(documentEvent);
542         }
543
544         /**
545          * {@inheritDoc}
546          */
547         public void changedUpdate(DocumentEvent documentEvent) {
548                 setTextField(documentEvent);
549         }
550
551         //
552         // INTERFACE ClipboardOwner
553         //
554
555         /**
556          * {@inheritDoc}
557          */
558         public void lostOwnership(Clipboard clipboard, Transferable contents) {
559         }
560
561 }