1 package com.jpeterson.virtlcd.swing.ui;
2
3 import java.awt.Color;
4 import java.awt.Graphics;
5 import java.awt.GridLayout;
6 import java.awt.LayoutManager;
7 import java.io.BufferedReader;
8 import java.io.IOException;
9 import java.io.StringReader;
10
11 import javax.swing.JComponent;
12 import javax.swing.border.EmptyBorder;
13 import javax.swing.event.ChangeEvent;
14 import javax.swing.event.ChangeListener;
15 import javax.swing.plaf.ComponentUI;
16
17 import com.jpeterson.virtlcd.swing.JDotMatrixDisplay;
18 import com.jpeterson.virtlcd.swing.JDotMatrixSixByEight;
19
20
21
22
23
24
25
26 public class BasicDotMatrixDisplayUI extends DotMatrixDisplayUI {
27
28
29
30 protected JDotMatrixDisplay dotMatrixDisplay;
31
32 protected ChangeListener dotMatrixDisplayChangeListener;
33
34 protected static final int VERTICAL_GAP = 2;
35
36 protected JDotMatrixSixByEight dotMatrixCharacters[][];
37
38
39
40
41
42
43 public static ComponentUI createUI(JComponent c) {
44 return new BasicDotMatrixDisplayUI();
45 }
46
47
48
49
50
51
52 public void installUI(JComponent c) {
53 this.dotMatrixDisplay = (JDotMatrixDisplay) c;
54 installDefaults();
55 installComponents();
56 installListeners();
57
58 c.setLayout(createLayoutManager());
59 c.setBorder(new EmptyBorder(1, 1, 1, 1));
60 }
61
62
63
64
65
66
67 public void uninstallUI(JComponent c) {
68 c.setLayout(null);
69 uninstallListeners();
70 uninstallComponents();
71 uninstallDefaults();
72
73 this.dotMatrixDisplay = null;
74 }
75
76 public void installDefaults() {
77
78 }
79
80 public void installComponents() {
81 int rows = dotMatrixDisplay.getRows();
82 int columns = dotMatrixDisplay.getColumns();
83
84 dotMatrixCharacters = new JDotMatrixSixByEight[rows][columns];
85
86 for (int row = 0; row < rows; row++) {
87 for (int column = 0; column < columns; column++) {
88 dotMatrixCharacters[row][column] = new JDotMatrixSixByEight();
89 dotMatrixDisplay.add(dotMatrixCharacters[row][column]);
90 }
91 }
92 }
93
94 public void installListeners() {
95 this.dotMatrixDisplayChangeListener = new ChangeListener() {
96 public void stateChanged(ChangeEvent e) {
97 processModelChange();
98
99 dotMatrixDisplay.repaint();
100 }
101 };
102 this.dotMatrixDisplay.getModel().addChangeListener(
103 this.dotMatrixDisplayChangeListener);
104 }
105
106 public void uninstallDefaults() {
107 }
108
109 public void uninstallComponents() {
110
111 }
112
113 public void uninstallListeners() {
114 this.dotMatrixDisplay.getModel().removeChangeListener(
115 this.dotMatrixDisplayChangeListener);
116 this.dotMatrixDisplayChangeListener = null;
117 }
118
119 @Override
120 public void paint(Graphics g, JComponent c) {
121 super.paint(g, c);
122
123
124 Color color;
125 int rows = dotMatrixDisplay.getRows();
126 int columns = dotMatrixDisplay.getColumns();
127
128 color = dotMatrixCharacters[0][0].getForeground();
129 if (!color.equals(dotMatrixDisplay.getForeground())) {
130 color = dotMatrixDisplay.getForeground();
131 for (int row = 0; row < rows; row++) {
132 for (int column = 0; column < columns; column++) {
133 dotMatrixCharacters[row][column].setForeground(color);
134 }
135 }
136 }
137
138 color = dotMatrixCharacters[0][0].getBackground();
139 if (!color.equals(dotMatrixDisplay.getBackground())) {
140 color = dotMatrixDisplay.getBackground();
141 for (int row = 0; row < rows; row++) {
142 for (int column = 0; column < columns; column++) {
143 dotMatrixCharacters[row][column].setBackground(color);
144 }
145 }
146 }
147 }
148
149
150
151
152 void processModelChange() {
153 String text = dotMatrixDisplay.getModel().getText();
154 int columns = dotMatrixDisplay.getColumns();
155 int rows = dotMatrixDisplay.getRows();
156 char c;
157 String line;
158 int lineLength;
159 int row = 0;
160
161
162
163
164 BufferedReader source = new BufferedReader(new StringReader(text));
165
166 try {
167 line = source.readLine();
168 while ((line != null) && (row < rows)) {
169 lineLength = line.length();
170 for (int column = 0; column < columns; column++) {
171 if (column < lineLength) {
172 c = line.charAt(column);
173 dotMatrixCharacters[row][column].setChar(c);
174 } else {
175 dotMatrixCharacters[row][column].setChar(' ');
176 }
177 }
178
179 ++row;
180 line = source.readLine();
181 }
182
183 while (row < rows) {
184 for (int column = 0; column < columns; column++) {
185 dotMatrixCharacters[row][column].setChar(' ');
186 }
187
188 ++row;
189 }
190 } catch (IOException e) {
191
192 while (row < rows) {
193 for (int column = 0; column < columns; column++) {
194 dotMatrixCharacters[row][column].setChar(' ');
195 }
196
197 ++row;
198 }
199 }
200 }
201
202
203
204
205
206
207
208 protected LayoutManager createLayoutManager() {
209 GridLayout layoutManager;
210
211 layoutManager = new GridLayout(dotMatrixDisplay.getRows(),
212 dotMatrixDisplay.getColumns(), 0, VERTICAL_GAP);
213
214 return layoutManager;
215 }
216 }