1 package siouxsie.desktop.impl;
2
3 import java.awt.BorderLayout;
4 import java.awt.FlowLayout;
5 import java.awt.event.WindowAdapter;
6 import java.awt.event.WindowEvent;
7 import java.util.prefs.Preferences;
8
9 import javax.swing.JFrame;
10 import javax.swing.JOptionPane;
11 import javax.swing.JPanel;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16 import siouxsie.desktop.IApplication;
17 import siouxsie.desktop.IDesktopUIProvider;
18 import siouxsie.desktop.IStatusBar;
19 import siouxsie.desktop.IViewHandler;
20 import siouxsie.desktop.commands.ICommand;
21 import siouxsie.desktop.commands.impl.CommandsHandler;
22 import siouxsie.desktop.events.ApplicationSelectedEvent;
23 import siouxsie.desktop.events.DesktopEvent;
24 import siouxsie.desktop.events.DesktopListener;
25 import siouxsie.desktop.prefs.IPreferencesService;
26
27 /**
28 *
29 * @author Arnaud Cogoluegnes
30 * @version $Id$
31 */
32 public class DesktopUIProvider implements IDesktopUIProvider, DesktopListener {
33
34 private static final Log LOG = LogFactory.getLog(DesktopUIProvider.class);
35
36 private JFrame frame;
37
38 private JXTaskPaneListApplications applicationsMenu;
39
40 private CommandsHandler commandsHandler;
41
42 private JPanel toolBarPanel;
43
44 private IPreferencesService preferencesService;
45
46 private IViewHandler viewHandler;
47
48 private IStatusBar statusBar;
49
50 public IStatusBar getStatusBar() {
51 return statusBar;
52 }
53
54
55
56 public void setStatusBar(IStatusBar statusBar) {
57 this.statusBar = statusBar;
58 }
59
60
61
62 public IViewHandler getViewHandler() {
63 return viewHandler;
64 }
65
66
67
68 public void setViewHandler(IViewHandler viewHandler) {
69 this.viewHandler = viewHandler;
70 }
71
72
73
74 public IPreferencesService getPreferencesService() {
75 return preferencesService;
76 }
77
78
79
80 public void setPreferencesService(IPreferencesService preferencesService) {
81 this.preferencesService = preferencesService;
82 }
83
84
85
86 public JXTaskPaneListApplications getApplicationsMenu() {
87 return applicationsMenu;
88 }
89
90
91
92 public void setApplicationsMenu(JXTaskPaneListApplications applicationsMenu) {
93 this.applicationsMenu = applicationsMenu;
94 }
95
96
97
98 public CommandsHandler getCommandsHandler() {
99 return commandsHandler;
100 }
101
102
103
104 public void setCommandsHandler(CommandsHandler commandsHandler) {
105 this.commandsHandler = commandsHandler;
106 }
107
108
109
110 public void setFrame(JFrame frame) {
111 this.frame = frame;
112 }
113
114
115
116
117
118
119
120 public JFrame getFrame() {
121 return frame;
122 }
123
124
125
126 public void handle(DesktopEvent event) {
127
128
129 if(event instanceof ApplicationSelectedEvent) {
130 ApplicationSelectedEvent evt = (ApplicationSelectedEvent) event;
131 IApplication app = evt.getApplication();
132
133 frame.setJMenuBar(commandsHandler.getAppMenuBar(app.getId()));
134 toolBarPanel.removeAll();
135 toolBarPanel.add(commandsHandler.getAppToolBar(app.getId()));
136
137 getViewHandler().displayApplicationView(app.getId());
138
139 frame.setTitle(app.getName());
140 if(app.getIcon() != null) {
141 frame.setIconImage(app.getIcon().getImage());
142 } else {
143 frame.setIconImage(null);
144 }
145
146
147
148 frame.validate();
149 }
150
151 }
152
153 public void init() {
154 frame.getContentPane().setLayout(new BorderLayout());
155
156 int appsCount = getApplicationsMenu().getApplicationRegistry().getApplications().size();
157 if(appsCount > 1) {
158 LOG.info(appsCount + " application(s) counted, using application menu");
159 frame.getContentPane().add(getApplicationsMenu().getComponent(),BorderLayout.WEST);
160 } else {
161 LOG.info(appsCount + " application counted, not using application menu");
162 }
163
164 frame.getContentPane().add(getViewHandler().getDefaultContainer(),BorderLayout.CENTER);
165
166 toolBarPanel = new JPanel();
167 toolBarPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
168 frame.getContentPane().add(toolBarPanel,BorderLayout.NORTH);
169 frame.getContentPane().add(getStatusBar().getComponent(),BorderLayout.SOUTH);
170
171 JOptionPane.setRootFrame(frame);
172
173
174 Preferences pref = getPreferencesService().getSiouxsieUserPreferences();
175 int width = pref.getInt("main.frame.width", 800);
176 int height = pref.getInt("main.frame.height", 600);
177 int x = pref.getInt("main.frame.x", 0);
178 int y = pref.getInt("main.frame.y", 0);
179
180 frame.setSize(width, height);
181 frame.setLocation(x, y);
182
183 frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
184
185 frame.addWindowListener(new WindowsClosingListener());
186
187
188 if(getApplicationsMenu().getApplicationRegistry().getApplications().size() == 1) {
189 IApplication loneApp = getApplicationsMenu().getApplicationRegistry().getApplications().iterator().next();
190 getApplicationsMenu().fireDesktopEvent(new ApplicationSelectedEvent(loneApp));
191 }
192
193 }
194
195
196 class WindowsClosingListener extends WindowAdapter {
197
198 @Override
199 public void windowClosing(WindowEvent e) {
200 Preferences pref = getPreferencesService().getSiouxsieUserPreferences();
201 pref.putInt("main.frame.width",frame.getWidth());
202 pref.putInt("main.frame.height",frame.getHeight());
203 pref.putInt("main.frame.x",frame.getLocation().x);
204 pref.putInt("main.frame.y",frame.getLocation().y);
205
206 if(commandsHandler.getCommand(ICommand.COMMAND_EXIT) == null) {
207
208 LOG.warn("no default exit action, exit from DesktopProvider");
209 System.exit(0);
210 } else {
211 commandsHandler.getCommand(ICommand.COMMAND_EXIT).execute();
212 }
213
214 }
215
216 }
217
218
219 }