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.event.ActionEvent;
29 import java.awt.event.KeyEvent;
30 import java.io.IOException;
31 import java.text.MessageFormat;
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;
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;
68 * @author David Roden <droden@gmail.com>
69 * @version $Id: ProjectPage.java 418 2006-03-29 17:49:16Z bombe $
71 public class ProjectPage extends TWizardPage implements ListSelectionListener, ChangeListener, DocumentListener {
73 private Freenet7Interface freenetInterface;
75 private Action projectLocalPathBrowseAction;
76 private Action projectAddAction;
77 private Action projectDeleteAction;
78 private Action projectCloneAction;
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;
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);
176 private JComponent createInformationPanel() {
177 JPanel informationPanel = new JPanel(new BorderLayout(12, 12));
179 JPanel informationTable = new JPanel(new GridBagLayout());
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));
187 informationPanel.add(functionButtons, BorderLayout.PAGE_START);
188 informationPanel.add(informationTable, BorderLayout.CENTER);
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));
192 projectNameTextField = new JTextField();
193 projectNameTextField.getDocument().putProperty("name", "project.name");
194 projectNameTextField.getDocument().addDocumentListener(this);
195 projectNameTextField.setEnabled(false);
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));
200 projectDescriptionTextField = new JTextField();
201 projectDescriptionTextField.getDocument().putProperty("name", "project.description");
202 projectDescriptionTextField.getDocument().addDocumentListener(this);
203 projectDescriptionTextField.setEnabled(false);
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));
208 projectLocalPathTextField = new JTextField();
209 projectLocalPathTextField.getDocument().putProperty("name", "project.localpath");
210 projectLocalPathTextField.getDocument().addDocumentListener(this);
211 projectLocalPathTextField.setEnabled(false);
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));
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));
219 projectPublicKeyTextField = new JTextField(27);
220 projectPublicKeyTextField.getDocument().putProperty("name", "project.publickey");
221 projectPublicKeyTextField.getDocument().addDocumentListener(this);
222 projectPublicKeyTextField.setEnabled(false);
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));
227 projectPrivateKeyTextField = new JTextField(27);
228 projectPrivateKeyTextField.getDocument().putProperty("name", "project.privatekey");
229 projectPrivateKeyTextField.getDocument().addDocumentListener(this);
230 projectPrivateKeyTextField.setEnabled(false);
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));
235 projectPathTextField = new JTextField();
236 projectPathTextField.getDocument().putProperty("name", "project.path");
237 projectPathTextField.getDocument().addDocumentListener(this);
238 projectPathTextField.setEnabled(false);
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));
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);
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));
252 return informationPanel;
255 public void setProjects(Project[] projects) {
256 projectListModel.clear();
257 for (Project project: projects) {
258 projectListModel.add(project);
262 public Project[] getProjects() {
263 return (Project[]) projectListModel.toArray(new Project[projectListModel.size()]);
267 * @param freenetInterface
268 * The freenetInterface to set.
270 public void setFreenetInterface(Freenet7Interface freenetInterface) {
271 this.freenetInterface = freenetInterface;
274 public Project getSelectedProject() {
275 return (Project) projectList.getSelectedValue();
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) {
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);
301 } catch (BadLocationException e) {
309 protected void actionLocalPathBrowse() {
310 Project project = (Project) projectList.getSelectedValue();
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());
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);
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);
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);
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));
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));
368 // INTERFACE ListSelectionListener
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());
399 projectNameTextField.setText("");
400 projectDescriptionTextField.setText("");
401 projectLocalPathTextField.setText("");
402 projectPublicKeyTextField.setText("");
403 projectPrivateKeyTextField.setText("");
404 projectPathTextField.setText("");
405 projectEditionSpinner.setValue(0);
410 // INTERFACE ChangeListener
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) {
424 if ("project.edition".equals(spinner.getName())) {
425 ((EditionProject) currentProject).setEdition((Integer) spinner.getValue());
431 // INTERFACE DocumentListener
437 public void insertUpdate(DocumentEvent documentEvent) {
438 setTextField(documentEvent);
444 public void removeUpdate(DocumentEvent documentEvent) {
445 setTextField(documentEvent);
451 public void changedUpdate(DocumentEvent documentEvent) {
452 setTextField(documentEvent);