001: /*
002: * Copyright (c) 2007, Sun Microsystems, Inc.
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * * Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in
012: * the documentation and/or other materials provided with the distribution.
013: * * Neither the name of Sun Microsystems, Inc. nor the names of its
014: * contributors may be used to endorse or promote products derived
015: * from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
021: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
022: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
023: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028:
029:
030: /*
031: * MarsRoverViewerApp.java
032: */
033:
034: package marsroverviewer;
035:
036: import java.awt.event.ActionEvent;
037: import java.net.MalformedURLException;
038: import java.net.URL;
039: import java.util.ArrayList;
040: import java.util.List;
041: import java.util.logging.Level;
042: import java.util.logging.Logger;
043: import javax.swing.Action;
044: import org.jdesktop.application.Application;
045: import org.jdesktop.application.SingleFrameApplication;
046:
047: /**
048: * This example highlights the importance of background tasks by
049: * downloading some very large Mars rover images from JPL's
050: * photojournal web site. There are about a dozen images, most with
051: * 10-15M pixels. Clicking the next/prev buttons (or control-N,P)
052: * cancels the current download and starts loading a new image. The
053: * stop button also cancels the current download. The list of images
054: * is defined in the startup() method. The first image is shown by
055: * the application's ready() method.
056: * <p>
057: * More images of Mars can be found here:
058: * <a href="http://photojournal.jpl.nasa.gov/target/Mars">
059: * http://photojournal.jpl.nasa.gov/target/Mars</a>. Some of the
060: * MER images are quite large (like this 22348x4487 whopper,
061: * http://photojournal.jpl.nasa.gov/jpeg/PIA06917.jpg) and can't
062: * be loaded without reconfiguring the Java heap parameters.
063: * <p>
064: * This file contains the main class of the application. It extends the
065: * Swing Application Framework's {@code SingleFrameApplication} class
066: * and therefore takes care or simplifies things like
067: * loading resources and saving the session state.
068: *
069: * This class calls the {@code MarsRoverViewerView} class, which
070: * contains the code for constructing the user interface and
071: * much of the application logic.
072: *
073: */
074: public class MarsRoverViewerApp extends SingleFrameApplication {
075:
076: /**
077: * At startup create and show the main frame of the application.
078: */
079: @Override
080: protected void startup() {
081: // create URLs for a set of selected images
082: String imageDir = "http://photojournal.jpl.nasa.gov/jpeg/";
083: String[] imageNames = { "PIA03171", "PIA02652", "PIA05108",
084: "PIA02696", "PIA05049", "PIA05460", "PIA07327",
085: "PIA05117", "PIA05199", "PIA05990", "PIA03623" };
086: List<URL> imageLocations = new ArrayList<URL>(imageNames.length);
087: for (String imageName : imageNames) {
088: String path = imageDir + imageName + ".jpg";
089: try {
090: URL url = new URL(path);
091: imageLocations.add(url);
092: } catch (MalformedURLException e) {
093: Logger.getLogger(MarsRoverViewerApp.class.getName())
094: .log(Level.WARNING, "bad image URL " + path, e);
095: }
096: }
097: // create and show the application's main window
098: show(new MarsRoverViewerView(this , imageLocations));
099: }
100:
101: /**
102: * Runs after the startup has completed and the GUI is up and ready.
103: * We show the first image here, rather than initializing it at startup
104: * time, so loading the first image doesn't impede getting the
105: * GUI visible.
106: */
107: @Override
108: protected void ready() {
109: Action refreshAction = getContext().getActionMap(
110: MarsRoverViewerView.class, getMainView()).get(
111: "refreshImage");
112: refreshAction.actionPerformed(new ActionEvent(this ,
113: ActionEvent.ACTION_PERFORMED, ""));
114: }
115:
116: /**
117: * This method is to initialize the specified window by injecting resources.
118: * Windows shown in our application come fully initialized from the GUI
119: * builder, so this additional configuration is not needed.
120: */
121: @Override
122: protected void configureWindow(java.awt.Window root) {
123: }
124:
125: /**
126: * A convenient static getter for the application instance.
127: * @return the instance of MarsRoverViewerApp
128: */
129: public static MarsRoverViewerApp getApplication() {
130: return Application.getInstance(MarsRoverViewerApp.class);
131: }
132:
133: /**
134: * Main method launching the application.
135: */
136: public static void main(String[] args) {
137: launch(MarsRoverViewerApp.class, args);
138: }
139: }
|