1/* 2 * Copyright (c) 2008, 2015, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24package com.sun.hotspot.igv.filter; 25 26import com.sun.hotspot.igv.data.ChangedEvent; 27import com.sun.hotspot.igv.data.ChangedEventProvider; 28import com.sun.hotspot.igv.data.ChangedListener; 29import com.sun.hotspot.igv.graph.Diagram; 30import java.util.ArrayList; 31import java.util.Collections; 32import java.util.List; 33 34/** 35 * 36 * @author Thomas Wuerthinger 37 */ 38public class FilterChain implements ChangedEventProvider<FilterChain> { 39 40 private List<Filter> filters; 41 private transient ChangedEvent<FilterChain> changedEvent; 42 43 private ChangedListener<Filter> changedListener = new ChangedListener<Filter>() { 44 @Override 45 public void changed(Filter source) { 46 changedEvent.fire(); 47 } 48 }; 49 50 public FilterChain() { 51 filters = new ArrayList<>(); 52 changedEvent = new ChangedEvent<>(this); 53 } 54 55 public FilterChain(FilterChain f) { 56 this.filters = new ArrayList<>(f.filters); 57 changedEvent = new ChangedEvent<>(this); 58 } 59 60 @Override 61 public ChangedEvent<FilterChain> getChangedEvent() { 62 return changedEvent; 63 } 64 65 public Filter getFilterAt(int index) { 66 assert index >= 0 && index < filters.size(); 67 return filters.get(index); 68 } 69 70 public void apply(Diagram d) { 71 for (Filter f : filters) { 72 f.apply(d); 73 } 74 } 75 76 public void apply(Diagram d, FilterChain sequence) { 77 List<Filter> applied = new ArrayList<>(); 78 for (Filter f : sequence.getFilters()) { 79 if (filters.contains(f)) { 80 f.apply(d); 81 applied.add(f); 82 } 83 } 84 85 86 for (Filter f : filters) { 87 if (!applied.contains(f)) { 88 f.apply(d); 89 } 90 } 91 } 92 93 94 public void addFilter(Filter filter) { 95 assert filter != null; 96 filters.add(filter); 97 filter.getChangedEvent().addListener(changedListener); 98 changedEvent.fire(); 99 } 100 101 public boolean containsFilter(Filter filter) { 102 return filters.contains(filter); 103 } 104 105 public void removeFilter(Filter filter) { 106 assert filters.contains(filter); 107 filters.remove(filter); 108 filter.getChangedEvent().removeListener(changedListener); 109 changedEvent.fire(); 110 } 111 112 public void moveFilterUp(Filter filter) { 113 assert filters.contains(filter); 114 int index = filters.indexOf(filter); 115 if (index != 0) { 116 filters.remove(index); 117 filters.add(index - 1, filter); 118 } 119 changedEvent.fire(); 120 } 121 122 public void moveFilterDown(Filter filter) { 123 assert filters.contains(filter); 124 int index = filters.indexOf(filter); 125 if (index != filters.size() - 1) { 126 filters.remove(index); 127 filters.add(index + 1, filter); 128 } 129 changedEvent.fire(); 130 } 131 132 public List<Filter> getFilters() { 133 return Collections.unmodifiableList(filters); 134 } 135} 136