001    /*
002     * Copyright 2002-2004 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.application.support;
017    
018    import java.awt.Cursor;
019    import java.awt.Frame;
020    import java.awt.Image;
021    import java.awt.Window;
022    
023    import javax.help.HelpSet;
024    import javax.help.JHelp;
025    import javax.swing.JFrame;
026    
027    import org.springframework.core.io.ClassPathResource;
028    import org.springframework.core.io.Resource;
029    import org.springframework.richclient.application.Application;
030    import org.springframework.richclient.util.WindowUtils;
031    
032    /**
033     * A simple implementation of a help contents frame for an application using
034     * javahelp.
035     * 
036     * @author Keith Donald
037     */
038    public class HelpContents {
039    
040        private Resource helpSetPath = new ClassPathResource("help/helpset.hs");
041    
042        private JFrame helpFrame;
043    
044        public HelpContents() {
045    
046        }
047    
048        public void setHelpSetPath(Resource helpSetPath) {
049            this.helpSetPath = helpSetPath;
050        }
051    
052        protected String getApplicationName() {
053            return Application.instance().getName();
054        }
055    
056        protected Image getApplicationImage() {
057            return Application.instance().getImage();
058        }
059    
060        public void display(Window parent) {
061            if (helpFrame == null) {
062                helpFrame = new JFrame();
063                helpFrame.getGlassPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
064                try {
065                    HelpSet helpSet = new HelpSet(null, helpSetPath.getURL());
066                    JHelp jhelp = new JHelp(helpSet);
067                    helpFrame = new JFrame("Help - " + getApplicationName());
068                    helpFrame.getContentPane().add(jhelp);
069                    helpFrame.setIconImage(getApplicationImage());
070                    helpFrame.pack();
071                }
072                catch (Exception e) {
073                    e.printStackTrace();
074                }
075                helpFrame.getGlassPane().setCursor(Cursor.getDefaultCursor());
076            }
077            if (!helpFrame.isVisible()) {
078                    WindowUtils.centerOnParent(helpFrame, parent);
079                helpFrame.setVisible(true);
080            }
081            if ((helpFrame.getExtendedState() & Frame.NORMAL) == 0) {
082                helpFrame.setExtendedState(Frame.NORMAL);
083            }
084            helpFrame.toFront();
085        }
086    
087    }