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