086493ca19e2a9981e270446c4fdfa5cf104137f
[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.BadLocationException;
56 import javax.swing.text.Document;
57
58 import de.todesbaum.jsite.application.Freenet7Interface;
59 import de.todesbaum.jsite.application.Project;
60 import de.todesbaum.jsite.i18n.I18n;
61 import de.todesbaum.util.swing.SortedListModel;
62 import de.todesbaum.util.swing.TLabel;
63 import de.todesbaum.util.swing.TWizard;
64 import de.todesbaum.util.swing.TWizardPage;
65
66 /**
67  * @author David Roden <droden@gmail.com>
68  * @version $Id$
69  */
70 public class ProjectPage extends TWizardPage implements ListSelectionListener, DocumentListener, ClipboardOwner {
71
72         private Freenet7Interface freenetInterface;
73
74         private Action projectLocalPathBrowseAction;
75         private Action projectAddAction;
76         private Action projectDeleteAction;
77         private Action projectCloneAction;
78         private Action projectCopyURIAction;
79         private Action projectGenerateKeyAction;
80
81         private JFileChooser pathChooser;
82         private SortedListModel projectListModel;
83         private JList projectList;
84         private JTextField projectNameTextField;
85         private JTextField projectDescriptionTextField;
86         private JTextField projectLocalPathTextField;
87         private JTextField projectPublicKeyTextField;
88         private JTextField projectPrivateKeyTextField;
89         private JTextField projectPathTextField;
90
91         public ProjectPage() {
92                 super();
93                 setLayout(new BorderLayout(12, 12));
94                 dialogInit();
95                 setHeading(I18n.getMessage("jsite.project.heading"));
96                 setDescription(I18n.getMessage("jsite.project.description"));
97         }
98
99         protected void dialogInit() {
100                 createActions();
101
102                 pathChooser = new JFileChooser();
103                 projectListModel = new SortedListModel();
104                 projectList = new JList(projectListModel);
105                 projectList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
106                 projectList.addListSelectionListener(this);
107                 projectList.setPreferredSize(new Dimension(150, projectList.getPreferredSize().height));
108
109                 add(new JScrollPane(projectList), BorderLayout.LINE_START);
110                 add(createInformationPanel(), BorderLayout.CENTER);
111         }
112
113         /**
114          * {@inheritDoc}
115          */
116         @Override
117         public void pageAdded(TWizard wizard) {
118                 super.pageAdded(wizard);
119                 projectList.clearSelection();
120                 wizard.setNextEnabled(false);
121         }
122
123         /**
124          */
125         public void addListSelectionListener(ListSelectionListener listener) {
126                 projectList.addListSelectionListener(listener);
127         }
128
129         /**
130          */
131         public void removeListSelectionListener(ListSelectionListener listener) {
132                 projectList.removeListSelectionListener(listener);
133         }
134
135         private void createActions() {
136                 projectLocalPathBrowseAction = new AbstractAction(I18n.getMessage("jsite.project.action.browse")) {
137
138                         public void actionPerformed(ActionEvent actionEvent) {
139                                 actionLocalPathBrowse();
140                         }
141                 };
142                 projectLocalPathBrowseAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.browse.tooltip"));
143                 projectLocalPathBrowseAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_B);
144                 projectLocalPathBrowseAction.setEnabled(false);
145
146                 projectAddAction = new AbstractAction(I18n.getMessage("jsite.project.action.add-project")) {
147
148                         public void actionPerformed(ActionEvent actionEvent) {
149                                 actionAdd();
150                         }
151                 };
152                 projectAddAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.add-project.tooltip"));
153                 projectAddAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_A);
154
155                 projectDeleteAction = new AbstractAction(I18n.getMessage("jsite.project.action.delete-project")) {
156
157                         public void actionPerformed(ActionEvent actionEvent) {
158                                 actionDelete();
159                         }
160                 };
161                 projectDeleteAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.delete-project.tooltip"));
162                 projectDeleteAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_D);
163                 projectDeleteAction.setEnabled(false);
164
165                 projectCloneAction = new AbstractAction(I18n.getMessage("jsite.project.action.clone-project")) {
166
167                         public void actionPerformed(ActionEvent actionEvent) {
168                                 actionClone();
169                         }
170                 };
171                 projectCloneAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.clone-project.tooltip"));
172                 projectCloneAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_L);
173                 projectCloneAction.setEnabled(false);
174                 
175                 projectCopyURIAction = new AbstractAction(I18n.getMessage("jsite.project.action.copy-uri")) {
176                         public void actionPerformed(ActionEvent actionEvent) {
177                                 actionCopyURI();
178                         }
179                 };
180                 projectCopyURIAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.copy-uri.tooltip"));
181                 projectCopyURIAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_U);
182                 projectCopyURIAction.setEnabled(false);
183                 
184                 projectGenerateKeyAction = new AbstractAction(I18n.getMessage("jsite.project.action.generate-new-key")) {
185                         public void actionPerformed(ActionEvent actionEvent) {
186                                 actionGenerateNewKey();
187                         }
188                 };
189                 projectGenerateKeyAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.generate-new-key.tooltip"));
190                 projectGenerateKeyAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_G);
191                 projectGenerateKeyAction.setEnabled(false);
192         }
193
194         private JComponent createInformationPanel() {
195                 JPanel informationPanel = new JPanel(new BorderLayout(12, 12));
196
197                 JPanel informationTable = new JPanel(new GridBagLayout());
198
199                 JPanel functionButtons = new JPanel(new FlowLayout(FlowLayout.LEADING, 12, 12));
200                 functionButtons.setBorder(new EmptyBorder(-12, -12, -12, -12));
201                 functionButtons.add(new JButton(projectAddAction));
202                 functionButtons.add(new JButton(projectDeleteAction));
203                 functionButtons.add(new JButton(projectCloneAction));
204                 functionButtons.add(new JButton(projectCopyURIAction));
205
206                 informationPanel.add(functionButtons, BorderLayout.PAGE_START);
207                 informationPanel.add(informationTable, BorderLayout.CENTER);
208
209                 informationTable.add(new JLabel("<html><b>" + I18n.getMessage("jsite.project.project.information") + "</b></html>"), new GridBagConstraints(0, 0, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
210
211                 projectNameTextField = new JTextField();
212                 projectNameTextField.getDocument().putProperty("name", "project.name");
213                 projectNameTextField.getDocument().addDocumentListener(this);
214                 projectNameTextField.setEnabled(false);
215
216                 informationTable.add(new TLabel(I18n.getMessage("jsite.project.project.name") + ":", KeyEvent.VK_N, projectNameTextField), new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
217                 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));
218
219                 projectDescriptionTextField = new JTextField();
220                 projectDescriptionTextField.getDocument().putProperty("name", "project.description");
221                 projectDescriptionTextField.getDocument().addDocumentListener(this);
222                 projectDescriptionTextField.setEnabled(false);
223
224                 informationTable.add(new TLabel(I18n.getMessage("jsite.project.project.description") + ":", KeyEvent.VK_D, projectDescriptionTextField), new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
225                 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));
226
227                 projectLocalPathTextField = new JTextField();
228                 projectLocalPathTextField.getDocument().putProperty("name", "project.localpath");
229                 projectLocalPathTextField.getDocument().addDocumentListener(this);
230                 projectLocalPathTextField.setEnabled(false);
231
232                 informationTable.add(new TLabel(I18n.getMessage("jsite.project.project.local-path") + ":", KeyEvent.VK_L, projectLocalPathTextField), new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
233                 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));
234                 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));
235
236                 informationTable.add(new JLabel("<html><b>" + I18n.getMessage("jsite.project.project.address") + "</b></html>"), new GridBagConstraints(0, 4, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(12, 0, 0, 0), 0, 0));
237
238                 projectPublicKeyTextField = new JTextField(27);
239                 projectPublicKeyTextField.getDocument().putProperty("name", "project.publickey");
240                 projectPublicKeyTextField.getDocument().addDocumentListener(this);
241                 projectPublicKeyTextField.setEnabled(false);
242
243                 informationTable.add(new TLabel(I18n.getMessage("jsite.project.project.public-key") + ":", KeyEvent.VK_U, projectPublicKeyTextField), new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
244                 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));
245                 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));
246
247                 projectPrivateKeyTextField = new JTextField(27);
248                 projectPrivateKeyTextField.getDocument().putProperty("name", "project.privatekey");
249                 projectPrivateKeyTextField.getDocument().addDocumentListener(this);
250                 projectPrivateKeyTextField.setEnabled(false);
251
252                 informationTable.add(new TLabel(I18n.getMessage("jsite.project.project.private-key") + ":", KeyEvent.VK_R, projectPrivateKeyTextField), new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
253                 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));
254
255                 projectPathTextField = new JTextField();
256                 projectPathTextField.getDocument().putProperty("name", "project.path");
257                 projectPathTextField.getDocument().addDocumentListener(this);
258                 projectPathTextField.setEnabled(false);
259
260                 informationTable.add(new TLabel(I18n.getMessage("jsite.project.project.path") + ":", KeyEvent.VK_P, projectPathTextField), new GridBagConstraints(0, 7, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
261                 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));
262
263                 return informationPanel;
264         }
265
266         public void setProjects(Project[] projects) {
267                 projectListModel.clear();
268                 for (Project project: projects) {
269                         projectListModel.add(project);
270                 }
271         }
272
273         public Project[] getProjects() {
274                 return (Project[]) projectListModel.toArray(new Project[projectListModel.size()]);
275         }
276
277         /**
278          * @param freenetInterface
279          *            The freenetInterface to set.
280          */
281         public void setFreenetInterface(Freenet7Interface freenetInterface) {
282                 this.freenetInterface = freenetInterface;
283         }
284
285         public Project getSelectedProject() {
286                 return (Project) projectList.getSelectedValue();
287         }
288
289         private void setTextField(DocumentEvent documentEvent) {
290                 Document document = documentEvent.getDocument();
291                 String propertyName = (String) document.getProperty("name");
292                 Project project = (Project) projectList.getSelectedValue();
293                 if (project == null) {
294                         return;
295                 }
296                 try {
297                         String text = document.getText(0, document.getLength()).trim();
298                         if ("project.name".equals(propertyName)) {
299                                 project.setName(text);
300                                 projectList.repaint();
301                         } else if ("project.description".equals(propertyName)) {
302                                 project.setDescription(text);
303                         } else if ("project.localpath".equals(propertyName)) {
304                                 project.setLocalPath(text);
305                         } else if ("project.privatekey".equals(propertyName)) {
306                                 project.setInsertURI(text);
307                         } else if ("project.publickey".equals(propertyName)) {
308                                 project.setRequestURI(text);
309                         } else if ("project.path".equals(propertyName)) {
310                                 project.setPath(text);
311                         }
312                 } catch (BadLocationException e) {
313                 }
314         }
315
316         //
317         // ACTIONS
318         //
319
320         protected void actionLocalPathBrowse() {
321                 Project project = (Project) projectList.getSelectedValue();
322                 if (project == null) {
323                         return;
324                 }
325                 pathChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
326                 if (pathChooser.showDialog(this, I18n.getMessage("jsite.project.action.browse.choose")) == JFileChooser.APPROVE_OPTION) {
327                         projectLocalPathTextField.setText(pathChooser.getSelectedFile().getPath());
328                 }
329         }
330
331         protected void actionAdd() {
332                 String[] keyPair = null;
333                 if (!freenetInterface.hasNode()) {
334                         JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
335                         return;
336                 }
337                 try {
338                         keyPair = freenetInterface.generateKeyPair();
339                 } catch (IOException ioe1) {
340                         JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.project.keygen.io-error"), ioe1.getMessage()), null, JOptionPane.ERROR_MESSAGE);
341                         return;
342                 }
343                 Project newProject = new Project();
344                 newProject.setName(I18n.getMessage("jsite.project.new-project.name"));
345                 newProject.setInsertURI(keyPair[0]);
346                 newProject.setRequestURI(keyPair[1]);
347                 newProject.setEdition(1);
348                 projectListModel.add(newProject);
349                 projectList.setSelectedIndex(projectListModel.size() - 1);
350         }
351
352         protected void actionDelete() {
353                 int selectedIndex = projectList.getSelectedIndex();
354                 if (selectedIndex > -1) {
355                         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) {
356                                 projectListModel.remove(selectedIndex);
357                                 projectList.clearSelection();
358                                 if (projectListModel.getSize() != 0) {
359                                         projectList.setSelectedIndex(Math.min(selectedIndex, projectListModel.getSize() - 1));
360                                 }
361                         }
362                 }
363         }
364
365         protected void actionClone() {
366                 int selectedIndex = projectList.getSelectedIndex();
367                 if (selectedIndex > -1) {
368                         Project newProject = new Project((Project) projectList.getSelectedValue());
369                         newProject.setName(MessageFormat.format(I18n.getMessage("jsite.project.action.clone-project.copy"), newProject.getName()));
370                         projectListModel.add(newProject);
371                         projectList.setSelectedIndex(projectListModel.indexOf(newProject));
372                 }
373         }
374         
375         protected void actionCopyURI() {
376                 int selectedIndex = projectList.getSelectedIndex();
377                 if (selectedIndex > -1) {
378                         Project selectedProject = (Project) projectList.getSelectedValue();
379                         Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
380                         clipboard.setContents(new StringSelection(selectedProject.getFinalRequestURI(0)), this);
381                 }
382         }
383         
384         protected void actionGenerateNewKey() {
385                 if (JOptionPane.showConfirmDialog(this, I18n.getMessage("jsite.project.warning.generate-new-key"), null, JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) {
386                         return;
387                 }
388                 int selectedIndex = projectList.getSelectedIndex();
389                 if (selectedIndex > -1) {
390                         Project selectedProject = (Project) projectList.getSelectedValue();
391                         String[] keyPair = null;
392                         try {
393                                 keyPair = freenetInterface.generateKeyPair();
394                         } catch (IOException ioe1) {
395                                 JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.project.keygen.io-error"), ioe1.getMessage()), null, JOptionPane.ERROR_MESSAGE);
396                                 return;
397                         }
398                         selectedProject.setInsertURI(keyPair[0]);
399                         selectedProject.setRequestURI(keyPair[1]);
400                         projectPublicKeyTextField.setText(selectedProject.getRequestURI());
401                         projectPrivateKeyTextField.setText(selectedProject.getInsertURI());
402                 }
403         }
404
405         //
406         // INTERFACE ListSelectionListener
407         //
408
409         /**
410          * {@inheritDoc}
411          */
412         public void valueChanged(ListSelectionEvent listSelectionEvent) {
413                 int selectedRow = projectList.getSelectedIndex();
414                 Project selectedProject = (Project) projectList.getSelectedValue();
415                 projectNameTextField.setEnabled(selectedRow > -1);
416                 projectDescriptionTextField.setEnabled(selectedRow > -1);
417                 projectLocalPathTextField.setEnabled(selectedRow > -1);
418                 projectPublicKeyTextField.setEnabled(selectedRow > -1);
419                 projectPrivateKeyTextField.setEnabled(selectedRow > -1);
420                 projectPathTextField.setEnabled(selectedRow > -1);
421                 projectLocalPathBrowseAction.setEnabled(selectedRow > -1);
422                 projectDeleteAction.setEnabled(selectedRow > -1);
423                 projectCloneAction.setEnabled(selectedRow > -1);
424                 projectCopyURIAction.setEnabled(selectedRow > -1);
425                 projectGenerateKeyAction.setEnabled(selectedRow > -1);
426                 if (selectedRow > -1) {
427                         projectNameTextField.setText(selectedProject.getName());
428                         projectDescriptionTextField.setText(selectedProject.getDescription());
429                         projectLocalPathTextField.setText(selectedProject.getLocalPath());
430                         projectPublicKeyTextField.setText(selectedProject.getRequestURI());
431                         projectPrivateKeyTextField.setText(selectedProject.getInsertURI());
432                         projectPathTextField.setText(selectedProject.getPath());
433                 } else {
434                         projectNameTextField.setText("");
435                         projectDescriptionTextField.setText("");
436                         projectLocalPathTextField.setText("");
437                         projectPublicKeyTextField.setText("");
438                         projectPrivateKeyTextField.setText("");
439                         projectPathTextField.setText("");
440                 }
441         }
442
443         //
444         // INTERFACE ChangeListener
445         //
446
447         //
448         // INTERFACE DocumentListener
449         //
450
451         /**
452          * {@inheritDoc}
453          */
454         public void insertUpdate(DocumentEvent documentEvent) {
455                 setTextField(documentEvent);
456         }
457
458         /**
459          * {@inheritDoc}
460          */
461         public void removeUpdate(DocumentEvent documentEvent) {
462                 setTextField(documentEvent);
463         }
464
465         /**
466          * {@inheritDoc}
467          */
468         public void changedUpdate(DocumentEvent documentEvent) {
469                 setTextField(documentEvent);
470         }
471         
472         //
473         // INTERFACE ClipboardOwner
474         //
475
476         /**
477          * {@inheritDoc}
478          */
479         public void lostOwnership(Clipboard clipboard, Transferable contents) {
480         }
481
482 }