|
| 1 | +/* Copyright (C) 2010 Gilleain Torrance <gilleain.torrance@gmail.com> |
| 2 | + * |
| 3 | + * Contact: cdk-devel@lists.sourceforge.net |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or modify it under |
| 6 | + * the terms of the GNU Lesser General Public License as published by the Free |
| 7 | + * Software Foundation; either version 2.1 of the License, or (at your option) |
| 8 | + * any later version. All we ask is that proper credit is given for our work, |
| 9 | + * which includes - but is not limited to - adding the above copyright notice to |
| 10 | + * the beginning of your source code files, and to any copyright notice that you |
| 11 | + * may distribute with programs based on this work. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 14 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 15 | + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 16 | + * details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Lesser General Public License |
| 19 | + * along with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | + */ |
| 22 | +package org.openscience.cdk.renderer; |
| 23 | + |
| 24 | +import org.openscience.cdk.renderer.RendererModel; |
| 25 | +import org.openscience.cdk.renderer.elements.AtomSymbolElement; |
| 26 | +import org.openscience.cdk.renderer.elements.Bounds; |
| 27 | +import org.openscience.cdk.renderer.elements.ElementGroup; |
| 28 | +import org.openscience.cdk.renderer.elements.IRenderingElement; |
| 29 | +import org.openscience.cdk.renderer.elements.LineElement; |
| 30 | +import org.openscience.cdk.renderer.elements.MarkedElement; |
| 31 | +import org.openscience.cdk.renderer.elements.OvalElement; |
| 32 | +import org.openscience.cdk.renderer.font.IFontManager; |
| 33 | +import org.openscience.cdk.renderer.visitor.IDrawVisitor; |
| 34 | + |
| 35 | +import java.awt.geom.AffineTransform; |
| 36 | +import java.io.PrintStream; |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.List; |
| 39 | + |
| 40 | +/** |
| 41 | + * Utility class for testing. |
| 42 | + * |
| 43 | + * @author maclean |
| 44 | + * @cdk.module test-renderbasic |
| 45 | + */ |
| 46 | +public class ElementUtility implements IDrawVisitor { |
| 47 | + |
| 48 | + private List<IRenderingElement> elements = new ArrayList<IRenderingElement>(); |
| 49 | + |
| 50 | + private AffineTransform transform; |
| 51 | + |
| 52 | + private RendererModel model; |
| 53 | + |
| 54 | + private boolean getElementGroups = false; |
| 55 | + |
| 56 | + public int numberOfElements() { |
| 57 | + return this.elements.size(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void setTransform(AffineTransform transform) { |
| 62 | + this.transform = transform; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void visit(IRenderingElement element) { |
| 67 | + if (element instanceof ElementGroup) { |
| 68 | + if (getElementGroups) { |
| 69 | + this.elements.add(element); |
| 70 | + } |
| 71 | + ((ElementGroup) element).visitChildren(this); |
| 72 | + } else if (element instanceof MarkedElement) { |
| 73 | + visit(((MarkedElement) element).element()); |
| 74 | + } else if (element instanceof Bounds) { |
| 75 | + visit(((Bounds) element).root()); |
| 76 | + } else { |
| 77 | + this.elements.add(element); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public List<IRenderingElement> getElements() { |
| 82 | + return this.elements; |
| 83 | + } |
| 84 | + |
| 85 | + public List<IRenderingElement> getAllSimpleElements(IRenderingElement root) { |
| 86 | + elements.clear(); |
| 87 | + getElementGroups = false; |
| 88 | + root.accept(this); |
| 89 | + return new ArrayList<IRenderingElement>(elements); |
| 90 | + } |
| 91 | + |
| 92 | + public int[] transformPoint(double x, double y) { |
| 93 | + double[] src = new double[]{x, y}; |
| 94 | + double[] dest = new double[2]; |
| 95 | + this.transform.transform(src, 0, dest, 0, 1); |
| 96 | + return new int[]{(int) dest[0], (int) dest[1]}; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void setFontManager(IFontManager fontManager) { |
| 101 | + // TODO Auto-generated method stub |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void setRendererModel(RendererModel rendererModel) { |
| 107 | + this.model = rendererModel; |
| 108 | + } |
| 109 | + |
| 110 | + public RendererModel getModel() { |
| 111 | + return this.model; |
| 112 | + } |
| 113 | + |
| 114 | + public String toString(int[] p) { |
| 115 | + return String.format("(%d, %d)", p[0], p[1]); |
| 116 | + } |
| 117 | + |
| 118 | + public String toString(double x, double y) { |
| 119 | + return String.format("(%+3.1f, %+3.1f)", x, y); |
| 120 | + } |
| 121 | + |
| 122 | + public String toString(double x, double y, double r) { |
| 123 | + return String.format("(%+3.1f, %+3.1f, %+3.1f)", x, y, r); |
| 124 | + } |
| 125 | + |
| 126 | + public String toString(IRenderingElement element) { |
| 127 | + if (element instanceof LineElement) { |
| 128 | + LineElement e = (LineElement) element; |
| 129 | + String p1 = toString(e.firstPointX, e.firstPointY); |
| 130 | + String p2 = toString(e.secondPointX, e.secondPointY); |
| 131 | + String p1T = toString(transformPoint(e.firstPointX, e.firstPointY)); |
| 132 | + String p2T = toString(transformPoint(e.secondPointX, e.secondPointY)); |
| 133 | + String lineFormat = "Line [%s, %s] -> [%s, %s]\n"; |
| 134 | + return String.format(lineFormat, p1, p2, p1T, p2T); |
| 135 | + } else if (element instanceof OvalElement) { |
| 136 | + OvalElement e = (OvalElement) element; |
| 137 | + double r = e.radius; |
| 138 | + String c = toString(e.xCoord, e.yCoord, r); |
| 139 | + String p1 = toString(transformPoint(e.xCoord - r, e.yCoord - r)); |
| 140 | + String p2 = toString(transformPoint(e.xCoord + r, e.yCoord + r)); |
| 141 | + return String.format("Oval [%s] -> [%s, %s]\n", c, p1, p2); |
| 142 | + } else if (element instanceof AtomSymbolElement) { |
| 143 | + AtomSymbolElement e = (AtomSymbolElement) element; |
| 144 | + return String.format("AtomSymbol [%s]\n", e.text); |
| 145 | + } else if (element instanceof ElementGroup) { |
| 146 | + return "Element Group\n"; |
| 147 | + } else { |
| 148 | + return "Unknown element\n"; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + public void printToStream(IRenderingElement root, PrintStream stream) { |
| 153 | + root.accept(this); |
| 154 | + for (IRenderingElement element : this.elements) { |
| 155 | + stream.print(toString(element)); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | +} |
0 commit comments