com.googlecode.blaisemath.graphics.core.GraphicUtils Maven / Gradle / Ivy
/**
* GraphicUtils.java
* Created Jul 11, 2014
*/
package com.googlecode.blaisemath.graphics.core;
/*
* #%L
* BlaiseGraphics
* --
* Copyright (C) 2009 - 2015 Elisha Peterson
* --
* Licensed 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.
* #L%
*/
import com.google.common.base.Objects;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.googlecode.blaisemath.style.StyleHints;
import com.googlecode.blaisemath.style.Styles;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RectangularShape;
import java.util.Collections;
import javax.annotation.Nullable;
/**
* Utility class for working with {@link Graphic}s.
* @author Elisha
*/
public class GraphicUtils {
//
/** Filter that can be applied to pass only visible graphics */
private static final Predicate VISIBLE_FILTER = new Predicate(){
@Override
public boolean apply(Graphic input) {
return !StyleHints.isInvisible(input.getStyleHints());
}
};
/** Filter that can be applied to pass only visible graphics */
private static final Predicate FUNCTIONAL_FILTER = new Predicate(){
@Override
public boolean apply(Graphic input) {
return StyleHints.isFunctional(input.getStyleHints());
}
};
//
// utility class
private GraphicUtils() {
}
//
/**
* Return visibility filter for graphics.
* @param type of graphic being filtered
* @return visible filter
*/
public static Predicate visibleFilter() {
return (Predicate) VISIBLE_FILTER;
}
/**
* Return functional filter for graphics.
* @param type of graphic being filtered
* @return functional filter
*/
public static Predicate functionalFilter() {
return (Predicate) FUNCTIONAL_FILTER;
}
/**
* Return true if graphic is currently invisible
* @param gr the graphic
* @return true if hidden
*/
public static boolean isInvisible(Graphic> gr) {
return !visibleFilter().apply(gr);
}
/**
* Return true if graphic is currently functional
* @param gr the graphic
* @return true if invisible
*/
public static boolean isFunctional(Graphic> gr) {
return functionalFilter().apply(gr);
}
//
//
/**
* Search for graphic with the given id in the composite, returning it.
* The id is stored in the {@link Styles#ID} attribute of the graphic's style.
* @param graphics canvas type
* @param gr regular or composite graphic to search in
* @param id what to search for
* @return the found graphic, or null if none is found
*/
public Graphic select(Graphic gr, String id) {
checkNotNull(id);
if (Objects.equal(id, gr.getStyle().getString(Styles.ID, null))) {
return gr;
} else if (gr instanceof GraphicComposite) {
GraphicComposite gc = (GraphicComposite) gr;
for (Graphic g : gc.getGraphics()) {
Graphic r = select(g, id);
if (r != null) {
return r;
}
}
}
return null;
}
//
/**
* Get the bounding box surrounding the given set of graphics.
* @param type of graphic canvas
* @param entries the graphics
* @return bounding box, or null if the provided iterable is empty
*/
@Nullable
public static Rectangle2D boundingBox(Iterable extends Graphic> entries) {
Rectangle2D res = null;
for (Graphic en : entries) {
Rectangle2D enBox = en.boundingBox();
if (enBox != null) {
res = res == null ? enBox : res.createUnion(enBox);
}
}
return res;
}
/**
* Get the bounding box surrounding the given set of rectangles.
* @param shapes the rectangular shapes
* @return bounding box, or null if the provided iterable is empty
*/
@Nullable
public static Rectangle2D boundingBoxRect(Iterable extends RectangularShape> shapes) {
Rectangle2D res = null;
for (RectangularShape sh : shapes) {
if (sh != null) {
res = res == null ? sh.getBounds2D() : res.createUnion(sh.getBounds2D());
}
}
return res;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy