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