|
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
|
2 |
*
|
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
|
4 |
* of the Valencian Government (CIT)
|
|
5 |
*
|
|
6 |
* This program is free software; you can redistribute it and/or
|
|
7 |
* modify it under the terms of the GNU General Public License
|
|
8 |
* as published by the Free Software Foundation; either version 2
|
|
9 |
* of the License, or (at your option) any later version.
|
|
10 |
*
|
|
11 |
* This program is distributed in the hope that it will be useful,
|
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 |
* GNU General Public License for more details.
|
|
15 |
*
|
|
16 |
* You should have received a copy of the GNU General Public License
|
|
17 |
* along with this program; if not, write to the Free Software
|
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
19 |
* MA 02110-1301, USA.
|
|
20 |
*
|
|
21 |
*/
|
|
22 |
|
|
23 |
package org.gvsig.fortunecookies.main;
|
|
24 |
|
|
25 |
import java.awt.BorderLayout;
|
|
26 |
import java.awt.Dimension;
|
|
27 |
import java.awt.Toolkit;
|
|
28 |
import java.awt.event.ActionEvent;
|
|
29 |
import java.awt.event.ActionListener;
|
|
30 |
import java.awt.event.ComponentEvent;
|
|
31 |
import java.awt.event.ComponentListener;
|
|
32 |
|
|
33 |
import javax.swing.BorderFactory;
|
|
34 |
import javax.swing.Box;
|
|
35 |
import javax.swing.BoxLayout;
|
|
36 |
import javax.swing.JButton;
|
|
37 |
import javax.swing.JFrame;
|
|
38 |
import javax.swing.JMenu;
|
|
39 |
import javax.swing.JMenuBar;
|
|
40 |
import javax.swing.JMenuItem;
|
|
41 |
import javax.swing.JPanel;
|
|
42 |
import javax.swing.JScrollPane;
|
|
43 |
import javax.swing.JTextArea;
|
|
44 |
import javax.swing.JToolBar;
|
|
45 |
import javax.swing.WindowConstants;
|
|
46 |
|
|
47 |
import org.gvsig.fortunecookies.FortuneCookieLocator;
|
|
48 |
import org.gvsig.fortunecookies.FortuneCookieManager;
|
|
49 |
import org.gvsig.fortunecookies.FortuneCookieMessageException;
|
|
50 |
import org.gvsig.fortunecookies.FortuneCookieService;
|
|
51 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
|
|
52 |
import org.gvsig.tools.service.ServiceException;
|
|
53 |
|
|
54 |
/**
|
|
55 |
* Main executable class for testing the fortune cookies library.
|
|
56 |
*
|
|
57 |
* @author gvSIG Team
|
|
58 |
* @version $Id$
|
|
59 |
*/
|
|
60 |
public class Main implements ComponentListener {
|
|
61 |
|
|
62 |
FortuneCookieManager manager;
|
|
63 |
|
|
64 |
public static final int MODE_DIALOG = 1;
|
|
65 |
public static final int MODE_WINDOW = 2;
|
|
66 |
public static final int MODE_TOOL = 3;
|
|
67 |
|
|
68 |
public static void main(String args[]) {
|
|
69 |
Main main = new Main();
|
|
70 |
main.show();
|
|
71 |
}
|
|
72 |
|
|
73 |
public void show() {
|
|
74 |
|
|
75 |
org.apache.log4j.BasicConfigurator.configure();
|
|
76 |
|
|
77 |
new DefaultLibrariesInitializer().fullInitialize();
|
|
78 |
manager = FortuneCookieLocator.getManager();
|
|
79 |
|
|
80 |
JFrame frame = new JFrame("Fortune Cookie example app");
|
|
81 |
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
|
82 |
|
|
83 |
// Create the menu bar.
|
|
84 |
JMenuBar menuBar = new JMenuBar();
|
|
85 |
|
|
86 |
// Build the menu.
|
|
87 |
JMenu menu_file = new JMenu("File");
|
|
88 |
JMenuItem menuItem_get = new JMenuItem("Get a fortune cookie");
|
|
89 |
menuItem_get.addActionListener(new ActionListener() {
|
|
90 |
|
|
91 |
public void actionPerformed(ActionEvent e) {
|
|
92 |
showFortuneCookie(manager);
|
|
93 |
}
|
|
94 |
});
|
|
95 |
menu_file.add(menuItem_get);
|
|
96 |
|
|
97 |
JMenuItem menuItem_exit = new JMenuItem("Exit");
|
|
98 |
menuItem_exit.addActionListener(new ActionListener() {
|
|
99 |
|
|
100 |
public void actionPerformed(ActionEvent e) {
|
|
101 |
System.exit(0);
|
|
102 |
}
|
|
103 |
});
|
|
104 |
menu_file.add(menuItem_exit);
|
|
105 |
|
|
106 |
menuBar.add(menu_file);
|
|
107 |
|
|
108 |
JToolBar toolBar = new JToolBar();
|
|
109 |
|
|
110 |
JButton get = new JButton("Get a fortune cookie");
|
|
111 |
get.addActionListener(new ActionListener() {
|
|
112 |
|
|
113 |
public void actionPerformed(ActionEvent arg0) {
|
|
114 |
showFortuneCookie(manager);
|
|
115 |
}
|
|
116 |
|
|
117 |
});
|
|
118 |
toolBar.add(get);
|
|
119 |
|
|
120 |
frame.setPreferredSize(new Dimension(160, 100));
|
|
121 |
frame.add(menuBar, BorderLayout.NORTH);
|
|
122 |
frame.add(toolBar, BorderLayout.LINE_END);
|
|
123 |
|
|
124 |
// Display the window.
|
|
125 |
frame.pack();
|
|
126 |
frame.setVisible(true);
|
|
127 |
|
|
128 |
}
|
|
129 |
|
|
130 |
public void showFortuneCookie(FortuneCookieManager manager) {
|
|
131 |
try {
|
|
132 |
FortuneCookieService service =
|
|
133 |
(FortuneCookieService) manager.getFortuneCookieService();
|
|
134 |
JPanel panel = createFortuneCookieServicePanel(service);
|
|
135 |
showWindow(panel, "Fortune Cookie", MODE_WINDOW);
|
|
136 |
|
|
137 |
} catch (ServiceException e) {
|
|
138 |
e.printStackTrace();
|
|
139 |
}
|
|
140 |
}
|
|
141 |
|
|
142 |
public JPanel createFortuneCookieServicePanel(
|
|
143 |
final FortuneCookieService cookie) {
|
|
144 |
final JPanel panel = new JPanel();
|
|
145 |
panel.setLayout(new BorderLayout());
|
|
146 |
panel.setPreferredSize(new Dimension(550, 150));
|
|
147 |
|
|
148 |
JTextArea text = new JTextArea();
|
|
149 |
try {
|
|
150 |
text.setText(cookie.getMessage());
|
|
151 |
} catch (FortuneCookieMessageException e) {
|
|
152 |
text.setText(this.getTranslation("Error obtaining message....."));
|
|
153 |
e.printStackTrace();
|
|
154 |
}
|
|
155 |
text.setEditable(false);
|
|
156 |
|
|
157 |
JScrollPane scrollPane = new JScrollPane(text);
|
|
158 |
scrollPane.setPreferredSize(new Dimension(550, 150));
|
|
159 |
|
|
160 |
JButton accept = new JButton(this.getTranslation("Accept"));
|
|
161 |
JButton info = new JButton(this.getTranslation("Info"));
|
|
162 |
|
|
163 |
JPanel optionsPane = new JPanel();
|
|
164 |
optionsPane.setLayout(new BoxLayout(optionsPane, BoxLayout.LINE_AXIS));
|
|
165 |
optionsPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
|
|
166 |
optionsPane.add(Box.createHorizontalGlue());
|
|
167 |
|
|
168 |
accept.addActionListener(new ActionListener() {
|
|
169 |
|
|
170 |
public void actionPerformed(ActionEvent arg0) {
|
|
171 |
panel.setVisible(false);
|
|
172 |
}
|
|
173 |
});
|
|
174 |
info.addActionListener(new ActionListener() {
|
|
175 |
|
|
176 |
public void actionPerformed(ActionEvent arg0) {
|
|
177 |
showInfo(cookie);
|
|
178 |
}
|
|
179 |
});
|
|
180 |
|
|
181 |
optionsPane.add(accept);
|
|
182 |
optionsPane.add(info);
|
|
183 |
optionsPane.add(Box.createRigidArea(new Dimension(10, 0)));
|
|
184 |
|
|
185 |
panel.add(scrollPane, BorderLayout.CENTER);
|
|
186 |
panel.add(optionsPane, BorderLayout.SOUTH);
|
|
187 |
|
|
188 |
return panel;
|
|
189 |
}
|
|
190 |
|
|
191 |
private void showInfo(FortuneCookieService cookie) {
|
|
192 |
JPanel infoPanel = createFortuneCookieServiceInfoPanel(cookie);
|
|
193 |
showWindow(infoPanel, this.getTranslation("Information"), MODE_DIALOG);
|
|
194 |
}
|
|
195 |
|
|
196 |
private JPanel createFortuneCookieServiceInfoPanel(
|
|
197 |
FortuneCookieService cookie) {
|
|
198 |
final JPanel panel = new JPanel();
|
|
199 |
|
|
200 |
panel.setLayout(new BorderLayout());
|
|
201 |
panel.setPreferredSize(new Dimension(550, 150));
|
|
202 |
|
|
203 |
JTextArea text = new JTextArea();
|
|
204 |
try {
|
|
205 |
text.setText("Message: " + cookie.getMessage() + "\nDate: "
|
|
206 |
+ cookie.getDate());
|
|
207 |
} catch (FortuneCookieMessageException e) {
|
|
208 |
text.setText(this.getTranslation("Error obtaining message....."));
|
|
209 |
e.printStackTrace();
|
|
210 |
}
|
|
211 |
text.setEditable(false);
|
|
212 |
|
|
213 |
JScrollPane scrollPane = new JScrollPane(text);
|
|
214 |
scrollPane.setPreferredSize(new Dimension(550, 150));
|
|
215 |
|
|
216 |
JButton accept = new JButton(this.getTranslation("Accept"));
|
|
217 |
|
|
218 |
JPanel optionsPane = new JPanel();
|
|
219 |
optionsPane.setLayout(new BoxLayout(optionsPane, BoxLayout.LINE_AXIS));
|
|
220 |
optionsPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
|
|
221 |
optionsPane.add(Box.createHorizontalGlue());
|
|
222 |
|
|
223 |
accept.addActionListener(new ActionListener() {
|
|
224 |
|
|
225 |
public void actionPerformed(ActionEvent arg0) {
|
|
226 |
panel.setVisible(false);
|
|
227 |
}
|
|
228 |
});
|
|
229 |
optionsPane.add(accept);
|
|
230 |
optionsPane.add(Box.createRigidArea(new Dimension(10, 0)));
|
|
231 |
|
|
232 |
panel.add(scrollPane, BorderLayout.CENTER);
|
|
233 |
panel.add(optionsPane, BorderLayout.SOUTH);
|
|
234 |
|
|
235 |
return panel;
|
|
236 |
}
|
|
237 |
|
|
238 |
public void showWindow(JPanel panel, String title, int mode) {
|
|
239 |
JFrame frame;
|
|
240 |
|
|
241 |
frame = new JFrame();
|
|
242 |
// frame.setAlwaysOnTop(true);
|
|
243 |
|
|
244 |
final Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
|
|
245 |
frame.setLocation(s.width / 4, s.height / 4);
|
|
246 |
|
|
247 |
frame.setLayout(new BorderLayout());
|
|
248 |
frame.setTitle(title);
|
|
249 |
frame.add(panel, BorderLayout.CENTER);
|
|
250 |
|
|
251 |
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
|
252 |
|
|
253 |
frame.setSize(400, 400);
|
|
254 |
panel.addComponentListener(this);
|
|
255 |
|
|
256 |
frame.pack();
|
|
257 |
frame.setVisible(true);
|
|
258 |
}
|
|
259 |
|
|
260 |
public void componentHidden(ComponentEvent arg0) {
|
|
261 |
JFrame frame =
|
|
262 |
(JFrame) ((JPanel) arg0.getSource()).getRootPane().getParent();
|
|
263 |
frame.setVisible(false);
|
|
264 |
frame.dispose();
|
|
265 |
}
|
|
266 |
|
|
267 |
public void componentMoved(ComponentEvent arg0) {
|
|
268 |
// Do nothing
|
|
269 |
|
|
270 |
}
|
|
271 |
|
|
272 |
public void componentResized(ComponentEvent arg0) {
|
|
273 |
// Do nothing
|
|
274 |
|
|
275 |
}
|
|
276 |
|
|
277 |
public void componentShown(ComponentEvent arg0) {
|
|
278 |
// Do nothing
|
|
279 |
|
|
280 |
}
|
|
281 |
|
|
282 |
public String getTranslation(String string) {
|
|
283 |
|
|
284 |
return string;
|
|
285 |
}
|
|
286 |
}
|