Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.mkmvnproject / templates / API / 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.74 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.library.impl.DefaultLibrariesInitializer;
52
import org.gvsig.tools.service.ServiceException;
53

    
54
public class FortuneCookieExtension extends Extension implements ComponentListener{
55

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

    
65
                org.apache.log4j.BasicConfigurator.configure();
66

    
67
                new DefaultLibrariesInitializer().fullInitialize();
68
                manager = FortuneCookieLocator.getManager();
69

    
70
                JFrame frame = new JFrame("Fortune Cookie");
71

    
72
                // Create the menu bar.
73
                JMenuBar menuBar = new JMenuBar();
74

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

    
85

    
86
                JMenuItem menuItem_exit = new JMenuItem("Exit");
87
                menuItem_exit.addActionListener(new ActionListener(){
88
                        public void actionPerformed(ActionEvent e) {
89
                                System.exit(0);
90
                        }
91
                });
92
                menu_file.add(menuItem_exit);
93

    
94
                menuBar.add(menu_file);
95

    
96
                JToolBar toolBar = new JToolBar();
97

    
98
                JButton get = new JButton("Get a fortune cookie");
99
                get.addActionListener(new ActionListener(){
100

    
101
                        public void actionPerformed(ActionEvent arg0) {
102
                                showFortuneCookie(manager);                        
103
                        }
104

    
105
                });
106
                toolBar.add(get);
107

    
108
                frame.setPreferredSize(new Dimension(160, 100));
109
                frame.add(menuBar, BorderLayout.NORTH);
110
                frame.add(toolBar, BorderLayout.LINE_END);
111

    
112
                //Display the window.
113
                frame.pack();
114
                frame.setVisible(true);
115

    
116
        }
117

    
118
        public void showFortuneCookie(FortuneCookieManager manager) {
119
                try {
120
                        FortuneCookieService service = (FortuneCookieService) manager.getFortuneCookieService();
121
                        JPanel panel = createFortuneCookieServicePanel(service);
122
                        showWindow(
123
                                        panel, 
124
                                        "Fortune Cookie", 
125
                                        MODE_WINDOW
126
                        );
127

    
128
                } catch (ServiceException e) {
129
                        e.printStackTrace();
130
                }
131
        }
132

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

    
138
                JTextArea text = new JTextArea();
139
                try {
140
                        text.setText(cookie.getMessage());
141
                } catch (FortuneCookieMessageException e) {
142
                        text.setText(this.getTranslation("Error obtaining message....."));
143
                        e.printStackTrace();
144
                }
145
                text.setEditable(false);
146

    
147
                JScrollPane scrollPane = new JScrollPane(text);
148
                scrollPane.setPreferredSize(new Dimension(550, 150));
149

    
150
                JButton accept = new JButton(this.getTranslation("Accept"));
151
                JButton info = new JButton(this.getTranslation("Info"));
152

    
153
                JPanel optionsPane = new JPanel();
154
                optionsPane.setLayout(new BoxLayout(optionsPane, BoxLayout.LINE_AXIS));
155
                optionsPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
156
                optionsPane.add(Box.createHorizontalGlue());
157

    
158
                accept.addActionListener(new ActionListener(){
159
                        public void actionPerformed(ActionEvent arg0) {
160
                                panel.setVisible(false);
161
                        }
162
                });
163
                info.addActionListener(new ActionListener(){
164
                        public void actionPerformed(ActionEvent arg0) {
165
                                showInfo(cookie);
166
                        }
167
                });
168

    
169
                optionsPane.add(accept);
170
                optionsPane.add(info);
171
                optionsPane.add(Box.createRigidArea(new Dimension(10, 0)));
172

    
173
                panel.add(scrollPane, BorderLayout.CENTER);
174
                panel.add(optionsPane, BorderLayout.SOUTH);
175

    
176
                return panel;
177
        }
178

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

    
188
        private JPanel createFortuneCookieServiceInfoPanel(FortuneCookieService cookie) {
189
                final JPanel panel = new JPanel();
190

    
191
                panel.setLayout(new BorderLayout());
192
                panel.setPreferredSize(new Dimension(550,150));
193

    
194
                JTextArea text = new JTextArea();
195
                try {
196
                        text.setText("Message: " +cookie.getMessage()+"\nDate: " + cookie.getDate());
197
                } catch (FortuneCookieMessageException e) {
198
                        text.setText(this.getTranslation("Error obtaining message....."));
199
                        e.printStackTrace();
200
                }
201
                text.setEditable(false);
202

    
203
                JScrollPane scrollPane = new JScrollPane(text);
204
                scrollPane.setPreferredSize(new Dimension(550, 150));
205

    
206
                JButton accept = new JButton(this.getTranslation("Accept"));
207

    
208
                JPanel optionsPane = new JPanel();
209
                optionsPane.setLayout(new BoxLayout(optionsPane, BoxLayout.LINE_AXIS));
210
                optionsPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
211
                optionsPane.add(Box.createHorizontalGlue());
212

    
213
                accept.addActionListener(new ActionListener(){
214
                        public void actionPerformed(ActionEvent arg0) {
215
                                panel.setVisible(false);
216
                        }
217
                });
218
                optionsPane.add(accept);
219
                optionsPane.add(Box.createRigidArea(new Dimension(10, 0)));
220

    
221
                panel.add(scrollPane, BorderLayout.CENTER);
222
                panel.add(optionsPane, BorderLayout.SOUTH);
223

    
224
                return panel;
225
        }
226

    
227
        public void showWindow(JPanel panel, String title, int mode) {
228
                JFrame frame;
229

    
230
                frame = new JFrame();
231
                //frame.setAlwaysOnTop(true);
232

    
233
                final Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
234
                frame.setLocation(s.width / 4, s.height / 4);
235

    
236
                frame.setLayout(new BorderLayout());
237
                frame.setTitle(title);
238
                frame.add(panel, BorderLayout.CENTER);
239

    
240
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
241

    
242
                frame.setSize(400,400);
243
                panel.addComponentListener(this);
244

    
245
                frame.pack();
246
                frame.setVisible(true);
247
        }
248

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

    
255
        public void componentMoved(ComponentEvent arg0) {
256
                // Do nothing
257
        }
258

    
259
        public void componentResized(ComponentEvent arg0) {
260
                // Do nothing
261
        }
262

    
263
        public void componentShown(ComponentEvent arg0) {
264
                // Do nothing
265

    
266
        }
267

    
268
        public String getTranslation(String string) {
269

    
270
                return string;
271
        }
272

    
273
        public void initialize() {
274
                // Do nothing
275

    
276
        }
277

    
278
        public boolean isEnabled() {
279
                return true;
280
        }
281

    
282
        public boolean isVisible() {
283
                return true;
284
        }
285

    
286
}