001 /* 002 * cs101 Graphical Semaphore 003 * $Id: GraphicalSemaphore.java,v 1.1.1.1 2002/06/05 21:56:32 root Exp $ 004 * 005 * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group. 006 * For more information, see <a href="http://www.ai.mit.edu/projects/cs101/">the 007 * CS101 homepage</a> or email <las@ai.mit.edu>. 008 * 009 * Copyright (C) 1996 Massachusetts Institute of Technology. 010 * Please do not redistribute without obtaining permission. 011 */ 012 013 package cs101.util.semaphore; 014 import java.awt.*; 015 016 /** 017 * cs101.util.GraphicalSemaphore is the abstract super class for both 018 * the GBS (Graphical Binary semaphore) and the GCS 019 * (Graphical Counting semaphore). <br> 020 * It provides a common interface for the two sub-classes: 021 * gs.request(), gs.release() as well as doing alot of the 022 * graphical grunt work. 023 * <br> 024 * Copyright 1996 Massachusetts Institute of Technology 025 * 026 * @see GCS 027 * @see GBS 028 * 029 * @author Todd C. Parnell, tparnell@ai.mit.edu 030 * @author Joshua R. Brown, reuben@ai.mit.edu 031 * @version $Id: GraphicalSemaphore.java,v 1.1.1.1 2002/06/05 21:56:32 root Exp $ 032 * 033 */ 034 public abstract class GraphicalSemaphore extends Panel { 035 /** Number of instances created */ 036 protected static int InstanceCounter = 0; 037 /** my number in the InstanceCount(er) */ 038 protected int myNumber; 039 /** label for the semaphore */ 040 protected Label label; 041 /** display panel for the semaphore */ 042 protected Panel display; 043 044 /** 045 * Creates some of the GUI objects needed by it's subclasses. 046 * 047 * Creates a panel to display the semaphore in and a label with 048 * the text passed. 049 * 050 * @param label A string that will identify the semaphore in the 051 * display. 052 */ 053 public GraphicalSemaphore(String label) { 054 super(); 055 this.label = new Label(label); 056 this.myNumber = this.InstanceCounter++; 057 } 058 059 /** 060 * This method should be Called by the subclass after it's part of the gui 061 * setup is complete. 062 * 063 * It expects the the display Panel has been initialized by the subclss. 064 * It Adds the completed gui components to the panel. 065 */ 066 protected void setupGUI() { 067 // Graphics setup 068 this.add(this.label); 069 this.add(this.display); 070 } 071 072 /** <i>abstract - must be overidden in subclass</i> */ 073 protected abstract void showStatus(); 074 /** <i>abstract - must be overidden in subclass</i> */ 075 public abstract void request(); 076 /** <i>abstract - must be overidden in subclass</i> */ 077 public abstract void release(); 078 079 /** 080 * Determines the mininum size necessary for this component. 081 * Called by this objects container. 082 * 083 * @return the minimum size of the object 084 */ 085 public Dimension getMinimumSize() { 086 Dimension sizeOfLabel = this.label.getMinimumSize(); 087 Dimension sizeOfDisplay = this.display.getMinimumSize(); 088 089 // see which has the greater height 090 if (sizeOfDisplay.height >= sizeOfLabel.height) 091 return new Dimension(sizeOfLabel.width 092 +sizeOfDisplay.width+15, 093 sizeOfDisplay.height); 094 else 095 return new Dimension(sizeOfLabel.width 096 +sizeOfDisplay.width+15, 097 sizeOfDisplay.height); 098 } 099 100 /** 101 * Determines the preferedSize necessary for this component. 102 * Called by this objects container. 103 * 104 * @return the preferred size of the object 105 */ 106 public Dimension getPreferredSize() { 107 Dimension sizeOfLabel = this.label.getPreferredSize(); 108 Dimension sizeOfDisplay = this.display.getPreferredSize(); 109 110 // see which has the greater height 111 if (sizeOfDisplay.height >= sizeOfLabel.height) 112 return new Dimension(sizeOfLabel.width 113 +sizeOfDisplay.width+15, 114 sizeOfDisplay.height+15); 115 else 116 return new Dimension(sizeOfLabel.width 117 +sizeOfDisplay.width+15, 118 sizeOfDisplay.height+15); 119 } 120 121 } 122 123 /* Comments: 124 * 125 * History: 126 * $Log: GraphicalSemaphore.java,v $ 127 * Revision 1.1.1.1 2002/06/05 21:56:32 root 128 * CS101 comes to Olin finally. 129 * 130 * Revision 1.1 2000/04/24 22:17:22 nathanw 131 * Bulk reorganization 132 * 133 * Revision 1.3 1998/07/24 17:19:28 tparnell 134 * Placate new javadoc behavior 135 * 136 * Revision 1.2 1998/06/03 19:40:55 tparnell 137 * update from Java 1.0 to 1.1 138 * 139 * Revision 1.1 1998/03/13 22:18:14 tparnell 140 * Import from server crash. I think the src and class files match up. 141 * 142 * Revision 1.5 1996/08/02 23:37:02 reuben 143 * Improved javadoc comments. 144 * 145 * Revision 1.4 1996/08/01 18:26:27 reuben 146 * More javadoc tweaking (hopefully the final pass) 147 * 148 * Revision 1.3 1996/08/01 16:19:56 reuben 149 * Fixed javadoc problem with return. 150 * 151 * Revision 1.2 1996/07/25 18:27:42 reuben 152 * Added all kinds of comments. 153 * Compiled and tested. 154 * 155 */ 156 157 158 159 160