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