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>
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;
90 public ProjectPage() {
92 setLayout(new BorderLayout(12, 12));
94 setHeading(I18n.getMessage("jsite.project.heading"));
95 setDescription(I18n.getMessage("jsite.project.description"));
98 protected void dialogInit() {
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));
108 add(new JScrollPane(projectList), BorderLayout.LINE_START);
109 add(createInformationPanel(), BorderLayout.CENTER);
116 public void pageAdded(TWizard wizard) {
117 super.pageAdded(wizard);
118 projectList.clearSelection();
119 wizard.setNextEnabled(false);
124 public void addListSelectionListener(ListSelectionListener listener) {
125 projectList.addListSelectionListener(listener);
130 public void removeListSelectionListener(ListSelectionListener listener) {
131 projectList.removeListSelectionListener(listener);
134 private void createActions() {
135 projectLocalPathBrowseAction = new AbstractAction(I18n.getMessage("jsite.project.action.browse")) {
137 public void actionPerformed(ActionEvent actionEvent) {
138 actionLocalPathBrowse();
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);
145 projectAddAction = new AbstractAction(I18n.getMessage("jsite.project.action.add-project")) {
147 public void actionPerformed(ActionEvent actionEvent) {
151 projectAddAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.add-project.tooltip"));
152 projectAddAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_A);
154 projectDeleteAction = new AbstractAction(I18n.getMessage("jsite.project.action.delete-project")) {
156 public void actionPerformed(ActionEvent actionEvent) {
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);
164 projectCloneAction = new AbstractAction(I18n.getMessage("jsite.project.action.clone-project")) {
166 public void actionPerformed(ActionEvent actionEvent) {
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);
175 private JComponent createInformationPanel() {
176 JPanel informationPanel = new JPanel(new BorderLayout(12, 12));
178 JPanel informationTable = new JPanel(new GridBagLayout());
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));
186 informationPanel.add(functionButtons, BorderLayout.PAGE_START);
187 informationPanel.add(informationTable, BorderLayout.CENTER);
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));
191 projectNameTextField = new JTextField();
192 projectNameTextField.getDocument().putProperty("name", "project.name");
193 projectNameTextField.getDocument().addDocumentListener(this);
194 projectNameTextField.setEnabled(false);
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));
199 projectDescriptionTextField = new JTextField();
200 projectDescriptionTextField.getDocument().putProperty("name", "project.description");
201 projectDescriptionTextField.getDocument().addDocumentListener(this);
202 projectDescriptionTextField.setEnabled(false);
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));
207 projectLocalPathTextField = new JTextField();
208 projectLocalPathTextField.getDocument().putProperty("name", "project.localpath");
209 projectLocalPathTextField.getDocument().addDocumentListener(this);
210 projectLocalPathTextField.setEnabled(false);
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));
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));
218 projectPublicKeyTextField = new JTextField(27);
219 projectPublicKeyTextField.getDocument().putProperty("name", "project.publickey");
220 projectPublicKeyTextField.getDocument().addDocumentListener(this);
221 projectPublicKeyTextField.setEnabled(false);
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));
226 projectPrivateKeyTextField = new JTextField(27);
227 projectPrivateKeyTextField.getDocument().putProperty("name", "project.privatekey");
228 projectPrivateKeyTextField.getDocument().addDocumentListener(this);
229 projectPrivateKeyTextField.setEnabled(false);
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));
234 projectPathTextField = new JTextField();
235 projectPathTextField.getDocument().putProperty("name", "project.path");
236 projectPathTextField.getDocument().addDocumentListener(this);
237 projectPathTextField.setEnabled(false);
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));
242 return informationPanel;
245 public void setProjects(Project[] projects) {
246 projectListModel.clear();
247 for (Project project: projects) {
248 projectListModel.add(project);
252 public Project[] getProjects() {
253 return (Project[]) projectListModel.toArray(new Project[projectListModel.size()]);
257 * @param freenetInterface
258 * The freenetInterface to set.
260 public void setFreenetInterface(Freenet7Interface freenetInterface) {
261 this.freenetInterface = freenetInterface;
264 public Project getSelectedProject() {
265 return (Project) projectList.getSelectedValue();
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) {
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);
291 } catch (BadLocationException e) {
299 protected void actionLocalPathBrowse() {
300 Project project = (Project) projectList.getSelectedValue();
301 if (project == null) {
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());
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);
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);
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);
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));
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));
359 // INTERFACE ListSelectionListener
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;
388 projectNameTextField.setText("");
389 projectDescriptionTextField.setText("");
390 projectLocalPathTextField.setText("");
391 projectPublicKeyTextField.setText("");
392 projectPrivateKeyTextField.setText("");
393 projectPathTextField.setText("");
398 // INTERFACE ChangeListener
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) {
412 if ("project.edition".equals(spinner.getName())) {
413 ((EditionProject) currentProject).setEdition((Integer) spinner.getValue());
419 // INTERFACE DocumentListener
425 public void insertUpdate(DocumentEvent documentEvent) {
426 setTextField(documentEvent);
432 public void removeUpdate(DocumentEvent documentEvent) {
433 setTextField(documentEvent);
439 public void changedUpdate(DocumentEvent documentEvent) {
440 setTextField(documentEvent);