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