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