package org.pentaho.reporting.designer.extensions.toc;
import java.awt.Container;
import java.awt.Point;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.geom.Point2D;
import java.util.Locale;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import org.pentaho.reporting.designer.core.editor.ReportRenderContext;
import org.pentaho.reporting.designer.core.editor.report.DndElementOverlay;
import org.pentaho.reporting.designer.core.editor.report.ReportElementDragHandler;
import org.pentaho.reporting.designer.core.editor.report.ReportElementEditorContext;
import org.pentaho.reporting.designer.core.model.CachedLayoutData;
import org.pentaho.reporting.designer.core.model.ModelUtility;
import org.pentaho.reporting.designer.core.util.exceptions.UncaughtExceptionsModel;
import org.pentaho.reporting.designer.core.util.undo.BandedSubreportEditUndoEntry;
import org.pentaho.reporting.designer.core.util.undo.ElementEditUndoEntry;
import org.pentaho.reporting.designer.core.util.undo.UndoManager;
import org.pentaho.reporting.engine.classic.core.AbstractRootLevelBand;
import org.pentaho.reporting.engine.classic.core.Band;
import org.pentaho.reporting.engine.classic.core.DetailsFooter;
import org.pentaho.reporting.engine.classic.core.DetailsHeader;
import org.pentaho.reporting.engine.classic.core.Element;
import org.pentaho.reporting.engine.classic.core.PageFooter;
import org.pentaho.reporting.engine.classic.core.PageHeader;
import org.pentaho.reporting.engine.classic.core.RootLevelBand;
import org.pentaho.reporting.engine.classic.core.Watermark;
import org.pentaho.reporting.engine.classic.core.metadata.ElementMetaData;
import org.pentaho.reporting.engine.classic.core.metadata.ElementType;
import org.pentaho.reporting.engine.classic.core.style.ElementStyleKeys;
import org.pentaho.reporting.engine.classic.core.style.ElementStyleSheet;
import org.pentaho.reporting.engine.classic.extensions.parsers.reportdesigner.ReportDesignerParserModule;
import org.pentaho.reporting.engine.classic.extensions.toc.TocElement;
import org.pentaho.reporting.libraries.fonts.tools.StrictGeomUtility;
/**
* Todo: Document me!
*
* @author Thomas Morgner.
*/
public class TocReportElementDragHandler implements ReportElementDragHandler
{
protected static final Float DEFAULT_WIDTH = new Float(-100);
protected static final Float DEFAULT_HEIGHT = new Float(20);
private DndElementOverlay representation;
public TocReportElementDragHandler()
{
representation = new DndElementOverlay();
}
public int dragStarted(final DropTargetDragEvent event,
final ReportElementEditorContext dragContext,
final ElementMetaData elementMetaData,
final String fieldName)
{
final Container representationContainer = dragContext.getRepresentationContainer();
final ReportRenderContext renderContext = dragContext.getRenderContext();
final Point pos = event.getLocation();
final Point2D point = dragContext.normalize(pos);
if (point.getX() < 0 || point.getY() < 0)
{
representationContainer.removeAll();
return DnDConstants.ACTION_NONE;
}
final Element rootBand = findRootBand(dragContext, point);
if (rootBand instanceof PageHeader ||
rootBand instanceof PageFooter ||
rootBand instanceof DetailsHeader ||
rootBand instanceof DetailsFooter ||
rootBand instanceof Watermark)
{
representationContainer.removeAll();
return DnDConstants.ACTION_NONE;
}
representation.setZoom(renderContext.getZoomModel().getZoomAsPercentage());
representation.setVisible(true);
representation.setText(elementMetaData.getDisplayName(Locale.getDefault()));
representation.setLocation(pos.x, pos.y);
representation.setSize(representation.getMinimumSize());
representationContainer.removeAll();
representationContainer.add(representation);
return DnDConstants.ACTION_COPY;
}
private Element findRootBand(final ReportElementEditorContext dragContext,
final Point2D point)
{
Element element = dragContext.getElementForLocation(point, false);
while (element != null && ((element instanceof RootLevelBand) == false))
{
element = element.getParent();
}
if (element != null)
{
return element;
}
return dragContext.getDefaultElement();
}
public int dragUpdated(final DropTargetDragEvent event,
final ReportElementEditorContext dragContext,
final ElementMetaData elementMetaData,
final String fieldName)
{
return dragStarted(event, dragContext, elementMetaData, fieldName);
}
public void dragAborted(final DropTargetEvent event,
final ReportElementEditorContext dragContext)
{
final Container representationContainer = dragContext.getRepresentationContainer();
representationContainer.removeAll();
}
public void drop(final DropTargetDropEvent event,
final ReportElementEditorContext dragContext,
final ElementMetaData elementMetaData,
final String fieldName)
{
final Point2D point = dragContext.normalize(event.getLocation());
final Element rootBand = findRootBand(dragContext, point);
if (rootBand instanceof PageHeader ||
rootBand instanceof PageFooter ||
rootBand instanceof DetailsHeader ||
rootBand instanceof DetailsFooter ||
rootBand instanceof Watermark)
{
event.rejectDrop();
return;
}
try
{
final ElementType type = elementMetaData.create();
final TocElement visualElement = new TocElement();
visualElement.getRootGroup().getHeader().setAttribute(ReportDesignerParserModule.NAMESPACE, ReportDesignerParserModule.HIDE_IN_LAYOUT_GUI_ATTRIBUTE,Boolean.TRUE);
visualElement.getRootGroup().getFooter().setAttribute(ReportDesignerParserModule.NAMESPACE, ReportDesignerParserModule.HIDE_IN_LAYOUT_GUI_ATTRIBUTE,Boolean.TRUE);
visualElement.getDetailsFooter().setAttribute(ReportDesignerParserModule.NAMESPACE, ReportDesignerParserModule.HIDE_IN_LAYOUT_GUI_ATTRIBUTE,Boolean.TRUE);
visualElement.getDetailsHeader().setAttribute(ReportDesignerParserModule.NAMESPACE, ReportDesignerParserModule.HIDE_IN_LAYOUT_GUI_ATTRIBUTE, Boolean.TRUE);
visualElement.getNoDataBand().setAttribute(ReportDesignerParserModule.NAMESPACE, ReportDesignerParserModule.HIDE_IN_LAYOUT_GUI_ATTRIBUTE, Boolean.TRUE);
visualElement.getWatermark().setAttribute(ReportDesignerParserModule.NAMESPACE, ReportDesignerParserModule.HIDE_IN_LAYOUT_GUI_ATTRIBUTE,Boolean.TRUE);
type.configureDesignTimeDefaults(visualElement, Locale.getDefault());
final ElementStyleSheet styleSheet = visualElement.getStyle();
styleSheet.setStyleProperty(ElementStyleKeys.MIN_WIDTH, DEFAULT_WIDTH);
styleSheet.setStyleProperty(ElementStyleKeys.MIN_HEIGHT, DEFAULT_HEIGHT);
final Element element = dragContext.getElementForLocation(point, false);
final Band band;
if (element instanceof Band)
{
band = (Band) element;
}
else if (element != null)
{
band = element.getParent();
}
else
{
final Element defaultEntry = dragContext.getDefaultElement();
if (defaultEntry instanceof Band == false)
{
event.rejectDrop();
dragContext.getRepresentationContainer().removeAll();
return;
}
band = (Band) defaultEntry;
}
event.acceptDrop(DnDConstants.ACTION_COPY);
styleSheet.setStyleProperty(ElementStyleKeys.POS_X, new Float(Math.max(0, point.getX() - getParentX(band))));
styleSheet.setStyleProperty(ElementStyleKeys.POS_Y, new Float(Math.max(0, point.getY() - getParentY(band))));
SwingUtilities.invokeLater(new SubreportConfigureHandler(visualElement, band, dragContext, rootBand == band));
representation.setVisible(false);
dragContext.getRepresentationContainer().removeAll();
event.dropComplete(true);
}
catch (final Exception e)
{
UncaughtExceptionsModel.getInstance().addException(e);
dragContext.getRepresentationContainer().removeAll();
event.dropComplete(false);
}
}
private double getParentX(final Band band)
{
final CachedLayoutData data = ModelUtility.getCachedLayoutData(band);
return StrictGeomUtility.toExternalValue(data.getX());
}
private double getParentY(final Band band)
{
final CachedLayoutData data = ModelUtility.getCachedLayoutData(band);
return StrictGeomUtility.toExternalValue(data.getY());
}
private static class SubreportConfigureHandler implements Runnable
{
private TocElement subReport;
private Band parent;
private ReportElementEditorContext dragContext;
private boolean rootband;
private SubreportConfigureHandler(final TocElement subReport,
final Band parent,
final ReportElementEditorContext dragContext,
final boolean rootband)
{
this.subReport = subReport;
this.parent = parent;
this.dragContext = dragContext;
this.rootband = rootband;
}
public void run()
{
if (rootband)
{
final int result = JOptionPane.showOptionDialog(dragContext.getRepresentationContainer(),
Messages.getInstance().getString("TocElementDragHandler.BandedOrInlineSubreportQuestion"),
Messages.getInstance().getString("TocElementDragHandler.InsertSubreport"),
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null,
new String[]{Messages.getInstance().getString("TocElementDragHandler.Inline"),
Messages.getInstance().getString("TocElementDragHandler.Banded"),
Messages.getInstance().getString("TocElementDragHandler.Cancel")},
Messages.getInstance().getString("TocElementDragHandler.Inline"));
if (result == JOptionPane.CLOSED_OPTION || result == 2)
{
return;
}
if (result == 0)
{
final ReportRenderContext context = dragContext.getRenderContext();
final UndoManager undo = context.getUndo();
undo.addChange(Messages.getInstance().getString("TocElementDragHandler.UndoEntry"),
new ElementEditUndoEntry(parent.getObjectID(), parent.getElementCount(), null, subReport));
parent.addElement(subReport);
}
else
{
final AbstractRootLevelBand arb = (AbstractRootLevelBand) parent;
final ReportRenderContext context = dragContext.getRenderContext();
final UndoManager undo = context.getUndo();
undo.addChange(Messages.getInstance().getString("TocElementDragHandler.UndoEntry"),
new BandedSubreportEditUndoEntry(parent.getObjectID(), arb.getSubReportCount(), null, subReport));
arb.addSubReport(subReport);
}
}
else
{
final ReportRenderContext context = dragContext.getRenderContext();
final UndoManager undo = context.getUndo();
undo.addChange(Messages.getInstance().getString("TocElementDragHandler.UndoEntry"),
new ElementEditUndoEntry(parent.getObjectID(), parent.getElementCount(), null, subReport));
parent.addElement(subReport);
}
dragContext.getRenderContext().getSelectionModel().setSelectedElements(new Object[]{subReport});
}
}
}