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