1 33 34 package com.icesoft.faces.component.outputchart; 35 36 import com.icesoft.faces.component.CSS_DEFAULT; 37 import com.icesoft.faces.component.ext.HtmlCommandButton; 38 import com.icesoft.faces.component.ext.renderkit.FormRenderer; 39 import com.icesoft.faces.component.ext.taglib.Util; 40 import com.icesoft.faces.context.DOMContext; 41 import com.icesoft.faces.context.DOMResponseWriter; 42 import org.krysalis.jcharts.Chart; 43 import org.krysalis.jcharts.imageMap.ImageMapArea; 44 import org.w3c.dom.Element ; 45 import org.w3c.dom.Text ; 46 47 import javax.faces.component.UIComponent; 48 import javax.faces.context.ExternalContext; 49 import javax.faces.context.FacesContext; 50 import javax.faces.el.MethodBinding; 51 import javax.faces.el.ValueBinding; 52 import javax.faces.event.AbortProcessingException; 53 import javax.faces.event.ActionEvent; 54 import javax.faces.event.FacesEvent; 55 import javax.faces.event.ActionListener; 56 import javax.servlet.ServletContext ; 57 import javax.servlet.http.HttpServletRequest ; 58 59 import java.beans.Beans ; 60 import java.io.File ; 61 import java.io.FileNotFoundException ; 62 import java.io.FileOutputStream ; 63 import java.io.IOException ; 64 import java.io.OutputStream ; 65 import java.io.Serializable ; 66 import java.util.Iterator ; 67 import java.util.Map ; 68 69 public class OutputChart extends HtmlCommandButton implements Serializable { 70 71 public static String AREA_CHART_TYPE = "area"; 72 public static String AREA_STACKED_CHART_TYPE = "areastacked"; 73 public static String BAR_CHART_TYPE = "bar"; 74 public static String BAR_CLUSTERED_CHART_TYPE = "barclustered"; 75 public static String BAR_STACKED_CHART_TYPE = "barstacked"; 76 public static String LINE_CHART_TYPE = "line"; 77 public static String PIE2D_CHART_TYPE = "pie2D"; 78 public static String PIE3D_CHART_TYPE = "pie3D"; 79 public static String POINT_CHART_TYPE = "point"; 80 public static String STOCK_CHART_TYPE = "stock"; 81 public static String CUSTOM_CHART_TYPE = "custom"; 82 public static String DEFAULT_CHART_TYPE = BAR_CHART_TYPE; 83 84 private static int COMPONENT_ID = 0; 85 private static int CLIENT_SIDE_IMAGE_MAP_KEY = 1; 86 private static String DEFAULT_HEIGHT = "400"; 87 private static String DEFAULT_WIDTH = "400"; 88 89 private static String DEFAULT_CHART_TITLE = "Default Chart title"; 90 private static String DEFAULT_YAXIS_TITLE = "Default Y title"; 91 private static String DEFAULT_XAXIS_TITLE = "Default X title"; 92 private static String DEFAULT_DATA = "20, 30, 40"; 93 static String ICE_CHART_COMPONENT = "iceChartComponent"; 94 private int imageCounter = 0; 95 private AbstractChart abstractChart; 96 private String width; 97 private String height; 98 99 private boolean render = false; 100 private FileOutputStream out; 101 102 private String chartTitle; 103 private Object data; 104 private Object labels; 105 private Object colors; 106 private Object shapes; 107 108 private Object xaxisLabels; 109 private String xaxisTitle; 110 private String yaxisTitle; 111 private String style = null; 112 private String styleClass = null; 113 private Object legendPlacement; 114 private Object legendColumns; 115 private boolean horizontal; 116 private boolean horizontalSet; 117 File folder = null; 118 119 public OutputChart() { 120 setRendererType("com.icesoft.faces.OutputChartRenderer"); 121 } 122 123 124 127 public Object getLabels() { 128 if (labels != null) { 129 return labels; 130 } 131 ValueBinding vb = getValueBinding("labels"); 132 return vb != null ? vb.getValue(getFacesContext()) : null; 133 } 134 135 138 public void setLabels(Object labels) { 139 this.labels = labels; 140 } 141 142 146 public boolean isClientSideImageMap() { 147 if (hasActionListener() && 148 (!getType().equalsIgnoreCase(AREA_CHART_TYPE) && 149 !getType().equalsIgnoreCase(AREA_STACKED_CHART_TYPE))) { 150 return true; 151 } else { 152 return false; 153 } 154 } 155 156 protected boolean hasActionListener() { 157 MethodBinding actionListener = getActionListener(); 158 if( actionListener != null ) { 159 return true; 160 } 161 ActionListener[] actionListeners = getActionListeners(); 162 if( actionListeners != null && actionListeners.length > 0 ) { 163 return true; 164 } 165 return false; 166 } 167 168 171 public Object getData() { 172 if (data != null) { 173 return data; 174 } 175 ValueBinding vb = getValueBinding("data"); 176 if (vb != null) { 177 return vb.getValue(getFacesContext()); 178 } else { 179 180 if(!Beans.isDesignTime()){ 181 setChartTitle(getChartTitle() + " with default data"); 182 } 183 return DEFAULT_DATA; 184 } 185 } 186 187 190 public void setData(Object data) { 191 this.data = data; 192 } 193 194 197 public Object getColors() { 198 if (colors != null) { 199 return colors; 200 } 201 ValueBinding vb = getValueBinding("colors"); 202 return vb != null ? vb.getValue(getFacesContext()) : null; 203 } 204 205 208 public void setColors(Object colors) { 209 this.colors = colors; 210 } 211 212 215 public Object getShapes() { 216 if (shapes != null) { 217 return shapes; 218 } 219 ValueBinding vb = getValueBinding("shapes"); 220 return vb != null ? vb.getValue(getFacesContext()) : null; 221 } 222 223 226 public void setShapes(Object shapes) { 227 this.shapes = shapes; 228 } 229 230 233 public String getXaxisTitle() { 234 if (xaxisTitle != null) { 235 return xaxisTitle; 236 } 237 ValueBinding vb = getValueBinding("xaxisTitle"); 238 return vb != null ? (String ) vb.getValue(getFacesContext()) : 239 DEFAULT_XAXIS_TITLE; 240 } 241 242 245 public void setXaxisTitle(String xaxisTitle) { 246 this.xaxisTitle = xaxisTitle; 247 } 248 249 252 public String getYaxisTitle() { 253 if (yaxisTitle != null) { 254 return yaxisTitle; 255 } 256 ValueBinding vb = getValueBinding("yaxisTitle"); 257 return vb != null ? (String ) vb.getValue(getFacesContext()) : 258 DEFAULT_YAXIS_TITLE; 259 } 260 261 264 public void setYaxisTitle(String yaxisTitle) { 265 this.yaxisTitle = yaxisTitle; 266 } 267 268 271 public Object getXaxisLabels() { 272 if (xaxisLabels != null) { 273 return xaxisLabels; 274 } 275 ValueBinding vb = getValueBinding("xaxisLabels"); 276 return vb != null ? vb.getValue(getFacesContext()) : null; 277 } 278 279 282 public void setXaxisLabels(Object xaxisLabels) { 283 this.xaxisLabels = xaxisLabels; 284 } 285 286 289 public String getChartTitle() { 290 if (chartTitle != null) { 291 return chartTitle; 292 } 293 ValueBinding vb = getValueBinding("chartTitle"); 294 return vb != null ? (String ) vb.getValue(getFacesContext()) : 295 DEFAULT_CHART_TITLE; 296 } 297 298 301 public void setChartTitle(String chartTitle) { 302 this.chartTitle = chartTitle; 303 } 304 305 308 public String getWidth() { 309 if (width != null) { 310 return width; 311 } 312 ValueBinding vb = getValueBinding("width"); 313 return vb != null ? (String ) vb.getValue(getFacesContext()) : 314 DEFAULT_WIDTH; 315 } 316 317 320 public void setWidth(String width) { 321 this.width = width; 322 } 323 324 327 public String getHeight() { 328 if (height != null) { 329 return height; 330 } 331 ValueBinding vb = getValueBinding("height"); 332 return vb != null ? (String ) vb.getValue(getFacesContext()) : 333 DEFAULT_HEIGHT; 334 } 335 336 339 public void setHeight(String height) { 340 this.height = height; 341 } 342 343 private MethodBinding renderOnSubmitMethodBinding; 344 345 public void setRenderOnSubmit(MethodBinding renderOnSubmit) { 346 renderOnSubmitMethodBinding = renderOnSubmit; 347 } 348 349 public MethodBinding getRenderOnSubmit() { 350 return renderOnSubmitMethodBinding; 351 } 352 353 public Boolean evaluateRenderOnSubmit(FacesContext context) { 354 if (renderOnSubmitMethodBinding != null) { 355 Boolean b = (Boolean ) renderOnSubmitMethodBinding.invoke( 356 context, new Object []{this}); 357 return b; 358 } 359 return Boolean.FALSE; 360 } 361 362 366 public void encodeBegin(FacesContext context) throws IOException { 367 368 if(!Beans.isDesignTime()){ 369 try { 370 if (abstractChart == null) { 371 abstractChart = AbstractChart.createChart(this); 372 if (getType().equalsIgnoreCase(OutputChart.CUSTOM_CHART_TYPE)) { 373 evaluateRenderOnSubmit(context); 374 } 375 abstractChart.encode(); 376 } else if (evaluateRenderOnSubmit(context).booleanValue()) { 377 abstractChart.encode(); 378 } 379 } catch (Throwable e) { 380 e.printStackTrace(); 381 } 382 } 383 super.encodeBegin(context); 384 } 385 386 390 public void broadcast(FacesEvent event) 391 throws AbortProcessingException { 392 super.broadcast(event); 393 } 394 395 399 public void decode(FacesContext context) { 400 401 Map requestParameterMap = 402 context.getExternalContext().getRequestParameterMap(); 403 String chartComponentRequestIdentifier = (String ) requestParameterMap 404 .get(OutputChart.ICE_CHART_COMPONENT); 405 if (chartComponentRequestIdentifier != null) { 406 String [] submittedValue = 407 chartComponentRequestIdentifier.split("id-key"); 408 if (submittedValue[COMPONENT_ID].equals(getClientId(context))) { 409 ImageMapArea area = (ImageMapArea) getGeneratedImageMapArea() 410 .get(submittedValue[CLIENT_SIDE_IMAGE_MAP_KEY]); 411 if (area != null) { 412 setClickedImageMapArea(area); 413 queueEvent(new ActionEvent(this)); 414 } 415 } 416 } 417 super.decode(context); 418 } 419 420 private String getChartFileName() { 421 HttpServletRequest req = 422 (HttpServletRequest ) ((ExternalContext) FacesContext 423 .getCurrentInstance().getExternalContext()) 424 .getRequest(); 425 return this.getType() + this 426 .getClientId(FacesContext.getCurrentInstance()) 427 .replaceAll(":", "") + req.getRequestedSessionId() + 428 imageCounter++ + ".jpeg"; 429 } 430 431 434 public String getPath() { 435 String folder = "/"; 436 if (getFacesContext() != null && 437 getFacesContext().getExternalContext() != null) { 438 if (DOMResponseWriter.isStreamWriting()) { 439 folder = "./web/chart/images"; 440 } else { 441 folder = 442 ((ServletContext ) getFacesContext().getExternalContext() 443 .getContext()).getRealPath("/charts"); 444 } 445 } 446 return folder; 447 } 448 451 public Chart getChart() { 452 return abstractChart.getChart(); 453 } 454 455 458 public void setChart(Chart chart) { 459 abstractChart.setChart(chart); 460 } 461 462 public void render() { 463 render = true; 464 } 465 466 public boolean isRender() { 467 return render; 468 } 469 470 private String type = null; 471 472 475 public void setType(String type) { 476 this.type = type; 477 } 478 479 482 public String getType() { 483 if (type != null) { 484 return type; 485 } 486 ValueBinding vb = getValueBinding("type"); 487 return vb != null ? (String ) vb.getValue(getFacesContext()) : 488 DEFAULT_CHART_TYPE; 489 } 490 491 private String fileName = null; 492 493 496 public String getFileName() { 497 return fileName; 498 } 499 500 private File imageFile; 501 502 505 OutputStream getNewOutputStream() { 506 if (imageFile != null) { 508 imageFile.delete(); 509 } 510 imageFile = new File (getFolder(), getChartFileName()); 511 fileName = imageFile.getName(); 512 try { 513 return out = new FileOutputStream (imageFile); 514 } catch (FileNotFoundException e) { 515 e.printStackTrace(); 517 } 518 return null; 519 } 520 521 File getFolder() { 522 523 if(Beans.isDesignTime()){ 524 folder = new File (getPath()); 525 return folder; 526 } 527 528 if (folder == null) { 529 folder = new File (getPath()); 530 if (folder != null) { 531 if (!folder.exists()) { 532 folder.mkdirs(); 533 } 534 } 535 } 536 return folder; 537 } 538 539 542 public OutputStream getOutputStream() { 543 return out; 544 } 545 546 Map getGeneratedImageMapArea() { 547 return abstractChart.getGeneratedImageMapArea(); 548 } 549 550 553 public ImageMapArea getClickedImageMapArea() { 554 return abstractChart.getClickedImageMapArea(); 555 } 556 557 public void setClickedImageMapArea(ImageMapArea clickedImageMapArea) { 558 abstractChart.setClickedImageMapArea(clickedImageMapArea); 559 } 560 561 void generateClientSideImageMap(DOMContext domContext, Element map) { 562 if (isClientSideImageMap()) { 563 Iterator area = getGeneratedImageMapArea().values().iterator(); 564 while (area.hasNext()) { 565 ImageMapArea areaMap = (ImageMapArea) area.next(); 566 Text areaNode = domContext.createTextNode(areaMap.toHTML( 567 "title ='" + areaMap.getLengendLabel() + 568 "' HREF=\"return false;\" onclick=\"document.forms['" + 569 getParentFormId() + "']['" + ICE_CHART_COMPONENT + 570 "'].value='" + getClientId(getFacesContext()) + 571 "id-key" + areaMap.hashCode() + 572 "';iceSubmitPartial(document.forms['" + 573 getParentFormId() + "'],this,event); return false;\"")); 574 map.appendChild(areaNode); 575 } 576 } else { 577 } 579 } 580 581 private String parentFormId; 583 584 private String getParentFormId() { 585 if (parentFormId != null) { 586 return parentFormId; 587 } 588 UIComponent uiForm = FormRenderer.findForm(this); 589 if (uiForm == null) { 590 return null; 593 } 594 return (parentFormId = uiForm.getClientId(getFacesContext())); 595 } 596 597 600 public void setStyleClass(String styleClass) { 601 this.styleClass = styleClass; 602 } 603 604 607 public String getStyleClass() { 608 return Util.getQualifiedStyleClass(this, 609 styleClass, 610 CSS_DEFAULT.OUTPUT_CHART_DEFAULT_STYLE_CLASS, 611 "styleClass"); 612 } 613 614 617 public void setStyle(String style) { 618 this.style = style; 619 } 620 621 624 public String getStyle() { 625 if (style != null) { 626 return style; 627 } 628 ValueBinding vb = getValueBinding("style"); 629 return vb != null ? (String ) vb.getValue(getFacesContext()) : null; 630 } 631 632 635 public Object getLegendPlacement() { 636 if (legendPlacement != null) { 637 return legendPlacement; 638 } 639 ValueBinding vb = getValueBinding("legendPlacement"); 640 return vb != null ? vb.getValue(getFacesContext()) : "bottom"; 641 } 642 643 646 public void setLegendPlacement(Object legendPlacement) { 647 this.legendPlacement = legendPlacement; 648 } 649 650 653 public Object getLegendColumns() { 654 if (legendColumns != null) { 655 return legendColumns; 656 } 657 ValueBinding vb = getValueBinding("legendColumns"); 658 return vb != null ? vb.getValue(getFacesContext()) : "0"; 659 } 660 661 664 public void setLegendColumns(Object legendColumns) { 665 this.legendColumns = legendColumns; 666 } 667 668 669 public boolean isHorizontal() { 670 if (this.horizontalSet) { 671 return (this.horizontal); 672 } 673 ValueBinding vb = getValueBinding("horizontal"); 674 if (vb != null) { 675 return (Boolean.TRUE.equals(vb.getValue(getFacesContext()))); 676 } else { 677 return (this.horizontal); 678 } 679 } 680 681 682 public void setHorizontal(boolean horizontal) { 683 if (horizontal != this.horizontal) { 684 this.horizontal = horizontal; 685 } 686 this.horizontalSet = true; 687 } 688 689 693 public Object saveState(FacesContext context) { 694 Object values[] = new Object [22]; 695 values[0] = super.saveState(context); 696 values[1] = new Integer (imageCounter); 697 values[2] = abstractChart; 698 values[3] = width; 699 values[4] = height; 700 values[5] = render ? Boolean.TRUE : Boolean.FALSE; 701 values[6] = chartTitle; 702 values[7] = data; 703 values[8] = labels; 704 values[9] = colors; 705 values[10] = shapes; 706 values[11] = xaxisLabels; 707 values[12] = xaxisTitle; 708 values[13] = yaxisTitle; 709 values[14] = style; 710 values[15] = styleClass; 711 values[16] = legendPlacement; 712 values[17] = legendColumns; 713 values[18] = horizontal ? Boolean.TRUE : Boolean.FALSE; 714 values[19] = horizontalSet ? Boolean.TRUE : Boolean.FALSE; 715 values[20] = type; 716 values[21] = saveAttachedState(context, renderOnSubmitMethodBinding); 717 return ((Object ) (values)); 718 } 719 720 724 public void restoreState(FacesContext context, Object state) { 725 Object values[] = (Object []) state; 726 super.restoreState(context, values[0]); 727 imageCounter = ((Integer ) values[1]).intValue(); 728 abstractChart = (AbstractChart) values[2]; 729 width = (String ) values[3]; 730 height = (String ) values[4]; 731 render = ((Boolean ) values[5]).booleanValue(); 732 chartTitle = (String ) values[6]; 733 data = values[7]; 734 labels = values[8]; 735 colors = values[9]; 736 shapes = values[10]; 737 xaxisLabels = values[11]; 738 xaxisTitle = (String ) values[12]; 739 yaxisTitle = (String ) values[13]; 740 style = (String ) values[14]; 741 styleClass = (String ) values[15]; 742 legendPlacement = values[16]; 743 legendColumns = values[17]; 744 horizontal = ((Boolean ) values[18]).booleanValue(); 745 horizontalSet = ((Boolean ) values[19]).booleanValue(); 746 type = (String ) values[20]; 747 renderOnSubmitMethodBinding = (MethodBinding) restoreAttachedState(context, values[21]); 748 } 749 } 750 751 | Popular Tags |