DocPanel.java :  » 6.0-JDK-Modules » java-3d » org » jdesktop » j3dfly » doctools » Java Open Source

Java Open Source » 6.0 JDK Modules » java 3d 
java 3d » org » jdesktop » j3dfly » doctools » DocPanel.java
/*
 *  $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/doctools/DocPanel.java,v 1.1 2005/04/20 21:04:24 paulby Exp $
 *
 *                         Sun Public License Notice
 *
 *  The contents of this file are subject to the Sun Public License Version
 *  1.0 (the "License"). You may not use this file except in compliance with
 *  the License. A copy of the License is available at http://www.sun.com/
 *  
 *  The Original Code is Java 3D(tm) Fly Through.
 *  The Initial Developer of the Original Code is Paul Byrne.
 *  Portions created by Paul Byrne are Copyright (C) 2002.
 *  All Rights Reserved.
 *  
 *  Contributor(s): Paul Byrne.
 *  
 **/
package org.jdesktop.j3dfly.doctools;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import org.jdesktop.j3dfly.event.EventProcessor;
import org.jdesktop.j3dfly.event.FileLoadEvent;
import org.jdesktop.j3dfly.event.ObjectPickEvent;
import org.jdesktop.j3dfly.event.FlyEventListener;
import org.jdesktop.j3dfly.J3dFlyContext;

/**
 *
 * @author  paulby
 * @version 
 */
public class DocPanel extends javax.swing.JPanel implements FlyEventListener {

    private DocManager docManager;
    private J3dFlyContext context;
    
    /** Creates new form DocPanel */
    public DocPanel( J3dFlyContext context ) {
        this.context = context;
        initComponents ();
        
        docManager = new DocManager( this );
        docP.addHyperlinkListener(docManager);
        
        context.getEventProcessor().addListener( this, FileLoadEvent.class );
    }
    
    /**
     * Return the Context for this Panel
     */
    J3dFlyContext getJ3dFlyContext() {
        return context;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the FormEditor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jScrollPane1 = new javax.swing.JScrollPane();
        docP = new javax.swing.JEditorPane();
        setLayout(new java.awt.BorderLayout());
        
        jScrollPane1.setPreferredSize(new java.awt.Dimension(200, 400));
        jScrollPane1.setMinimumSize(new java.awt.Dimension(200, 400));
        
        docP.setEditable(false);
          jScrollPane1.setViewportView(docP);
          
          
        add(jScrollPane1, java.awt.BorderLayout.CENTER);
        
    }//GEN-END:initComponents

    /**
     * Called by EventProcessor when an event occurs for which this
     * listener has registered interest
     */
    public void processFlyEvent( final org.jdesktop.j3dfly.event.FlyEvent evt) {
        if (evt instanceof FileLoadEvent)
            loadDocIndex( (FileLoadEvent)evt );
        else if (evt instanceof ObjectPickEvent)
            processObjectPick( (ObjectPickEvent)evt);
    }    
    
    /**
     * Load the Document index for the file that triggered the load event
     */
    private void loadDocIndex( FileLoadEvent evt ) {
        System.out.println( evt.getFile().getName() );
        String indexName = evt.getFile().getPath();
        indexName = indexName.substring(0, indexName.lastIndexOf('.'));
        indexName += ".idx";
        System.out.println("Index "+indexName);
        
        File file = new File( indexName );
        if (!file.exists()) {
            System.out.println("ERROR Can't open document index file "+indexName);
            return;
        }

        docManager.loadIndex( file );
        context.getEventProcessor().addListener( this, ObjectPickEvent.class );
        
        try {
            docP.setPage( docManager.getDoc(null));
        } catch( IOException e ) {
            //e.printStackTrace();
        }
    }
    
    /**
     * Show the HTML page at the URL
     */
    void showPage( URL url ) throws IOException {
        docP.setPage( url );
    }
    
    private void processObjectPick( ObjectPickEvent evt ) {
        URL url = docManager.getDoc( evt.getNode() );
        
        //System.out.println(evt+" "+url);
        try {
            if (url!=null)
                docP.setPage( url );
        } catch(IOException e ) {
            e.printStackTrace();
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JEditorPane docP;
    // End of variables declaration//GEN-END:variables

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.