c24f7bfcdd0c98ca2ef4f6f8c5edf57cdc90f4bd
[jSite.git] / src / de / todesbaum / jsite / gui / ProjectPage.java
1 /*
2  * jSite - a tool for uploading websites into Freenet
3  * Copyright (C) 2006 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package de.todesbaum.jsite.gui;
21
22 import java.awt.BorderLayout;
23 import java.awt.Dimension;
24 import java.awt.FlowLayout;
25 import java.awt.GridBagConstraints;
26 import java.awt.GridBagLayout;
27 import java.awt.Insets;
28 import java.awt.Toolkit;
29 import java.awt.datatransfer.Clipboard;
30 import java.awt.datatransfer.ClipboardOwner;
31 import java.awt.datatransfer.StringSelection;
32 import java.awt.datatransfer.Transferable;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.KeyEvent;
35 import java.io.IOException;
36 import java.text.MessageFormat;
37
38 import javax.swing.AbstractAction;
39 import javax.swing.Action;
40 import javax.swing.JButton;
41 import javax.swing.JComponent;
42 import javax.swing.JFileChooser;
43 import javax.swing.JLabel;
44 import javax.swing.JList;
45 import javax.swing.JOptionPane;
46 import javax.swing.JPanel;
47 import javax.swing.JScrollPane;
48 import javax.swing.JTextField;
49 import javax.swing.ListSelectionModel;
50 import javax.swing.border.EmptyBorder;
51 import javax.swing.event.DocumentEvent;
52 import javax.swing.event.DocumentListener;
53 import javax.swing.event.ListSelectionEvent;
54 import javax.swing.event.ListSelectionListener;
55 import javax.swing.text.AbstractDocument;
56 import javax.swing.text.AttributeSet;
57 import javax.swing.text.BadLocationException;
58 import javax.swing.text.Document;
59 import javax.swing.text.DocumentFilter;
60
61 import 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.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 “copy URI” action. */
93         private Action projectCopyURIAction;
94
95         /** The “generate key” action. */
96         private Action projectGenerateKeyAction;
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 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 public key textfield. */
126         private JTextField projectPublicKeyTextField;
127
128         /** The private key textfield. */
129         private JTextField projectPrivateKeyTextField;
130
131         /** The project path textfield. */
132         private JTextField projectPathTextField;
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                         public void run() {
150                                 setHeading(I18n.getMessage("jsite.project.heading"));
151                                 setDescription(I18n.getMessage("jsite.project.description"));
152                         }
153                 });
154         }
155
156         /**
157          * Initializes the page.
158          */
159         private void dialogInit() {
160                 createActions();
161
162                 pathChooser = new JFileChooser();
163                 projectListModel = new SortedListModel();
164                 projectList = new JList(projectListModel);
165                 projectList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
166                 projectList.addListSelectionListener(this);
167                 projectList.setPreferredSize(new Dimension(150, projectList.getPreferredSize().height));
168
169                 add(projectScrollPane = new JScrollPane(projectList), BorderLayout.LINE_START);
170                 add(createInformationPanel(), BorderLayout.CENTER);
171         }
172
173         /**
174          * {@inheritDoc}
175          */
176         @Override
177         public void pageAdded(TWizard wizard) {
178                 super.pageAdded(wizard);
179                 projectList.clearSelection();
180                 this.wizard.setPreviousName(I18n.getMessage("jsite.menu.nodes.manage-nodes"));
181                 this.wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
182                 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
183                 this.wizard.setNextEnabled(false);
184         }
185
186         /**
187          * Adds the given listener to the list of listeners.
188          *
189          * @param listener
190          *            The listener to add
191          */
192         public void addListSelectionListener(ListSelectionListener listener) {
193                 projectList.addListSelectionListener(listener);
194         }
195
196         /**
197          * Removes the given listener from the list of listeners.
198          *
199          * @param listener
200          *            The listener to remove
201          */
202         public void removeListSelectionListener(ListSelectionListener listener) {
203                 projectList.removeListSelectionListener(listener);
204         }
205
206         /**
207          * Creates all actions.
208          */
209         private void createActions() {
210                 projectLocalPathBrowseAction = new AbstractAction(I18n.getMessage("jsite.project.action.browse")) {
211
212                         @SuppressWarnings("synthetic-access")
213                         public void actionPerformed(ActionEvent actionEvent) {
214                                 actionLocalPathBrowse();
215                         }
216                 };
217                 projectLocalPathBrowseAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.browse.tooltip"));
218                 projectLocalPathBrowseAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_B);
219                 projectLocalPathBrowseAction.setEnabled(false);
220
221                 projectAddAction = new AbstractAction(I18n.getMessage("jsite.project.action.add-project")) {
222
223                         @SuppressWarnings("synthetic-access")
224                         public void actionPerformed(ActionEvent actionEvent) {
225                                 actionAdd();
226                         }
227                 };
228                 projectAddAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.add-project.tooltip"));
229                 projectAddAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_A);
230
231                 projectDeleteAction = new AbstractAction(I18n.getMessage("jsite.project.action.delete-project")) {
232
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                         @SuppressWarnings("synthetic-access")
245                         public void actionPerformed(ActionEvent actionEvent) {
246                                 actionClone();
247                         }
248                 };
249                 projectCloneAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.clone-project.tooltip"));
250                 projectCloneAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_L);
251                 projectCloneAction.setEnabled(false);
252
253                 projectCopyURIAction = new AbstractAction(I18n.getMessage("jsite.project.action.copy-uri")) {
254
255                         @SuppressWarnings("synthetic-access")
256                         public void actionPerformed(ActionEvent actionEvent) {
257                                 actionCopyURI();
258                         }
259                 };
260                 projectCopyURIAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.copy-uri.tooltip"));
261                 projectCopyURIAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_U);
262                 projectCopyURIAction.setEnabled(false);
263
264                 projectGenerateKeyAction = new AbstractAction(I18n.getMessage("jsite.project.action.generate-new-key")) {
265
266                         @SuppressWarnings("synthetic-access")
267                         public void actionPerformed(ActionEvent actionEvent) {
268                                 actionGenerateNewKey();
269                         }
270                 };
271                 projectGenerateKeyAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.generate-new-key.tooltip"));
272                 projectGenerateKeyAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_G);
273                 projectGenerateKeyAction.setEnabled(false);
274
275                 projectResetEditionAction = new AbstractAction(I18n.getMessage("jsite.project.action.reset-edition")) {
276
277                         @SuppressWarnings("synthetic-access")
278                         public void actionPerformed(ActionEvent actionEvent) {
279                                 actionResetEdition();
280                         }
281                 };
282                 projectResetEditionAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.reset-edition.tooltip"));
283                 projectResetEditionAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_R);
284                 projectResetEditionAction.setEnabled(false);
285
286                 I18nContainer.getInstance().registerRunnable(new Runnable() {
287
288                         @SuppressWarnings("synthetic-access")
289                         public void run() {
290                                 projectLocalPathBrowseAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.browse"));
291                                 projectLocalPathBrowseAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.browse.tooltip"));
292                                 projectAddAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.add-project"));
293                                 projectAddAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.add-project.tooltip"));
294                                 projectDeleteAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.delete-project"));
295                                 projectDeleteAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.delete-project.tooltip"));
296                                 projectCloneAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.clone-project"));
297                                 projectCloneAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.clone-project.tooltip"));
298                                 projectCopyURIAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.copy-uri"));
299                                 projectCopyURIAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.copy-uri.tooltip"));
300                                 projectGenerateKeyAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.generate-new-key"));
301                                 projectGenerateKeyAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.generate-new-key.tooltip"));
302                                 projectResetEditionAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.reset-edition"));
303                                 projectResetEditionAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.reset-edition.tooltip"));
304                                 pathChooser.setApproveButtonText(I18n.getMessage("jsite.project.action.browse.choose"));
305                         }
306                 });
307         }
308
309         /**
310          * Creates the information panel.
311          *
312          * @return The information panel
313          */
314         private JComponent createInformationPanel() {
315                 JPanel informationPanel = new JPanel(new BorderLayout(12, 12));
316
317                 JPanel informationTable = new JPanel(new GridBagLayout());
318
319                 JPanel functionButtons = new JPanel(new FlowLayout(FlowLayout.LEADING, 12, 12));
320                 functionButtons.setBorder(new EmptyBorder(-12, -12, -12, -12));
321                 functionButtons.add(new JButton(projectAddAction));
322                 functionButtons.add(new JButton(projectDeleteAction));
323                 functionButtons.add(new JButton(projectCloneAction));
324
325                 informationPanel.add(functionButtons, BorderLayout.PAGE_START);
326                 informationPanel.add(informationTable, BorderLayout.CENTER);
327
328                 final JLabel projectInformationLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.project.project.information") + "</b></html>");
329                 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));
330
331                 projectNameTextField = new JTextField();
332                 projectNameTextField.getDocument().putProperty("name", "project.name");
333                 projectNameTextField.getDocument().addDocumentListener(this);
334                 projectNameTextField.setEnabled(false);
335
336                 final TLabel projectNameLabel = new TLabel(I18n.getMessage("jsite.project.project.name") + ":", KeyEvent.VK_N, projectNameTextField);
337                 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));
338                 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));
339
340                 projectDescriptionTextField = new JTextField();
341                 projectDescriptionTextField.getDocument().putProperty("name", "project.description");
342                 projectDescriptionTextField.getDocument().addDocumentListener(this);
343                 projectDescriptionTextField.setEnabled(false);
344
345                 final TLabel projectDescriptionLabel = new TLabel(I18n.getMessage("jsite.project.project.description") + ":", KeyEvent.VK_D, projectDescriptionTextField);
346                 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));
347                 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));
348
349                 projectLocalPathTextField = new JTextField();
350                 projectLocalPathTextField.getDocument().putProperty("name", "project.localpath");
351                 projectLocalPathTextField.getDocument().addDocumentListener(this);
352                 projectLocalPathTextField.setEnabled(false);
353
354                 final TLabel projectLocalPathLabel = new TLabel(I18n.getMessage("jsite.project.project.local-path") + ":", KeyEvent.VK_L, projectLocalPathTextField);
355                 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));
356                 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));
357                 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));
358
359                 final JLabel projectAddressLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.project.project.address") + "</b></html>");
360                 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));
361
362                 projectPublicKeyTextField = new JTextField(27);
363                 projectPublicKeyTextField.getDocument().putProperty("name", "project.publickey");
364                 projectPublicKeyTextField.getDocument().addDocumentListener(this);
365                 projectPublicKeyTextField.setEnabled(false);
366
367                 final TLabel projectPublicKeyLabel = new TLabel(I18n.getMessage("jsite.project.project.public-key") + ":", KeyEvent.VK_U, projectPublicKeyTextField);
368                 informationTable.add(projectPublicKeyLabel, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
369                 informationTable.add(projectPublicKeyTextField, new GridBagConstraints(1, 5, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
370                 informationTable.add(new JButton(projectGenerateKeyAction), new GridBagConstraints(2, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
371
372                 projectPrivateKeyTextField = new JTextField(27);
373                 projectPrivateKeyTextField.getDocument().putProperty("name", "project.privatekey");
374                 projectPrivateKeyTextField.getDocument().addDocumentListener(this);
375                 projectPrivateKeyTextField.setEnabled(false);
376
377                 final TLabel projectPrivateKeyLabel = new TLabel(I18n.getMessage("jsite.project.project.private-key") + ":", KeyEvent.VK_R, projectPrivateKeyTextField);
378                 informationTable.add(projectPrivateKeyLabel, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
379                 informationTable.add(projectPrivateKeyTextField, new GridBagConstraints(1, 6, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
380                 informationTable.add(new JButton(projectResetEditionAction), new GridBagConstraints(2, 6, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
381
382                 projectPathTextField = new JTextField();
383                 projectPathTextField.getDocument().putProperty("name", "project.path");
384                 projectPathTextField.getDocument().addDocumentListener(this);
385                 ((AbstractDocument) projectPathTextField.getDocument()).setDocumentFilter(new DocumentFilter() {
386
387                         /**
388                          * {@inheritDoc}
389                          */
390                         @Override
391                         public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
392                                 super.insertString(fb, offset, string.replaceAll("/", ""), attr);
393                         }
394
395                         /**
396                          * {@inheritDoc}
397                          */
398                         @Override
399                         public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
400                                 super.replace(fb, offset, length, text.replaceAll("/", ""), attrs);
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, 7, 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, 7, 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, 8, 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, 8, 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, 8, 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                         public void run() {
419                                 projectInformationLabel.setText("<html><b>" + I18n.getMessage("jsite.project.project.information") + "</b></html>");
420                                 projectNameLabel.setText(I18n.getMessage("jsite.project.project.name") + ":");
421                                 projectDescriptionLabel.setText(I18n.getMessage("jsite.project.project.description") + ":");
422                                 projectLocalPathLabel.setText(I18n.getMessage("jsite.project.project.local-path") + ":");
423                                 projectAddressLabel.setText("<html><b>" + I18n.getMessage("jsite.project.project.address") + "</b></html>");
424                                 projectPublicKeyLabel.setText(I18n.getMessage("jsite.project.project.public-key") + ":");
425                                 projectPrivateKeyLabel.setText(I18n.getMessage("jsite.project.project.private-key") + ":");
426                                 projectPathLabel.setText(I18n.getMessage("jsite.project.project.path") + ":");
427                                 projectUriLabel.setText(I18n.getMessage("jsite.project.project.uri") + ":");
428                         }
429                 });
430
431                 return informationPanel;
432         }
433
434         /**
435          * Sets the project list.
436          *
437          * @param projects
438          *            The list of projects
439          */
440         public void setProjects(Project[] projects) {
441                 projectListModel.clear();
442                 for (Project project : projects) {
443                         projectListModel.add(project);
444                 }
445         }
446
447         /**
448          * Returns the list of projects.
449          *
450          * @return The list of projects
451          */
452         public Project[] getProjects() {
453                 return (Project[]) projectListModel.toArray(new Project[projectListModel.size()]);
454         }
455
456         /**
457          * Sets the freenet interface to use.
458          *
459          * @param freenetInterface
460          *            The freenetInterface to use
461          */
462         public void setFreenetInterface(Freenet7Interface freenetInterface) {
463                 this.freenetInterface = freenetInterface;
464         }
465
466         /**
467          * Returns the currently selected project.
468          *
469          * @return The currently selected project
470          */
471         public Project getSelectedProject() {
472                 return (Project) projectList.getSelectedValue();
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                 }
595         }
596
597         /**
598          * Generates a new key for the currently selected project.
599          */
600         private void actionGenerateNewKey() {
601                 if (JOptionPane.showConfirmDialog(this, I18n.getMessage("jsite.project.warning.generate-new-key"), null, JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) {
602                         return;
603                 }
604                 int selectedIndex = projectList.getSelectedIndex();
605                 if (selectedIndex > -1) {
606                         Project selectedProject = (Project) projectList.getSelectedValue();
607                         String[] keyPair = null;
608                         try {
609                                 keyPair = freenetInterface.generateKeyPair();
610                         } catch (IOException ioe1) {
611                                 JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.project.keygen.io-error"), ioe1.getMessage()), null, JOptionPane.ERROR_MESSAGE);
612                                 return;
613                         }
614                         selectedProject.setInsertURI(keyPair[0]);
615                         selectedProject.setRequestURI(keyPair[1]);
616                         projectPublicKeyTextField.setText(selectedProject.getRequestURI());
617                         projectPrivateKeyTextField.setText(selectedProject.getInsertURI());
618                 }
619         }
620
621         /**
622          * Resets the edition of the currently selected project.
623          */
624         private void actionResetEdition() {
625                 if (JOptionPane.showConfirmDialog(this, I18n.getMessage("jsite.project.warning.reset-edition"), null, JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) {
626                         return;
627                 }
628                 int selectedIndex = projectList.getSelectedIndex();
629                 if (selectedIndex > -1) {
630                         Project selectedProject = (Project) projectList.getSelectedValue();
631                         selectedProject.setEdition(-1);
632                 }
633         }
634
635         //
636         // INTERFACE ListSelectionListener
637         //
638
639         /**
640          * {@inheritDoc}
641          */
642         public void valueChanged(ListSelectionEvent listSelectionEvent) {
643                 int selectedRow = projectList.getSelectedIndex();
644                 Project selectedProject = (Project) projectList.getSelectedValue();
645                 projectNameTextField.setEnabled(selectedRow > -1);
646                 projectDescriptionTextField.setEnabled(selectedRow > -1);
647                 projectLocalPathTextField.setEnabled(selectedRow > -1);
648                 projectPublicKeyTextField.setEnabled(selectedRow > -1);
649                 projectPrivateKeyTextField.setEnabled(selectedRow > -1);
650                 projectPathTextField.setEnabled(selectedRow > -1);
651                 projectLocalPathBrowseAction.setEnabled(selectedRow > -1);
652                 projectDeleteAction.setEnabled(selectedRow > -1);
653                 projectCloneAction.setEnabled(selectedRow > -1);
654                 projectCopyURIAction.setEnabled(selectedRow > -1);
655                 projectGenerateKeyAction.setEnabled(selectedRow > -1);
656                 projectResetEditionAction.setEnabled(selectedRow > -1);
657                 if (selectedRow > -1) {
658                         projectNameTextField.setText(selectedProject.getName());
659                         projectDescriptionTextField.setText(selectedProject.getDescription());
660                         projectLocalPathTextField.setText(selectedProject.getLocalPath());
661                         projectPublicKeyTextField.setText(selectedProject.getRequestURI());
662                         projectPrivateKeyTextField.setText(selectedProject.getInsertURI());
663                         projectPathTextField.setText(selectedProject.getPath());
664                         projectCompleteUriTextField.setText("freenet:" + selectedProject.getFinalRequestURI(0));
665                 } else {
666                         projectNameTextField.setText("");
667                         projectDescriptionTextField.setText("");
668                         projectLocalPathTextField.setText("");
669                         projectPublicKeyTextField.setText("");
670                         projectPrivateKeyTextField.setText("");
671                         projectPathTextField.setText("");
672                         projectCompleteUriTextField.setText("");
673                 }
674         }
675
676         //
677         // INTERFACE ChangeListener
678         //
679
680         //
681         // INTERFACE DocumentListener
682         //
683
684         /**
685          * {@inheritDoc}
686          */
687         public void insertUpdate(DocumentEvent documentEvent) {
688                 setTextField(documentEvent);
689         }
690
691         /**
692          * {@inheritDoc}
693          */
694         public void removeUpdate(DocumentEvent documentEvent) {
695                 setTextField(documentEvent);
696         }
697
698         /**
699          * {@inheritDoc}
700          */
701         public void changedUpdate(DocumentEvent documentEvent) {
702                 setTextField(documentEvent);
703         }
704
705         //
706         // INTERFACE ClipboardOwner
707         //
708
709         /**
710          * {@inheritDoc}
711          */
712         public void lostOwnership(Clipboard clipboard, Transferable contents) {
713                 /* ignore. */
714         }
715
716 }