Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Instant Update


/*
 * TemperaturePanel.java
 */

import java.awt.*;
import java.awt.event.*;
import java.util.Observer;

/**
 * This class models both View and Controller functionality. 
 * 
 * 

Note that the actual implementation of the Observer (View) interface is 
 * left to concrete subclasses. This is because subclasses will render a 
 * different view, depending whether the a Fahrenheit or Celsius representation 
 * of the data model is to be rendered.
 */
public abstract class TemperaturePanel extends java.awt.Panel 
		implements java.util.Observer 
{
	private String				label;
	private TemperatureModel	model;			// data model that is both
viewed and controlled from this object
	private TextField			display;		// textbox to display the
current temperature
	private Button				raiseButton;	// button to increase the temp
by 1 degree (F or C)
	private Button				lowerButton;	// button to decrease the temp
by 1 degree (F or C)
	
	/**
	 * Construct a new TemperaturePanel object. Need a String that labels 
	 * the panel with a name, and a TemperatureModel that will be linked 
	 * to this object (which is both a View and a Controller).
	 * 
	 * @param label String to name this Panel
	 * @param model data model to be linked w/ the View/Controller embedded in
this object
	 */
	public TemperaturePanel(String label,TemperatureModel model)
	{
		this.label = label;	
		this.model = model;
		
		this.setLayout(new BorderLayout());
		
		display = new TextField();
		raiseButton = new Button("Raise");
		lowerButton = new Button("Lower");

		this.add("North",new Label(label));
		this.add("Center",display);
		
		Panel buttons = new Panel();
		buttons.add(raiseButton);
		buttons.add(lowerButton);
		this.add("South",buttons);
		
		model.addObserver(this);	// <-- MVC method that links the data Model
with the View
	}
	
	/**
	 * Hook through which program users can update the data model. Code
	 * that implements updates to data model is found in the concrete
	 * subclasses of this class. In the subclasses, the "display" TextField 
	 * object is linked to an ActionListener that interfaces with the data
model.
	 * This method merely sets the text in the TextField object.
	 * 
	 * @param s new temperature value, entered by user at the screen.
	 */
	public void setDisplay(String s) { display.setText(s); }
	
	/**
	 * Hook through which program users can view the current state of the 
	 * data (as rendered through this particular View object). Code that 
	 * actually updates the View (based on changes made to the data Model by 
	 * Controller objects) is found in the concrete subclasses of this class. 
	 * Concrete subclasses implement the Observer method by implementing the 
	 * update() method, which is called from the Model whenever its state has 
	 * changed. This method merely parses a double value out of the text 
	 * value found in the TextField object.
	 * 
	 * @return double value in the TextField object, displayed to user.
	 */
	public double getDisplay() {
		double result = 0.0;
		try {
			result = Double.valueOf(display.getText()).doubleValue();
		} catch ( NumberFormatException ex ) { }
		return result;
	}
	
	/**
	 * Allow child objects access to the data model.
	 * 
	 * @return TemperatureModel return model object to client
	 */
	protected TemperatureModel model() { return model; }	
	
	// next three methods allow child objects to ActionListener
	// objects the GUI controls available to the end user. This 
	// implements Controller functionality for this object.
	
	protected void addRaiseListener(ActionListener a) {
raiseButton.addActionListener(a); }
	protected void addLowerListener(ActionListener a) {
lowerButton.addActionListener(a); }
	protected void addDisplayListener(ActionListener a) {
display.addActionListener(a); }
}

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.