1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * 
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * The contents of this file are subject to the terms of either the Universal Permissive License
   7  * v 1.0 as shown at http://oss.oracle.com/licenses/upl
   8  *
   9  * or the following license:
  10  *
  11  * Redistribution and use in source and binary forms, with or without modification, are permitted
  12  * provided that the following conditions are met:
  13  * 
  14  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions
  15  * and the following disclaimer.
  16  * 
  17  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
  18  * conditions and the following disclaimer in the documentation and/or other materials provided with
  19  * the distribution.
  20  * 
  21  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to
  22  * endorse or promote products derived from this software without specific prior written permission.
  23  * 
  24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  26  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  31  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32  */
  33 package org.openjdk.jmc.greychart.testutil;
  34 
  35 import java.awt.event.MouseAdapter;
  36 import java.awt.event.MouseEvent;
  37 import java.util.ArrayList;
  38 import java.util.Iterator;
  39 import java.util.List;
  40 
  41 import org.openjdk.jmc.ui.common.xydata.DataSeries;
  42 
  43 import org.openjdk.jmc.greychart.DefaultMetadataProvider;
  44 import org.openjdk.jmc.greychart.GreyChartPanel;
  45 import org.openjdk.jmc.greychart.data.SeriesProviderSet;
  46 import org.openjdk.jmc.greychart.impl.DefaultPieGreyChart;
  47 import org.openjdk.jmc.greychart.util.ChartRenderingToolkit;
  48 
  49 /**
  50  * A simple class testing the pie chart.
  51  */
  52 public class PieChartTester {
  53         private static class MyMouseListener extends MouseAdapter {
  54                 /**
  55                  * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
  56                  */
  57                 @Override
  58                 public void mouseClicked(MouseEvent e) {
  59                         System.out.println("Adding value!"); //$NON-NLS-1$
  60                 }
  61 
  62                 /**
  63                  * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
  64                  */
  65                 @Override
  66                 public void mouseEntered(MouseEvent e) {
  67                         System.out.println("Entered graph!"); //$NON-NLS-1$
  68                 }
  69 
  70                 /**
  71                  * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
  72                  */
  73                 @Override
  74                 public void mouseExited(MouseEvent e) {
  75                         System.out.println("Exited graph!"); //$NON-NLS-1$
  76                 }
  77         }
  78 
  79         /**
  80          * Entry point.
  81          *
  82          * @param args
  83          */
  84         public static void main(String[] args) {
  85                 DefaultPieGreyChart<Number> graph = new DefaultPieGreyChart<>();
  86                 SeriesProviderSet<Number> seriesProvider = new SeriesProviderSet<>();
  87 
  88                 addNumberSeries(graph, seriesProvider, 40);
  89                 addNumberSeries(graph, seriesProvider, 60);
  90                 addNumberSeries(graph, seriesProvider, 80);
  91                 graph.setTitle("Test pie chart"); //$NON-NLS-1$
  92                 graph.setMetadataProvider(new DefaultMetadataProvider());
  93                 graph.setDataProvider(seriesProvider);
  94                 GreyChartPanel panel = new GreyChartPanel(graph);
  95                 panel.setName("default pie chart"); //$NON-NLS-1$
  96                 panel.addMouseListener(new MyMouseListener());
  97                 ChartRenderingToolkit.testComponent(panel, 320, 250);
  98         }
  99 
 100         private static void addNumberSeries(
 101                 DefaultPieGreyChart<Number> graph, SeriesProviderSet<Number> seriesProvider, final Number figure) {
 102                 DataSeries<Number> s = new DataSeries<Number>() {
 103                         @Override
 104                         public Iterator<Number> createIterator(long min, long max) {
 105                                 List<Number> number = new ArrayList<>();
 106                                 number.add(figure);
 107                                 return number.iterator();
 108                         }
 109                 };
 110                 seriesProvider.addDataSeries(s);
 111         }
 112 }