1 /* 2 * File : Scale.java 3 * Created : 15 d�c. 2003} 4 * By : Olivier 5 * 6 * Azureus - a Java Bittorrent client 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 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 General Public License for more details ( see the LICENSE file ). 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 */ 22 package org.gudy.azureus2.ui.swt.components.graphics; 23 24 import org.gudy.azureus2.core3.config.COConfigurationManager; 25 import org.gudy.azureus2.core3.config.ParameterListener; 26 27 /** 28 * @author Olivier 29 * 30 */ 31 public class Scale { 32 33 private static boolean wantBinary; 34 private static boolean useSI; 35 static{ COConfigurationManager.addAndFireParameterListeners( new String[]{ R, R, R, }, new ParameterListener() { public void parameterChanged(String name) { wantBinary = COConfigurationManager.getBooleanParameter(R); boolean wantSI = COConfigurationManager.getBooleanParameter(R); boolean forceSI = COConfigurationManager.getBooleanParameter(R); useSI = wantSI || forceSI; } })36 COConfigurationManager.addAndFireParameterListeners( 37 new String[]{ 38 "ui.scaled.graphics.binary.based", 39 "config.style.useSIUnits", 40 "config.style.forceSIValues", 41 }, 42 new ParameterListener() { 43 44 public void parameterChanged(String name) { 45 wantBinary = COConfigurationManager.getBooleanParameter("ui.scaled.graphics.binary.based"); 46 47 boolean wantSI = COConfigurationManager.getBooleanParameter("config.style.useSIUnits"); 48 boolean forceSI = COConfigurationManager.getBooleanParameter("config.style.forceSIValues"); 49 50 useSI = wantSI || forceSI; 51 } 52 }); 53 } 54 //The target number of pixels per scale level 55 private int pixelsPerLevel = 50; 56 57 //The max value 58 private int max = 1; 59 60 //The displayed number of levels 61 private int nbLevels; 62 63 //The computed (dislayed max) 64 private int displayedMax; 65 66 67 //The number of pixels 68 private int nbPixels = 1; 69 70 71 boolean isSIIECSensitive; 72 73 private int[] scaleValues = {}; 74 75 public Scale()76 Scale() 77 { 78 this( true ); 79 } 80 81 public Scale( boolean _isSIIECSensitive )82 Scale( 83 boolean _isSIIECSensitive ) 84 { 85 isSIIECSensitive = _isSIIECSensitive; 86 } 87 88 public boolean isSIIECSensitive()89 isSIIECSensitive() 90 { 91 return( isSIIECSensitive ); 92 } 93 setMax(int max)94 public void setMax(int max) { 95 this.max = max; 96 if(max < 1) 97 max = 1; 98 computeValues(); 99 } 100 getMax()101 public int getMax() { 102 return this.max; 103 } 104 setNbPixels(int nbPixels)105 public void setNbPixels(int nbPixels) { 106 this.nbPixels = nbPixels; 107 if(nbPixels < 1) 108 nbPixels = 1; 109 computeValues(); 110 } 111 computeValues()112 private void computeValues() { 113 int targetNbLevels = nbPixels / pixelsPerLevel; 114 if(targetNbLevels < 1) 115 targetNbLevels = 1; 116 double scaleFactor = max / targetNbLevels; 117 long powFactor = 1; 118 119 120 int scaleThing = wantBinary?2:10; 121 double scaleMax = wantBinary?4:5; 122 123 124 while(scaleFactor >= scaleThing) { 125 powFactor = scaleThing * powFactor; 126 scaleFactor = scaleFactor / scaleThing; 127 } 128 129 130 if(scaleFactor >= scaleMax){ 131 scaleFactor = scaleMax; 132 }else if(scaleFactor >= 2){ 133 scaleFactor = scaleMax/2; 134 }else{ 135 scaleFactor = 1; 136 } 137 138 long increment = (long)( scaleFactor * powFactor ); 139 140 if ( isSIIECSensitive ){ 141 142 /* 143 * Problem is that when using SI units we render 1000B as 1K, 2000B as 2K etc so we have to adjust 144 * the increment appropriately from a 1000 based value to a 1024 based value and vice-versa 145 */ 146 147 int divBy = 0; 148 int multBy = 0; 149 150 if ( useSI && !wantBinary ){ 151 152 divBy = 1000; 153 multBy = 1024; 154 155 }else if ( !useSI && wantBinary ){ 156 157 divBy = 1024; 158 multBy = 1000; 159 } 160 161 if ( divBy > 0 ){ 162 163 long temp = increment; 164 int pow = -1; 165 166 while( temp > 0 ){ 167 168 temp = temp / divBy; 169 pow++; 170 } 171 172 long temp2 = 1; 173 long temp3 = 1; 174 175 for ( int i=0;i<pow;i++){ 176 177 temp2 *= multBy; 178 temp3 *= divBy; 179 } 180 181 increment = (long)((((double)increment)/temp3)*temp2); 182 } 183 } 184 185 nbLevels = (int)(max / ( increment) + 1); 186 displayedMax = (int)( increment * nbLevels ); 187 188 189 int[] result = new int[nbLevels+1]; 190 for(int i = 0 ; i < nbLevels + 1 ; i++) { 191 result[i] = (int)( i * increment ); 192 } 193 194 scaleValues = result; 195 } 196 197 getScaleValues()198 public int[] getScaleValues() { 199 200 return( scaleValues ); 201 } 202 getScaledValue(int value)203 public int getScaledValue(int value) { 204 return(int)( ((long)value * nbPixels) / displayedMax ); 205 } 206 207 } 208