org.netbeans.api.visual.print.ScenePrinter Maven / Gradle / Ivy
/*
* 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.netbeans.api.visual.print;
import java.awt.Rectangle;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.netbeans.api.visual.model.ObjectScene;
import org.netbeans.api.visual.widget.LayerWidget;
import org.netbeans.api.visual.widget.Scene;
import org.netbeans.api.visual.widget.Widget;
import org.netbeans.modules.visual.print.PageableScene;
/**
* A class used to print a Scene to a printer. Multiple convenience methods are
* provided to initiate the print with various constraints.
* @author krichard
*/
public final class ScenePrinter {
/**
* Creates a new instance of a ScenePrinter object.
*/
private ScenePrinter() {
}
/**
* Print the scene exactly as it is. There is to be no scaling or special
* formatting.
* @param scene The Scene to be printed.
*/
public static void print(Scene scene) {
print(scene, null, ScaleStrategy.NO_SCALING);
}
/**
* Print the Scene with provided PageFormat.
* @param scene The Scene to be printed.
* @param format format used for printing Scene.
*/
public static void print(Scene scene, PageFormat format) {
print(scene, format, ScaleStrategy.NO_SCALING);
}
/**
* Print the Scene with the default PageFormat and the provided scale type.
* @param scene The Scene to be printed.
* @param scaleStrategy Value representing the scaling strategy to use for the printing.
*/
public static void print(Scene scene, ScaleStrategy scaleStrategy) {
print(scene, null, scaleStrategy);
}
/**
* Print the Scene with specific scaling percentages for the horizontal and
* vertical direction.
* @param scene The Scene to be printed.
* @param scaleX scaling percentage in the horizontal dimension.
* @param scaleY scaling percentage in the vertical dimension.
*/
public static void print(Scene scene, double scaleX, double scaleY) {
print(scene, null, ScaleStrategy.SCALE_PERCENT, scaleX, scaleY,
false, false, null, null);
}
/**
* Print the Scene with the provided format and scale type.
* @param scene The Scene to be printed.
* @param format Format used for printing Scene.
* @param scaleStrategy Value representing the scaling strategy to use for the printing.
*/
public static void print(Scene scene, PageFormat format, ScaleStrategy scaleStrategy) {
print(scene, format, scaleStrategy, 1.0, 1.0, false, false, null, null);
}
/**
* Print a specific section of the Scene according to the provided Format.
* @param scene The Scene to be printed.
* @param format Format used for printing Scene.
* @param region The rectangle representing the are of the Scene to be printed.
*/
public static void print(Scene scene, PageFormat format, Rectangle region) {
print(scene, format, ScaleStrategy.NO_SCALING, 1.0, 1.0, false, false, region, null);
}
/**
* This is the master print call. All other print calls in this class pass
* through to here after setting the appropriate parameters. Note that the
* scale type parameter take precedent over the horizontal and vertical scaling
* percentages. Therefore, if the scale type was "SCALE_TO_FIT_Y", then the
* values of horizontal and vertical scaling percentages would not be used.
* @param scene The Scene to be printed.
* @param format format used for printing Scene. If null then a new default
* PageFormat is created.
* @param scaleStrategy value representing the how to scale the printing.
* @param scaleX Directly set the horizontal scale percentage. This parameter
* is only used when the scale strategy is ScaleStrategy.SCALE_PERCENT. Otherwise
* it is ignored.
* @param scaleY Directly set the vertical scale percentage. This parameter
* is only used when the scale strategy is ScaleStrategy.SCALE_PERCENT. Otherwise
* it is ignored.
* @param selectedOnly Print only the objects from the Scene that have been
* selected. Note that in this case the Scene must be an instnace of an ObjectScene
* since this is required to determine the selected objects.
* @param visibleOnly Print only the object in the visible window.
* @param region The rectangle representing the are of the Scene to be printed.
* @param hiddenLayerWidgets Layer that are not to be printed. Might be used to
* hide the background while printing.
*/
public static void print(Scene scene, PageFormat format, ScaleStrategy scaleStrategy,
double scaleX, double scaleY,
boolean selectedOnly, boolean visibleOnly,
Rectangle region, List hiddenLayers) {
//quick return if the scene is null
if (scene == null) return ;
if (scaleStrategy == null) scaleStrategy = ScaleStrategy.NO_SCALING ;
PageableScene ps = new PageableScene(scene, format,
scaleStrategy, scaleX, scaleY,
selectedOnly, visibleOnly, region);
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPageable(ps);
if (printJob.printDialog()) {
try {
//this can not go here because it hides all the widgets everytime.
ArrayList hiddenWidgets = new ArrayList();
if (hiddenLayers != null && hiddenLayers.size() > 0) {
for (Widget widget: hiddenLayers) {
widget.setVisible(false);
hiddenWidgets.add(widget);
}
}
//if selectedOnly is true then we need to hide all the non-selected
//widgets. Note that if the widgets were already hidden due to hiding
//a layer, they are not hidden again.
if (selectedOnly) {
//in order to use getSelectedObject the scene must be an ObjectScene
if (scene instanceof ObjectScene) {
ObjectScene gScene = (ObjectScene) scene;
// hide unselected widget
HashSet
© 2015 - 2024 Weber Informatics LLC | Privacy Policy