Revision 60

View differences:

org.gvsig.fortunecookies/trunk/basic-with-user-interface/org.gvsig.fortunecookies/pom.xml
127 127
		<module>org.gvsig.fortunecookies.lib</module>
128 128
		<module>org.gvsig.fortunecookies.swing</module>
129 129
		<module>org.gvsig.fortunecookies.main</module>
130
		<module>org.gvsig.fortunecookies.main.noswinglib</module>
130 131
	</modules>
131 132

  
132 133
</project>
org.gvsig.fortunecookies/trunk/basic-with-user-interface/org.gvsig.fortunecookies/org.gvsig.fortunecookies.main.noswinglib/src/main/java/org/gvsig/fortunecookies/main/Main.java
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
}
org.gvsig.fortunecookies/trunk/basic-with-user-interface/org.gvsig.fortunecookies/org.gvsig.fortunecookies.main.noswinglib/src/main/java/org/gvsig/fortunecookies/main/package.html
1
<?xml version="1.0" encoding="UTF-8" ?>
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
<html xmlns="http://www.w3.org/1999/xhtml">
4
<head>
5
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6
<title>org.gvsig.fortunecookies package documentation</title>
7
</head>
8
<body>
9

  
10
	<p>Fortune cookies library testing and demo application.</p>
11

  
12
</body>
13
</html>
org.gvsig.fortunecookies/trunk/basic-with-user-interface/org.gvsig.fortunecookies/org.gvsig.fortunecookies.main.noswinglib/src/main/resources/README.txt
1
Put into this folder the resources needed by your classes.
2

  
3
This folder is added to the classpath, so you can load any resources 
4
through the ClassLoader.
5

  
6
By default, in this folder you can find an example of log4j configuration,
7
prepared to log messages through the console, so logging works when you
8
run your classes.
org.gvsig.fortunecookies/trunk/basic-with-user-interface/org.gvsig.fortunecookies/org.gvsig.fortunecookies.main.noswinglib/src/main/resources/log4j.xml
1
<?xml version="1.0" encoding="ISO-8859-1" ?>
2
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
3

  
4
<!-- 
5
Log4J configuration file for unit tests execution.
6
 -->
7
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
8

  
9
	<!-- Appender configuration to show logging messages through the console -->
10
	<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
11
		<layout class="org.apache.log4j.PatternLayout">
12
			<param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p [%c{2}.%M()]\n  %m%n" />
13
		</layout>
14
	</appender>
15

  
16
	<!-- 
17
	Activate logging messages of DEBUG level of higher only for the
18
	org.gvsig.tools packages.
19
	You can put full classes names or packages instead, to configure
20
	logging for all the classes and subpackages of the package.
21
	-->
22
	<category name="org.gvsig.tools">
23
		<priority value="DEBUG" />
24
	</category>
25
	<category name="org.gvsig.fortunecookies">
26
		<priority value="DEBUG" />
27
	</category>
28

  
29
	<!-- 
30
	By default, show only logging messages of INFO level or higher, 
31
	through the previously configured CONSOLE appender. 
32
	-->
33
	<root>
34
		<priority value="INFO" />
35
		<appender-ref ref="CONSOLE" />
36
	</root>
37
</log4j:configuration>
org.gvsig.fortunecookies/trunk/basic-with-user-interface/org.gvsig.fortunecookies/org.gvsig.fortunecookies.main.noswinglib/pom.xml
1
<?xml version="1.0" encoding="ISO-8859-1"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4
	<modelVersion>4.0.0</modelVersion>
5
	<artifactId>org.gvsig.fortunecookies.main.noswinglib</artifactId>
6
	<packaging>jar</packaging>
7
	<name>org.gvsig.fortunecookies.main.noswinglib</name>
8
	<parent>
9
		<groupId>org.gvsig</groupId>
10
		<artifactId>org.gvsig.fortunecookies</artifactId>
11
		<version>1.0.0-SNAPSHOT</version>
12
	</parent>
13
	<dependencies>
14
		<dependency>
15
			<groupId>org.gvsig</groupId>
16
			<artifactId>org.gvsig.fortunecookies.lib.api</artifactId>
17
			<version>1.0.0-SNAPSHOT</version>
18
		</dependency>
19
		<dependency>
20
			<groupId>org.gvsig</groupId>
21
			<artifactId>org.gvsig.fortunecookies.lib.impl</artifactId>
22
			<version>1.0.0-SNAPSHOT</version>
23
			<scope>runtime</scope>
24
		</dependency>
25
	</dependencies>
26
</project>

Also available in: Unified diff