0ff7b3c217b88b9c25ac1556e10feb9b3778bf2f
[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$
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
90         public ProjectPage() {
91                 super();
92                 setLayout(new BorderLayout(12, 12));
93                 dialogInit();
94                 setHeading(I18n.getMessage("jsite.project.heading"));
95                 setDescription(I18n.getMessage("jsite.project.description"));
96         }
97
98         protected void dialogInit() {
99                 createActions();
100
101                 pathChooser = new JFileChooser();
102                 projectListModel = new SortedListModel();
103                 projectList = new JList(projectListModel);
104                 projectList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
105                 projectList.addListSelectionListener(this);
106                 projectList.setPreferredSize(new Dimension(150, projectList.getPreferredSize().height));
107
108                 add(new JScrollPane(projectList), BorderLayout.LINE_START);
109                 add(createInformationPanel(), BorderLayout.CENTER);
110         }
111
112         /**
113          * {@inheritDoc}
114          */
115         @Override
116         public void pageAdded(TWizard wizard) {
117                 super.pageAdded(wizard);
118                 projectList.clearSelection();
119                 wizard.setNextEnabled(false);
120         }
121
122         /**
123          */
124         public void addListSelectionListener(ListSelectionListener listener) {
125                 projectList.addListSelectionListener(listener);
126         }
127
128         /**
129          */
130         public void removeListSelectionListener(ListSelectionListener listener) {
131                 projectList.removeListSelectionListener(listener);
132         }
133
134         private void createActions() {
135                 projectLocalPathBrowseAction = new AbstractAction(I18n.getMessage("jsite.project.action.browse")) {
136
137                         public void actionPerformed(ActionEvent actionEvent) {
138                                 actionLocalPathBrowse();
139                         }
140                 };
141                 projectLocalPathBrowseAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.browse.tooltip"));
142                 projectLocalPathBrowseAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_B);
143                 projectLocalPathBrowseAction.setEnabled(false);
144
145                 projectAddAction = new AbstractAction(I18n.getMessage("jsite.project.action.add-project")) {
146
147                         public void actionPerformed(ActionEvent actionEvent) {
148                                 actionAdd();
149                         }
150                 };
151                 projectAddAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.add-project.tooltip"));
152                 projectAddAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_A);
153
154                 projectDeleteAction = new AbstractAction(I18n.getMessage("jsite.project.action.delete-project")) {
155
156                         public void actionPerformed(ActionEvent actionEvent) {
157                                 actionDelete();
158                         }
159                 };
160                 projectDeleteAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.delete-project.tooltip"));
161                 projectDeleteAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_D);
162                 projectDeleteAction.setEnabled(false);
163
164                 projectCloneAction = new AbstractAction(I18n.getMessage("jsite.project.action.clone-project")) {
165
166                         public void actionPerformed(ActionEvent actionEvent) {
167                                 actionClone();
168                         }
169                 };
170                 projectCloneAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.clone-project.tooltip"));
171                 projectCloneAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_L);
172                 projectCloneAction.setEnabled(false);
173         }
174
175         private JComponent createInformationPanel() {
176                 JPanel informationPanel = new JPanel(new BorderLayout(12, 12));
177
178                 JPanel informationTable = new JPanel(new GridBagLayout());
179
180                 JPanel functionButtons = new JPanel(new FlowLayout(FlowLayout.LEADING, 12, 12));
181                 functionButtons.setBorder(new EmptyBorder(-12, -12, -12, -12));
182                 functionButtons.add(new JButton(projectAddAction));
183                 functionButtons.add(new JButton(projectDeleteAction));
184                 functionButtons.add(new JButton(projectCloneAction));
185
186                 informationPanel.add(functionButtons, BorderLayout.PAGE_START);
187                 informationPanel.add(informationTable, BorderLayout.CENTER);
188
189                 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));
190
191                 projectNameTextField = new JTextField();
192                 projectNameTextField.getDocument().putProperty("name", "project.name");
193                 projectNameTextField.getDocument().addDocumentListener(this);
194                 projectNameTextField.setEnabled(false);
195
196                 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));
197                 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));
198
199                 projectDescriptionTextField = new JTextField();
200                 projectDescriptionTextField.getDocument().putProperty("name", "project.description");
201                 projectDescriptionTextField.getDocument().addDocumentListener(this);
202                 projectDescriptionTextField.setEnabled(false);
203
204                 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));
205                 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));
206
207                 projectLocalPathTextField = new JTextField();
208                 projectLocalPathTextField.getDocument().putProperty("name", "project.localpath");
209                 projectLocalPathTextField.getDocument().addDocumentListener(this);
210                 projectLocalPathTextField.setEnabled(false);
211
212                 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));
213                 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));
214                 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));
215
216                 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));
217
218                 projectPublicKeyTextField = new JTextField(27);
219                 projectPublicKeyTextField.getDocument().putProperty("name", "project.publickey");
220                 projectPublicKeyTextField.getDocument().addDocumentListener(this);
221                 projectPublicKeyTextField.setEnabled(false);
222
223                 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));
224                 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));
225
226                 projectPrivateKeyTextField = new JTextField(27);
227                 projectPrivateKeyTextField.getDocument().putProperty("name", "project.privatekey");
228                 projectPrivateKeyTextField.getDocument().addDocumentListener(this);
229                 projectPrivateKeyTextField.setEnabled(false);
230
231                 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));
232                 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));
233
234                 projectPathTextField = new JTextField();
235                 projectPathTextField.getDocument().putProperty("name", "project.path");
236                 projectPathTextField.getDocument().addDocumentListener(this);
237                 projectPathTextField.setEnabled(false);
238
239                 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));
240                 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));
241
242                 return informationPanel;
243         }
244
245         public void setProjects(Project[] projects) {
246                 projectListModel.clear();
247                 for (Project project: projects) {
248                         projectListModel.add(project);
249                 }
250         }
251
252         public Project[] getProjects() {
253                 return (Project[]) projectListModel.toArray(new Project[projectListModel.size()]);
254         }
255
256         /**
257          * @param freenetInterface
258          *            The freenetInterface to set.
259          */
260         public void setFreenetInterface(Freenet7Interface freenetInterface) {
261                 this.freenetInterface = freenetInterface;
262         }
263
264         public Project getSelectedProject() {
265                 return (Project) projectList.getSelectedValue();
266         }
267
268         private void setTextField(DocumentEvent documentEvent) {
269                 Document document = documentEvent.getDocument();
270                 String propertyName = (String) document.getProperty("name");
271                 Project project = (Project) projectList.getSelectedValue();
272                 if (project == null) {
273                         return;
274                 }
275                 try {
276                         String text = document.getText(0, document.getLength()).trim();
277                         if ("project.name".equals(propertyName)) {
278                                 project.setName(text);
279                                 projectList.repaint();
280                         } else if ("project.description".equals(propertyName)) {
281                                 project.setDescription(text);
282                         } else if ("project.localpath".equals(propertyName)) {
283                                 project.setLocalPath(text);
284                         } else if ("project.privatekey".equals(propertyName)) {
285                                 project.setInsertURI(text);
286                         } else if ("project.publickey".equals(propertyName)) {
287                                 project.setRequestURI(text);
288                         } else if ("project.path".equals(propertyName)) {
289                                 project.setPath(text);
290                         }
291                 } catch (BadLocationException e) {
292                 }
293         }
294
295         //
296         // ACTIONS
297         //
298
299         protected void actionLocalPathBrowse() {
300                 Project project = (Project) projectList.getSelectedValue();
301                 if (project == null) {
302                         return;
303                 }
304                 pathChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
305                 if (pathChooser.showDialog(this, I18n.getMessage("jsite.project.action.browse.choose")) == JFileChooser.APPROVE_OPTION) {
306                         projectLocalPathTextField.setText(pathChooser.getSelectedFile().getPath());
307                 }
308         }
309
310         protected void actionAdd() {
311                 String[] keyPair = null;
312                 if (!freenetInterface.hasNode()) {
313                         JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
314                         return;
315                 }
316                 try {
317                         keyPair = freenetInterface.generateKeyPair();
318                 } catch (IOException ioe1) {
319                         JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.project.keygen.io-error"), ioe1.getMessage()), null, JOptionPane.ERROR_MESSAGE);
320                         return;
321                 }
322                 EditionProject newProject = new EditionProject();
323                 newProject.setName(I18n.getMessage("jsite.project.new-project.name"));
324                 newProject.setInsertURI(keyPair[0]);
325                 newProject.setRequestURI(keyPair[1]);
326                 newProject.setEdition(1);
327                 projectListModel.add(newProject);
328                 projectList.setSelectedIndex(projectListModel.size() - 1);
329         }
330
331         protected void actionDelete() {
332                 int selectedIndex = projectList.getSelectedIndex();
333                 if (selectedIndex > -1) {
334                         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) {
335                                 projectListModel.remove(selectedIndex);
336                                 projectList.clearSelection();
337                                 if (projectListModel.getSize() != 0) {
338                                         projectList.setSelectedIndex(Math.min(selectedIndex, projectListModel.getSize() - 1));
339                                 }
340                         }
341                 }
342         }
343
344         protected void actionClone() {
345                 int selectedIndex = projectList.getSelectedIndex();
346                 if (selectedIndex > -1) {
347                         Project newProject = null;
348                         Project selectedProject = (Project) projectList.getSelectedValue();
349                         if (selectedProject instanceof EditionProject) {
350                                 newProject = new EditionProject(selectedProject);
351                         } // else { /* BUG! */ }
352                         newProject.setName(MessageFormat.format(I18n.getMessage("jsite.project.action.clone-project.copy"), newProject.getName()));
353                         projectListModel.add(newProject);
354                         projectList.setSelectedIndex(projectListModel.indexOf(newProject));
355                 }
356         }
357
358         //
359         // INTERFACE ListSelectionListener
360         //
361
362         /**
363          * {@inheritDoc}
364          */
365         public void valueChanged(ListSelectionEvent listSelectionEvent) {
366                 int selectedRow = projectList.getSelectedIndex();
367                 Project selectedProject = (Project) projectList.getSelectedValue();
368                 projectNameTextField.setEnabled(selectedRow > -1);
369                 projectDescriptionTextField.setEnabled(selectedRow > -1);
370                 projectLocalPathTextField.setEnabled(selectedRow > -1);
371                 projectPublicKeyTextField.setEnabled(selectedRow > -1);
372                 projectPrivateKeyTextField.setEnabled(selectedRow > -1);
373                 projectPathTextField.setEnabled(selectedRow > -1);
374                 projectLocalPathBrowseAction.setEnabled(selectedRow > -1);
375                 projectDeleteAction.setEnabled(selectedRow > -1);
376                 projectCloneAction.setEnabled(selectedRow > -1);
377                 if (selectedRow > -1) {
378                         projectNameTextField.setText(selectedProject.getName());
379                         projectDescriptionTextField.setText(selectedProject.getDescription());
380                         projectLocalPathTextField.setText(selectedProject.getLocalPath());
381                         projectPublicKeyTextField.setText(selectedProject.getRequestURI());
382                         projectPrivateKeyTextField.setText(selectedProject.getInsertURI());
383                         projectPathTextField.setText(selectedProject.getPath());
384                         if (selectedProject instanceof EditionProject) {
385                                 EditionProject editionProject = (EditionProject) selectedProject;
386                         }
387                 } else {
388                         projectNameTextField.setText("");
389                         projectDescriptionTextField.setText("");
390                         projectLocalPathTextField.setText("");
391                         projectPublicKeyTextField.setText("");
392                         projectPrivateKeyTextField.setText("");
393                         projectPathTextField.setText("");
394                 }
395         }
396
397         //
398         // INTERFACE ChangeListener
399         //
400
401         /**
402          * {@inheritDoc}
403          */
404         public void stateChanged(ChangeEvent changeEvent) {
405                 Object source = changeEvent.getSource();
406                 if (source instanceof JSpinner) {
407                         JSpinner spinner = (JSpinner) source;
408                         Project currentProject = (Project) projectList.getSelectedValue();
409                         if (currentProject == null) {
410                                 return;
411                         }
412                         if ("project.edition".equals(spinner.getName())) {
413                                 ((EditionProject) currentProject).setEdition((Integer) spinner.getValue());
414                         }
415                 }
416         }
417
418         //
419         // INTERFACE DocumentListener
420         //
421
422         /**
423          * {@inheritDoc}
424          */
425         public void insertUpdate(DocumentEvent documentEvent) {
426                 setTextField(documentEvent);
427         }
428
429         /**
430          * {@inheritDoc}
431          */
432         public void removeUpdate(DocumentEvent documentEvent) {
433                 setTextField(documentEvent);
434         }
435
436         /**
437          * {@inheritDoc}
438          */
439         public void changedUpdate(DocumentEvent documentEvent) {
440                 setTextField(documentEvent);
441         }
442
443 }