File: JugglingLab.java

package info (click to toggle)
jugglinglab 0.6.2%2Bds.1-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 2,272 kB
  • sloc: java: 16,345; xml: 104; makefile: 5; sh: 2; csh: 1
file content (293 lines) | stat: -rwxr-xr-x 9,362 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// JugglingLab.java
//
// Copyright 2004 by Jack Boyce (jboyce@users.sourceforge.net) and others

/*
    This file is part of Juggling Lab.

    Juggling Lab is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Juggling Lab is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Juggling Lab; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import org.xml.sax.*;

import jugglinglab.core.*;
import jugglinglab.jml.*;
import jugglinglab.util.*;
import jugglinglab.notation.*;
import jugglinglab.view.*;


public class JugglingLab extends JApplet {
    static ResourceBundle guistrings;
    static ResourceBundle errorstrings;

	protected Animator ja = null;
    protected JugglingLabPanel jlp = null;
	protected AnimatorPrefs jc = null;
	
    
    public JugglingLab() {
        initAudioClips();
		initDefaultPropImages();
    }


    protected void configure_applet() {
		String config = getParameter("config");
        String prefs = getParameter("animprefs");
        String jmldir = getParameter("jmldir");
        String jmlfile = getParameter("jmlfile");
		
		// default values
		int entry_type = Notation.NOTATION_SITESWAP;
		int view_type = View.VIEW_SIMPLE;
		
		if (config != null) {
			ParameterList param = new ParameterList(config);
			String entry = param.getParameter("entry");
			if (entry != null) {
				if (entry.equalsIgnoreCase("none"))
					entry_type = Notation.NOTATION_NONE;
				else if (entry.equalsIgnoreCase("siteswap"))
					entry_type = Notation.NOTATION_SITESWAP;
			}
			String view = param.getParameter("view");
			if (view != null) {
				if (view.equalsIgnoreCase("none"))
					view_type = View.VIEW_NONE;
				else if (view.equalsIgnoreCase("simple"))
					view_type = View.VIEW_SIMPLE;
				else if (view.equalsIgnoreCase("edit"))
					view_type = View.VIEW_EDIT;
				else if (view.equalsIgnoreCase("selection"))
					view_type = View.VIEW_SELECTION;
				else if (view.equalsIgnoreCase("jml"))
					view_type = View.VIEW_JML;
			}
			String loc = param.getParameter("locale");
			if (loc != null) {
				// want to get to this before the resource bundles are needed
				// by this or any other objects
				String[] locarray = loc.split("_", 5);
				System.out.println("locarray = " + locarray);
				Locale newloc = null;
				if (locarray.length == 1)
					newloc = new Locale(locarray[0]);
				else if (locarray.length == 2)
					newloc = new Locale(locarray[0], locarray[1]);
				else if (locarray.length > 2)
					newloc = new Locale(locarray[0], locarray[1], locarray[2]);
				
				JLLocale.setLocale(newloc);
			}
		}

		JugglingLab.guistrings = JLLocale.getBundle("GUIStrings");
        JugglingLab.errorstrings = JLLocale.getBundle("ErrorStrings");

		try {
			jc = new AnimatorPrefs();
			if (prefs != null)
				jc.parseInput(prefs);

			JMLPattern pat = null;
			PatternList pl = null;
			boolean readerror = false;
			
			if (jmlfile != null) {
				if (!jmlfile.endsWith(".jml"))
					throw new JuggleExceptionUser(errorstrings.getString("Error_JML_extension"));
				if (jmlfile.indexOf(".") != (jmlfile.length()-4))
					throw new JuggleExceptionUser(errorstrings.getString("Error_JML_filename"));
				
				try {
					URL jmlfileURL = null;
					if (jmldir != null)
						jmlfileURL = new URL(new URL(jmldir), jmlfile);
					else
						jmlfileURL = new URL(getDocumentBase(), jmlfile);

					JMLParser parse = new JMLParser();
					parse.parse(new InputStreamReader(jmlfileURL.openStream()));
					if (parse.getFileType() == JMLParser.JML_PATTERN)
						pat = new JMLPattern(parse.getTree());
					else {
						pl = new PatternList();
						pl.readJML(parse.getTree());
					}
				} catch (MalformedURLException mue) {
					throw new JuggleExceptionUser(errorstrings.getString("Error_URL_syntax")+" '"+jmldir+"'");
				} catch (IOException ioe) {
					readerror = true;
					// System.out.println(ioe.getMessage());
					// throw new JuggleExceptionUser(errorstrings.getString("Error_reading_pattern"));
				} catch (SAXException se) {
					throw new JuggleExceptionUser(errorstrings.getString("Error_parsing_pattern"));
				}
			}
			
			if (pat == null) {
				String notation = getParameter("notation");
				String pattern = getParameter("pattern");
				if ((notation != null) && (pattern != null)) {
					Notation not = null;
					if (notation.equalsIgnoreCase("jml")) {
						try {
							pat = new JMLPattern(new StringReader(pattern));
						} catch (IOException ioe) {
							throw new JuggleExceptionUser(errorstrings.getString("Error_reading_JML"));
						} catch (SAXException se) {
							throw new JuggleExceptionUser(errorstrings.getString("Error_parsing_JML"));
						}
					} else {
						not = Notation.getNotation(notation);
						pat = not.getJMLPattern(pattern);
					}
				}
			}
			
			if (readerror)
				throw new JuggleExceptionUser(errorstrings.getString("Error_reading_pattern"));
				
			JugglingLabPanel jlp = new JugglingLabPanel(null, entry_type, pl, view_type);
			jlp.setDoubleBuffered(true);
			this.setBackground(new Color(0.9f, 0.9f, 0.9f));
			setContentPane(jlp);
			
			Locale loc = JLLocale.getLocale();
			this.applyComponentOrientation(ComponentOrientation.getOrientation(loc));

			validate();
			
			// NOTE: animprefs will only be applied when some kind of pattern is defined
			if (pat != null)
				jlp.getView().restartView(pat, jc);

		} catch (Exception e) {
			String message = "";
			if (e instanceof JuggleExceptionUser)
				message = e.getMessage();
			else {
				if (e instanceof JuggleExceptionInternal)
					message = "Internal error";
				else
					message = e.getClass().getName();
				if (e.getMessage() != null)
					message = message + ": " + e.getMessage();
			}
			
			JPanel p = new JPanel();
			p.setBackground(Color.white);
			GridBagLayout gb = new GridBagLayout();
			p.setLayout(gb);
		
			JLabel lab = new JLabel(message);
			lab.setHorizontalAlignment(SwingConstants.CENTER);
			p.add(lab);
			GridBagConstraints gbc = new GridBagConstraints();
			gbc.anchor = GridBagConstraints.CENTER;
			gbc.fill = GridBagConstraints.BOTH;
			gbc.gridwidth = gbc.gridheight = 1;
			gbc.gridx = 0;
			gbc.gridy = 0;
			gbc.insets = new Insets(0,0,0,0);
			gbc.weightx = 1.0;
			gbc.weighty = 1.0;
			gb.setConstraints(lab, gbc);

			setContentPane(p);
			validate();
			
			if (e instanceof JuggleExceptionInternal)
				ErrorDialog.handleException(e);
			else if (!(e instanceof JuggleExceptionUser))
				e.printStackTrace();
		}
    }

    // applet version starts here
	public void init() {
		// do it this way, so that calls to Swing methods happen on the event dispatch thread
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				configure_applet();
			}
		});	
	}
	
    public void start() {
		if ((jlp != null) && (jlp.getView() != null) && !jc.mousePause)
			jlp.getView().setPaused(false);
		if ((ja != null) && (ja.message == null) && !jc.mousePause)
			ja.setPaused(false);
    }

    public void stop() {
		if ((jlp != null) && (jlp.getView() != null) && !jc.mousePause)
			jlp.getView().setPaused(true);
		if ((ja != null) && (ja.message == null) && !jc.mousePause)
			ja.setPaused(true);
    }

    public void destroy() {
		if ((jlp != null) && (jlp.getView() != null))
			jlp.getView().dispose();
		if (ja != null)
			ja.dispose();
    }

    // It's easiest to load the audioclips here and pass them along to Animator.
    // For some reason AudioClip is part of the Applet package.
    protected void initAudioClips() {
        AudioClip[] clips = new AudioClip[2];
        URL catchurl = this.getClass().getResource("/resources/catch.au");
		if (catchurl != null)
			clips[0] = newAudioClip(catchurl);
        URL bounceurl = this.getClass().getResource("/resources/bounce.au");
		if (bounceurl != null)
			clips[1] = newAudioClip(bounceurl);
        Animator.setAudioClips(clips);
    }
	
	// The class loader delegation model makes it difficult to find resources from
	// within the VersionSpecific class.  There must be a better way to do it but I 
	// give it up already.
	protected void initDefaultPropImages() {
		URL[] images = new URL[2];
		images[0] = this.getClass().getResource("/resources/ball.gif");
        images[1] = this.getClass().getResource("/resources/ball.png");
        VersionSpecific.setDefaultPropImages(images);
	}

	
    // application version starts here
    public static void main(String[] args) {
        try {
            new JugglingLab();		// to load audio clips
            new ApplicationWindow("Juggling Lab");
        } catch (JuggleExceptionUser jeu) {
            new ErrorDialog(null, jeu.getMessage());
        } catch (JuggleExceptionInternal jei) {
            ErrorDialog.handleException(jei);
        }
    }
}