1 /*
2  * This file is part of ELKI:
3  * Environment for Developing KDD-Applications Supported by Index-Structures
4  *
5  * Copyright (C) 2018
6  * ELKI Development Team
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Affero General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Affero General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 package de.lmu.ifi.dbs.elki.visualization.batikutil;
22 
23 import java.awt.Component;
24 import java.awt.event.ComponentAdapter;
25 import java.awt.event.ComponentEvent;
26 
27 /**
28  * Class to lazily process canvas resize events by applying a threshold.
29  *
30  * @author Erich Schubert
31  * @since 0.2
32  */
33 public abstract class LazyCanvasResizer extends ComponentAdapter {
34   /**
35    * Default threshold for resizing.
36    */
37   public static final double DEFAULT_THRESHOLD = 0.05;
38 
39   /**
40    * Active threshold
41    */
42   double threshold;
43 
44   /**
45    * Last ratio of the Canvas applied
46    */
47   double activeRatio;
48 
49   /**
50    * Component the ratio applies to.
51    */
52   Component component;
53 
54   /**
55    * Full constructor.
56    *
57    * @param component Component to track
58    * @param threshold Threshold
59    */
LazyCanvasResizer(Component component, double threshold)60   public LazyCanvasResizer(Component component, double threshold) {
61     super();
62     this.threshold = threshold;
63     this.component = component;
64     this.activeRatio = getCurrentRatio();
65   }
66 
67   /**
68    * Simplified constructor using the default threshold {@link #DEFAULT_THRESHOLD}
69    *
70    * @param component Component to track.
71    */
LazyCanvasResizer(Component component)72   public LazyCanvasResizer(Component component) {
73     this(component, DEFAULT_THRESHOLD);
74   }
75 
76   /**
77    * React to a component resize event.
78    */
79   @Override
componentResized(ComponentEvent e)80   public void componentResized(ComponentEvent e) {
81     if (e.getComponent() == component) {
82       double newRatio = getCurrentRatio();
83       if (Math.abs(newRatio - activeRatio) > threshold) {
84         activeRatio = newRatio;
85         executeResize(newRatio);
86       }
87     }
88   }
89 
90   /**
91    * Get the components current ratio.
92    *
93    * @return Current ratio.
94    */
getCurrentRatio()95   public final double getCurrentRatio() {
96     return (double) component.getWidth() / (double) component.getHeight();
97   }
98 
99   /**
100    * Callback function that needs to be overridden with actual implementations.
101    *
102    * @param newratio New ratio to apply.
103    */
executeResize(double newratio)104   public abstract void executeResize(double newratio);
105 
106   /**
107    * Get the components last applied ratio.
108    *
109    * @return Last applied ratio.
110    */
getActiveRatio()111   public double getActiveRatio() {
112     return activeRatio;
113   }
114 }
115