add javadoc
[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  * Wizard page that lets the user manage his projects and start inserts.
72  * 
73  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
74  */
75 public class ProjectPage extends TWizardPage implements ListSelectionListener, DocumentListener, ClipboardOwner {
76
77         /** The freenet interface. */
78         private Freenet7Interface freenetInterface;
79
80         /** The “browse” action. */
81         private Action projectLocalPathBrowseAction;
82
83         /** The “add project” action. */
84         private Action projectAddAction;
85
86         /** The “delete project” action. */
87         private Action projectDeleteAction;
88
89         /** The “clone project” action. */
90         private Action projectCloneAction;
91
92         /** The “copy URI” action. */
93         private Action projectCopyURIAction;
94
95         /** The “generate key” action. */
96         private Action projectGenerateKeyAction;
97
98         /** The file chooser. */
99         private JFileChooser pathChooser;
100
101         /** The project list model. */
102         private SortedListModel projectListModel;
103
104         /** The project list scroll pane. */
105         private JScrollPane projectScrollPane;
106
107         /** The project list. */
108         private JList projectList;
109
110         /** The project name textfield. */
111         private JTextField projectNameTextField;
112
113         /** The project description textfield. */
114         private JTextField projectDescriptionTextField;
115
116         /** The local path textfield. */
117         private JTextField projectLocalPathTextField;
118
119         /** The public key textfield. */
120         private JTextField projectPublicKeyTextField;
121
122         /** The private key textfield. */
123         private JTextField projectPrivateKeyTextField;
124
125         /** The project path textfield. */
126         private JTextField projectPathTextField;
127
128         /**
129          * Creates a new project page.
130          * 
131          * @param wizard
132          *            The wizard this page belongs to
133          */
134         public ProjectPage(final TWizard wizard) {
135                 super(wizard);
136                 setLayout(new BorderLayout(12, 12));
137                 dialogInit();
138                 setHeading(I18n.getMessage("jsite.project.heading"));
139                 setDescription(I18n.getMessage("jsite.project.description"));
140
141                 I18nContainer.getInstance().registerRunnable(new Runnable() {
142
143                         public void run() {
144                                 setHeading(I18n.getMessage("jsite.project.heading"));
145                                 setDescription(I18n.getMessage("jsite.project.description"));
146                         }
147                 });
148         }
149
150         /**
151          * Initializes the page.
152          */
153         private void dialogInit() {
154                 createActions();
155
156                 pathChooser = new JFileChooser();
157                 projectListModel = new SortedListModel();
158                 projectList = new JList(projectListModel);
159                 projectList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
160                 projectList.addListSelectionListener(this);
161                 projectList.setPreferredSize(new Dimension(150, projectList.getPreferredSize().height));
162
163                 add(projectScrollPane = new JScrollPane(projectList), BorderLayout.LINE_START);
164                 add(createInformationPanel(), BorderLayout.CENTER);
165         }
166
167         /**
168          * {@inheritDoc}
169          */
170         @Override
171         public void pageAdded(TWizard wizard) {
172                 super.pageAdded(wizard);
173                 projectList.clearSelection();
174                 this.wizard.setPreviousName(I18n.getMessage("jsite.menu.nodes.manage-nodes"));
175                 this.wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
176                 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
177                 this.wizard.setNextEnabled(false);
178         }
179
180         /**
181          * Adds the given listener to the list of listeners.
182          * 
183          * @param listener
184          *            The listener to add
185          */
186         public void addListSelectionListener(ListSelectionListener listener) {
187                 projectList.addListSelectionListener(listener);
188         }
189
190         /**
191          * Removes the given listener from the list of listeners.
192          * 
193          * @param listener
194          *            The listener to remove
195          */
196         public void removeListSelectionListener(ListSelectionListener listener) {
197                 projectList.removeListSelectionListener(listener);
198         }
199
200         /**
201          * Creates all actions.
202          */
203         private void createActions() {
204                 projectLocalPathBrowseAction = new AbstractAction(I18n.getMessage("jsite.project.action.browse")) {
205
206                         @SuppressWarnings("synthetic-access")
207                         public void actionPerformed(ActionEvent actionEvent) {
208                                 actionLocalPathBrowse();
209                         }
210                 };
211                 projectLocalPathBrowseAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.browse.tooltip"));
212                 projectLocalPathBrowseAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_B);
213                 projectLocalPathBrowseAction.setEnabled(false);
214
215                 projectAddAction = new AbstractAction(I18n.getMessage("jsite.project.action.add-project")) {
216
217                         @SuppressWarnings("synthetic-access")
218                         public void actionPerformed(ActionEvent actionEvent) {
219                                 actionAdd();
220                         }
221                 };
222                 projectAddAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.add-project.tooltip"));
223                 projectAddAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_A);
224
225                 projectDeleteAction = new AbstractAction(I18n.getMessage("jsite.project.action.delete-project")) {
226
227                         @SuppressWarnings("synthetic-access")
228                         public void actionPerformed(ActionEvent actionEvent) {
229                                 actionDelete();
230                         }
231                 };
232                 projectDeleteAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.delete-project.tooltip"));
233                 projectDeleteAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_D);
234                 projectDeleteAction.setEnabled(false);
235
236                 projectCloneAction = new AbstractAction(I18n.getMessage("jsite.project.action.clone-project")) {
237
238                         @SuppressWarnings("synthetic-access")
239                         public void actionPerformed(ActionEvent actionEvent) {
240                                 actionClone();
241                         }
242                 };
243                 projectCloneAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.clone-project.tooltip"));
244                 projectCloneAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_L);
245                 projectCloneAction.setEnabled(false);
246
247                 projectCopyURIAction = new AbstractAction(I18n.getMessage("jsite.project.action.copy-uri")) {
248
249                         @SuppressWarnings("synthetic-access")
250                         public void actionPerformed(ActionEvent actionEvent) {
251                                 actionCopyURI();
252                         }
253                 };
254                 projectCopyURIAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.copy-uri.tooltip"));
255                 projectCopyURIAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_U);
256                 projectCopyURIAction.setEnabled(false);
257
258                 projectGenerateKeyAction = new AbstractAction(I18n.getMessage("jsite.project.action.generate-new-key")) {
259
260                         @SuppressWarnings("synthetic-access")
261                         public void actionPerformed(ActionEvent actionEvent) {
262                                 actionGenerateNewKey();
263                         }
264                 };
265                 projectGenerateKeyAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.generate-new-key.tooltip"));
266                 projectGenerateKeyAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_G);
267                 projectGenerateKeyAction.setEnabled(false);
268
269                 I18nContainer.getInstance().registerRunnable(new Runnable() {
270
271                         @SuppressWarnings("synthetic-access")
272                         public void run() {
273                                 projectLocalPathBrowseAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.browse"));
274                                 projectLocalPathBrowseAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.browse.tooltip"));
275                                 projectAddAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.add-project"));
276                                 projectAddAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.add-project.tooltip"));
277                                 projectDeleteAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.delete-project"));
278                                 projectDeleteAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.delete-project.tooltip"));
279                                 projectCloneAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.clone-project"));
280                                 projectCloneAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.clone-project.tooltip"));
281                                 projectCopyURIAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.copy-uri"));
282                                 projectCopyURIAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.copy-uri.tooltip"));
283                                 projectGenerateKeyAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.generate-new-key"));
284                                 projectGenerateKeyAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.generate-new-key.tooltip"));
285                                 pathChooser.setApproveButtonText(I18n.getMessage("jsite.project.action.browse.choose"));
286                         }
287                 });
288         }
289
290         /**
291          * Creates the information panel.
292          * 
293          * @return The information panel
294          */
295         private JComponent createInformationPanel() {
296                 JPanel informationPanel = new JPanel(new BorderLayout(12, 12));
297
298                 JPanel informationTable = new JPanel(new GridBagLayout());
299
300                 JPanel functionButtons = new JPanel(new FlowLayout(FlowLayout.LEADING, 12, 12));
301                 functionButtons.setBorder(new EmptyBorder(-12, -12, -12, -12));
302                 functionButtons.add(new JButton(projectAddAction));
303                 functionButtons.add(new JButton(projectDeleteAction));
304                 functionButtons.add(new JButton(projectCloneAction));
305                 functionButtons.add(new JButton(projectCopyURIAction));
306
307                 informationPanel.add(functionButtons, BorderLayout.PAGE_START);
308                 informationPanel.add(informationTable, BorderLayout.CENTER);
309
310                 final JLabel projectInformationLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.project.project.information") + "</b></html>");
311                 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));
312
313                 projectNameTextField = new JTextField();
314                 projectNameTextField.getDocument().putProperty("name", "project.name");
315                 projectNameTextField.getDocument().addDocumentListener(this);
316                 projectNameTextField.setEnabled(false);
317
318                 final TLabel projectNameLabel = new TLabel(I18n.getMessage("jsite.project.project.name") + ":", KeyEvent.VK_N, projectNameTextField);
319                 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));
320                 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));
321
322                 projectDescriptionTextField = new JTextField();
323                 projectDescriptionTextField.getDocument().putProperty("name", "project.description");
324                 projectDescriptionTextField.getDocument().addDocumentListener(this);
325                 projectDescriptionTextField.setEnabled(false);
326
327                 final TLabel projectDescriptionLabel = new TLabel(I18n.getMessage("jsite.project.project.description") + ":", KeyEvent.VK_D, projectDescriptionTextField);
328                 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));
329                 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));
330
331                 projectLocalPathTextField = new JTextField();
332                 projectLocalPathTextField.getDocument().putProperty("name", "project.localpath");
333                 projectLocalPathTextField.getDocument().addDocumentListener(this);
334                 projectLocalPathTextField.setEnabled(false);
335
336                 final TLabel projectLocalPathLabel = new TLabel(I18n.getMessage("jsite.project.project.local-path") + ":", KeyEvent.VK_L, projectLocalPathTextField);
337                 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));
338                 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));
339                 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));
340
341                 final JLabel projectAddressLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.project.project.address") + "</b></html>");
342                 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));
343
344                 projectPublicKeyTextField = new JTextField(27);
345                 projectPublicKeyTextField.getDocument().putProperty("name", "project.publickey");
346                 projectPublicKeyTextField.getDocument().addDocumentListener(this);
347                 projectPublicKeyTextField.setEnabled(false);
348
349                 final TLabel projectPublicKeyLabel = new TLabel(I18n.getMessage("jsite.project.project.public-key") + ":", KeyEvent.VK_U, projectPublicKeyTextField);
350                 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));
351                 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));
352                 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));
353
354                 projectPrivateKeyTextField = new JTextField(27);
355                 projectPrivateKeyTextField.getDocument().putProperty("name", "project.privatekey");
356                 projectPrivateKeyTextField.getDocument().addDocumentListener(this);
357                 projectPrivateKeyTextField.setEnabled(false);
358
359                 final TLabel projectPrivateKeyLabel = new TLabel(I18n.getMessage("jsite.project.project.private-key") + ":", KeyEvent.VK_R, projectPrivateKeyTextField);
360                 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));
361                 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));
362
363                 projectPathTextField = new JTextField();
364                 projectPathTextField.getDocument().putProperty("name", "project.path");
365                 projectPathTextField.getDocument().addDocumentListener(this);
366                 ((AbstractDocument) projectPathTextField.getDocument()).setDocumentFilter(new DocumentFilter() {
367
368                         /**
369                          * {@inheritDoc}
370                          */
371                         @Override
372                         public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
373                                 super.insertString(fb, offset, string.replaceAll("/", ""), attr);
374                         }
375
376                         /**
377                          * {@inheritDoc}
378                          */
379                         @Override
380                         public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
381                                 super.replace(fb, offset, length, text.replaceAll("/", ""), attrs);
382                         }
383                 });
384                 projectPathTextField.setEnabled(false);
385
386                 final TLabel projectPathLabel = new TLabel(I18n.getMessage("jsite.project.project.path") + ":", KeyEvent.VK_P, projectPathTextField);
387                 I18nContainer.getInstance().registerRunnable(new Runnable() {
388
389                         public void run() {
390                                 projectInformationLabel.setText("<html><b>" + I18n.getMessage("jsite.project.project.information") + "</b></html>");
391                                 projectNameLabel.setText(I18n.getMessage("jsite.project.project.name") + ":");
392                                 projectDescriptionLabel.setText(I18n.getMessage("jsite.project.project.description") + ":");
393                                 projectLocalPathLabel.setText(I18n.getMessage("jsite.project.project.local-path") + ":");
394                                 projectAddressLabel.setText("<html><b>" + I18n.getMessage("jsite.project.project.address") + "</b></html>");
395                                 projectPublicKeyLabel.setText(I18n.getMessage("jsite.project.project.public-key") + ":");
396                                 projectPrivateKeyLabel.setText(I18n.getMessage("jsite.project.project.private-key") + ":");
397                                 projectPathLabel.setText(I18n.getMessage("jsite.project.project.path") + ":");
398                         }
399                 });
400                 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));
401                 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));
402
403                 return informationPanel;
404         }
405
406         /**
407          * Sets the project list.
408          * 
409          * @param projects
410          *            The list of projects
411          */
412         public void setProjects(Project[] projects) {
413                 projectListModel.clear();
414                 for (Project project : projects) {
415                         projectListModel.add(project);
416                 }
417         }
418
419         /**
420          * Returns the list of projects.
421          * 
422          * @return The list of projects
423          */
424         public Project[] getProjects() {
425                 return (Project[]) projectListModel.toArray(new Project[projectListModel.size()]);
426         }
427
428         /**
429          * Sets the freenet interface to use.
430          * 
431          * @param freenetInterface
432          *            The freenetInterface to use
433          */
434         public void setFreenetInterface(Freenet7Interface freenetInterface) {
435                 this.freenetInterface = freenetInterface;
436         }
437
438         /**
439          * Returns the currently selected project.
440          * 
441          * @return The currently selected project
442          */
443         public Project getSelectedProject() {
444                 return (Project) projectList.getSelectedValue();
445         }
446
447         /**
448          * Updates the currently selected project with changed information from a
449          * textfield.
450          * 
451          * @param documentEvent
452          *            The document event to process
453          */
454         private void setTextField(DocumentEvent documentEvent) {
455                 Document document = documentEvent.getDocument();
456                 String propertyName = (String) document.getProperty("name");
457                 Project project = (Project) projectList.getSelectedValue();
458                 if (project == null) {
459                         return;
460                 }
461                 try {
462                         String text = document.getText(0, document.getLength()).trim();
463                         if ("project.name".equals(propertyName)) {
464                                 project.setName(text);
465                                 projectList.repaint();
466                         } else if ("project.description".equals(propertyName)) {
467                                 project.setDescription(text);
468                         } else if ("project.localpath".equals(propertyName)) {
469                                 project.setLocalPath(text);
470                         } else if ("project.privatekey".equals(propertyName)) {
471                                 project.setInsertURI(text);
472                         } else if ("project.publickey".equals(propertyName)) {
473                                 project.setRequestURI(text);
474                         } else if ("project.path".equals(propertyName)) {
475                                 project.setPath(text);
476                         }
477                 } catch (BadLocationException e) {
478                         /* ignore. */
479                 }
480         }
481
482         //
483         // ACTIONS
484         //
485
486         /**
487          * Lets the user choose a local path for a project.
488          */
489         private void actionLocalPathBrowse() {
490                 Project project = (Project) projectList.getSelectedValue();
491                 if (project == null) {
492                         return;
493                 }
494                 pathChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
495                 if (pathChooser.showDialog(this, I18n.getMessage("jsite.project.action.browse.choose")) == JFileChooser.APPROVE_OPTION) {
496                         projectLocalPathTextField.setText(pathChooser.getSelectedFile().getPath());
497                 }
498         }
499
500         /**
501          * Adds a new project.
502          */
503         private void actionAdd() {
504                 String[] keyPair = null;
505                 if (!freenetInterface.hasNode()) {
506                         JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
507                         return;
508                 }
509                 try {
510                         keyPair = freenetInterface.generateKeyPair();
511                 } catch (IOException ioe1) {
512                         JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.project.keygen.io-error"), ioe1.getMessage()), null, JOptionPane.ERROR_MESSAGE);
513                         return;
514                 }
515                 Project newProject = new Project();
516                 newProject.setName(I18n.getMessage("jsite.project.new-project.name"));
517                 newProject.setInsertURI(keyPair[0]);
518                 newProject.setRequestURI(keyPair[1]);
519                 newProject.setEdition(0);
520                 projectListModel.add(newProject);
521                 projectScrollPane.revalidate();
522                 projectScrollPane.repaint();
523                 projectList.setSelectedIndex(projectListModel.size() - 1);
524         }
525
526         /**
527          * Deletes the currently selected project.
528          */
529         private void actionDelete() {
530                 int selectedIndex = projectList.getSelectedIndex();
531                 if (selectedIndex > -1) {
532                         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) {
533                                 projectListModel.remove(selectedIndex);
534                                 projectList.clearSelection();
535                                 if (projectListModel.getSize() != 0) {
536                                         projectList.setSelectedIndex(Math.min(selectedIndex, projectListModel.getSize() - 1));
537                                 }
538                         }
539                 }
540         }
541
542         /**
543          * Clones the currently selected project.
544          */
545         private void actionClone() {
546                 int selectedIndex = projectList.getSelectedIndex();
547                 if (selectedIndex > -1) {
548                         Project newProject = new Project((Project) projectList.getSelectedValue());
549                         newProject.setName(MessageFormat.format(I18n.getMessage("jsite.project.action.clone-project.copy"), newProject.getName()));
550                         projectListModel.add(newProject);
551                         projectList.setSelectedIndex(projectListModel.indexOf(newProject));
552                 }
553         }
554
555         /**
556          * Copies the request URI of the currently selected project to the
557          * clipboard.
558          */
559         private void actionCopyURI() {
560                 int selectedIndex = projectList.getSelectedIndex();
561                 if (selectedIndex > -1) {
562                         Project selectedProject = (Project) projectList.getSelectedValue();
563                         Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
564                         clipboard.setContents(new StringSelection(selectedProject.getFinalRequestURI(0)), this);
565                 }
566         }
567
568         /**
569          * Generates a new key for the currently selected project.
570          */
571         private void actionGenerateNewKey() {
572                 if (JOptionPane.showConfirmDialog(this, I18n.getMessage("jsite.project.warning.generate-new-key"), null, JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) {
573                         return;
574                 }
575                 int selectedIndex = projectList.getSelectedIndex();
576                 if (selectedIndex > -1) {
577                         Project selectedProject = (Project) projectList.getSelectedValue();
578                         String[] keyPair = null;
579                         try {
580                                 keyPair = freenetInterface.generateKeyPair();
581                         } catch (IOException ioe1) {
582                                 JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.project.keygen.io-error"), ioe1.getMessage()), null, JOptionPane.ERROR_MESSAGE);
583                                 return;
584                         }
585                         selectedProject.setInsertURI(keyPair[0]);
586                         selectedProject.setRequestURI(keyPair[1]);
587                         projectPublicKeyTextField.setText(selectedProject.getRequestURI());
588                         projectPrivateKeyTextField.setText(selectedProject.getInsertURI());
589                 }
590         }
591
592         //
593         // INTERFACE ListSelectionListener
594         //
595
596         /**
597          * {@inheritDoc}
598          */
599         public void valueChanged(ListSelectionEvent listSelectionEvent) {
600                 int selectedRow = projectList.getSelectedIndex();
601                 Project selectedProject = (Project) projectList.getSelectedValue();
602                 projectNameTextField.setEnabled(selectedRow > -1);
603                 projectDescriptionTextField.setEnabled(selectedRow > -1);
604                 projectLocalPathTextField.setEnabled(selectedRow > -1);
605                 projectPublicKeyTextField.setEnabled(selectedRow > -1);
606                 projectPrivateKeyTextField.setEnabled(selectedRow > -1);
607                 projectPathTextField.setEnabled(selectedRow > -1);
608                 projectLocalPathBrowseAction.setEnabled(selectedRow > -1);
609                 projectDeleteAction.setEnabled(selectedRow > -1);
610                 projectCloneAction.setEnabled(selectedRow > -1);
611                 projectCopyURIAction.setEnabled(selectedRow > -1);
612                 projectGenerateKeyAction.setEnabled(selectedRow > -1);
613                 if (selectedRow > -1) {
614                         projectNameTextField.setText(selectedProject.getName());
615                         projectDescriptionTextField.setText(selectedProject.getDescription());
616                         projectLocalPathTextField.setText(selectedProject.getLocalPath());
617                         projectPublicKeyTextField.setText(selectedProject.getRequestURI());
618                         projectPrivateKeyTextField.setText(selectedProject.getInsertURI());
619                         projectPathTextField.setText(selectedProject.getPath());
620                 } else {
621                         projectNameTextField.setText("");
622                         projectDescriptionTextField.setText("");
623                         projectLocalPathTextField.setText("");
624                         projectPublicKeyTextField.setText("");
625                         projectPrivateKeyTextField.setText("");
626                         projectPathTextField.setText("");
627                 }
628         }
629
630         //
631         // INTERFACE ChangeListener
632         //
633
634         //
635         // INTERFACE DocumentListener
636         //
637
638         /**
639          * {@inheritDoc}
640          */
641         public void insertUpdate(DocumentEvent documentEvent) {
642                 setTextField(documentEvent);
643         }
644
645         /**
646          * {@inheritDoc}
647          */
648         public void removeUpdate(DocumentEvent documentEvent) {
649                 setTextField(documentEvent);
650         }
651
652         /**
653          * {@inheritDoc}
654          */
655         public void changedUpdate(DocumentEvent documentEvent) {
656                 setTextField(documentEvent);
657         }
658
659         //
660         // INTERFACE ClipboardOwner
661         //
662
663         /**
664          * {@inheritDoc}
665          */
666         public void lostOwnership(Clipboard clipboard, Transferable contents) {
667                 /* ignore. */
668         }
669
670 }