/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.pdfbox.tools.gui;

import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.io.IOException;
import javax.swing.JLabel;


/**
 * A class to handle some prettyness around a single PDF page.
 * @author Ben Litchfield
 */
public class PageWrapper implements MouseMotionListener
{
    private final JPanel pageWrapper = new JPanel();
    private PDFPagePanel pagePanel = null;
    private final JLabel statusLabel;

    private static final int SPACE_AROUND_DOCUMENT = 20;

    /**
     * Constructor.
     * @param statusLabel The status label to use for status messages.
     * @throws IOException If there is an error creating the page drawing objects.
     */
    public PageWrapper(JLabel statusLabel) throws IOException
    {
        this.statusLabel = statusLabel;
        pagePanel = new PDFPagePanel();
        pageWrapper.setLayout(null);
        pageWrapper.add(pagePanel);
        pagePanel.setLocation(SPACE_AROUND_DOCUMENT, SPACE_AROUND_DOCUMENT);
        pageWrapper.setBorder(LineBorder.createBlackLineBorder());
        pagePanel.addMouseMotionListener(this);
    }

    /**
     * 
     * This will display the PDF page in this component.
     * @param renderer The renderer to render the page.
     * @param page The PDF page to display.
     * @param pageNum The number of the page.
     * @throws IOException if something goes wrong.
     */
    public void displayPage(PDFRenderer renderer, PDPage page, int pageNum) throws IOException
    {
        pagePanel.setPage(renderer, page, pageNum);
        pagePanel.setPreferredSize(pagePanel.getSize());
        Dimension d = pagePanel.getSize();
        d.width+=(SPACE_AROUND_DOCUMENT*2);
        d.height+=(SPACE_AROUND_DOCUMENT*2);

        pageWrapper.setPreferredSize(d);
        pageWrapper.validate();
    }

    /**
     * This will get the JPanel that can be displayed.
     * @return The panel with the displayed PDF page.
     */
    public JPanel getPanel()
    {
        return pageWrapper;
    }

    @Override
    public void mouseDragged(MouseEvent e)
    {
        //do nothing when mouse moves.
    }

    @Override
    public void mouseMoved(MouseEvent e)
    {
        statusLabel.setText(e.getX() + "," + e.getY());
    }
}
@@ -846,116 +846,97 @@
 ort 
-org.apache.pdfbox.pdmodel.PDPage;%0Aimport org.apache.pdfbox.rendering.PDFRenderer;%0A%0Aimport javax.swing.JPanel
+java.awt.Dimension;%0Aimport java.awt.event.MouseEvent;%0Aimport java.awt.event.MouseListener
 ;%0Aim
@@ -948,31 +948,36 @@
 java
-x.swing.border.LineBord
+.awt.event.MouseMotionListen
 er;%0A
@@ -984,34 +984,35 @@
 import java.
-awt.Dimens
+io.IOExcept
 ion;%0Aimport 
@@ -1019,127 +1019,177 @@
 java
-.awt.event.MouseEvent;%0Aimport java.awt.event.MouseMotionListen
+x.swing.JLabel;%0Aimport javax.swing.JPanel;%0Aimport javax.swing.border.LineBord
 er;%0A
+%0A
 import 
-java.io.IOException;%0Aimport javax.swing.JLabel
+org.apache.pdfbox.pdmodel.PDPage;%0Aimport org.apache.pdfbox.rendering.PDFRenderer
 ;%0A%0A%0A
@@ -1340,16 +1340,31 @@
 Listener
+, MouseListener
 %0A%7B%0A    p
@@ -2154,16 +2154,58 @@
 (this);%0A
+        pagePanel.addMouseListener(this);%0A
     %7D%0A%0A 
@@ -3332,9 +3332,394 @@
 ;%0A    %7D%0A
-%7D
+%0A    @Override%0A    public void mouseClicked(MouseEvent e)%0A    %7B%0A    %7D%0A%0A    @Override%0A    public void mousePressed(MouseEvent e)%0A    %7B%0A    %7D%0A%0A    @Override%0A    public void mouseReleased(MouseEvent e)%0A    %7B%0A    %7D%0A%0A    @Override%0A    public void mouseEntered(MouseEvent e)%0A    %7B%0A    %7D%0A%0A    @Override%0A    public void mouseExited(MouseEvent e)%0A    %7B%0A        statusLabel.setText(%22%22);%0A    %7D%0A%0A%7D%0A

clear mouse position from status bar when outside the window