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