PageRenderTime 42ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/edu/uncc/parsets/parsets/BasicRibbon.java

https://code.google.com/p/parsets/
Java | 242 lines | 153 code | 53 blank | 36 comment | 33 complexity | e2d8941be8d11178f7f35bc3866a6040 MD5 | raw file
  1. package edu.uncc.parsets.parsets;
  2. import java.awt.Graphics2D;
  3. import java.awt.Polygon;
  4. import edu.uncc.parsets.data.CategoryNode;
  5. import edu.uncc.parsets.util.AnimatableProperty;
  6. import edu.uncc.parsets.util.ColorBrewer;
  7. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
  8. * Copyright (c) 2009, Robert Kosara, Caroline Ziemkiewicz,
  9. * and others (see Authors.txt for full list)
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * * Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * * Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * * Neither the name of UNC Charlotte nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY ITS AUTHORS ''AS IS'' AND ANY
  25. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  26. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  28. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  29. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  30. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  31. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  33. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  35. public class BasicRibbon extends VisualConnection implements Comparable<BasicRibbon> {
  36. private CategoryBar upperBar, lowerBar;
  37. private AnimatableProperty upperOffset = new AnimatableProperty();
  38. private AnimatableProperty lowerOffset = new AnimatableProperty();
  39. public BasicRibbon(VisualConnection parent, CategoryNode categoryNode, CategoricalAxis upperAxis, CategoricalAxis lowerAxis) {
  40. super(parent);
  41. node = categoryNode;
  42. // upperOffset.setDontAnimate(true);
  43. // lowerOffset.setDontAnimate(true);
  44. this.upperBar = upperAxis.getCategoryBar(node.getParent().getToCategory());
  45. this.lowerBar = lowerAxis.getCategoryBar(node.getToCategory());
  46. lowerWidth.setValue(0);
  47. }
  48. public BasicRibbon(CategoryNode categoryNode, CategoricalAxis upperAxis, CategoricalAxis lowerAxis) {
  49. node = categoryNode;
  50. // upperOffset.setDontAnimate(true);
  51. // lowerOffset.setDontAnimate(true);
  52. this.upperBar = upperAxis.getCategoryBar(node.getParent().getToCategory());
  53. this.lowerBar = lowerAxis.getCategoryBar(node.getToCategory());
  54. lowerWidth.setValue(0);
  55. }
  56. public int compareTo(BasicRibbon o) {
  57. return node.compareTo(o.node);
  58. }
  59. @Override
  60. public boolean equals(Object o) {
  61. if (o instanceof BasicRibbon)
  62. return this.getNode().equals(((BasicRibbon) o).getNode());
  63. else
  64. return false;
  65. }
  66. @Override
  67. public int hashCode() {
  68. assert false : "hashCode not designed";
  69. return 42; // any arbitrary constant will do
  70. }
  71. public String toString() {
  72. return node.toString();
  73. }
  74. public String getTooltip(int filteredTotal) {
  75. return node.getTooltipText(filteredTotal);
  76. }
  77. public void layout(float parentWidth) {
  78. if (node == null || node.getCount() == 0) {
  79. width.setValue(0);
  80. } else {
  81. if(currentState == BarState.NORMAL){
  82. width.setValue(parentWidth * node.getRatio());
  83. upperOffset.setValue(upperBar.getBottomIndexPoint());
  84. upperBar.setBottomIndexPoint(upperOffset.getFutureValue() + width.getFutureValue());
  85. lowerOffset.setValue(lowerBar.getTopIndexPoint());
  86. lowerBar.setTopIndexPoint(lowerOffset.getFutureValue() + width.getFutureValue());
  87. lowerWidth.setValue(parentWidth* node.getRatio());
  88. }
  89. else if(currentState == BarState.OTHER){
  90. if(parent.getFutureLowerWidth() != 0){
  91. width.setValue(parent.getFutureLowerWidth() * node.getRatio());
  92. }
  93. else{
  94. width.setValue(upperBar.getFutureWidth() * node.getRatio());
  95. }
  96. upperOffset.setValue(upperBar.getBottomIndexPoint());
  97. upperBar.setBottomIndexPoint(upperOffset.getFutureValue() + width.getFutureValue());
  98. lowerOffset.setValue(lowerBar.getTopIndexPoint());
  99. lowerBar.setTopIndexPoint(lowerOffset.getFutureValue() + ((float)lowerBar.getFutureWidth()*((float)node.getCount()/(float)lowerBar.getFrequency())));
  100. lowerWidth.setValue((float)lowerBar.getFutureWidth()*((float)node.getCount()/(float)lowerBar.getFrequency()));
  101. }
  102. }
  103. }
  104. public void paint(Graphics2D g, float alpha) {
  105. if (width.getValue() == 0 || isSelected)
  106. return;
  107. ColorBrewer.setColor(colorBrewerIndex, false, alpha, g);
  108. if (width.getValue() >= 1) {
  109. int xPoints[] = new int[4];
  110. int yPoints[] = new int[4];
  111. if(currentState == BarState.NORMAL){
  112. xPoints[0] = upperBar.getLeftX() + (int)upperOffset.getValue();
  113. yPoints[0] = upperBar.getOutRibbonY();
  114. xPoints[1] = upperBar.getLeftX() + (int)upperOffset.getValue() + (int)Math.round(width.getValue());
  115. yPoints[1] = upperBar.getOutRibbonY();
  116. xPoints[2] = lowerBar.getLeftX() + (int)lowerOffset.getValue() + (int)Math.round(lowerWidth.getValue());
  117. yPoints[2] = lowerBar.getInRibbonY();
  118. xPoints[3] = lowerBar.getLeftX() + (int)lowerOffset.getValue();
  119. yPoints[3] = lowerBar.getInRibbonY();
  120. }
  121. else if(currentState == BarState.OTHER){
  122. xPoints[0] = upperBar.getLeftX() + (int)upperOffset.getValue();
  123. yPoints[0] = upperBar.getOutRibbonY();
  124. xPoints[1] = upperBar.getLeftX() + (int)upperOffset.getValue() + (int)Math.round(width.getValue());
  125. yPoints[1] = upperBar.getOutRibbonY();
  126. xPoints[2] = lowerBar.getLeftX() + (int)lowerOffset.getValue() + (int)Math.round((float)lowerBar.getWidth()*((float)node.getCount()/(float)lowerBar.getFrequency()));
  127. yPoints[2] = lowerBar.getInRibbonY();
  128. xPoints[3] = lowerBar.getLeftX() + (int)lowerOffset.getValue();
  129. yPoints[3] = lowerBar.getInRibbonY();
  130. }
  131. g.fillPolygon(xPoints, yPoints, 4);
  132. } else {
  133. g.drawLine(upperBar.getLeftX() + (int)upperOffset.getValue(), upperBar.getOutRibbonY(), lowerBar.getLeftX() + (int)lowerOffset.getValue() + (int)width.getValue(), lowerBar.getInRibbonY());
  134. }
  135. }
  136. public void paintSelected(Graphics2D g) {
  137. if (width.getValue() == 0)
  138. return;
  139. if (width.getValue() >= 1) {
  140. ColorBrewer.setColor(colorBrewerIndex, false, g);
  141. int xPoints[] = new int[4];
  142. int yPoints[] = new int[4];
  143. if(currentState == BarState.NORMAL){
  144. xPoints[0] = upperBar.getLeftX() + (int)upperOffset.getValue();
  145. yPoints[0] = upperBar.getOutRibbonY();
  146. xPoints[1] = upperBar.getLeftX() + (int)upperOffset.getValue() + (int)Math.round(width.getValue());
  147. yPoints[1] = upperBar.getOutRibbonY();
  148. xPoints[2] = lowerBar.getLeftX() + (int)lowerOffset.getValue() + (int)Math.round(width.getValue());
  149. yPoints[2] = lowerBar.getInRibbonY();
  150. xPoints[3] = lowerBar.getLeftX() + (int)lowerOffset.getValue();
  151. yPoints[3] = lowerBar.getInRibbonY();
  152. }
  153. else if(currentState == BarState.OTHER){
  154. xPoints[0] = upperBar.getLeftX() + (int)upperOffset.getValue();
  155. yPoints[0] = upperBar.getOutRibbonY();
  156. xPoints[1] = upperBar.getLeftX() + (int)upperOffset.getValue() + (int)Math.round(width.getValue());
  157. yPoints[1] = upperBar.getOutRibbonY();
  158. xPoints[2] = lowerBar.getLeftX() + (int)lowerOffset.getValue() + (int)Math.round((float)lowerBar.getWidth()*((float)node.getCount()/(float)lowerBar.getFrequency()));
  159. yPoints[2] = lowerBar.getInRibbonY();
  160. xPoints[3] = lowerBar.getLeftX() + (int)lowerOffset.getValue();
  161. yPoints[3] = lowerBar.getInRibbonY();
  162. }
  163. g.fillPolygon(xPoints, yPoints, 4);
  164. ColorBrewer.setColor(colorBrewerIndex, true, .8f, g);
  165. g.drawPolygon(xPoints, yPoints, 4);
  166. } else {
  167. ColorBrewer.setColor(colorBrewerIndex, true, g);
  168. g.drawLine(upperBar.getLeftX() + (int)upperOffset.getValue(), upperBar.getOutRibbonY(), lowerBar.getLeftX() + (int)lowerOffset.getValue() + (int)width.getValue(), lowerBar.getInRibbonY());
  169. }
  170. }
  171. /**
  172. * @param colorBrewerIndex the colorBrewerIndex to set
  173. */
  174. public void setColorBrewerIndex(int colorBrewerIndex) {
  175. this.colorBrewerIndex = colorBrewerIndex;
  176. }
  177. public boolean contains(int x, int y) {
  178. //test to see if the node has been filtered from the display
  179. if(!this.getNode().isVisible()){
  180. return false;
  181. }
  182. Polygon poly = new Polygon();
  183. poly.addPoint((int)(upperBar.getLeftX() + upperOffset.getValue()), (int)upperBar.getOutRibbonY());
  184. poly.addPoint((int)(upperBar.getLeftX() + upperOffset.getValue() + width.getValue()), (int)upperBar.getOutRibbonY());
  185. poly.addPoint((int)(lowerBar.getLeftX() + lowerOffset.getValue() + width.getValue()), (int)lowerBar.getInRibbonY());
  186. poly.addPoint((int)(lowerBar.getLeftX() + lowerOffset.getValue()), (int)lowerBar.getInRibbonY());
  187. return poly.contains(x, y);
  188. }
  189. }