View Javadoc

1   package com.jpeterson.virtlcd.swing;
2   
3   import javax.swing.JComponent;
4   import javax.swing.UIDefaults;
5   import javax.swing.UIManager;
6   
7   import com.jpeterson.virtlcd.swing.ui.BasicDotMatrixDisplayUI;
8   import com.jpeterson.virtlcd.swing.ui.DotMatrixDisplayUI;
9   
10  /**
11   * Simple component composed of multiple <a href="JDotMatrixSixByEight.html"><code>JDotMatrixSixByEight</code></a>
12   * components arranged in a grid. You can specify the number of columns and rows
13   * of <code>JDotMatrixSixByEight</code> components to include in the display.
14   * 
15   * @author Jesse Peterson
16   * @see JDotMatrixSixByEight
17   */
18  public class JDotMatrixDisplay extends JComponent {
19  	/**
20  	 * 
21  	 */
22  	private static final long serialVersionUID = 1L;
23  
24  	/**
25  	 * The UI class ID string.
26  	 */
27  	private static final String uiClassID = "DotMatrixDisplayUI";
28  
29  	protected DotMatrixDisplayModel model;
30  
31  	private int columns;
32  
33  	private int rows;
34  
35  	/**
36  	 * Create a new dot matrix display.
37  	 * 
38  	 * @param columns
39  	 *            Number of columns, or characters, per row.
40  	 * @param rows
41  	 *            Number of rows.
42  	 * @throws IllegalArgumentException
43  	 *             Invalid columns or rows specified.
44  	 */
45  	public JDotMatrixDisplay(int columns, int rows)
46  			throws IllegalArgumentException {
47  		this.model = new DefaultDotMatrixDisplayModel();
48  
49  		setColumns(columns);
50  		setRows(rows);
51  
52  		this.updateUI();
53  	}
54  
55  	/**
56  	 * Get the number of columns, characters, in the display.
57  	 * 
58  	 * @return The number of columns.
59  	 */
60  	public int getColumns() {
61  		return columns;
62  	}
63  
64  	/**
65  	 * Set the number of columns in the display.
66  	 * 
67  	 * @param columns
68  	 *            The number of columns in the display.
69  	 * @throws IllegalArgumentException
70  	 *             Invalid number of columns in the display.
71  	 */
72  	private void setColumns(int columns) throws IllegalArgumentException {
73  		if (columns <= 0) {
74  			throw new IllegalArgumentException(
75  					"Number of columns must be greater than zero");
76  		}
77  
78  		this.columns = columns;
79  	}
80  
81  	/**
82  	 * Get the number of rows in the display.
83  	 * 
84  	 * @return The number of rows.
85  	 */
86  	public int getRows() {
87  		return rows;
88  	}
89  
90  	/**
91  	 * Set the number of rows in the display.
92  	 * 
93  	 * @param rows
94  	 *            The number of rows in the display.
95  	 * @throws IllegalArgumentException
96  	 *             Invalid number of rows in the display.
97  	 */
98  	private void setRows(int rows) throws IllegalArgumentException {
99  		if (rows <= 0) {
100 			throw new IllegalArgumentException(
101 					"Number of rows must be greater than zero");
102 		}
103 
104 		this.rows = rows;
105 	}
106 
107 	/**
108 	 * Sets the new UI delegate.
109 	 * 
110 	 * @param ui
111 	 *            New UI delegate.
112 	 */
113 	public void setUI(DotMatrixDisplayUI ui) {
114 		super.setUI(ui);
115 	}
116 
117 	/**
118 	 * Resets the UI property to a value from the current look and feel.
119 	 * 
120 	 * @see JComponent#updateUI
121 	 */
122 	public void updateUI() {
123 		if (UIManager.get(getUIClassID()) != null) {
124 			setUI((DotMatrixDisplayUI) UIManager.getUI(this));
125 		} else {
126 			setUI(new BasicDotMatrixDisplayUI());
127 		}
128 	}
129 
130 	/**
131 	 * Returns the UI object which implements the L&F for this component.
132 	 * 
133 	 * @return UI object which implements the L&F for this component.
134 	 * @see #setUI
135 	 */
136 	public DotMatrixDisplayUI getUI() {
137 		return (DotMatrixDisplayUI) ui;
138 	}
139 
140 	/**
141 	 * Returns the name of the UI class that implements the L&F for this
142 	 * component.
143 	 * 
144 	 * @return The name of the UI class that implements the L&F for this
145 	 *         component.
146 	 * @see JComponent#getUIClassID
147 	 * @see UIDefaults#getUI
148 	 */
149 	public String getUIClassID() {
150 		return uiClassID;
151 	}
152 
153 	public DotMatrixDisplayModel getModel() {
154 		return this.model;
155 	}
156 
157 	/**
158 	 * Get the text displayed.
159 	 * 
160 	 * @return The text displayed.
161 	 */
162 	public String getText() {
163 		return model.getText();
164 	}
165 
166 	/**
167 	 * Set the text to display.
168 	 * 
169 	 * @param text
170 	 *            The text displayed. Multiple line displays will most likely
171 	 *            break the string by a new line. Lines that are too long will
172 	 *            most likely be truncated. The actual display is controlled by
173 	 *            the UI component.
174 	 */
175 	public void setText(String text) {
176 		model.setText(text);
177 	}
178 }