// XAAL toolkit // Copyright (C) 2009 Ville Karavirta // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // 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 General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . /** * */ package xaal.draw; import java.awt.Graphics; import java.awt.image.BufferedImage; import xaal.objects.Xaal; import xaal.objects.graphical.Coordinate; import xaal.objects.graphical.Line; import xaal.objects.graphical.Rectangle; import xaal.objects.graphical.Style; import xaal.objects.graphical.Text; import xaal.objects.structures.Array; import xaal.objects.structures.DataStructure; import xaal.objects.structures.Index; import xaal.objects.structures.Key; import xaal.objects.structures.Marker; import xaal.objects.structures.Structure; import xaal.util.ReflectionHelper; /** * @author vkaravir * */ public class StructureDraw { private static final int NODEHEIGHT = 20; public static java.awt.Rectangle drawStructure(DataStructure s, Xaal xaal) { return (java.awt.Rectangle) ReflectionHelper.invoke(new Object[] {s, xaal}, new StructureDraw(), "appendGraphicalInfo"); } public void appendGraphicalInfo(DataStructure s, Xaal xaal) { System.out.println("unsupporter structure:"+s.getClass().getName()); } public void appendGraphicalInfo(Marker m, Xaal xaal) { if (!m.isVisible()) return; Structure target = m.getTarget(); if (target != null && target.getCoordinate() != null) { int x = target.getCoordinate().getX() + 10; int y = target.getCoordinate().getY() - 10; Line l = new Line(xaal); l.setStartCoordinate(new Coordinate(xaal, x, y-20)); l.setEndCoordinate(new Coordinate(xaal, x, y)); Style s = new Style(m.getStyle()); xaal.getDefs().addStyle(s); s.setForwardArrow(true); l.setStyle(s); m.addGraphical(l); if (m.getLabel() != null) { Text t = new Text(xaal); t.setContents(m.getLabel()); t.addCoordinate(x-3, y-25); m.addGraphical(t); t.setStyle(m.getStyle()); } } else { System.out.println("Failed to draw marker"); } } public java.awt.Rectangle appendGraphicalInfo(Array a, Xaal xaal) { if (!a.isVisible()) return null; int x = a.getCoordinate().getX(); int y = a.getCoordinate().getY(); int lastX = x; int textWidth = 0; java.awt.Rectangle bounds = null; boolean isFrame = a.hasProperty("draw-frame")?(a.getProperty("draw-frame")!="false"):true; Graphics graphics = new BufferedImage(1,1,BufferedImage.TYPE_BYTE_GRAY).getGraphics(); for (int index = 0; index < a.getSize(); ++index) { Index ind = a.getIndex(index); Key item = (Key) ind.getContent(); Text text = new Text(xaal); text.addCoordinate(new Coordinate(xaal, lastX + 2 + NODEHEIGHT/2, y + (isFrame?40:15))); text.setContents(item.getValue()); text.setStyle(item.getStyle()); textWidth = graphics.getFontMetrics(item.getStyle().getFont().getAwtFont()).stringWidth(item.getValue()); Rectangle indRect = new Rectangle(xaal); indRect.setBounds(lastX, y + (isFrame?25:0), NODEHEIGHT + textWidth, StructureDraw.NODEHEIGHT); indRect.setStyle(ind.getStyle()); indRect.setRound(10, 10); a.addGraphical(indRect); a.addGraphical(text); if (bounds == null) { bounds = new java.awt.Rectangle(text.getBounds()); } else { bounds.add(text.getBounds()); } bounds.add(indRect.getBounds()); // set the position of the index for later use by possible pointers ind.setCoordinate(lastX, y+(isFrame?25:0)); // next draw the indices if (a.isIndexed()) { text = new Text(xaal); int indLength = graphics.getFontMetrics(ind.getStyle().getFont().getAwtFont()).stringWidth(ind.getLabel()); text.addCoordinate(new Coordinate(xaal, lastX + ((NODEHEIGHT + textWidth - indLength)/2), y + 80)); text.setContents(ind.getLabel()); text.setStyle(ind.getStyle()); a.addGraphical(text); bounds.add(text.getBounds()); } lastX += NODEHEIGHT + textWidth; } int height = 100; if (!a.isIndexed()) { height -= StructureDraw.NODEHEIGHT/2.0; } if (isFrame) { StructureDraw.drawFrame(x, y, lastX - x, height, a); } return bounds; } private static void drawFrame(int x, int y, int i, int height, DataStructure ds) { //Rectangle rect = new Rectangle(); } }