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