jSite: First commit : verion 4.0 (written by Bombe)
[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.event.ActionEvent;
29 import java.awt.event.KeyEvent;
30 import java.io.IOException;
31 import java.text.MessageFormat;
32
33 import javax.swing.AbstractAction;
34 import javax.swing.Action;
35 import javax.swing.JButton;
36 import javax.swing.JComponent;
37 import javax.swing.JFileChooser;
38 import javax.swing.JLabel;
39 import javax.swing.JList;
40 import javax.swing.JOptionPane;
41 import javax.swing.JPanel;
42 import javax.swing.JScrollPane;
43 import javax.swing.JSpinner;
44 import javax.swing.JTextField;
45 import javax.swing.ListSelectionModel;
46 import javax.swing.SpinnerNumberModel;
47 import javax.swing.JSpinner.NumberEditor;
48 import javax.swing.border.EmptyBorder;
49 import javax.swing.event.ChangeEvent;
50 import javax.swing.event.ChangeListener;
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.EditionProject;
59 import de.todesbaum.jsite.application.Freenet7Interface;
60 import de.todesbaum.jsite.application.Project;
61 import de.todesbaum.jsite.i18n.I18n;
62 import de.todesbaum.util.swing.SortedListModel;
63 import de.todesbaum.util.swing.TLabel;
64 import de.todesbaum.util.swing.TWizard;
65 import de.todesbaum.util.swing.TWizardPage;
66
67 /**
68  * @author David Roden <droden@gmail.com>
69  * @version $Id: ProjectPage.java 418 2006-03-29 17:49:16Z bombe $
70  */
71 public class ProjectPage extends TWizardPage implements ListSelectionListener, ChangeListener, DocumentListener {
72
73         private Freenet7Interface freenetInterface;
74
75         private Action projectLocalPathBrowseAction;
76         private Action projectAddAction;
77         private Action projectDeleteAction;
78         private Action projectCloneAction;
79
80         private JFileChooser pathChooser;
81         private SortedListModel projectListModel;
82         private JList projectList;
83         private JTextField projectNameTextField;
84         private JTextField projectDescriptionTextField;
85         private JTextField projectLocalPathTextField;
86         private JTextField projectPublicKeyTextField;
87         private JTextField projectPrivateKeyTextField;
88         private JTextField projectPathTextField;
89         private JSpinner projectEditionSpinner;
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
176         private JComponent createInformationPanel() {
177                 JPanel informationPanel = new JPanel(new BorderLayout(12, 12));
178
179                 JPanel informationTable = new JPanel(new GridBagLayout());
180
181                 JPanel functionButtons = new JPanel(new FlowLayout(FlowLayout.LEADING, 12, 12));
182                 functionButtons.setBorder(new EmptyBorder(-12, -12, -12, -12));
183                 functionButtons.add(new JButton(projectAddAction));
184                 functionButtons.add(new JButton(projectDeleteAction));
185                 functionButtons.add(new JButton(projectCloneAction));
186
187                 informationPanel.add(functionButtons, BorderLayout.PAGE_START);
188                 informationPanel.add(informationTable, BorderLayout.CENTER);
189
190                 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));
191
192                 projectNameTextField = new JTextField();
193                 projectNameTextField.getDocument().putProperty("name", "project.name");
194                 projectNameTextField.getDocument().addDocumentListener(this);
195                 projectNameTextField.setEnabled(false);
196
197                 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));
198                 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));
199
200                 projectDescriptionTextField = new JTextField();
201                 projectDescriptionTextField.getDocument().putProperty("name", "project.description");
202                 projectDescriptionTextField.getDocument().addDocumentListener(this);
203                 projectDescriptionTextField.setEnabled(false);
204
205                 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));
206                 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));
207
208                 projectLocalPathTextField = new JTextField();
209                 projectLocalPathTextField.getDocument().putProperty("name", "project.localpath");
210                 projectLocalPathTextField.getDocument().addDocumentListener(this);
211                 projectLocalPathTextField.setEnabled(false);
212
213                 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));
214                 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));
215                 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));
216
217                 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));
218
219                 projectPublicKeyTextField = new JTextField(27);
220                 projectPublicKeyTextField.getDocument().putProperty("name", "project.publickey");
221                 projectPublicKeyTextField.getDocument().addDocumentListener(this);
222                 projectPublicKeyTextField.setEnabled(false);
223
224                 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));
225                 informationTable.add(projectPublicKeyTextField, new GridBagConstraints(1, 5, 2, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
226
227                 projectPrivateKeyTextField = new JTextField(27);
228                 projectPrivateKeyTextField.getDocument().putProperty("name", "project.privatekey");
229                 projectPrivateKeyTextField.getDocument().addDocumentListener(this);
230                 projectPrivateKeyTextField.setEnabled(false);
231
232                 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));
233                 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));
234
235                 projectPathTextField = new JTextField();
236                 projectPathTextField.getDocument().putProperty("name", "project.path");
237                 projectPathTextField.getDocument().addDocumentListener(this);
238                 projectPathTextField.setEnabled(false);
239
240                 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));
241                 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));
242
243                 projectEditionSpinner = new JSpinner(new SpinnerNumberModel(1, 0, Integer.MAX_VALUE, 1));
244                 ((NumberEditor) projectEditionSpinner.getEditor()).getTextField().setColumns(6);
245                 projectEditionSpinner.setName("project.edition");
246                 projectEditionSpinner.addChangeListener(this);
247                 projectEditionSpinner.setEnabled(false);
248
249                 informationTable.add(new TLabel(I18n.getMessage("jsite.project.project.edition") + ":", KeyEvent.VK_E, projectEditionSpinner), new GridBagConstraints(0, 8, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
250                 informationTable.add(projectEditionSpinner, new GridBagConstraints(1, 8, 2, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 6, 0, 0), 0, 0));
251
252                 return informationPanel;
253         }
254
255         public void setProjects(Project[] projects) {
256                 projectListModel.clear();
257                 for (Project project: projects) {
258                         projectListModel.add(project);
259                 }
260         }
261
262         public Project[] getProjects() {
263                 return (Project[]) projectListModel.toArray(new Project[projectListModel.size()]);
264         }
265
266         /**
267          * @param freenetInterface
268          *            The freenetInterface to set.
269          */
270         public void setFreenetInterface(Freenet7Interface freenetInterface) {
271                 this.freenetInterface = freenetInterface;
272         }
273
274         public Project getSelectedProject() {
275                 return (Project) projectList.getSelectedValue();
276         }
277
278         private void setTextField(DocumentEvent documentEvent) {
279                 Document document = documentEvent.getDocument();
280                 String propertyName = (String) document.getProperty("name");
281                 Project project = (Project) projectList.getSelectedValue();
282                 if (project == null) {
283                         return;
284                 }
285                 try {
286                         String text = document.getText(0, document.getLength()).trim();
287                         if ("project.name".equals(propertyName)) {
288                                 project.setName(text);
289                                 projectList.repaint();
290                         } else if ("project.description".equals(propertyName)) {
291                                 project.setDescription(text);
292                         } else if ("project.localpath".equals(propertyName)) {
293                                 project.setLocalPath(text);
294                         } else if ("project.privatekey".equals(propertyName)) {
295                                 project.setInsertURI(text);
296                         } else if ("project.publickey".equals(propertyName)) {
297                                 project.setRequestURI(text);
298                         } else if ("project.path".equals(propertyName)) {
299                                 project.setPath(text);
300                         }
301                 } catch (BadLocationException e) {
302                 }
303         }
304
305         //
306         // ACTIONS
307         //
308
309         protected void actionLocalPathBrowse() {
310                 Project project = (Project) projectList.getSelectedValue();
311                 if (project == null)
312                         return;
313                 pathChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
314                 if (pathChooser.showDialog(this, I18n.getMessage("jsite.project.action.browse.choose")) == JFileChooser.APPROVE_OPTION) {
315                         projectLocalPathTextField.setText(pathChooser.getSelectedFile().getPath());
316                 }
317         }
318
319         protected void actionAdd() {
320                 String[] keyPair = null;
321                 if (!freenetInterface.hasNode()) {
322                         JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
323                         return;
324                 }
325                 try {
326                         keyPair = freenetInterface.generateKeyPair();
327                 } catch (IOException ioe1) {
328                         JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.project.keygen.io-error"), ioe1.getMessage()), null, JOptionPane.ERROR_MESSAGE);
329                         return;
330                 }
331                 EditionProject newProject = new EditionProject();
332                 newProject.setName(I18n.getMessage("jsite.project.new-project.name"));
333                 newProject.setInsertURI(keyPair[0]);
334                 newProject.setRequestURI(keyPair[1]);
335                 newProject.setEdition(1);
336                 projectListModel.add(newProject);
337                 projectList.setSelectedIndex(projectListModel.size() - 1);
338         }
339
340         protected void actionDelete() {
341                 int selectedIndex = projectList.getSelectedIndex();
342                 if (selectedIndex > -1) {
343                         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) {
344                                 projectListModel.remove(selectedIndex);
345                                 projectList.clearSelection();
346                                 if (projectListModel.getSize() != 0) {
347                                         projectList.setSelectedIndex(Math.min(selectedIndex, projectListModel.getSize() - 1));
348                                 }
349                         }
350                 }
351         }
352
353         protected void actionClone() {
354                 int selectedIndex = projectList.getSelectedIndex();
355                 if (selectedIndex > -1) {
356                         Project newProject = null;
357                         Project selectedProject = (Project) projectList.getSelectedValue();
358                         if (selectedProject instanceof EditionProject) {
359                                 newProject = new EditionProject(selectedProject);
360                         } // else { /* BUG! */ }
361                         newProject.setName(MessageFormat.format(I18n.getMessage("jsite.project.action.clone-project.copy"), newProject.getName()));
362                         projectListModel.add(newProject);
363                         projectList.setSelectedIndex(projectListModel.indexOf(newProject));
364                 }
365         }
366
367         //
368         // INTERFACE ListSelectionListener
369         //
370
371         /**
372          * {@inheritDoc}
373          */
374         public void valueChanged(ListSelectionEvent listSelectionEvent) {
375                 int selectedRow = projectList.getSelectedIndex();
376                 Project selectedProject = (Project) projectList.getSelectedValue();
377                 projectNameTextField.setEnabled(selectedRow > -1);
378                 projectDescriptionTextField.setEnabled(selectedRow > -1);
379                 projectLocalPathTextField.setEnabled(selectedRow > -1);
380                 projectPublicKeyTextField.setEnabled(selectedRow > -1);
381                 projectPrivateKeyTextField.setEnabled(selectedRow > -1);
382                 projectPathTextField.setEnabled(selectedRow > -1);
383                 projectEditionSpinner.setEnabled(selectedRow > -1);
384                 projectLocalPathBrowseAction.setEnabled(selectedRow > -1);
385                 projectDeleteAction.setEnabled(selectedRow > -1);
386                 projectCloneAction.setEnabled(selectedRow > -1);
387                 if (selectedRow > -1) {
388                         projectNameTextField.setText(selectedProject.getName());
389                         projectDescriptionTextField.setText(selectedProject.getDescription());
390                         projectLocalPathTextField.setText(selectedProject.getLocalPath());
391                         projectPublicKeyTextField.setText(selectedProject.getRequestURI());
392                         projectPrivateKeyTextField.setText(selectedProject.getInsertURI());
393                         projectPathTextField.setText(selectedProject.getPath());
394                         if (selectedProject instanceof EditionProject) {
395                                 EditionProject editionProject = (EditionProject) selectedProject;
396                                 projectEditionSpinner.setValue(editionProject.getEdition());
397                         }
398                 } else {
399                         projectNameTextField.setText("");
400                         projectDescriptionTextField.setText("");
401                         projectLocalPathTextField.setText("");
402                         projectPublicKeyTextField.setText("");
403                         projectPrivateKeyTextField.setText("");
404                         projectPathTextField.setText("");
405                         projectEditionSpinner.setValue(0);
406                 }
407         }
408
409         //
410         // INTERFACE ChangeListener
411         //
412
413         /**
414          * {@inheritDoc}
415          */
416         public void stateChanged(ChangeEvent changeEvent) {
417                 Object source = changeEvent.getSource();
418                 if (source instanceof JSpinner) {
419                         JSpinner spinner = (JSpinner) source;
420                         Project currentProject = (Project) projectList.getSelectedValue();
421                         if (currentProject == null) {
422                                 return;
423                         }
424                         if ("project.edition".equals(spinner.getName())) {
425                                 ((EditionProject) currentProject).setEdition((Integer) spinner.getValue());
426                         }
427                 }
428         }
429
430         //
431         // INTERFACE DocumentListener
432         //
433
434         /**
435          * {@inheritDoc}
436          */
437         public void insertUpdate(DocumentEvent documentEvent) {
438                 setTextField(documentEvent);
439         }
440
441         /**
442          * {@inheritDoc}
443          */
444         public void removeUpdate(DocumentEvent documentEvent) {
445                 setTextField(documentEvent);
446         }
447
448         /**
449          * {@inheritDoc}
450          */
451         public void changedUpdate(DocumentEvent documentEvent) {
452                 setTextField(documentEvent);
453         }
454
455 }