version 0.4.8
[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
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                 projectCopyURIAction = new AbstractAction(I18n.getMessage("jsite.project.action.copy-uri")) {
175                         public void actionPerformed(ActionEvent actionEvent) {
176                                 actionCopyURI();
177                         }
178                 };
179                 projectCopyURIAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.copy-uri.tooltip"));
180                 projectCopyURIAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_U);
181                 projectCopyURIAction.setEnabled(false);
182         }
183
184         private JComponent createInformationPanel() {
185                 JPanel informationPanel = new JPanel(new BorderLayout(12, 12));
186
187                 JPanel informationTable = new JPanel(new GridBagLayout());
188
189                 JPanel functionButtons = new JPanel(new FlowLayout(FlowLayout.LEADING, 12, 12));
190                 functionButtons.setBorder(new EmptyBorder(-12, -12, -12, -12));
191                 functionButtons.add(new JButton(projectAddAction));
192                 functionButtons.add(new JButton(projectDeleteAction));
193                 functionButtons.add(new JButton(projectCloneAction));
194                 functionButtons.add(new JButton(projectCopyURIAction));
195
196                 informationPanel.add(functionButtons, BorderLayout.PAGE_START);
197                 informationPanel.add(informationTable, BorderLayout.CENTER);
198
199                 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));
200
201                 projectNameTextField = new JTextField();
202                 projectNameTextField.getDocument().putProperty("name", "project.name");
203                 projectNameTextField.getDocument().addDocumentListener(this);
204                 projectNameTextField.setEnabled(false);
205
206                 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));
207                 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));
208
209                 projectDescriptionTextField = new JTextField();
210                 projectDescriptionTextField.getDocument().putProperty("name", "project.description");
211                 projectDescriptionTextField.getDocument().addDocumentListener(this);
212                 projectDescriptionTextField.setEnabled(false);
213
214                 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));
215                 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));
216
217                 projectLocalPathTextField = new JTextField();
218                 projectLocalPathTextField.getDocument().putProperty("name", "project.localpath");
219                 projectLocalPathTextField.getDocument().addDocumentListener(this);
220                 projectLocalPathTextField.setEnabled(false);
221
222                 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));
223                 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));
224                 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));
225
226                 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));
227
228                 projectPublicKeyTextField = new JTextField(27);
229                 projectPublicKeyTextField.getDocument().putProperty("name", "project.publickey");
230                 projectPublicKeyTextField.getDocument().addDocumentListener(this);
231                 projectPublicKeyTextField.setEnabled(false);
232
233                 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));
234                 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));
235
236                 projectPrivateKeyTextField = new JTextField(27);
237                 projectPrivateKeyTextField.getDocument().putProperty("name", "project.privatekey");
238                 projectPrivateKeyTextField.getDocument().addDocumentListener(this);
239                 projectPrivateKeyTextField.setEnabled(false);
240
241                 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));
242                 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));
243
244                 projectPathTextField = new JTextField();
245                 projectPathTextField.getDocument().putProperty("name", "project.path");
246                 projectPathTextField.getDocument().addDocumentListener(this);
247                 projectPathTextField.setEnabled(false);
248
249                 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));
250                 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));
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                 }
314                 pathChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
315                 if (pathChooser.showDialog(this, I18n.getMessage("jsite.project.action.browse.choose")) == JFileChooser.APPROVE_OPTION) {
316                         projectLocalPathTextField.setText(pathChooser.getSelectedFile().getPath());
317                 }
318         }
319
320         protected void actionAdd() {
321                 String[] keyPair = null;
322                 if (!freenetInterface.hasNode()) {
323                         JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
324                         return;
325                 }
326                 try {
327                         keyPair = freenetInterface.generateKeyPair();
328                 } catch (IOException ioe1) {
329                         JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.project.keygen.io-error"), ioe1.getMessage()), null, JOptionPane.ERROR_MESSAGE);
330                         return;
331                 }
332                 Project newProject = new Project();
333                 newProject.setName(I18n.getMessage("jsite.project.new-project.name"));
334                 newProject.setInsertURI(keyPair[0]);
335                 newProject.setRequestURI(keyPair[1]);
336                 newProject.setEdition(1);
337                 projectListModel.add(newProject);
338                 projectList.setSelectedIndex(projectListModel.size() - 1);
339         }
340
341         protected void actionDelete() {
342                 int selectedIndex = projectList.getSelectedIndex();
343                 if (selectedIndex > -1) {
344                         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) {
345                                 projectListModel.remove(selectedIndex);
346                                 projectList.clearSelection();
347                                 if (projectListModel.getSize() != 0) {
348                                         projectList.setSelectedIndex(Math.min(selectedIndex, projectListModel.getSize() - 1));
349                                 }
350                         }
351                 }
352         }
353
354         protected void actionClone() {
355                 int selectedIndex = projectList.getSelectedIndex();
356                 if (selectedIndex > -1) {
357                         Project newProject = new Project((Project) projectList.getSelectedValue());
358                         newProject.setName(MessageFormat.format(I18n.getMessage("jsite.project.action.clone-project.copy"), newProject.getName()));
359                         projectListModel.add(newProject);
360                         projectList.setSelectedIndex(projectListModel.indexOf(newProject));
361                 }
362         }
363         
364         protected void actionCopyURI() {
365                 int selectedIndex = projectList.getSelectedIndex();
366                 if (selectedIndex > -1) {
367                         Project selectedProject = (Project) projectList.getSelectedValue();
368                         Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
369                         clipboard.setContents(new StringSelection(selectedProject.getFinalRequestURI(0)), this);
370                 }
371         }
372
373         //
374         // INTERFACE ListSelectionListener
375         //
376
377         /**
378          * {@inheritDoc}
379          */
380         public void valueChanged(ListSelectionEvent listSelectionEvent) {
381                 int selectedRow = projectList.getSelectedIndex();
382                 Project selectedProject = (Project) projectList.getSelectedValue();
383                 projectNameTextField.setEnabled(selectedRow > -1);
384                 projectDescriptionTextField.setEnabled(selectedRow > -1);
385                 projectLocalPathTextField.setEnabled(selectedRow > -1);
386                 projectPublicKeyTextField.setEnabled(selectedRow > -1);
387                 projectPrivateKeyTextField.setEnabled(selectedRow > -1);
388                 projectPathTextField.setEnabled(selectedRow > -1);
389                 projectLocalPathBrowseAction.setEnabled(selectedRow > -1);
390                 projectDeleteAction.setEnabled(selectedRow > -1);
391                 projectCloneAction.setEnabled(selectedRow > -1);
392                 projectCopyURIAction.setEnabled(selectedRow > -1);
393                 if (selectedRow > -1) {
394                         projectNameTextField.setText(selectedProject.getName());
395                         projectDescriptionTextField.setText(selectedProject.getDescription());
396                         projectLocalPathTextField.setText(selectedProject.getLocalPath());
397                         projectPublicKeyTextField.setText(selectedProject.getRequestURI());
398                         projectPrivateKeyTextField.setText(selectedProject.getInsertURI());
399                         projectPathTextField.setText(selectedProject.getPath());
400                 } else {
401                         projectNameTextField.setText("");
402                         projectDescriptionTextField.setText("");
403                         projectLocalPathTextField.setText("");
404                         projectPublicKeyTextField.setText("");
405                         projectPrivateKeyTextField.setText("");
406                         projectPathTextField.setText("");
407                 }
408         }
409
410         //
411         // INTERFACE ChangeListener
412         //
413
414         //
415         // INTERFACE DocumentListener
416         //
417
418         /**
419          * {@inheritDoc}
420          */
421         public void insertUpdate(DocumentEvent documentEvent) {
422                 setTextField(documentEvent);
423         }
424
425         /**
426          * {@inheritDoc}
427          */
428         public void removeUpdate(DocumentEvent documentEvent) {
429                 setTextField(documentEvent);
430         }
431
432         /**
433          * {@inheritDoc}
434          */
435         public void changedUpdate(DocumentEvent documentEvent) {
436                 setTextField(documentEvent);
437         }
438         
439         //
440         // INTERFACE ClipboardOwner
441         //
442
443         /**
444          * {@inheritDoc}
445          */
446         public void lostOwnership(Clipboard clipboard, Transferable contents) {
447         }
448
449 }