|
| 1 | +/* |
| 2 | +Copyright 2008-2011 Gephi |
| 3 | +Authors : Eduardo Ramos <eduramiba@gmail.com> |
| 4 | +Website : http://www.gephi.org |
| 5 | +
|
| 6 | +This file is part of Gephi. |
| 7 | +
|
| 8 | +DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 9 | +
|
| 10 | +Copyright 2011 Gephi Consortium. All rights reserved. |
| 11 | +
|
| 12 | +The contents of this file are subject to the terms of either the GNU |
| 13 | +General Public License Version 3 only ("GPL") or the Common |
| 14 | +Development and Distribution License("CDDL") (collectively, the |
| 15 | +"License"). You may not use this file except in compliance with the |
| 16 | +License. You can obtain a copy of the License at |
| 17 | +http://gephi.org/about/legal/license-notice/ |
| 18 | +or /cddl-1.0.txt and /gpl-3.0.txt. See the License for the |
| 19 | +specific language governing permissions and limitations under the |
| 20 | +License. When distributing the software, include this License Header |
| 21 | +Notice in each file and include the License files at |
| 22 | +/cddl-1.0.txt and /gpl-3.0.txt. If applicable, add the following below the |
| 23 | +License Header, with the fields enclosed by brackets [] replaced by |
| 24 | +your own identifying information: |
| 25 | +"Portions Copyrighted [year] [name of copyright owner]" |
| 26 | +
|
| 27 | +If you wish your version of this file to be governed by only the CDDL |
| 28 | +or only the GPL Version 3, indicate your decision by adding |
| 29 | +"[Contributor] elects to include this software in this distribution |
| 30 | +under the [CDDL or GPL Version 3] license." If you do not indicate a |
| 31 | +single choice of license, a recipient has the option to distribute |
| 32 | +your version of this file under either the CDDL, the GPL Version 3 or |
| 33 | +to extend the choice of license to its licensees as provided above. |
| 34 | +However, if you add GPL Version 3 code and therefore, elected the GPL |
| 35 | +Version 3 license, then the option applies only if the new code is |
| 36 | +made subject to such option by the copyright holder. |
| 37 | +
|
| 38 | +Contributor(s): |
| 39 | +
|
| 40 | +Portions Copyrighted 2011 Gephi Consortium. |
| 41 | + */ |
| 42 | +package org.gephi.plugins.example.preview; |
| 43 | + |
| 44 | +import java.awt.Color; |
| 45 | +import org.gephi.preview.api.*; |
| 46 | +import org.gephi.preview.plugin.items.NodeItem; |
| 47 | +import org.gephi.preview.plugin.renderers.NodeRenderer; |
| 48 | +import org.gephi.preview.spi.Renderer; |
| 49 | +import org.gephi.preview.types.DependantColor; |
| 50 | +import org.openide.util.NbBundle; |
| 51 | +import org.openide.util.lookup.ServiceProvider; |
| 52 | +import processing.core.PGraphics; |
| 53 | + |
| 54 | +/** |
| 55 | + * Extends and replaces default nodes renderer and implements square shaped nodes. |
| 56 | + * <p> |
| 57 | + * By specifically extending a preview default renderer, this replaces it and the old one is no longer available. |
| 58 | + * A default renderer can be extended in order to add more features to it, or simply replace it completely. |
| 59 | + * Default renderers are contained in <code>org.gephi.preview.plugin.renderers</code> package. |
| 60 | + * <p> |
| 61 | + * Note that we need to set the position parameter even this replaces other renderer. |
| 62 | + * In this case and normally, the position has been set the same that the replaced renderer has (300). |
| 63 | + * @author Eduardo Ramos<eduramiba@gmail.com> |
| 64 | + */ |
| 65 | +@ServiceProvider(service = Renderer.class, position=300) |
| 66 | +public class SquareNodes extends NodeRenderer { |
| 67 | + |
| 68 | + @Override |
| 69 | + public String getDisplayName() { |
| 70 | + return NbBundle.getMessage(SquareNodes.class, "SquareNodes.name"); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void render(Item item, RenderTarget target, PreviewProperties properties) { |
| 75 | + if (properties.getBooleanValue("SquareNodes.property.enable")) { |
| 76 | + if (target instanceof ProcessingTarget) { |
| 77 | + renderSquaresProcessing(item, (ProcessingTarget) target, properties); |
| 78 | + } else if (target instanceof SVGTarget) { |
| 79 | + renderSquaresSVG(item, (SVGTarget) target, properties); |
| 80 | + } else if (target instanceof PDFTarget) { |
| 81 | + renderSquaresPDF(item, (PDFTarget) target, properties); |
| 82 | + } |
| 83 | + } else { |
| 84 | + super.render(item, target, properties); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public void renderSquaresProcessing(Item item, ProcessingTarget target, PreviewProperties properties) { |
| 89 | + //Params |
| 90 | + Float x = item.getData(NodeItem.X); |
| 91 | + Float y = item.getData(NodeItem.Y); |
| 92 | + Float size = item.getData(NodeItem.SIZE); |
| 93 | + Color color = item.getData(NodeItem.COLOR); |
| 94 | + Color borderColor = ((DependantColor) properties.getValue(PreviewProperty.NODE_BORDER_COLOR)).getColor(color); |
| 95 | + float borderSize = properties.getFloatValue(PreviewProperty.NODE_BORDER_WIDTH); |
| 96 | + int alpha = (int) ((properties.getFloatValue(PreviewProperty.NODE_OPACITY) / 100f) * 255f); |
| 97 | + if (alpha > 255) { |
| 98 | + alpha = 255; |
| 99 | + } |
| 100 | + |
| 101 | + //Graphics |
| 102 | + PGraphics graphics = target.getGraphics(); |
| 103 | + |
| 104 | + if (borderSize > 0) { |
| 105 | + graphics.stroke(borderColor.getRed(), borderColor.getGreen(), borderColor.getBlue(), alpha); |
| 106 | + graphics.strokeWeight(borderSize); |
| 107 | + } else { |
| 108 | + graphics.noStroke(); |
| 109 | + } |
| 110 | + graphics.fill(color.getRed(), color.getGreen(), color.getBlue(), alpha); |
| 111 | + graphics.rect(x, y, size, size); |
| 112 | + } |
| 113 | + |
| 114 | + public void renderSquaresPDF(Item item, PDFTarget target, PreviewProperties properties) { |
| 115 | + //TODO Not implemented |
| 116 | + } |
| 117 | + |
| 118 | + public void renderSquaresSVG(Item item, SVGTarget target, PreviewProperties properties) { |
| 119 | + //TODO Not implemented |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public PreviewProperty[] getProperties() { |
| 124 | + //Creates the same properties as the default renderer |
| 125 | + //but adds a new one to control square shaped nodes rendering |
| 126 | + PreviewProperty[] props = super.getProperties(); |
| 127 | + PreviewProperty[] newProps = new PreviewProperty[props.length + 1]; |
| 128 | + |
| 129 | + for (int i = 0; i < props.length; i++) { |
| 130 | + newProps[i] = props[i]; |
| 131 | + } |
| 132 | + |
| 133 | + newProps[newProps.length - 1] = PreviewProperty.createProperty(this, "SquareNodes.property.enable", Boolean.class, |
| 134 | + NbBundle.getMessage(SquareNodes.class, "SquareNodes.property.name"), |
| 135 | + NbBundle.getMessage(SquareNodes.class, "SquareNodes.property.description"), |
| 136 | + PreviewProperty.CATEGORY_NODES).setValue(false); |
| 137 | + return newProps; |
| 138 | + } |
| 139 | +} |
0 commit comments