Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.mkmvnproject / templates / SPI / org.gvsig.fortunecookies.app / org.gvsig.fortunecookies.app.extension / src / main / java / org / gvsig / fortunecookies / app / extension / FortuneCookieExtension.java @ 32378

History | View | Annotate | Download (7.95 KB)

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.app.extension;
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

    
46
import org.gvsig.andami.plugins.Extension;
47
import org.gvsig.fortunecookies.FortuneCookieLocator;
48
import org.gvsig.fortunecookies.FortuneCookieManager;
49
import org.gvsig.fortunecookies.FortuneCookieService;
50
import org.gvsig.fortunecookies.exception.FortuneCookieMessageException;
51
import org.gvsig.tools.dynobject.DynObject;
52
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
53
import org.gvsig.tools.service.ServiceException;
54

    
55
public class FortuneCookieExtension extends Extension implements ComponentListener{
56

    
57
        FortuneCookieManager manager;
58
        
59
        public static final int MODE_DIALOG = 1;
60
        public static final int MODE_WINDOW = 2;
61
        public static final int MODE_TOOL = 3;
62
        
63
        
64
        public void execute(String actionCommand) {
65

    
66
                org.apache.log4j.BasicConfigurator.configure();
67
                
68
                new DefaultLibrariesInitializer().fullInitialize();
69
                manager = FortuneCookieLocator.getManager();
70
                                                
71
                JFrame frame = new JFrame("Fortune Cookie");
72
                
73
                // Create the menu bar.
74
                JMenuBar menuBar = new JMenuBar();
75

    
76
                // Build the menu.
77
                JMenu menu_file = new JMenu("File");
78
                JMenuItem menuItem_get = new JMenuItem("Get a fortune cookie");
79
                menuItem_get.addActionListener(new ActionListener(){
80
                        public void actionPerformed(ActionEvent e) {
81
                                showFortuneCookie(manager);
82
                        }
83
            });
84
                menu_file.add(menuItem_get);
85

    
86
        
87
                JMenuItem menuItem_exit = new JMenuItem("Exit");
88
                menuItem_exit.addActionListener(new ActionListener(){
89
                        public void actionPerformed(ActionEvent e) {
90
                                System.exit(0);
91
                        }
92
            });
93
                menu_file.add(menuItem_exit);
94
                
95
                menuBar.add(menu_file);
96
                
97
                JToolBar toolBar = new JToolBar();
98
                
99
                JButton get = new JButton("Get a fortune cookie");
100
                get.addActionListener(new ActionListener(){
101

    
102
                        public void actionPerformed(ActionEvent arg0) {
103
                                showFortuneCookie(manager);                        
104
                        }
105
                        
106
                });
107
                toolBar.add(get);
108

    
109
                frame.setPreferredSize(new Dimension(160, 100));
110
                frame.add(menuBar, BorderLayout.NORTH);
111
                frame.add(toolBar, BorderLayout.LINE_END);
112
                
113
    //Display the window.
114
    frame.pack();
115
    frame.setVisible(true);
116

    
117
        }
118

    
119
        public void showFortuneCookie(FortuneCookieManager manager) {
120
                try {
121
                        DynObject parameters = manager.createServiceParameters("FortuneCookie.providers.web");
122
                        FortuneCookieService service = (FortuneCookieService) manager.getService(parameters);
123
                        JPanel panel = createFortuneCookieServicePanel(service);
124
                        showWindow(
125
                                        panel, 
126
                                        "Fortune Cookie", 
127
                                        MODE_WINDOW
128
                        );
129

    
130
                } catch (ServiceException e) {
131
                        e.printStackTrace();
132
                }
133
        }
134

    
135
        public JPanel createFortuneCookieServicePanel(final FortuneCookieService cookie) {
136
                final JPanel panel = new JPanel();
137
                panel.setLayout(new BorderLayout());
138
                panel.setPreferredSize(new Dimension(550,150));
139

    
140
                JTextArea text = new JTextArea();
141
                try {
142
                        text.setText(cookie.getMessage());
143
                } catch (FortuneCookieMessageException e) {
144
                        text.setText(this.getTranslation("Error obtaining message....."));
145
                        e.printStackTrace();
146
                }
147
                text.setEditable(false);
148
                
149
            JScrollPane scrollPane = new JScrollPane(text);
150
            scrollPane.setPreferredSize(new Dimension(550, 150));
151

    
152
            JButton accept = new JButton(this.getTranslation("Accept"));
153
            JButton info = new JButton(this.getTranslation("Info"));
154
                
155
                JPanel optionsPane = new JPanel();
156
                optionsPane.setLayout(new BoxLayout(optionsPane, BoxLayout.LINE_AXIS));
157
                optionsPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
158
                optionsPane.add(Box.createHorizontalGlue());
159
                
160
                accept.addActionListener(new ActionListener(){
161
                        public void actionPerformed(ActionEvent arg0) {
162
                                panel.setVisible(false);
163
                        }
164
                });
165
                info.addActionListener(new ActionListener(){
166
                        public void actionPerformed(ActionEvent arg0) {
167
                                showInfo(cookie);
168
                        }
169
                });
170
                
171
                optionsPane.add(accept);
172
                optionsPane.add(info);
173
                optionsPane.add(Box.createRigidArea(new Dimension(10, 0)));
174
                
175
                panel.add(scrollPane, BorderLayout.CENTER);
176
                panel.add(optionsPane, BorderLayout.SOUTH);
177
                
178
                return panel;
179
        }
180

    
181
        private void showInfo(FortuneCookieService cookie) {
182
                JPanel infoPanel = createFortuneCookieServiceInfoPanel(cookie);
183
                showWindow(
184
                                infoPanel, 
185
                                this.getTranslation("Information"), 
186
                                MODE_DIALOG
187
                );
188
        }
189

    
190
        private JPanel createFortuneCookieServiceInfoPanel(FortuneCookieService cookie) {
191
                final JPanel panel = new JPanel();
192
                
193
                panel.setLayout(new BorderLayout());
194
                panel.setPreferredSize(new Dimension(550,150));
195

    
196
                JTextArea text = new JTextArea();
197
                try {
198
                        text.setText("Message: " +cookie.getMessage()+"\nDate: " + cookie.getDate());
199
                } catch (FortuneCookieMessageException e) {
200
                        text.setText(this.getTranslation("Error obtaining message....."));
201
                        e.printStackTrace();
202
                }
203
                text.setEditable(false);
204
                
205
            JScrollPane scrollPane = new JScrollPane(text);
206
            scrollPane.setPreferredSize(new Dimension(550, 150));
207

    
208
            JButton accept = new JButton(this.getTranslation("Accept"));
209
                
210
                JPanel optionsPane = new JPanel();
211
                optionsPane.setLayout(new BoxLayout(optionsPane, BoxLayout.LINE_AXIS));
212
                optionsPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
213
                optionsPane.add(Box.createHorizontalGlue());
214
                
215
                accept.addActionListener(new ActionListener(){
216
                        public void actionPerformed(ActionEvent arg0) {
217
                                panel.setVisible(false);
218
                        }
219
                });
220
                optionsPane.add(accept);
221
                optionsPane.add(Box.createRigidArea(new Dimension(10, 0)));
222
                
223
                panel.add(scrollPane, BorderLayout.CENTER);
224
                panel.add(optionsPane, BorderLayout.SOUTH);
225
                
226
                return panel;
227
        }
228

    
229
        public void showWindow(JPanel panel, String title, int mode) {
230
                JFrame frame;
231
                
232
                frame = new JFrame();
233
                //frame.setAlwaysOnTop(true);
234
                
235
                final Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
236
                frame.setLocation(s.width / 4, s.height / 4);
237
                
238
                frame.setLayout(new BorderLayout());
239
                frame.setTitle(title);
240
                frame.add(panel, BorderLayout.CENTER);
241
                
242
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
243

    
244
                frame.setSize(400,400);
245
                panel.addComponentListener(this);
246
                
247
                frame.pack();
248
                frame.setVisible(true);
249
        }
250

    
251
        public void componentHidden(ComponentEvent arg0) {
252
                JFrame frame = (JFrame) ((JPanel) arg0.getSource()).getRootPane().getParent();
253
                frame.setVisible(false);
254
                frame.dispose();
255
        }
256

    
257
        public void componentMoved(ComponentEvent arg0) {
258
                // Do nothing
259
        }
260

    
261
        public void componentResized(ComponentEvent arg0) {
262
                // Do nothing
263
        }
264

    
265
        public void componentShown(ComponentEvent arg0) {
266
                // Do nothing
267

    
268
        }
269

    
270
        public String getTranslation(String string) {
271

    
272
                return string;
273
        }
274

    
275
        public void initialize() {
276
                // Do nothing
277

    
278
        }
279

    
280
        public boolean isEnabled() {
281
                return true;
282
        }
283

    
284
        public boolean isVisible() {
285
                return true;
286
        }
287

    
288
}