1 package ch.odi.justblog.gui.swing.control;
2
3 import java.net.MalformedURLException;
4 import java.net.URL;
5
6 import javax.swing.JOptionPane;
7
8 import ch.odi.justblog.api.Blog;
9 import ch.odi.justblog.api.Entry;
10 import ch.odi.justblog.command.ClearEntry;
11 import ch.odi.justblog.command.CommandException;
12 import ch.odi.justblog.command.ISession;
13 import ch.odi.justblog.command.PostEntry;
14 import ch.odi.justblog.command.SetUrl;
15 import ch.odi.justblog.gui.swing.ErrorMessage;
16 import ch.odi.justblog.gui.swing.MainWindow;
17 import ch.odi.justblog.gui.swing.SwingUi;
18
19
20 /***
21 * Controller for the main window.
22 *
23 * @author oglueck
24 */
25 public class Controller implements UrlBarController, ButtonBarController {
26 private MainWindow view;
27 private SwingUi gui;
28
29 /***
30 *
31 */
32 public Controller(SwingUi gui) {
33 this.gui = gui;
34 }
35
36 public void setView(MainWindow view) {
37 this.view = view;
38 }
39
40
41
42
43 public void newUrl(String urlstring) {
44 try {
45 ISession session = view.getSession();
46 URL url = new URL(urlstring);
47 SetUrl command = new SetUrl(url);
48 command.execute(session);
49 Blog blog = session.getBlog();
50 view.canEditEntry((blog != null));
51 } catch (MalformedURLException e) {
52 new ErrorMessage(view, e);
53 } catch (CommandException e) {
54
55 JOptionPane.showMessageDialog(view, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
56 }
57
58 }
59
60 public void postEntry() {
61 ISession session = view.getSession();
62 Entry entry = session.getEntry();
63 entry.setText(view.getEntryText());
64 PostEntry command = new PostEntry();
65 try {
66 command.execute(session);
67 } catch (CommandException e) {
68
69 JOptionPane.showMessageDialog(view, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
70 }
71 }
72
73 public void clearEntry() {
74 ISession session = view.getSession();
75 ClearEntry command = new ClearEntry();
76 try {
77 command.execute(session);
78 view.clearEntry();
79 } catch (CommandException e) {
80
81 JOptionPane.showMessageDialog(view, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
82 }
83 }
84
85 }