📄 reportbandeditor.java
字号:
/**
* ========================================
* JFreeReport : a free Java report library
* ========================================
*
* Project Info: http://www.jfree.org/jfreereport/index.html
* Project Lead: Thomas Morgner (taquera@sherito.org);
*
* (C) Copyright 2000-2003, by Simba Management Limited and Contributors.
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* ------------------------------
* ReportEditor.java
* ------------------------------
* (C)opyright 2003, by Thomas Morgner and Contributors.
*
* Original Author: Thomas Morgner;
* Contributor(s): David Gilbert (for Simba Management Limited);
*
* $Id: ReportBandEditor.java,v 1.3 2004/04/20 18:54:57 taqua Exp $
*
* Changes
* -------------------------
* 28.09.2003 : Initial version
*
*/
package org.jfree.designer.visualeditor.bandeditor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import javax.swing.CellRendererPane;
import javax.swing.JComponent;
import javax.swing.RepaintManager;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
import javax.swing.event.MouseInputAdapter;
import org.jfree.report.Band;
import org.jfree.report.Element;
import org.jfree.report.layout.BandLayoutManagerUtil;
import org.jfree.report.layout.DefaultLayoutSupport;
import org.jfree.report.layout.StaticLayoutManager;
public final class ReportBandEditor
extends JComponent
{
private final class ReportSelectionHandler
implements ReportSelectionListener
{
public ReportSelectionHandler ()
{
}
public final void selectionChanged (final ReportSelectionEvent event)
{
repaint();
}
}
/**
* This class will handle all mouse events.
* <p/>
* Multiple elements can be selected by pressing the shift key. If shift is not pressed,
* clicking on one element will clear the selection and select that element. Clicking
* outside of an element will just clear the selection.
* <p/>
* If the selection did not change, the click (mouse down) may start an drag event.
*/
private final class MouseHandler
extends MouseInputAdapter
{
private int orgX;
private int orgY;
// the elements for the drag operation ...
private Element[] selectedElements;
private float[] selectedElementsPosX;
private float[] selectedElementsPosY;
public MouseHandler ()
{
}
/**
* Invoked when a mouse button is pressed on a component and then dragged. Mouse drag
* events will continue to be delivered to the component where the first originated
* until the mouse button is released (regardless of whether the mouse position is
* within the bounds of the component).
*/
public final void mouseDragged (final MouseEvent e)
{
if (selectedElements == null)
{
return;
}
if (SwingUtilities.isLeftMouseButton(e) == false)
{
return;
}
final int y = e.getY();
if (y < 0)
{
//Log.debug ("Mouse Drag: " + getBounds() + " " + e.getPoint());
return;
}
final int x = e.getX();
if (x < 0)
{
//Log.debug ("Mouse Drag: " + getBounds() + " " + e.getPoint());
return;
}
// if zoomed, then our coordinate system would not fit the display sizes
final float deltaX = (x - orgX) / getDetailLevel();
final float deltaY = (y - orgY) / getDetailLevel();
if (deltaX == 0 && deltaY == 0)
{
return;
}
for (int i = 0; i < selectedElements.length; i++)
{
// ignore drag events for relative sizes for now.
final float selectedX = selectedElementsPosX[i];
final float selectedY = selectedElementsPosY[i];
if (selectedX < 0 || selectedY < 0)
{
continue;
}
final float targetX = selectedX + deltaX;
final float targetY = selectedY + deltaY;
if (targetX < 0 || targetY < 0)
{
continue;
}
final Point2D target = new Point2D.Double(targetX, targetY);
selectedElements[i].getStyle().setStyleProperty
(StaticLayoutManager.ABSOLUTE_POS, target);
}
// force layouting
revalidate();
repaint();
}
// implements java.awt.event.MouseListener
public final void mousePressed (final MouseEvent e)
{
if (SwingUtilities.isLeftMouseButton(e) == false)
{
return;
}
if (selectionModel.getSelectionCount() == 0)
{
selectedElements = null;
}
final Insets in = getInsets();
final Element element = getElementForLocation(e.getX() - in.right, e.getY() - in.top);
if (element == null)
{
if (e.isShiftDown() == false)
{
selectionModel.clearSelection();
}
// the selection is cleared, there is nothing more to do ...
// complete the event handling of mousePressed ...
// repaint();
return;
}
if (selectionModel.isSelected(element))
{
if (e.isShiftDown())
{
// remove that one element ...
selectionModel.removeSelection(element);
// in that case do not handle any drag event
// complete the event handling of mousePressed ...
// repaint();
return;
}
}
else
{
if (e.isShiftDown() == false)
{
selectionModel.clearSelection();
}
selectionModel.addSelection(element);
// selection changed, so do not handle any drag event
// complete the event handling of mousePressed ...
// repaint();
}
// at this point we can tell, that we have at least one selected element
orgX = e.getX();
orgY = e.getY();
// move elements ... currently only for absolutelayout
selectedElements = selectionModel.getSelectedElements();
selectedElementsPosX = new float[selectedElements.length];
selectedElementsPosY = new float[selectedElements.length];
for (int i = 0; i < selectedElements.length; i++)
{
final Point2D point = (Point2D) selectedElements[i].getStyle().getStyleProperty
(StaticLayoutManager.ABSOLUTE_POS);
if (point == null)
{
selectedElementsPosX[i] = 0;
selectedElementsPosY[i] = 0;
}
else
{
selectedElementsPosX[i] = (float) point.getX();
selectedElementsPosY[i] = (float) point.getY();
}
}
}
}
private Band band;
private String title;
private float width;
private float detailLevel;
private ElementRenderer renderer;
private CellRendererPane cellRendererPane;
private ReportDefinitionSelectionModel selectionModel;
private Dimension cachedRenderingSize;
private ReportSelectionHandler reportSelectionHandler;
public ReportBandEditor (final String title, final Band band, final float width)
{
// we don't have a layout manager.
cellRendererPane = new CellRendererPane();
this.detailLevel = 1;
this.band = band;
this.title = title;
this.width = width;
this.renderer = new DefaultElementRenderer();
this.selectionModel = new ReportDefinitionSelectionModel();
this.reportSelectionHandler = new ReportSelectionHandler();
this.selectionModel.addReportSelectionListener(reportSelectionHandler);
setLayout(null);
setBorder(new TitledBorder(title));
setDoubleBuffered(false);
final MouseHandler mh = new MouseHandler();
addMouseListener(mh);
addMouseMotionListener(mh);
}
public final ElementRenderer getRenderer ()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -