View Javadoc

1   package org.rpi.plugin.lcddisplay;
2   
3   import org.apache.log4j.Logger;
4   
5   import com.pi4j.util.StringUtil;
6   
7   public class LCDRow {
8   	
9   	private static Logger log = Logger.getLogger(LCDRow.class);
10  
11  	private int LCD_WIDTH = 20;
12  	private String current_text = "";
13  	private String next_text = "";
14  	private int position = 0;
15  
16  	public int getLCD_WIDTH() {
17  		return LCD_WIDTH;
18  	}
19  
20  	/***
21  	 * Set the Width of the Display
22  	 * @param lCD_WIDTH
23  	 */
24  	public void setLCD_WIDTH(int lCD_WIDTH) {
25  		LCD_WIDTH = lCD_WIDTH;
26  	}
27  
28  	/***
29  	 * Get the Text to Display
30  	 * @return
31  	 */
32  	public String getText() {
33  		UpdateText();
34  		if (current_text.length() <= LCD_WIDTH)
35  			return current_text;
36  		else
37  			return getNextString(current_text);
38  	}
39  
40  	/***
41  	 * Set the Text
42  	 * @param text
43  	 */
44  	public void setText(String text) {
45  		if (text.length() <= LCD_WIDTH) {
46  			next_text = StringUtil.padCenter(text, LCD_WIDTH);
47  		} else {
48  			next_text = StringUtil.padLeft("", LCD_WIDTH) + text + StringUtil.padRight("", LCD_WIDTH);
49  		}
50  	}
51  
52  	/***
53  	 * Change over from the existing Text to the new Text
54  	 */
55  	public void UpdateText() {
56  		if (current_text != next_text) {
57  			if (next_text.length() <= LCD_WIDTH) {
58  				position = 0;
59  			} else {
60  			}
61  			current_text = next_text;
62  		}
63  	}
64  
65  	/***
66  	 * If text if wider then the screen width scroll through the text
67  	 * @param in
68  	 * @return
69  	 */
70  	private String getNextString(String in) {
71  		String res = "";
72  		position++;
73  		if (position + LCD_WIDTH > in.length()) {
74  			position = 0;
75  		}
76  		if (in.length() > LCD_WIDTH) {
77  			try {
78  				res = in.substring(position, position + LCD_WIDTH);
79  			} catch (Exception e) {
80  				log.error("Error getNextString. Position: " + position + " Text:" + in, e);
81  				position = 0;
82  			}
83  		}
84  		return res;
85  	}
86  
87  }