001/*
002 * $Id: JXApplet.java 4175 2012-05-30 16:57:33Z kschaefe $
003 *
004 * Copyright 2010 Sun Microsystems, Inc., 4150 Network Circle,
005 * Santa Clara, California 95054, U.S.A. All rights reserved.
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 * 
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015 * Lesser General Public License for more details.
016 * 
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020 */
021package org.jdesktop.swingx;
022
023import java.awt.HeadlessException;
024import java.awt.IllegalComponentStateException;
025import java.lang.reflect.InvocationTargetException;
026import java.util.concurrent.Callable;
027
028import javax.swing.JApplet;
029import javax.swing.JButton;
030import javax.swing.JComponent;
031import javax.swing.JRootPane;
032import javax.swing.JToolBar;
033import javax.swing.SwingUtilities;
034
035/**
036 * An applet that uses {@link JXRootPane} as its root container.
037 * 
038 * @author kschaefer
039 */
040@SuppressWarnings("nls")
041public class JXApplet extends JApplet {
042    private static final long serialVersionUID = 2L;
043
044    /**
045     * Creates a the applet instance.
046     * <p>
047     * This constructor sets the component's locale property to the value returned by
048     * <code>JComponent.getDefaultLocale</code>.
049     * 
050     * @throws HeadlessException
051     *             if GraphicsEnvironment.isHeadless() returns true.
052     * @see java.awt.GraphicsEnvironment#isHeadless
053     * @see JComponent#getDefaultLocale
054     */
055    public JXApplet() throws HeadlessException {
056        super();
057    }
058
059    /**
060     * Overridden to create a JXRootPane and to ensure that the root pane is always created on the
061     * Event Dispatch Thread. Some applet containers do not start applets on the EDT; this method,
062     * therefore, protects against that. Actual, root pane creation occurs in
063     * {@link #createRootPaneSafely()}.
064     * 
065     * @return the root pane for this applet
066     * @see #createRootPaneSafely()
067     */
068    @Override
069    protected final JXRootPane createRootPane() {
070        if (SwingUtilities.isEventDispatchThread()) {
071            return createRootPaneSafely();
072        }
073        
074        try {
075            return SwingXUtilities.invokeAndWait(new Callable<JXRootPane>() {
076                @Override
077                public JXRootPane call() throws Exception {
078                    return createRootPaneSafely();
079                }
080            });
081        } catch (InterruptedException e) {
082            Thread.currentThread().interrupt();
083        } catch (InvocationTargetException e) {
084            IllegalComponentStateException thrown = new IllegalComponentStateException("cannot construct root pane");
085            thrown.initCause(e);
086            
087            throw thrown;
088        }
089        
090        return null;
091    }
092    
093    /**
094     * This method performs the actual creation of the root pane and is guaranteed to be performed on the Event Dispatch Thread.
095     * <p>
096     * Subclasses that need to configure the root pane or create a custom root pane should override this method.
097     * 
098     * @return the root pane for this applet
099     */
100    protected JXRootPane createRootPaneSafely() {
101        JXRootPane rp = new JXRootPane();
102        rp.setOpaque(true);
103        
104        return rp;
105    }
106    
107    /**
108     * {@inheritDoc}
109     */
110    @Override
111    public JXRootPane getRootPane() {
112        return (JXRootPane) super.getRootPane();
113    }
114    
115    /**
116     * Returns the value of the status bar property from the underlying
117     * {@code JXRootPane}.
118     * 
119     * @return the {@code JXStatusBar} which is the current status bar
120     * @see #setStatusBar(JXStatusBar)
121     * @see JXRootPane#getStatusBar()
122     */
123    public JXStatusBar getStatusBar() {
124        return getRootPane().getStatusBar();
125    }
126    
127    /**
128     * Sets the status bar property on the underlying {@code JXRootPane}.
129     * 
130     * @param statusBar
131     *            the {@code JXStatusBar} which is to be the status bar
132     * @see #getStatusBar()
133     * @see JXRootPane#setStatusBar(JXStatusBar)
134     */
135    public void setStatusBar(JXStatusBar statusBar) {
136        getRootPane().setStatusBar(statusBar);
137    }
138    
139    /**
140     * Returns the value of the tool bar property from the underlying
141     * {@code JXRootPane}.
142     * 
143     * @return the {@code JToolBar} which is the current tool bar
144     * @see #setToolBar(JToolBar)
145     * @see JXRootPane#getToolBar()
146     */
147    public JToolBar getToolBar() {
148        return getRootPane().getToolBar();
149    }
150    
151    /**
152     * Sets the tool bar property on the underlying {@code JXRootPane}.
153     * 
154     * @param toolBar
155     *            the {@code JToolBar} which is to be the tool bar
156     * @see #getToolBar()
157     * @see JXRootPane#setToolBar(JToolBar)
158     */
159    public void setToolBar(JToolBar toolBar) {
160        getRootPane().setToolBar(toolBar);
161    }
162    
163    /**
164     * Returns the value of the default button property from the underlying
165     * {@code JRootPane}.
166     * 
167     * @return the {@code JButton} which is the default button
168     * @see #setDefaultButton(JButton)
169     * @see JRootPane#getDefaultButton()
170     */
171    public JButton getDefaultButton() {
172        return getRootPane().getDefaultButton();
173    }
174    
175    /**
176     * Sets the default button property on the underlying {@code JRootPane}.
177     * 
178     * @param button
179     *            the {@code JButton} which is to be the default button
180     * @see #getDefaultButton()
181     * @see JRootPane#setDefaultButton(JButton)
182     */
183    public void setDefaultButton(JButton button) {
184        getRootPane().setDefaultButton(button);
185    }
186    
187    /**
188     * Returns the value of the cancel button property from the underlying
189     * {@code JXRootPane}.
190     * 
191     * @return the {@code JButton} which is the cancel button
192     * @see #setCancelButton(JButton)
193     * @see JXRootPane#getCancelButton()
194     */
195    public JButton getCancelButton() {
196        return getRootPane().getCancelButton();
197    }
198
199    /**
200     * Sets the cancel button property on the underlying {@code JXRootPane}.
201     * 
202     * @param button
203     *            the {@code JButton} which is to be the cancel button
204     * @see #getCancelButton()
205     * @see JXRootPane#setCancelButton(JButton)
206     */
207    public void setCancelButton(JButton button) {
208        getRootPane().setCancelButton(button);
209    }
210}