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