/*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program 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.
*
* Copyright (c) 2001 - 2009 Object Refinery Ltd, Pentaho Corporation and Contributors. All rights reserved.
*/
package org.pentaho.reporting.engine.classic.demo.ancient.demo.layouts.internalframe;
import java.awt.Container;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.beans.PropertyVetoException;
import java.net.URL;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.KeyStroke;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.pentaho.reporting.engine.classic.core.MasterReport;
import org.pentaho.reporting.engine.classic.core.TableDataFactory;
import org.pentaho.reporting.engine.classic.core.modules.gui.base.PreviewDialog;
import org.pentaho.reporting.engine.classic.core.modules.gui.commonswing.SwingUtil;
import org.pentaho.reporting.engine.classic.core.modules.gui.commonswing.action.AbstractActionDowngrade;
import org.pentaho.reporting.engine.classic.core.modules.gui.commonswing.action.ActionDowngrade;
import org.pentaho.reporting.engine.classic.core.modules.gui.commonswing.action.ActionMenuItem;
import org.pentaho.reporting.engine.classic.core.modules.parser.base.ReportGenerator;
import org.pentaho.reporting.engine.classic.demo.ancient.demo.functions.PaintComponentTableModel;
import org.pentaho.reporting.engine.classic.demo.ancient.demo.layouts.ComponentDrawingDemoHandler;
import org.pentaho.reporting.engine.classic.demo.util.AbstractDemoFrame;
import org.pentaho.reporting.libraries.base.util.ObjectUtilities;
/**
* Creation-Date: 11.12.2005, 12:49:55
*
* @author Thomas Morgner
*/
public class InternalFrameDemoFrame extends AbstractDemoFrame
{
private static final Log logger = LogFactory.getLog(InternalFrameDemoFrame.class);
private class NewFrameAction extends AbstractActionDowngrade
{
protected NewFrameAction()
{
putValue(ActionDowngrade.MNEMONIC_KEY, new Integer(KeyEvent.VK_N));
putValue(ActionDowngrade.NAME, "New");
putValue(ActionDowngrade.ACCELERATOR_KEY,
KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK));
}
/**
* Invoked when an action occurs.
*/
public void actionPerformed(final ActionEvent e)
{
final JInternalFrame frame = new DocumentInternalFrame();
frame.setSize(400, 300);
frame.setVisible(true); //necessary as of 1.3
desktop.add(frame);
try
{
frame.setSelected(true);
}
catch (PropertyVetoException ex)
{
// ignore exception ..
}
}
}
private JDesktopPane desktop;
public InternalFrameDemoFrame()
{
setTitle("InternalFrameDemo");
setJMenuBar(createMenuBar());
desktop = new JDesktopPane();
setContentPane(desktop);
}
public void updateFrameSize(final int inset)
{
final Rectangle screenSize = SwingUtil.getMaximumWindowBounds();
setBounds(inset, inset, screenSize.width - inset * 2, screenSize.height - inset * 2);
}
protected JMenuBar createMenuBar()
{
final JMenuBar menuBar = new JMenuBar();
//Set up the lone menu.
final JMenu menu = new JMenu("Document");
menu.setMnemonic(KeyEvent.VK_D);
menuBar.add(menu);
menu.add(new ActionMenuItem(new NewFrameAction()));
menu.add(new ActionMenuItem(getPreviewAction()));
menu.addSeparator();
menu.add(new ActionMenuItem(getCloseAction()));
final JMenu helpmenu = new JMenu("Help");
helpmenu.setMnemonic(KeyEvent.VK_H);
helpmenu.add(new ActionMenuItem(getAboutAction()));
return menuBar;
}
/**
* Handler method called by the preview action. This method should perform all operations to preview the report.
*/
protected void attemptPreview()
{
final JInternalFrame frame = findSelectedFrame();
if (frame == null)
{
return;
}
final Rectangle bounds = frame.getBounds();
final Container parent = frame.getParent();
final boolean visible = frame.isVisible();
final int layer = frame.getLayer();
// now print ..
previewReport(frame);
if (parent != null)
{
if (frame.getParent() != parent)
{
frame.getParent().remove(frame);
parent.add(frame);
}
}
frame.setBounds(bounds);
frame.setVisible(visible);
frame.setLayer(new Integer(layer));
}
protected void previewReport(final JInternalFrame frame)
{
final ReportGenerator generator = ReportGenerator.getInstance();
try
{
final URL in = ObjectUtilities.getResourceRelative
("component-drawing.xml", ComponentDrawingDemoHandler.class);
if (in == null)
{
return;
}
final MasterReport report = generator.parseReport(in);
report.getReportConfiguration().setConfigProperty
("org.pentaho.reporting.engine.classic.core.AllowOwnPeerForComponentDrawable", "true");
final PaintComponentTableModel tableModel = new PaintComponentTableModel();
tableModel.addComponent(frame);
report.setDataFactory(new TableDataFactory("default", tableModel));
// Important: The dialog must be modal, so that we know, when the report
// processing is finished.
final PreviewDialog previewDialog = new PreviewDialog(report, this, true);
previewDialog.setToolbarFloatable(true);
previewDialog.pack();
SwingUtil.positionFrameRandomly(previewDialog);
previewDialog.setVisible(true);
}
catch (Exception e)
{
logger.error("Failed to parse the report definition", e);
}
}
private JInternalFrame findSelectedFrame()
{
final JInternalFrame[] frames = desktop.getAllFrames();
for (int i = 0; i < frames.length; i++)
{
final JInternalFrame frame = frames[i];
if (frame.isSelected())
{
return frame;
}
}
return null;
}
}