📄 visualeditpanel.java
字号:
/**
* ========================================================================================
* JFreeReport Designer : a graphical designer for JFreeReport - a free Java report library
* ========================================================================================
*
* Project Info: http://www.jfree.org/jfreereport/index.html
* Project Lead: Thomas Morgner (taquera@sherito.org);
*
* (C) Copyright 2003, by Heiko Evermann (heiko@evermann.de) 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.
*
*/
package org.jfree.designer.visualeditor;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.border.EtchedBorder;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import org.jfree.designer.JFreeReportDesigner;
import org.jfree.designer.ReportDefinitionManager;
import org.jfree.designer.RootEditor;
import org.jfree.designer.util.ActionCheckBoxMenuItem;
import org.jfree.designer.util.DowngradeActionMap;
import org.jfree.designer.visualeditor.actions.DeleteElementAction;
import org.jfree.designer.visualeditor.actions.TestLayoutAction;
import org.jfree.designer.visualeditor.actions.ToBackAction;
import org.jfree.designer.visualeditor.actions.ToFrontAction;
import org.jfree.designer.visualeditor.actions.ZoomChangeToReportEditorHandler;
import org.jfree.designer.visualeditor.actions.ZoomInAction;
import org.jfree.designer.visualeditor.actions.ZoomOutAction;
import org.jfree.designer.visualeditor.actions.ZoomSelectHandler;
import org.jfree.designer.visualeditor.actions.layout.BottomAlignmentAction;
import org.jfree.designer.visualeditor.actions.layout.CenterAlignmentAction;
import org.jfree.designer.visualeditor.actions.layout.LeftAlignmentAction;
import org.jfree.designer.visualeditor.actions.layout.MiddleAlignmentAction;
import org.jfree.designer.visualeditor.actions.layout.RightAlignmentAction;
import org.jfree.designer.visualeditor.actions.layout.SameHeightAction;
import org.jfree.designer.visualeditor.actions.layout.SameWidthAction;
import org.jfree.designer.visualeditor.actions.layout.SameWidthAndHeightAction;
import org.jfree.designer.visualeditor.actions.layout.TopAlignmentAction;
import org.jfree.designer.visualeditor.bandeditor.DefaultElementRendererFactory;
import org.jfree.designer.visualeditor.bandeditor.ElementRenderer;
import org.jfree.designer.visualeditor.bandeditor.ReportBandEditor;
import org.jfree.designer.visualeditor.bandeditor.ReportDefinitionSelectionModel;
import org.jfree.designer.visualeditor.bandeditor.ReportSelectionEvent;
import org.jfree.designer.visualeditor.bandeditor.ReportSelectionListener;
import org.jfree.designer.visualeditor.elementeditor.ElementEditorDialog;
import org.jfree.designer.visualeditor.treemodel.BandNode;
import org.jfree.designer.visualeditor.treemodel.ElementNode;
import org.jfree.designer.visualeditor.treemodel.ReportTreeModel;
import org.jfree.designer.visualeditor.treemodel.ReportTreeRenderer;
import org.jfree.report.Band;
import org.jfree.report.Element;
import org.jfree.report.Group;
import org.jfree.report.JFreeReport;
import org.jfree.report.modules.gui.base.components.ActionButton;
import org.jfree.report.modules.gui.base.components.ActionMenuItem;
import org.jfree.report.modules.gui.base.components.FloatingButtonEnabler;
import org.jfree.report.modules.gui.config.VerticalLayout;
import org.jfree.report.util.ImageUtils;
import org.jfree.report.util.Log;
public final class VisualEditPanel
extends JPanel
implements RootEditor
{
/**
* Listens for property change of "selectedElement"
*/
private final class ElementSelectionHandler
implements PropertyChangeListener
{
public ElementSelectionHandler ()
{
}
/**
* This method gets called when a bound property is changed.
*
* @param evt A PropertyChangeEvent object describing the event source and the
* property that has changed.
*/
public final void propertyChange (final PropertyChangeEvent evt)
{
if (getSelectedElement() != null)
{
styleSheetDialog.setElement(getSelectedElement());
setAllActionsEnabled(layoutActions, true);
setAllActionsEnabled(insertActions, true);
deleteElementAction.setEnabled(true);
}
else
{
setAllActionsEnabled(insertActions, false);
setAllActionsEnabled(layoutActions, false);
styleSheetDialog.setElement(null);
deleteElementAction.setEnabled(false);
}
}
}
/**
* Keeps the tree and the band editors in sync. The band editors are the primary
* selection datasource
*/
private final class ReportSelectionHandler
implements ReportSelectionListener, TreeSelectionListener
{
//private ReportDefinitionSelectionModel currentSource;
private boolean locked;
public ReportSelectionHandler ()
{
}
public final synchronized void selectionChanged (final ReportSelectionEvent event)
{
if (locked)
{
return;
}
try
{
locked = true;
// new source, clear all other selections...
final Element[] selection = event.getSelection();
if (event.getType() == ReportSelectionEvent.SELECTION_ADDED)
{
// added events only
for (int i = 0; i < selection.length; i++)
{
reportModelTree.getSelectionModel().addSelectionPath
(treeModel.getPathForElement(selection[i]));
}
}
else
{
for (int i = 0; i < selection.length; i++)
{
reportModelTree.getSelectionModel().removeSelectionPath
(treeModel.getPathForElement(selection[i]));
}
}
updateSelectedElement(getCurrentRootBandEditors());
}
finally
{
locked = false;
}
}
/**
* Called whenever the value of the selection changes.
*
* @param e the event that characterizes the change.
*/
public final synchronized void valueChanged (final TreeSelectionEvent e)
{
if (locked)
{
return;
}
try
{
locked = true;
final TreePath[] selectedPaths =
reportModelTree.getSelectionModel().getSelectionPaths();
final ReportBandEditor[] editors = getCurrentRootBandEditors();
for (int i = 0; i < editors.length; i++)
{
// the tree's selection is not the most accurate view
editors[i].getSelectionModel().clearSelection();
}
if (selectedPaths == null)
{
return;
}
for (int i = 0; i < selectedPaths.length; i++)
{
final TreePath path = selectedPaths[i];
if (path.getPathCount() < 2)
{
continue;
}
final Object rootBand = path.getPathComponent(1);
if (rootBand instanceof BandNode == false)
{
continue;
}
final BandNode bandNode = (BandNode) rootBand;
final ReportBandEditor editor = findEditorForBand((Band) bandNode.getElement(), editors);
if (editor == null)
{
continue;
}
final ElementNode elementNode = (ElementNode)
path.getPathComponent(path.getPathCount() - 1);
editor.getSelectionModel().addSelection(elementNode.getElement());
}
/**
* Once all events are handled, update the selectedElement property
* and the currentSource property of this handler.
*/
updateSelectedElement(editors);
}
finally
{
locked = false;
}
}
private void updateSelectedElement (final ReportBandEditor[] editors)
{
final TreePath path = reportModelTree.getSelectionPath();
if (path == null)
{
return;
}
if (path.getPathCount() < 2)
{
setSelectedElement(null);
}
else
{
final Object rootBand = path.getPathComponent(1);
if (rootBand instanceof BandNode == false)
{
setSelectedElement(null);
}
else
{
final BandNode bandNode = (BandNode) rootBand;
final ReportBandEditor editor = findEditorForBand((Band) bandNode.getElement(), editors);
if (editor == null)
{
setSelectedElement(null);
}
else
{
final ElementNode elementNode = (ElementNode)
path.getPathComponent(path.getPathCount() - 1);
//editor.getSelectionModel().addSelection(elementNode.getElement());
setSelectedElement(elementNode.getElement());
}
}
}
}
private ReportBandEditor findEditorForBand (final Band band,
final ReportBandEditor[] editors)
{
for (int i = 0; i < editors.length; i++)
{
if (editors[i].getBand() == band)
{
return editors[i];
}
}
return null;
}
}
private final class StyleDialogVisibleAction
extends AbstractAction
{
/**
* Defines an <code>Action</code> object with a default description string and default
* icon.
*/
public StyleDialogVisibleAction ()
{
putValue(Action.NAME, "Edit Element");
}
/**
* Invoked when an action occurs.
*/
public final void actionPerformed (final ActionEvent e)
{
styleSheetDialog.setVisible(styleDialogVisibleMenuItem.isSelected());
}
}
private final class StyleDialogWindowAdapter
extends WindowAdapter
{
private final JCheckBoxMenuItem styleDialogMenuItem;
public StyleDialogWindowAdapter (final JCheckBoxMenuItem menuItem)
{
this.styleDialogMenuItem = menuItem;
}
/**
* Invoked when a window is in the process of being closed. The close operation can be
* overridden at this point.
*/
public final void windowClosing (final WindowEvent e)
{
styleDialogMenuItem.setSelected(false);
}
/**
* Invoked when a window has been opened.
*/
public final void windowOpened (final WindowEvent e)
{
styleDialogMenuItem.setSelected(true);
}
}
private static final float[] ZOOM_LEVELS =
{0.5f, 0.75f, 1.0f, 1.25f, 1.5f, 2.0f, 3.0f};
private static final int DEFAULT_ZOOM_INDEX = 2;
public static final String ZOOM_IN_ACTION_KEY = "ZoomIn";
public static final String ZOOM_OUT_ACTION_KEY = "ZoomOut";
public static final String LEFT_ALIGNMENT_ACTION_KEY = "LeftAlignment";
public static final String CENTER_ALIGNMENT_ACTION_KEY = "CenterAlignment";
public static final String RIGHT_ALIGNMENT_ACTION_KEY = "RightAlignment";
public static final String TOP_ALIGNMENT_ACTION_KEY = "TopAlignment";
public static final String MIDDLE_ALIGNMENT_ACTION_KEY = "MiddleAlignment";
public static final String BOTTOM_ALIGNMENT_ACTION_KEY = "BottomAlignment";
public static final String SAME_WIDTH_ACTION_KEY = "SameWidth";
public static final String SAME_HEIGHT_ACTION_KEY = "SameHeight";
public static final String SAME_WIDTH_AND_HEIGHT_ACTION_KEY = "SameWidthAndHeight";
public static final String ZOOM_LEVEL_PROPERTY_NAME = "zoomLevel";
public static final String TO_BACK_ACTION_KEY = "toBack";
public static final String TO_FRONT_ACTION_KEY = "toFront";
private final static String DELETE_ACTION_KEY = "deleteElement";
public static final String SELECTED_ELEMENT_PROPERTY_NAME = "selectedElement";
private JFreeReport report;
private int zoomLevel;
private JComponent contentPane;
private ElementRenderer renderer;
private ArrayList currentRootBandEditors;
private JMenu[] editorMenus;
private boolean active;
private ReportDefinitionManager manager;
private JPopupMenu insertPopupMenu;
private ReportSelectionHandler selectionHandler;
private ReportDefinitionSelectionModel selectionModel;
private JTree reportModelTree;
private ReportTreeModel treeModel;
private TreeModel emptyTreeModel;
private ElementEditorDialog styleSheetDialog;
private Element selectedElement;
private PlainDataRow dataRow;
private JCheckBoxMenuItem styleDialogVisibleMenuItem;
private boolean bandStyleDialogVisible;
private boolean elementStyleDialogVisible;
private DowngradeActionMap zoomActions;
private DowngradeActionMap insertActions;
private DowngradeActionMap allActions;
private DowngradeActionMap layoutActions;
private JToolBar toolBar;
private Action deleteElementAction;
public VisualEditPanel (final ReportDefinitionManager manager)
{
this.dataRow = new PlainDataRow();
this.manager = manager;
this.addPropertyChangeListener
(SELECTED_ELEMENT_PROPERTY_NAME, new ElementSelectionHandler());
selectionHandler = new ReportSelectionHandler();
selectionModel = new ReportDefinitionSelectionModel();
selectionModel.addReportSelectionListener(selectionHandler);
renderer = new DefaultElementRendererFactory();
currentRootBandEditors = new ArrayList();
//styleSheetPropertyEditorPane = new StyleSheetPropertyEditorPane();
//bandStyleSheetPropertyEditorPane = new StyleSheetPropertyEditorPane();
initActions();
editorMenus = new JMenu[2];
editorMenus[0] = buildEditMenu();
editorMenus[1] = buildLayoutMenu();
initComponents();
buildInsertPopupMenu();
// requires the menus to be initialized, as we reference some menu items.
styleSheetDialog = new ElementEditorDialog(this);
styleSheetDialog.setSize(400, 480);
styleSheetDialog.addWindowListener(new StyleDialogWindowAdapter(styleDialogVisibleMenuItem));
addPropertyChangeListener(ZOOM_LEVEL_PROPERTY_NAME, new ZoomChangeToReportEditorHandler(this));
setZoomLevel(DEFAULT_ZOOM_INDEX);
setAllActionsEnabled(layoutActions, getSelectedElement() != null);
}
private void initActions ()
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -