RangeSliderModel.java revision 1472:c18cbe5936b8
1/* 2 * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25package com.sun.hotspot.igv.util; 26 27import com.sun.hotspot.igv.data.ChangedEventProvider; 28import com.sun.hotspot.igv.data.ChangedEvent; 29import java.awt.Color; 30import java.util.ArrayList; 31import java.util.Collections; 32import java.util.List; 33 34/** 35 * 36 * @author Thomas Wuerthinger 37 */ 38public class RangeSliderModel implements ChangedEventProvider<RangeSliderModel> { 39 40 // Warning: Update setData method if fields are added 41 private ChangedEvent<RangeSliderModel> changedEvent; 42 private ChangedEvent<RangeSliderModel> colorChangedEvent; 43 private List<String> positions; 44 private int firstPosition; 45 private int secondPosition; 46 private List<Color> colors; 47 48 public void setData(RangeSliderModel model) { 49 boolean changed = false; 50 changed |= (positions != model.positions); 51 positions = model.positions; 52 changed |= (firstPosition != model.firstPosition); 53 firstPosition = model.firstPosition; 54 changed |= (secondPosition != model.secondPosition); 55 secondPosition = model.secondPosition; 56 boolean colorChanged = (colors != model.colors); 57 colors = model.colors; 58 if (changed) { 59 changedEvent.fire(); 60 } 61 if (colorChanged) { 62 colorChangedEvent.fire(); 63 } 64 } 65 66 public RangeSliderModel(List<String> positions) { 67 assert positions.size() > 0; 68 this.changedEvent = new ChangedEvent<RangeSliderModel>(this); 69 this.colorChangedEvent = new ChangedEvent<RangeSliderModel>(this); 70 setPositions(positions); 71 } 72 73 protected void setPositions(List<String> positions) { 74 this.positions = positions; 75 colors = new ArrayList<Color>(); 76 for (int i = 0; i < positions.size(); i++) { 77 colors.add(Color.black); 78 } 79 changedEvent.fire(); 80 colorChangedEvent.fire(); 81 } 82 83 public void setColors(List<Color> colors) { 84 this.colors = colors; 85 colorChangedEvent.fire(); 86 } 87 88 public List<Color> getColors() { 89 return colors; 90 } 91 92 public RangeSliderModel copy() { 93 RangeSliderModel newModel = new RangeSliderModel(positions); 94 newModel.firstPosition = firstPosition; 95 newModel.secondPosition = secondPosition; 96 newModel.colors = colors; 97 return newModel; 98 } 99 100 public List<String> getPositions() { 101 return Collections.unmodifiableList(positions); 102 } 103 104 public int getFirstPosition() { 105 return firstPosition; 106 } 107 108 public int getSecondPosition() { 109 return secondPosition; 110 } 111 112 public void setPositions(int fp, int sp) { 113 assert fp >= 0 && fp < positions.size(); 114 assert sp >= 0 && sp < positions.size(); 115 firstPosition = fp; 116 secondPosition = sp; 117 ensureOrder(); 118 changedEvent.fire(); 119 } 120 121 private void ensureOrder() { 122 if (secondPosition < firstPosition) { 123 int tmp = secondPosition; 124 secondPosition = firstPosition; 125 firstPosition = tmp; 126 } 127 } 128 129 public ChangedEvent<RangeSliderModel> getColorChangedEvent() { 130 return colorChangedEvent; 131 } 132 133 public ChangedEvent<RangeSliderModel> getChangedEvent() { 134 return changedEvent; 135 } 136} 137