|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| GUIAvailabilityNotifier.java | 92.9% | 90.3% | 90% | 90.9% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK | |
| 2 | * | |
| 3 | * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu) | |
| 4 | * All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * * Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * * Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the | |
| 14 | * names of its contributors may be used to endorse or promote products | |
| 15 | * derived from this software without specific prior written permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
| 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 28 | * | |
| 29 | * This software is Open Source Initiative approved Open Source Software. | |
| 30 | * Open Source Initative Approved is a trademark of the Open Source Initiative. | |
| 31 | * | |
| 32 | * This file is part of DrJava. Download the current version of this project | |
| 33 | * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ | |
| 34 | * | |
| 35 | * END_COPYRIGHT_BLOCK*/ | |
| 36 | ||
| 37 | package edu.rice.cs.drjava.ui.avail; | |
| 38 | ||
| 39 | import edu.rice.cs.drjava.model.EventNotifier; | |
| 40 | import java.util.HashMap; | |
| 41 | ||
| 42 | /** | |
| 43 | * Keeps track of all listeners to GUI availability. | |
| 44 | * <p> | |
| 45 | * | |
| 46 | * All methods in this class must use the synchronization methods | |
| 47 | * provided by ReaderWriterLock. This ensures that multiple notifications | |
| 48 | * (reads) can occur simultaneously, but only one thread can be adding | |
| 49 | * or removing listeners (writing) at a time, and no reads can occur | |
| 50 | * during a write. | |
| 51 | * <p> | |
| 52 | * | |
| 53 | * <i>No</i> methods on this class should be synchronized using traditional | |
| 54 | * Java synchronization! | |
| 55 | * <p> | |
| 56 | * | |
| 57 | * @version $Id: GUIAvailabilityNotifier.java 5355 2010-08-11 21:20:32Z mgricken $ | |
| 58 | */ | |
| 59 | public class GUIAvailabilityNotifier extends EventNotifier<GUIAvailabilityListener> implements GUIAvailabilityListener { | |
| 60 | /** The current availabilities of the individual components. */ | |
| 61 | protected final HashMap<ComponentType,Integer> _values = new HashMap<ComponentType,Integer>(); | |
| 62 | ||
| 63 | // public static final edu.rice.cs.util.Log LOG = new edu.rice.cs.util.Log("avail.txt",true); | |
| 64 | ||
| 65 | /** Create a new notifier with all components available. */ | |
| 66 | 38 | public GUIAvailabilityNotifier() { |
| 67 | 38 | for(ComponentType component: ComponentType.values()) { |
| 68 | 380 | _values.put(component, 0); |
| 69 | } | |
| 70 | } | |
| 71 | ||
| 72 | /** Return true if the component is available. | |
| 73 | * @param component the component to query | |
| 74 | * @return true if available */ | |
| 75 | 12249 | public boolean isAvailable(ComponentType component) { |
| 76 | 12249 | return (_values.get(component)==0); |
| 77 | } | |
| 78 | ||
| 79 | /** Returns the count for the specified component, where 0 means available, and 1 or greater means | |
| 80 | * unavailable (perhaps nested). | |
| 81 | * @param component the component to query | |
| 82 | * @return count */ | |
| 83 | 0 | public int getCount(ComponentType component) { |
| 84 | 0 | return _values.get(component); |
| 85 | } | |
| 86 | ||
| 87 | /** Make sure a component is unavailable, i.e. the count is at least 1. If the count is 0, | |
| 88 | * increase it to 1. If the count is already 1 or greater, than don't do anything. | |
| 89 | * @param component the component that needs to be unavailable */ | |
| 90 | 351 | public void ensureUnavailable(ComponentType component) { |
| 91 | 190 | if (isAvailable(component)) { availabilityChanged(component, false); } |
| 92 | } | |
| 93 | ||
| 94 | /** Make a component (more) unavailable. This may be nested. | |
| 95 | * @param component the component that is unavailable */ | |
| 96 | 10 | public void unavailable(ComponentType component) { |
| 97 | 10 | availabilityChanged(component, false); |
| 98 | } | |
| 99 | ||
| 100 | /** Make sure a component is available, i.e. the count is 0. If the count is greater than 0, | |
| 101 | * change it to 0. If the count is already 0, than don't do anything. | |
| 102 | * @param component the component that needs to be available */ | |
| 103 | 152 | public void ensureAvailable(ComponentType component) { |
| 104 | // LOG.log("ensureAvailable "+component, new RuntimeException()); | |
| 105 | 152 | if (!isAvailable(component)) { |
| 106 | 0 | _values.put(component, 0); |
| 107 | 0 | notifyListeners(component); |
| 108 | } | |
| 109 | } | |
| 110 | ||
| 111 | /** Make a component (more) available. This may be nested. If the count of the component | |
| 112 | * is still larger than 0 after this call, the component will remain unavailable. | |
| 113 | * @param component the component that is available */ | |
| 114 | 25 | public void available(ComponentType component) { |
| 115 | 25 | availabilityChanged(component, true); |
| 116 | } | |
| 117 | ||
| 118 | /** Make sure the availability of the component is as specified. If available is true, | |
| 119 | * this calls ensureAvailable; if available is false, it calls ensureUnavailable. | |
| 120 | * @param component the component that needs to be available | |
| 121 | * @param available true to make the component available, false otherwise */ | |
| 122 | 199 | public void ensureAvailabilityIs(ComponentType component, boolean available) { |
| 123 | 123 | if (available) { ensureAvailable(component); } else { ensureUnavailable(component); } |
| 124 | } | |
| 125 | ||
| 126 | /** Called to change a components availability. Nested (non-)availability is supported: | |
| 127 | * calling this method with available=false twice will require two calls with available=true | |
| 128 | * to make the component available again. | |
| 129 | * @param component the component whose availability changed | |
| 130 | * @param available true if component is available */ | |
| 131 | 225 | public void availabilityChanged(ComponentType component, boolean available) { |
| 132 | // LOG.log("availabilityChanged "+component+" "+available, new RuntimeException()); | |
| 133 | 225 | int value = _values.get(component); |
| 134 | 225 | boolean changed = false; |
| 135 | 225 | if (available) { |
| 136 | // made available, decrement count if >0 | |
| 137 | 25 | if (value==1) { |
| 138 | 8 | _values.put(component, 0); |
| 139 | 8 | changed = true; |
| 140 | } | |
| 141 | 17 | else if (value>1) { |
| 142 | 3 | _values.put(component, value-1); |
| 143 | } | |
| 144 | } | |
| 145 | else { | |
| 146 | // made unavailable, increment count | |
| 147 | 200 | _values.put(component, value+1); |
| 148 | 200 | changed = true; |
| 149 | } | |
| 150 | 208 | if (changed) { notifyListeners(component); } |
| 151 | } | |
| 152 | ||
| 153 | /** Notify the listeners for the specified component. | |
| 154 | * @param component the component whose listeners should be notified */ | |
| 155 | 208 | protected void notifyListeners(ComponentType component) { |
| 156 | 208 | _lock.startRead(); |
| 157 | 208 | try { for (GUIAvailabilityListener cl : _listeners) { |
| 158 | 6996 | cl.availabilityChanged(component, isAvailable(component)); |
| 159 | } } | |
| 160 | 208 | finally { _lock.endRead(); } |
| 161 | } | |
| 162 | } |
|
||||||||||