1 package com.calipso.reportgenerator.userinterface.dinamicchart; 2 3 import com.calipso.reportgenerator.userinterface.*; 4 import com.calipso.reportgenerator.reportdefinitions.types.DimensionDefinitionLocationType; 5 import com.calipso.reportgenerator.common.*; 6 7 import javax.swing.*; 8 import java.awt.*; 9 import java.awt.event.*; 10 import java.util.*; 11 import java.util.List ; 12 13 import com.calipso.reportgenerator.common.InfoException; 14 15 22 23 public class ChartPivotTable extends JPanel implements ComponentListener, AdjustmentListener{ 24 25 private DropPanel pageDimensionsPanel; 26 private DropPanel columnDimensionsPanel; 27 private DropPanel rowDimensionsPanel; 28 private JPanel chartPanel; 29 private JSplitPane splitPane; 30 private ChartPivotTableManager manager; 31 private ReportResult reportResult; 32 private ChartPivotTableSelection selection; 33 private ChartMetricsSelection metricsSelection; 34 int reportHandle; 35 private JScrollPane pageDimensionsScrollPane, columnDimensionsScrollPane, rowDimensionsScrollPane; 36 private JScrollBar pageDimensionsScrollBar, columnDimensionsScrollBar, rowDimensionsScrollBar; 37 private IReportManager reportManager; 38 private JButton btChartCustomizer; 39 private IReportFrame owner; 40 41 public ChartPivotTable(ReportSpec reportSpec, IReportManager reportManager, Map params, String userId, String reportDefId, IReportFrame owFrame) throws HeadlessException, InfoException { 42 this.reportManager = reportManager; 43 owner = owFrame; 44 initialize(reportSpec, reportManager, params, userId, reportDefId); 45 initComponents(reportResult.getReportSpec().getMetrics()); 46 manager = ChartPivotTableManager.getManager(); 47 manager.setManagerComponents(this, reportResult, getPanelsMap(), reportManager, params, userId, reportHandle); 48 } 49 50 private void initialize(ReportSpec reportSpec, IReportManager reportManager, Map params, String userId, String reportDefId) throws InfoException { 51 this.reportManager = reportManager; 52 reportSpec = reportManager.getReportSpec(reportDefId); 53 if (reportSpec.getCached()) { 54 reportHandle = reportManager.PrepareReport(reportDefId); 55 } 56 else { 57 reportHandle = reportManager.PrepareReport(reportDefId, params); 58 } 59 ReportQuery query = reportManager.getDefaultReportQuery(reportHandle, userId); 60 query.setParamValues(params); 61 query.setVisibleTotals(false); 62 reportResult = reportManager.ExecReportQuery(reportHandle, query); 63 } 64 65 private HashMap getPanelsMap() { 66 HashMap targetPanels = new HashMap(); 67 targetPanels.put(DimensionDefinitionLocationType.ROW.toString(), rowDimensionsPanel); 68 targetPanels.put(DimensionDefinitionLocationType.PAGE.toString(), pageDimensionsPanel); 69 targetPanels.put(DimensionDefinitionLocationType.COLUMN.toString(), columnDimensionsPanel); 70 return targetPanels; 71 } 72 73 private void initComponents(Object [] metrics) { 74 setLayout(new BorderLayout()); 75 JPanel firstNorthPanel = new JPanel(new BorderLayout()); 76 pageDimensionsPanel = new DropPanel(DimensionDefinitionLocationType.PAGE.toString(), BoxLayout.X_AXIS); 77 pageDimensionsScrollPane = new JScrollPane(pageDimensionsPanel); 78 pageDimensionsScrollPane.setSize(new Dimension(0, 21)); 79 pageDimensionsScrollPane.setPreferredSize(new Dimension(0, 21)); 80 pageDimensionsScrollPane.setMinimumSize(new Dimension(0, 21)); 81 pageDimensionsScrollPane.setMaximumSize(new Dimension(0, 21)); 82 pageDimensionsScrollBar = new JScrollBar(JScrollBar.HORIZONTAL); 83 pageDimensionsScrollBar.addAdjustmentListener(this); 84 pageDimensionsScrollBar.setSize(new Dimension(30, 0)); 85 pageDimensionsScrollBar.setPreferredSize(new Dimension(30, 0)); 86 pageDimensionsScrollBar.setMinimumSize(new Dimension(30, 0)); 87 pageDimensionsScrollBar.setMaximumSize(new Dimension(30, 0)); 88 pageDimensionsScrollBar.setUnitIncrement(5); 89 pageDimensionsScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 90 pageDimensionsScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER); 91 firstNorthPanel.add(pageDimensionsScrollPane, BorderLayout.CENTER); 92 firstNorthPanel.add(pageDimensionsScrollBar, BorderLayout.EAST); 93 add(firstNorthPanel, BorderLayout.NORTH); 94 JPanel firstCenterPanel = new JPanel(new BorderLayout()); 95 JPanel secondNorthCenterPanel = new JPanel(new BorderLayout()); 96 columnDimensionsPanel = new DropPanel(DimensionDefinitionLocationType.COLUMN.toString(), BoxLayout.X_AXIS); 97 columnDimensionsScrollPane = new JScrollPane(columnDimensionsPanel); 98 columnDimensionsScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER); 99 columnDimensionsScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 100 columnDimensionsScrollPane.setSize(new Dimension(0, 21)); 101 columnDimensionsScrollPane.setPreferredSize(new Dimension(0, 21)); 102 columnDimensionsScrollPane.setMinimumSize(new Dimension(0, 21)); 103 columnDimensionsScrollPane.setMaximumSize(new Dimension(0, 21)); 104 columnDimensionsScrollBar = new JScrollBar(JScrollBar.HORIZONTAL); 105 columnDimensionsScrollBar.addAdjustmentListener(this); 106 columnDimensionsScrollBar.setSize(new Dimension(30, 0)); 107 columnDimensionsScrollBar.setPreferredSize(new Dimension(30, 0)); 108 columnDimensionsScrollBar.setMinimumSize(new Dimension(30, 0)); 109 columnDimensionsScrollBar.setMaximumSize(new Dimension(30, 0)); 110 columnDimensionsScrollBar.setUnitIncrement(5); 111 secondNorthCenterPanel.add(columnDimensionsScrollPane, BorderLayout.CENTER); 112 secondNorthCenterPanel.add(columnDimensionsScrollBar, BorderLayout.EAST); 113 chartPanel = new JPanel(new FlowLayout()); 114 java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); 115 screenSize.width = (new Double (screenSize.width * 0.80)).intValue(); 116 screenSize.height = (new Double (screenSize.height * 0.65)).intValue(); 117 chartPanel.setSize(screenSize); 118 119 JScrollPane chartScrollPane = new JScrollPane(chartPanel); 120 firstCenterPanel.add(secondNorthCenterPanel, BorderLayout.NORTH); 121 firstCenterPanel.add(chartScrollPane, BorderLayout.CENTER); 122 JPanel firstWestPanel = new JPanel(new BorderLayout()); 123 rowDimensionsPanel = new DropPanel(DimensionDefinitionLocationType.ROW.toString(), BoxLayout.Y_AXIS); 124 rowDimensionsScrollPane = new JScrollPane(rowDimensionsPanel); 125 rowDimensionsScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER); 126 rowDimensionsScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 127 rowDimensionsScrollBar = new JScrollBar(JScrollBar.VERTICAL); 128 rowDimensionsScrollBar.addAdjustmentListener(this); 129 rowDimensionsScrollBar.setSize(new Dimension(0, 30)); 130 rowDimensionsScrollBar.setPreferredSize(new Dimension(0, 30)); 131 rowDimensionsScrollBar.setMinimumSize(new Dimension(0, 30)); 132 rowDimensionsScrollBar.setMaximumSize(new Dimension(0, 30)); 133 rowDimensionsScrollBar.setUnitIncrement(5); 134 rowDimensionsScrollBar.addComponentListener(this); 135 firstWestPanel.add(rowDimensionsScrollPane, BorderLayout.CENTER); 136 firstWestPanel.add(rowDimensionsScrollBar, BorderLayout.SOUTH); 137 splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, firstWestPanel, firstCenterPanel); 138 splitPane.setOneTouchExpandable(false); 139 splitPane.setDividerSize(5); 140 splitPane.setDividerLocation(75); 141 add(firstNorthPanel, BorderLayout.NORTH); 142 add(splitPane, BorderLayout.CENTER); 143 JPanel southPanel = new JPanel(new BorderLayout()); 144 selection = new ChartPivotTableSelection(owner); 145 southPanel.add(selection, BorderLayout.EAST); 146 metricsSelection = new ChartMetricsSelection(metrics); 147 JScrollPane metricsPane = new JScrollPane(metricsSelection); 148 metricsPane.setSize(new Dimension(getPreferredSize().width, 50)); 149 metricsPane.setPreferredSize(new Dimension(getPreferredSize().width, 50)); 150 metricsPane.setMaximumSize(new Dimension(getPreferredSize().width, 50)); 151 metricsPane.setMinimumSize(new Dimension(getPreferredSize().width, 50)); 152 southPanel.add(metricsPane, BorderLayout.CENTER); 153 JLabel lbUseless = new JLabel(""); 154 lbUseless.setSize(new Dimension(splitPane.getDividerLocation(), 10)); 155 lbUseless.setPreferredSize(new Dimension(splitPane.getDividerLocation(), 10)); 156 lbUseless.setMinimumSize(new Dimension(splitPane.getDividerLocation(), 10)); 157 lbUseless.setMaximumSize(new Dimension(splitPane.getDividerLocation(), 10)); 158 add(southPanel, BorderLayout.SOUTH); 159 } 160 161 public void paintComponents(boolean init) throws InfoException { 162 pageDimensionsPanel.removeAll(); 163 rowDimensionsPanel.removeAll(); 164 columnDimensionsPanel.removeAll(); 165 chartPanel.removeAll(); 166 167 List rowDimensions = reportResult.getReportQuery().getRowDimensions(); 168 List pageDimensions = reportResult.getReportQuery().getPageDimensions(); 169 List columnDimensions = reportResult.getReportQuery().getColumnDimensions(); 170 171 System.out.println(); 172 for(Iterator iterator = rowDimensions.iterator() ; iterator.hasNext() ; ){ 173 QueryDimension dimension = (QueryDimension) iterator.next(); 174 String caption = reportResult.getDimensionFromName(dimension.getName()).getCaption(); 175 System.out.print(caption + " "); 176 if(init) { 177 rowDimensionsPanel.addDimension(dimension.getName(), caption, (HashSet)reportResult.getDimensionUnCheckedValues(dimension.getName()), getReportManager().getDimensionValues(reportHandle,dimension.getName())); 178 } else { 179 rowDimensionsPanel.addDimensionToPanel(dimension.getName()); 180 } 181 } 182 183 System.out.println(); 184 for(Iterator iterator = pageDimensions.iterator() ; iterator.hasNext() ; ){ 185 QueryDimension dimension = (QueryDimension) iterator.next(); 186 String caption = reportResult.getDimensionFromName(dimension.getName()).getCaption(); 187 System.out.print(caption + " "); 188 if(init) { 189 pageDimensionsPanel.addDimension(dimension.getName(), caption, (HashSet)reportResult.getDimensionUnCheckedValues(dimension.getName()), getReportManager().getDimensionValues(reportHandle,dimension.getName())); 190 } else { 191 pageDimensionsPanel.addDimensionToPanel(dimension.getName()); 192 } 193 } 194 195 System.out.println(); 196 for(Iterator iterator = columnDimensions.iterator() ; iterator.hasNext() ; ){ 197 QueryDimension dimension = (QueryDimension) iterator.next(); 198 String caption = reportResult.getDimensionFromName(dimension.getName()).getCaption(); 199 System.out.print(caption + " "); 200 if(init) { 201 columnDimensionsPanel.addDimension(dimension.getName(), caption, (HashSet)reportResult.getDimensionUnCheckedValues(dimension.getName()), getReportManager().getDimensionValues(reportHandle,dimension.getName())); 202 } else { 203 columnDimensionsPanel.addDimensionToPanel(dimension.getName()); 204 } 205 } 206 generateChart(0, false); 207 } 208 209 public void repaintComponents() { 210 flushComponents(); 211 Vector rowDimensionsVector = rowDimensionsPanel.getDimensions(); 212 fillComponent(rowDimensionsVector, rowDimensionsPanel); 213 Vector colDimensionsVector = columnDimensionsPanel.getDimensions(); 214 fillComponent(colDimensionsVector, columnDimensionsPanel); 215 Vector pageDimensionsVector = pageDimensionsPanel.getDimensions(); 216 fillComponent(pageDimensionsVector, pageDimensionsPanel); 217 generateChart(selection.getCurrentChartType(), selection.getCurrentValue()); 218 validate(); 219 } 220 221 private void flushComponents() { 222 pageDimensionsPanel.removeAll(); 223 rowDimensionsPanel.removeAll(); 224 columnDimensionsPanel.removeAll(); 225 chartPanel.removeAll(); 226 validate(); 227 } 228 229 private void fillComponent(Vector vector, DropPanel panel) { 230 for(int i = 0 ; i < vector.size() ; i++) { 231 panel.addDimensionToPanel(((SourceDimension)vector.elementAt(i)).getName()); 232 } 233 pageDimensionsPanel.validate(); 234 rowDimensionsPanel.validate(); 235 columnDimensionsPanel.validate(); 236 } 237 238 public void generateChart(int type, boolean byRow) { 239 chartPanel.removeAll(); 240 DinamicChartData chartData = new DinamicChartData(); 241 chartData.updateDinamicChartData(reportResult); 242 Dimension size = new Dimension(chartPanel.getSize()); 243 size.height = (new Double (size.height * 0.80)).intValue(); 244 boolean legend = getLegendValue(); 245 boolean toolTips = getToolTipsValue(); 246 boolean multipleAxis = getMultipleAxis(); 247 switch(type) { 248 case 0: 249 Component component = new VerticalBarChart3D(chartData.getCategoryDataset(byRow), reportResult.getReportSpec().getTitle(), Color.white, legend, toolTips,size, multipleAxis); 250 chartPanel.add(component); 251 break; 252 case 1: 253 chartPanel.add(new HorizontalBarChart(chartData.getCategoryDataset(byRow), reportResult.getReportSpec().getTitle(), Color.white, legend, toolTips,size, multipleAxis)); 254 break; 255 case 2: 256 chartPanel.add(new HorizontalBarChart3D(chartData.getCategoryDataset(byRow), reportResult.getReportSpec().getTitle(), Color.white, legend, toolTips,size, multipleAxis)); 257 break; 258 case 3: 259 chartPanel.add(new LineChart(chartData.getCategoryDataset(byRow), reportResult.getReportSpec().getTitle(), Color.white, legend, toolTips,size, multipleAxis)); 260 break; 261 case 4: 262 chartPanel.add(new VerticalBarChart(chartData.getCategoryDataset(byRow), reportResult.getReportSpec().getTitle(), Color.white, legend, toolTips,size, multipleAxis)); 263 break; 264 case 5: 265 chartPanel.add(new AreaChart(chartData.getCategoryDataset(byRow), reportResult.getReportSpec().getTitle(), Color.white, legend, toolTips,size, multipleAxis)); 266 break; 267 } 268 validate(); 269 } 270 271 private boolean getMultipleAxis() { 272 if (selection.getChartCustomizer()!=null){ 273 return selection.getChartCustomizer().isMultipleAxis(); 274 }else{ 275 return false; 276 } 277 } 278 279 private boolean getToolTipsValue() { 280 if (selection.getChartCustomizer()!=null){ 281 return selection.getChartCustomizer().isTooltips(); 282 }else{ 283 return true; 284 } 285 } 286 287 private boolean getLegendValue() { 288 if (selection.getChartCustomizer()!=null){ 289 return selection.getChartCustomizer().isLegend(); 290 }else{ 291 return true; 292 } 293 } 294 295 public void setReportResult(ReportResult reportResult) { 296 this.reportResult = reportResult; 297 } 298 299 public void componentResized(ComponentEvent e) { 300 if(e.getSource() == rowDimensionsScrollBar) { 301 Vector rowDimensions = rowDimensionsPanel.getDimensions(); 302 for(int i = 0 ; i < rowDimensions.size() ; i++){ 303 SourceDimension current = (SourceDimension) rowDimensions.elementAt(i); 304 current.setSize(new Dimension(rowDimensionsScrollBar.getSize().width -4, 15)); 305 current.setPreferredSize(new Dimension(rowDimensionsScrollBar.getSize().width -4, 15)); 306 current.setMinimumSize(new Dimension(rowDimensionsScrollBar.getSize().width -4, 15)); 307 current.setMaximumSize(new Dimension(rowDimensionsScrollBar.getSize().width -4, 15)); 308 } 309 validate(); 310 } 311 } 312 313 public void componentMoved(ComponentEvent e) { 314 315 } 316 317 public void componentShown(ComponentEvent e) { 318 319 } 320 321 public void componentHidden(ComponentEvent e) { 322 323 } 324 325 public void adjustmentValueChanged(AdjustmentEvent e) { 326 if(e.getSource() == columnDimensionsScrollBar) { 327 columnDimensionsScrollPane.getHorizontalScrollBar().setValue(e.getValue()); 328 return; 329 } 330 if(e.getSource() == rowDimensionsScrollBar) { 331 rowDimensionsScrollPane.getVerticalScrollBar().setValue(e.getValue()); 332 return; 333 } 334 if(e.getSource() == pageDimensionsScrollBar) { 335 pageDimensionsScrollPane.getHorizontalScrollBar().setValue(e.getValue()); 336 return; 337 } 338 } 339 340 public IReportManager getReportManager() { 341 return reportManager; 342 } 343 } 344 | Popular Tags |