make action methods private and add annotations
[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  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
72  */
73 public class ProjectPage extends TWizardPage implements ListSelectionListener, DocumentListener, ClipboardOwner {
74
75         private Freenet7Interface freenetInterface;
76
77         private Action projectLocalPathBrowseAction;
78         private Action projectAddAction;
79         private Action projectDeleteAction;
80         private Action projectCloneAction;
81         private Action projectCopyURIAction;
82         private Action projectGenerateKeyAction;
83
84         private JFileChooser pathChooser;
85         private SortedListModel projectListModel;
86         private JScrollPane projectScrollPane;
87         private JList projectList;
88         private JTextField projectNameTextField;
89         private JTextField projectDescriptionTextField;
90         private JTextField projectLocalPathTextField;
91         private JTextField projectPublicKeyTextField;
92         private JTextField projectPrivateKeyTextField;
93         private JTextField projectPathTextField;
94
95         public ProjectPage(final TWizard wizard) {
96                 super(wizard);
97                 setLayout(new BorderLayout(12, 12));
98                 dialogInit();
99                 setHeading(I18n.getMessage("jsite.project.heading"));
100                 setDescription(I18n.getMessage("jsite.project.description"));
101
102                 I18nContainer.getInstance().registerRunnable(new Runnable() {
103
104                         public void run() {
105                                 setHeading(I18n.getMessage("jsite.project.heading"));
106                                 setDescription(I18n.getMessage("jsite.project.description"));
107                         }
108                 });
109         }
110
111         protected void dialogInit() {
112                 createActions();
113
114                 pathChooser = new JFileChooser();
115                 projectListModel = new SortedListModel();
116                 projectList = new JList(projectListModel);
117                 projectList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
118                 projectList.addListSelectionListener(this);
119                 projectList.setPreferredSize(new Dimension(150, projectList.getPreferredSize().height));
120
121                 add(projectScrollPane = new JScrollPane(projectList), BorderLayout.LINE_START);
122                 add(createInformationPanel(), BorderLayout.CENTER);
123         }
124
125         /**
126          * {@inheritDoc}
127          */
128         @Override
129         public void pageAdded(TWizard wizard) {
130                 super.pageAdded(wizard);
131                 projectList.clearSelection();
132                 this.wizard.setPreviousName(I18n.getMessage("jsite.menu.nodes.manage-nodes"));
133                 this.wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
134                 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
135                 this.wizard.setNextEnabled(false);
136         }
137
138         /**
139          */
140         public void addListSelectionListener(ListSelectionListener listener) {
141                 projectList.addListSelectionListener(listener);
142         }
143
144         /**
145          */
146         public void removeListSelectionListener(ListSelectionListener listener) {
147                 projectList.removeListSelectionListener(listener);
148         }
149
150         private void createActions() {
151                 projectLocalPathBrowseAction = new AbstractAction(I18n.getMessage("jsite.project.action.browse")) {
152
153                         @SuppressWarnings("synthetic-access")
154                         public void actionPerformed(ActionEvent actionEvent) {
155                                 actionLocalPathBrowse();
156                         }
157                 };
158                 projectLocalPathBrowseAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.browse.tooltip"));
159                 projectLocalPathBrowseAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_B);
160                 projectLocalPathBrowseAction.setEnabled(false);
161
162                 projectAddAction = new AbstractAction(I18n.getMessage("jsite.project.action.add-project")) {
163
164                         @SuppressWarnings("synthetic-access")
165                         public void actionPerformed(ActionEvent actionEvent) {
166                                 actionAdd();
167                         }
168                 };
169                 projectAddAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.add-project.tooltip"));
170                 projectAddAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_A);
171
172                 projectDeleteAction = new AbstractAction(I18n.getMessage("jsite.project.action.delete-project")) {
173
174                         @SuppressWarnings("synthetic-access")
175                         public void actionPerformed(ActionEvent actionEvent) {
176                                 actionDelete();
177                         }
178                 };
179                 projectDeleteAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.delete-project.tooltip"));
180                 projectDeleteAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_D);
181                 projectDeleteAction.setEnabled(false);
182
183                 projectCloneAction = new AbstractAction(I18n.getMessage("jsite.project.action.clone-project")) {
184
185                         @SuppressWarnings("synthetic-access")
186                         public void actionPerformed(ActionEvent actionEvent) {
187                                 actionClone();
188                         }
189                 };
190                 projectCloneAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.clone-project.tooltip"));
191                 projectCloneAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_L);
192                 projectCloneAction.setEnabled(false);
193
194                 projectCopyURIAction = new AbstractAction(I18n.getMessage("jsite.project.action.copy-uri")) {
195
196                         @SuppressWarnings("synthetic-access")
197                         public void actionPerformed(ActionEvent actionEvent) {
198                                 actionCopyURI();
199                         }
200                 };
201                 projectCopyURIAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.copy-uri.tooltip"));
202                 projectCopyURIAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_U);
203                 projectCopyURIAction.setEnabled(false);
204
205                 projectGenerateKeyAction = new AbstractAction(I18n.getMessage("jsite.project.action.generate-new-key")) {
206
207                         @SuppressWarnings("synthetic-access")
208                         public void actionPerformed(ActionEvent actionEvent) {
209                                 actionGenerateNewKey();
210                         }
211                 };
212                 projectGenerateKeyAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.generate-new-key.tooltip"));
213                 projectGenerateKeyAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_G);
214                 projectGenerateKeyAction.setEnabled(false);
215
216                 I18nContainer.getInstance().registerRunnable(new Runnable() {
217
218                         @SuppressWarnings("synthetic-access")
219                         public void run() {
220                                 projectLocalPathBrowseAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.browse"));
221                                 projectLocalPathBrowseAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.browse.tooltip"));
222                                 projectAddAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.add-project"));
223                                 projectAddAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.add-project.tooltip"));
224                                 projectDeleteAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.delete-project"));
225                                 projectDeleteAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.delete-project.tooltip"));
226                                 projectCloneAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.clone-project"));
227                                 projectCloneAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.clone-project.tooltip"));
228                                 projectCopyURIAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.copy-uri"));
229                                 projectCopyURIAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.copy-uri.tooltip"));
230                                 projectGenerateKeyAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.generate-new-key"));
231                                 projectGenerateKeyAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.generate-new-key.tooltip"));
232                                 pathChooser.setApproveButtonText(I18n.getMessage("jsite.project.action.browse.choose"));
233                         }
234                 });
235         }
236
237         private JComponent createInformationPanel() {
238                 JPanel informationPanel = new JPanel(new BorderLayout(12, 12));
239
240                 JPanel informationTable = new JPanel(new GridBagLayout());
241
242                 JPanel functionButtons = new JPanel(new FlowLayout(FlowLayout.LEADING, 12, 12));
243                 functionButtons.setBorder(new EmptyBorder(-12, -12, -12, -12));
244                 functionButtons.add(new JButton(projectAddAction));
245                 functionButtons.add(new JButton(projectDeleteAction));
246                 functionButtons.add(new JButton(projectCloneAction));
247                 functionButtons.add(new JButton(projectCopyURIAction));
248
249                 informationPanel.add(functionButtons, BorderLayout.PAGE_START);
250                 informationPanel.add(informationTable, BorderLayout.CENTER);
251
252                 final JLabel projectInformationLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.project.project.information") + "</b></html>");
253                 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));
254
255                 projectNameTextField = new JTextField();
256                 projectNameTextField.getDocument().putProperty("name", "project.name");
257                 projectNameTextField.getDocument().addDocumentListener(this);
258                 projectNameTextField.setEnabled(false);
259
260                 final TLabel projectNameLabel = new TLabel(I18n.getMessage("jsite.project.project.name") + ":", KeyEvent.VK_N, projectNameTextField);
261                 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));
262                 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));
263
264                 projectDescriptionTextField = new JTextField();
265                 projectDescriptionTextField.getDocument().putProperty("name", "project.description");
266                 projectDescriptionTextField.getDocument().addDocumentListener(this);
267                 projectDescriptionTextField.setEnabled(false);
268
269                 final TLabel projectDescriptionLabel = new TLabel(I18n.getMessage("jsite.project.project.description") + ":", KeyEvent.VK_D, projectDescriptionTextField);
270                 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));
271                 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));
272
273                 projectLocalPathTextField = new JTextField();
274                 projectLocalPathTextField.getDocument().putProperty("name", "project.localpath");
275                 projectLocalPathTextField.getDocument().addDocumentListener(this);
276                 projectLocalPathTextField.setEnabled(false);
277
278                 final TLabel projectLocalPathLabel = new TLabel(I18n.getMessage("jsite.project.project.local-path") + ":", KeyEvent.VK_L, projectLocalPathTextField);
279                 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));
280                 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));
281                 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));
282
283                 final JLabel projectAddressLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.project.project.address") + "</b></html>");
284                 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));
285
286                 projectPublicKeyTextField = new JTextField(27);
287                 projectPublicKeyTextField.getDocument().putProperty("name", "project.publickey");
288                 projectPublicKeyTextField.getDocument().addDocumentListener(this);
289                 projectPublicKeyTextField.setEnabled(false);
290
291                 final TLabel projectPublicKeyLabel = new TLabel(I18n.getMessage("jsite.project.project.public-key") + ":", KeyEvent.VK_U, projectPublicKeyTextField);
292                 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));
293                 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));
294                 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));
295
296                 projectPrivateKeyTextField = new JTextField(27);
297                 projectPrivateKeyTextField.getDocument().putProperty("name", "project.privatekey");
298                 projectPrivateKeyTextField.getDocument().addDocumentListener(this);
299                 projectPrivateKeyTextField.setEnabled(false);
300
301                 final TLabel projectPrivateKeyLabel = new TLabel(I18n.getMessage("jsite.project.project.private-key") + ":", KeyEvent.VK_R, projectPrivateKeyTextField);
302                 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));
303                 informationTable.add(projectPrivateKeyTextField, new GridBagConstraints(1, 6, 2, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
304
305                 projectPathTextField = new JTextField();
306                 projectPathTextField.getDocument().putProperty("name", "project.path");
307                 projectPathTextField.getDocument().addDocumentListener(this);
308                 ((AbstractDocument) projectPathTextField.getDocument()).setDocumentFilter(new DocumentFilter() {
309
310                         /**
311                          * {@inheritDoc}
312                          */
313                         @Override
314                         public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
315                                 super.insertString(fb, offset, string.replaceAll("/", ""), attr);
316                         }
317
318                         /**
319                          * {@inheritDoc}
320                          */
321                         @Override
322                         public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
323                                 super.replace(fb, offset, length, text.replaceAll("/", ""), attrs);
324                         }
325                 });
326                 projectPathTextField.setEnabled(false);
327
328                 final TLabel projectPathLabel = new TLabel(I18n.getMessage("jsite.project.project.path") + ":", KeyEvent.VK_P, projectPathTextField);
329                 I18nContainer.getInstance().registerRunnable(new Runnable() {
330
331                         public void run() {
332                                 projectInformationLabel.setText("<html><b>" + I18n.getMessage("jsite.project.project.information") + "</b></html>");
333                                 projectNameLabel.setText(I18n.getMessage("jsite.project.project.name") + ":");
334                                 projectDescriptionLabel.setText(I18n.getMessage("jsite.project.project.description") + ":");
335                                 projectLocalPathLabel.setText(I18n.getMessage("jsite.project.project.local-path") + ":");
336                                 projectAddressLabel.setText("<html><b>" + I18n.getMessage("jsite.project.project.address") + "</b></html>");
337                                 projectPublicKeyLabel.setText(I18n.getMessage("jsite.project.project.public-key") + ":");
338                                 projectPrivateKeyLabel.setText(I18n.getMessage("jsite.project.project.private-key") + ":");
339                                 projectPathLabel.setText(I18n.getMessage("jsite.project.project.path") + ":");
340                         }
341                 });
342                 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));
343                 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));
344
345                 return informationPanel;
346         }
347
348         public void setProjects(Project[] projects) {
349                 projectListModel.clear();
350                 for (Project project: projects) {
351                         projectListModel.add(project);
352                 }
353         }
354
355         public Project[] getProjects() {
356                 return (Project[]) projectListModel.toArray(new Project[projectListModel.size()]);
357         }
358
359         /**
360          * @param freenetInterface
361          *            The freenetInterface to set.
362          */
363         public void setFreenetInterface(Freenet7Interface freenetInterface) {
364                 this.freenetInterface = freenetInterface;
365         }
366
367         public Project getSelectedProject() {
368                 return (Project) projectList.getSelectedValue();
369         }
370
371         private void setTextField(DocumentEvent documentEvent) {
372                 Document document = documentEvent.getDocument();
373                 String propertyName = (String) document.getProperty("name");
374                 Project project = (Project) projectList.getSelectedValue();
375                 if (project == null) {
376                         return;
377                 }
378                 try {
379                         String text = document.getText(0, document.getLength()).trim();
380                         if ("project.name".equals(propertyName)) {
381                                 project.setName(text);
382                                 projectList.repaint();
383                         } else if ("project.description".equals(propertyName)) {
384                                 project.setDescription(text);
385                         } else if ("project.localpath".equals(propertyName)) {
386                                 project.setLocalPath(text);
387                         } else if ("project.privatekey".equals(propertyName)) {
388                                 project.setInsertURI(text);
389                         } else if ("project.publickey".equals(propertyName)) {
390                                 project.setRequestURI(text);
391                         } else if ("project.path".equals(propertyName)) {
392                                 project.setPath(text);
393                         }
394                 } catch (BadLocationException e) {
395                 }
396         }
397
398         //
399         // ACTIONS
400         //
401
402         private void actionLocalPathBrowse() {
403                 Project project = (Project) projectList.getSelectedValue();
404                 if (project == null) {
405                         return;
406                 }
407                 pathChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
408                 if (pathChooser.showDialog(this, I18n.getMessage("jsite.project.action.browse.choose")) == JFileChooser.APPROVE_OPTION) {
409                         projectLocalPathTextField.setText(pathChooser.getSelectedFile().getPath());
410                 }
411         }
412
413         private void actionAdd() {
414                 String[] keyPair = null;
415                 if (!freenetInterface.hasNode()) {
416                         JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
417                         return;
418                 }
419                 try {
420                         keyPair = freenetInterface.generateKeyPair();
421                 } catch (IOException ioe1) {
422                         JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.project.keygen.io-error"), ioe1.getMessage()), null, JOptionPane.ERROR_MESSAGE);
423                         return;
424                 }
425                 Project newProject = new Project();
426                 newProject.setName(I18n.getMessage("jsite.project.new-project.name"));
427                 newProject.setInsertURI(keyPair[0]);
428                 newProject.setRequestURI(keyPair[1]);
429                 newProject.setEdition(0);
430                 projectListModel.add(newProject);
431                 projectScrollPane.revalidate();
432                 projectScrollPane.repaint();
433                 projectList.setSelectedIndex(projectListModel.size() - 1);
434         }
435
436         private void actionDelete() {
437                 int selectedIndex = projectList.getSelectedIndex();
438                 if (selectedIndex > -1) {
439                         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) {
440                                 projectListModel.remove(selectedIndex);
441                                 projectList.clearSelection();
442                                 if (projectListModel.getSize() != 0) {
443                                         projectList.setSelectedIndex(Math.min(selectedIndex, projectListModel.getSize() - 1));
444                                 }
445                         }
446                 }
447         }
448
449         private void actionClone() {
450                 int selectedIndex = projectList.getSelectedIndex();
451                 if (selectedIndex > -1) {
452                         Project newProject = new Project((Project) projectList.getSelectedValue());
453                         newProject.setName(MessageFormat.format(I18n.getMessage("jsite.project.action.clone-project.copy"), newProject.getName()));
454                         projectListModel.add(newProject);
455                         projectList.setSelectedIndex(projectListModel.indexOf(newProject));
456                 }
457         }
458
459         private void actionCopyURI() {
460                 int selectedIndex = projectList.getSelectedIndex();
461                 if (selectedIndex > -1) {
462                         Project selectedProject = (Project) projectList.getSelectedValue();
463                         Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
464                         clipboard.setContents(new StringSelection(selectedProject.getFinalRequestURI(0)), this);
465                 }
466         }
467
468         private void actionGenerateNewKey() {
469                 if (JOptionPane.showConfirmDialog(this, I18n.getMessage("jsite.project.warning.generate-new-key"), null, JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) {
470                         return;
471                 }
472                 int selectedIndex = projectList.getSelectedIndex();
473                 if (selectedIndex > -1) {
474                         Project selectedProject = (Project) projectList.getSelectedValue();
475                         String[] keyPair = null;
476                         try {
477                                 keyPair = freenetInterface.generateKeyPair();
478                         } catch (IOException ioe1) {
479                                 JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.project.keygen.io-error"), ioe1.getMessage()), null, JOptionPane.ERROR_MESSAGE);
480                                 return;
481                         }
482                         selectedProject.setInsertURI(keyPair[0]);
483                         selectedProject.setRequestURI(keyPair[1]);
484                         projectPublicKeyTextField.setText(selectedProject.getRequestURI());
485                         projectPrivateKeyTextField.setText(selectedProject.getInsertURI());
486                 }
487         }
488
489         //
490         // INTERFACE ListSelectionListener
491         //
492
493         /**
494          * {@inheritDoc}
495          */
496         public void valueChanged(ListSelectionEvent listSelectionEvent) {
497                 int selectedRow = projectList.getSelectedIndex();
498                 Project selectedProject = (Project) projectList.getSelectedValue();
499                 projectNameTextField.setEnabled(selectedRow > -1);
500                 projectDescriptionTextField.setEnabled(selectedRow > -1);
501                 projectLocalPathTextField.setEnabled(selectedRow > -1);
502                 projectPublicKeyTextField.setEnabled(selectedRow > -1);
503                 projectPrivateKeyTextField.setEnabled(selectedRow > -1);
504                 projectPathTextField.setEnabled(selectedRow > -1);
505                 projectLocalPathBrowseAction.setEnabled(selectedRow > -1);
506                 projectDeleteAction.setEnabled(selectedRow > -1);
507                 projectCloneAction.setEnabled(selectedRow > -1);
508                 projectCopyURIAction.setEnabled(selectedRow > -1);
509                 projectGenerateKeyAction.setEnabled(selectedRow > -1);
510                 if (selectedRow > -1) {
511                         projectNameTextField.setText(selectedProject.getName());
512                         projectDescriptionTextField.setText(selectedProject.getDescription());
513                         projectLocalPathTextField.setText(selectedProject.getLocalPath());
514                         projectPublicKeyTextField.setText(selectedProject.getRequestURI());
515                         projectPrivateKeyTextField.setText(selectedProject.getInsertURI());
516                         projectPathTextField.setText(selectedProject.getPath());
517                 } else {
518                         projectNameTextField.setText("");
519                         projectDescriptionTextField.setText("");
520                         projectLocalPathTextField.setText("");
521                         projectPublicKeyTextField.setText("");
522                         projectPrivateKeyTextField.setText("");
523                         projectPathTextField.setText("");
524                 }
525         }
526
527         //
528         // INTERFACE ChangeListener
529         //
530
531         //
532         // INTERFACE DocumentListener
533         //
534
535         /**
536          * {@inheritDoc}
537          */
538         public void insertUpdate(DocumentEvent documentEvent) {
539                 setTextField(documentEvent);
540         }
541
542         /**
543          * {@inheritDoc}
544          */
545         public void removeUpdate(DocumentEvent documentEvent) {
546                 setTextField(documentEvent);
547         }
548
549         /**
550          * {@inheritDoc}
551          */
552         public void changedUpdate(DocumentEvent documentEvent) {
553                 setTextField(documentEvent);
554         }
555
556         //
557         // INTERFACE ClipboardOwner
558         //
559
560         /**
561          * {@inheritDoc}
562          */
563         public void lostOwnership(Clipboard clipboard, Transferable contents) {
564         }
565
566 }