/* yearSelector.java Paul Carlisle - November 15, 1997 This is an object which can be used as an input panel for years. It is designed to accept or display years from 3999 BC to 3999 AD. If the year 0 is entered, it is accpeted as the year 1BC, and the display is readjusted to reflect this. The font used for the display is passed in with the constructor, as is the width and a year to initialize the display. The year is displayed as four drop-down lists, one for each digit, and a two-element drop-down list to select AD or BC epochs. The object handles all interaction internally; no messages are returned to the client program, although the current setting can be changed or retrieved at any time. The class provides the following publid methods: public yearSelector(int yr, Font inputFont, int width) - Constructor, which Initializes the display to the year, using the font. Attempts to build the object so that it is width wide, although some browsers will ignore this. public void setYear(int yr) - Sets the display to the corresponding year. Note that NO ERROR CHECKING IS PERFORMED! Once the year is set, it should be manipulated through the display, which restricts entry to valid intervals. However, setYear() can still be used to feed the object non- sensical values. Care must be taken here, and we should probably add some error checking code later. public int getYear() - Returns the year as indicated on the display. If the epoch is BC, years are counted logically, with 1 BC being returned as year 0, 2 BC being year -1, 1 AD being year 1. */ import java.awt.*; public class yearSelector extends Panel { private int currentYear; // Drop-down lists for the digits. private Choice millenium, century, decade, year; // ... and for the epoch. private Choice epoch; private GridBagLayout selectorLayout; private GridBagConstraints selectorConstraints; private int Width; private boolean gotMessage = false; public yearSelector(int yr, Font inputFont, int width) { Width = width; // Set layout formatting, color and font. //selectorLayout = new GridLayout(1,0,0,0); selectorLayout = new GridBagLayout(); selectorConstraints = new GridBagConstraints(); setLayout(selectorLayout); setBackground(Color.lightGray); setFont(inputFont); // Instantiate the drop-down lists, and set their background // color to white. millenium = new Choice(); millenium.setBackground(Color.white); century = new Choice(); century.setBackground(Color.white); decade = new Choice(); decade.setBackground(Color.white); year = new Choice(); year.setBackground(Color.white); epoch = new Choice(); epoch.setBackground(Color.white); // The 1000's digit can only allow choices from 0 - 3. millenium.addItem("0"); millenium.addItem("1"); millenium.addItem("2"); millenium.addItem("3"); // The others can show 0 - 9. for (int i = 0; i <= 9; i++) { century.addItem(new String(Integer.toString(i)) ); decade.addItem(new String(Integer.toString(i)) ); year.addItem(new String(Integer.toString(i)) ); } // The epoch is either AD or BC. epoch.addItem("AD"); epoch.addItem("BC"); // Set the display to the selected year. setYear(yr); // Resize, and add to the panel. //resize(Width, 50); selectorConstraints.gridy = 0; selectorConstraints.anchor = GridBagConstraints.CENTER; selectorConstraints.fill = GridBagConstraints.BOTH; Panel pad = new Panel(); selectorConstraints.gridx = 0; selectorLayout.setConstraints(pad,selectorConstraints); add(pad); selectorConstraints.gridx = 1; selectorLayout.setConstraints(millenium,selectorConstraints); add(millenium); selectorConstraints.gridx = 2; selectorLayout.setConstraints(century,selectorConstraints); add(century); selectorConstraints.gridx = 3; selectorLayout.setConstraints(decade,selectorConstraints); add(decade); selectorConstraints.gridx = 4; selectorLayout.setConstraints(year,selectorConstraints); add(year); selectorConstraints.gridx = 5; selectorLayout.setConstraints(epoch,selectorConstraints); add(epoch); } // Handles user manipulation of the display. public boolean action(Event e, Object what) { int temp = currentYear; // If the user changed a digit... if ( (e.target == millenium) || (e.target == century) || (e.target == decade) || (e.target == year) ) { // Read in all the digit settings, and form a new // numeric year value from them. temp = millenium.getSelectedIndex() * 1000 + century.getSelectedIndex() * 100 + decade.getSelectedIndex() * 10 + year.getSelectedIndex(); // Adjust for epoch. if (temp == 0) epoch.select("BC"); else if (epoch.getSelectedItem() == "BC") temp = -(temp - 1); } // If the user changed the epoch... else if ( (e.target == epoch) ) { // Make adjustments so numbers on display don't change, but // internal year representation is correct. if ( (epoch.getSelectedItem() == "AD") && (currentYear <= 0) ) temp = Math.abs(currentYear) + 1; else if ( (epoch.getSelectedItem() == "BC") && (currentYear > 0) ) temp = -(currentYear - 1); } // Set the year, and reset the display. setYear(temp); gotMessage = true; // Pass on all events. return false; } public boolean getMessage() { boolean temp = gotMessage; gotMessage = false; return temp; } // Returns the numeric value of the year shown in the display. // Years BC are counted as: 1 BC = 0, 2 BC = -1, 1 AD = 1, etc. public int getYear() { return currentYear; } // Sets the display to the year passed in. No error checking is done! // Beware! Don't send values outside the range -3998 -> 3999! public void setYear(int yr) { currentYear = yr; // Adjust absolute value of displayed year upward by 1 if // it is in the BC epoch. if (currentYear <= 0) { epoch.select("BC"); yr = Math.abs(yr); yr++; } else epoch.select("AD"); // Calculate and set the value of each digit in the display. millenium.select(yr / 1000); century.select( (yr % 1000) / 100); decade.select( (yr % 100) / 10); year.select(yr % 10); } }