Treat an empty index file as no index file.
[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 public key textfield. */
123         private JTextField projectPublicKeyTextField;
124
125         /** The private key textfield. */
126         private JTextField projectPrivateKeyTextField;
127
128         /** The project path textfield. */
129         private JTextField projectPathTextField;
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();
161                 projectList = new JList(projectListModel);
162                 projectList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
163                 projectList.addListSelectionListener(this);
164                 projectList.setPreferredSize(new Dimension(150, projectList.getPreferredSize().height));
165
166                 add(projectScrollPane = new JScrollPane(projectList), BorderLayout.LINE_START);
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                 projectGenerateKeyAction = new AbstractAction(I18n.getMessage("jsite.project.action.generate-new-key")) {
262
263                         @SuppressWarnings("synthetic-access")
264                         public void actionPerformed(ActionEvent actionEvent) {
265                                 actionGenerateNewKey();
266                         }
267                 };
268                 projectGenerateKeyAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.generate-new-key.tooltip"));
269                 projectGenerateKeyAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_G);
270                 projectGenerateKeyAction.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                                 projectGenerateKeyAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.generate-new-key"));
298                                 projectGenerateKeyAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.generate-new-key.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(projectCopyURIAction));
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                 projectPublicKeyTextField = new JTextField(27);
361                 projectPublicKeyTextField.getDocument().putProperty("name", "project.publickey");
362                 projectPublicKeyTextField.getDocument().addDocumentListener(this);
363                 projectPublicKeyTextField.setEnabled(false);
364
365                 final TLabel projectPublicKeyLabel = new TLabel(I18n.getMessage("jsite.project.project.public-key") + ":", KeyEvent.VK_U, projectPublicKeyTextField);
366                 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));
367                 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));
368                 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));
369
370                 projectPrivateKeyTextField = new JTextField(27);
371                 projectPrivateKeyTextField.getDocument().putProperty("name", "project.privatekey");
372                 projectPrivateKeyTextField.getDocument().addDocumentListener(this);
373                 projectPrivateKeyTextField.setEnabled(false);
374
375                 final TLabel projectPrivateKeyLabel = new TLabel(I18n.getMessage("jsite.project.project.private-key") + ":", KeyEvent.VK_R, projectPrivateKeyTextField);
376                 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));
377                 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));
378                 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));
379
380                 projectPathTextField = new JTextField();
381                 projectPathTextField.getDocument().putProperty("name", "project.path");
382                 projectPathTextField.getDocument().addDocumentListener(this);
383                 ((AbstractDocument) projectPathTextField.getDocument()).setDocumentFilter(new DocumentFilter() {
384
385                         /**
386                          * {@inheritDoc}
387                          */
388                         @Override
389                         public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
390                                 super.insertString(fb, offset, string.replaceAll("/", ""), attr);
391                         }
392
393                         /**
394                          * {@inheritDoc}
395                          */
396                         @Override
397                         public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
398                                 super.replace(fb, offset, length, text.replaceAll("/", ""), attrs);
399                         }
400                 });
401                 projectPathTextField.setEnabled(false);
402
403                 final TLabel projectPathLabel = new TLabel(I18n.getMessage("jsite.project.project.path") + ":", KeyEvent.VK_P, projectPathTextField);
404                 I18nContainer.getInstance().registerRunnable(new Runnable() {
405
406                         public void run() {
407                                 projectInformationLabel.setText("<html><b>" + I18n.getMessage("jsite.project.project.information") + "</b></html>");
408                                 projectNameLabel.setText(I18n.getMessage("jsite.project.project.name") + ":");
409                                 projectDescriptionLabel.setText(I18n.getMessage("jsite.project.project.description") + ":");
410                                 projectLocalPathLabel.setText(I18n.getMessage("jsite.project.project.local-path") + ":");
411                                 projectAddressLabel.setText("<html><b>" + I18n.getMessage("jsite.project.project.address") + "</b></html>");
412                                 projectPublicKeyLabel.setText(I18n.getMessage("jsite.project.project.public-key") + ":");
413                                 projectPrivateKeyLabel.setText(I18n.getMessage("jsite.project.project.private-key") + ":");
414                                 projectPathLabel.setText(I18n.getMessage("jsite.project.project.path") + ":");
415                         }
416                 });
417                 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));
418                 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));
419
420                 return informationPanel;
421         }
422
423         /**
424          * Sets the project list.
425          *
426          * @param projects
427          *            The list of projects
428          */
429         public void setProjects(Project[] projects) {
430                 projectListModel.clear();
431                 for (Project project : projects) {
432                         projectListModel.add(project);
433                 }
434         }
435
436         /**
437          * Returns the list of projects.
438          *
439          * @return The list of projects
440          */
441         public Project[] getProjects() {
442                 return (Project[]) projectListModel.toArray(new Project[projectListModel.size()]);
443         }
444
445         /**
446          * Sets the freenet interface to use.
447          *
448          * @param freenetInterface
449          *            The freenetInterface to use
450          */
451         public void setFreenetInterface(Freenet7Interface freenetInterface) {
452                 this.freenetInterface = freenetInterface;
453         }
454
455         /**
456          * Returns the currently selected project.
457          *
458          * @return The currently selected project
459          */
460         public Project getSelectedProject() {
461                 return (Project) projectList.getSelectedValue();
462         }
463
464         /**
465          * Updates the currently selected project with changed information from a
466          * textfield.
467          *
468          * @param documentEvent
469          *            The document event to process
470          */
471         private void setTextField(DocumentEvent documentEvent) {
472                 Document document = documentEvent.getDocument();
473                 String propertyName = (String) document.getProperty("name");
474                 Project project = (Project) projectList.getSelectedValue();
475                 if (project == null) {
476                         return;
477                 }
478                 try {
479                         String text = document.getText(0, document.getLength()).trim();
480                         if ("project.name".equals(propertyName)) {
481                                 project.setName(text);
482                                 projectList.repaint();
483                         } else if ("project.description".equals(propertyName)) {
484                                 project.setDescription(text);
485                         } else if ("project.localpath".equals(propertyName)) {
486                                 project.setLocalPath(text);
487                         } else if ("project.privatekey".equals(propertyName)) {
488                                 project.setInsertURI(text);
489                         } else if ("project.publickey".equals(propertyName)) {
490                                 project.setRequestURI(text);
491                         } else if ("project.path".equals(propertyName)) {
492                                 project.setPath(text);
493                         }
494                 } catch (BadLocationException e) {
495                         /* ignore. */
496                 }
497         }
498
499         //
500         // ACTIONS
501         //
502
503         /**
504          * Lets the user choose a local path for a project.
505          */
506         private void actionLocalPathBrowse() {
507                 Project project = (Project) projectList.getSelectedValue();
508                 if (project == null) {
509                         return;
510                 }
511                 pathChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
512                 if (pathChooser.showDialog(this, I18n.getMessage("jsite.project.action.browse.choose")) == JFileChooser.APPROVE_OPTION) {
513                         projectLocalPathTextField.setText(pathChooser.getSelectedFile().getPath());
514                 }
515         }
516
517         /**
518          * Adds a new project.
519          */
520         private void actionAdd() {
521                 String[] keyPair = null;
522                 if (!freenetInterface.hasNode()) {
523                         JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
524                         return;
525                 }
526                 try {
527                         keyPair = freenetInterface.generateKeyPair();
528                 } catch (IOException ioe1) {
529                         JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.project.keygen.io-error"), ioe1.getMessage()), null, JOptionPane.ERROR_MESSAGE);
530                         return;
531                 }
532                 Project newProject = new Project();
533                 newProject.setName(I18n.getMessage("jsite.project.new-project.name"));
534                 newProject.setInsertURI(keyPair[0]);
535                 newProject.setRequestURI(keyPair[1]);
536                 newProject.setEdition(-1);
537                 newProject.setPath("");
538                 projectListModel.add(newProject);
539                 projectScrollPane.revalidate();
540                 projectScrollPane.repaint();
541                 projectList.setSelectedIndex(projectListModel.indexOf(newProject));
542         }
543
544         /**
545          * Deletes the currently selected project.
546          */
547         private void actionDelete() {
548                 int selectedIndex = projectList.getSelectedIndex();
549                 if (selectedIndex > -1) {
550                         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) {
551                                 projectListModel.remove(selectedIndex);
552                                 projectList.clearSelection();
553                                 if (projectListModel.getSize() != 0) {
554                                         projectList.setSelectedIndex(Math.min(selectedIndex, projectListModel.getSize() - 1));
555                                 }
556                         }
557                 }
558         }
559
560         /**
561          * Clones the currently selected project.
562          */
563         private void actionClone() {
564                 int selectedIndex = projectList.getSelectedIndex();
565                 if (selectedIndex > -1) {
566                         Project newProject = new Project((Project) projectList.getSelectedValue());
567                         newProject.setName(MessageFormat.format(I18n.getMessage("jsite.project.action.clone-project.copy"), newProject.getName()));
568                         projectListModel.add(newProject);
569                         projectList.setSelectedIndex(projectListModel.indexOf(newProject));
570                 }
571         }
572
573         /**
574          * Copies the request URI of the currently selected project to the
575          * clipboard.
576          */
577         private void actionCopyURI() {
578                 int selectedIndex = projectList.getSelectedIndex();
579                 if (selectedIndex > -1) {
580                         Project selectedProject = (Project) projectList.getSelectedValue();
581                         Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
582                         clipboard.setContents(new StringSelection(selectedProject.getFinalRequestURI(0)), this);
583                 }
584         }
585
586         /**
587          * Generates a new key for the currently selected project.
588          */
589         private void actionGenerateNewKey() {
590                 if (JOptionPane.showConfirmDialog(this, I18n.getMessage("jsite.project.warning.generate-new-key"), null, JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) {
591                         return;
592                 }
593                 int selectedIndex = projectList.getSelectedIndex();
594                 if (selectedIndex > -1) {
595                         Project selectedProject = (Project) projectList.getSelectedValue();
596                         String[] keyPair = null;
597                         try {
598                                 keyPair = freenetInterface.generateKeyPair();
599                         } catch (IOException ioe1) {
600                                 JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.project.keygen.io-error"), ioe1.getMessage()), null, JOptionPane.ERROR_MESSAGE);
601                                 return;
602                         }
603                         selectedProject.setInsertURI(keyPair[0]);
604                         selectedProject.setRequestURI(keyPair[1]);
605                         projectPublicKeyTextField.setText(selectedProject.getRequestURI());
606                         projectPrivateKeyTextField.setText(selectedProject.getInsertURI());
607                 }
608         }
609
610         /**
611          * Resets the edition of the currently selected project.
612          */
613         private void actionResetEdition() {
614                 if (JOptionPane.showConfirmDialog(this, I18n.getMessage("jsite.project.warning.reset-edition"), null, JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) {
615                         return;
616                 }
617                 int selectedIndex = projectList.getSelectedIndex();
618                 if (selectedIndex > -1) {
619                         Project selectedProject = (Project) projectList.getSelectedValue();
620                         selectedProject.setEdition(-1);
621                 }
622         }
623
624         //
625         // INTERFACE ListSelectionListener
626         //
627
628         /**
629          * {@inheritDoc}
630          */
631         public void valueChanged(ListSelectionEvent listSelectionEvent) {
632                 int selectedRow = projectList.getSelectedIndex();
633                 Project selectedProject = (Project) projectList.getSelectedValue();
634                 projectNameTextField.setEnabled(selectedRow > -1);
635                 projectDescriptionTextField.setEnabled(selectedRow > -1);
636                 projectLocalPathTextField.setEnabled(selectedRow > -1);
637                 projectPublicKeyTextField.setEnabled(selectedRow > -1);
638                 projectPrivateKeyTextField.setEnabled(selectedRow > -1);
639                 projectPathTextField.setEnabled(selectedRow > -1);
640                 projectLocalPathBrowseAction.setEnabled(selectedRow > -1);
641                 projectDeleteAction.setEnabled(selectedRow > -1);
642                 projectCloneAction.setEnabled(selectedRow > -1);
643                 projectCopyURIAction.setEnabled(selectedRow > -1);
644                 projectGenerateKeyAction.setEnabled(selectedRow > -1);
645                 projectResetEditionAction.setEnabled(selectedRow > -1);
646                 if (selectedRow > -1) {
647                         projectNameTextField.setText(selectedProject.getName());
648                         projectDescriptionTextField.setText(selectedProject.getDescription());
649                         projectLocalPathTextField.setText(selectedProject.getLocalPath());
650                         projectPublicKeyTextField.setText(selectedProject.getRequestURI());
651                         projectPrivateKeyTextField.setText(selectedProject.getInsertURI());
652                         projectPathTextField.setText(selectedProject.getPath());
653                 } else {
654                         projectNameTextField.setText("");
655                         projectDescriptionTextField.setText("");
656                         projectLocalPathTextField.setText("");
657                         projectPublicKeyTextField.setText("");
658                         projectPrivateKeyTextField.setText("");
659                         projectPathTextField.setText("");
660                 }
661         }
662
663         //
664         // INTERFACE ChangeListener
665         //
666
667         //
668         // INTERFACE DocumentListener
669         //
670
671         /**
672          * {@inheritDoc}
673          */
674         public void insertUpdate(DocumentEvent documentEvent) {
675                 setTextField(documentEvent);
676         }
677
678         /**
679          * {@inheritDoc}
680          */
681         public void removeUpdate(DocumentEvent documentEvent) {
682                 setTextField(documentEvent);
683         }
684
685         /**
686          * {@inheritDoc}
687          */
688         public void changedUpdate(DocumentEvent documentEvent) {
689                 setTextField(documentEvent);
690         }
691
692         //
693         // INTERFACE ClipboardOwner
694         //
695
696         /**
697          * {@inheritDoc}
698          */
699         public void lostOwnership(Clipboard clipboard, Transferable contents) {
700                 /* ignore. */
701         }
702
703 }