2 * jSite - a tool for uploading websites into Freenet
3 * Copyright (C) 2006 David Roden
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.
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.
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.
20 package de.todesbaum.jsite.gui;
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;
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;
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;
67 * @author David Roden <droden@gmail.com>
70 public class ProjectPage extends TWizardPage implements ListSelectionListener, DocumentListener, ClipboardOwner {
72 private Freenet7Interface freenetInterface;
74 private Action projectLocalPathBrowseAction;
75 private Action projectAddAction;
76 private Action projectDeleteAction;
77 private Action projectCloneAction;
78 private Action projectCopyURIAction;
79 private Action projectGenerateKeyAction;
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;
91 public ProjectPage() {
93 setLayout(new BorderLayout(12, 12));
95 setHeading(I18n.getMessage("jsite.project.heading"));
96 setDescription(I18n.getMessage("jsite.project.description"));
99 protected void dialogInit() {
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));
109 add(new JScrollPane(projectList), BorderLayout.LINE_START);
110 add(createInformationPanel(), BorderLayout.CENTER);
117 public void pageAdded(TWizard wizard) {
118 super.pageAdded(wizard);
119 projectList.clearSelection();
120 wizard.setNextEnabled(false);
125 public void addListSelectionListener(ListSelectionListener listener) {
126 projectList.addListSelectionListener(listener);
131 public void removeListSelectionListener(ListSelectionListener listener) {
132 projectList.removeListSelectionListener(listener);
135 private void createActions() {
136 projectLocalPathBrowseAction = new AbstractAction(I18n.getMessage("jsite.project.action.browse")) {
138 public void actionPerformed(ActionEvent actionEvent) {
139 actionLocalPathBrowse();
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);
146 projectAddAction = new AbstractAction(I18n.getMessage("jsite.project.action.add-project")) {
148 public void actionPerformed(ActionEvent actionEvent) {
152 projectAddAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.add-project.tooltip"));
153 projectAddAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_A);
155 projectDeleteAction = new AbstractAction(I18n.getMessage("jsite.project.action.delete-project")) {
157 public void actionPerformed(ActionEvent actionEvent) {
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);
165 projectCloneAction = new AbstractAction(I18n.getMessage("jsite.project.action.clone-project")) {
167 public void actionPerformed(ActionEvent actionEvent) {
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);
175 projectCopyURIAction = new AbstractAction(I18n.getMessage("jsite.project.action.copy-uri")) {
176 public void actionPerformed(ActionEvent actionEvent) {
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);
184 projectGenerateKeyAction = new AbstractAction(I18n.getMessage("jsite.project.action.generate-new-key")) {
185 public void actionPerformed(ActionEvent actionEvent) {
186 actionGenerateNewKey();
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);
194 private JComponent createInformationPanel() {
195 JPanel informationPanel = new JPanel(new BorderLayout(12, 12));
197 JPanel informationTable = new JPanel(new GridBagLayout());
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));
206 informationPanel.add(functionButtons, BorderLayout.PAGE_START);
207 informationPanel.add(informationTable, BorderLayout.CENTER);
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));
211 projectNameTextField = new JTextField();
212 projectNameTextField.getDocument().putProperty("name", "project.name");
213 projectNameTextField.getDocument().addDocumentListener(this);
214 projectNameTextField.setEnabled(false);
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));
219 projectDescriptionTextField = new JTextField();
220 projectDescriptionTextField.getDocument().putProperty("name", "project.description");
221 projectDescriptionTextField.getDocument().addDocumentListener(this);
222 projectDescriptionTextField.setEnabled(false);
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));
227 projectLocalPathTextField = new JTextField();
228 projectLocalPathTextField.getDocument().putProperty("name", "project.localpath");
229 projectLocalPathTextField.getDocument().addDocumentListener(this);
230 projectLocalPathTextField.setEnabled(false);
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));
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));
238 projectPublicKeyTextField = new JTextField(27);
239 projectPublicKeyTextField.getDocument().putProperty("name", "project.publickey");
240 projectPublicKeyTextField.getDocument().addDocumentListener(this);
241 projectPublicKeyTextField.setEnabled(false);
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));
247 projectPrivateKeyTextField = new JTextField(27);
248 projectPrivateKeyTextField.getDocument().putProperty("name", "project.privatekey");
249 projectPrivateKeyTextField.getDocument().addDocumentListener(this);
250 projectPrivateKeyTextField.setEnabled(false);
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));
255 projectPathTextField = new JTextField();
256 projectPathTextField.getDocument().putProperty("name", "project.path");
257 projectPathTextField.getDocument().addDocumentListener(this);
258 projectPathTextField.setEnabled(false);
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));
263 return informationPanel;
266 public void setProjects(Project[] projects) {
267 projectListModel.clear();
268 for (Project project: projects) {
269 projectListModel.add(project);
273 public Project[] getProjects() {
274 return (Project[]) projectListModel.toArray(new Project[projectListModel.size()]);
278 * @param freenetInterface
279 * The freenetInterface to set.
281 public void setFreenetInterface(Freenet7Interface freenetInterface) {
282 this.freenetInterface = freenetInterface;
285 public Project getSelectedProject() {
286 return (Project) projectList.getSelectedValue();
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) {
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);
312 } catch (BadLocationException e) {
320 protected void actionLocalPathBrowse() {
321 Project project = (Project) projectList.getSelectedValue();
322 if (project == null) {
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());
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);
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);
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);
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));
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));
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);
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) {
388 int selectedIndex = projectList.getSelectedIndex();
389 if (selectedIndex > -1) {
390 Project selectedProject = (Project) projectList.getSelectedValue();
391 String[] keyPair = null;
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);
398 selectedProject.setInsertURI(keyPair[0]);
399 selectedProject.setRequestURI(keyPair[1]);
400 projectPublicKeyTextField.setText(selectedProject.getRequestURI());
401 projectPrivateKeyTextField.setText(selectedProject.getInsertURI());
406 // INTERFACE ListSelectionListener
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());
434 projectNameTextField.setText("");
435 projectDescriptionTextField.setText("");
436 projectLocalPathTextField.setText("");
437 projectPublicKeyTextField.setText("");
438 projectPrivateKeyTextField.setText("");
439 projectPathTextField.setText("");
444 // INTERFACE ChangeListener
448 // INTERFACE DocumentListener
454 public void insertUpdate(DocumentEvent documentEvent) {
455 setTextField(documentEvent);
461 public void removeUpdate(DocumentEvent documentEvent) {
462 setTextField(documentEvent);
468 public void changedUpdate(DocumentEvent documentEvent) {
469 setTextField(documentEvent);
473 // INTERFACE ClipboardOwner
479 public void lostOwnership(Clipboard clipboard, Transferable contents) {