1 /*
2 * Created on Oct 26, 2003
3 *
4 */
5 package portaview.view;
6
7 import java.awt.BorderLayout;
8 import java.awt.Color;
9 import java.awt.Dimension;
10 import java.awt.Image;
11 import java.awt.image.BufferedImage;
12 import java.util.Observable;
13 import java.util.Observer;
14
15 import javax.swing.BorderFactory;
16 import javax.swing.ImageIcon;
17 import javax.swing.JButton;
18 import javax.swing.JLabel;
19 import javax.swing.JPanel;
20 import javax.swing.SwingConstants;
21
22 import portaview.AbstractViewer;
23 import portaview.model.PortaViewModel;
24 import portaview.model.SlideSettings;
25 import portaview.util.ImageUtils;
26
27 /***
28 * Abstract displayer view. The parent view object for the displayer.
29 *
30 * @author William Lee
31 */
32 public abstract class AbstractDisplayerView extends JPanel implements Observer
33 {
34 private PortaViewModel portaViewModel = null;
35 private AbstractViewer viewer = null;
36 private ImageControlBar controlBar = null;
37 private JLabel picLabel = null;
38 private JLabel titleLabel = null;
39
40 public AbstractDisplayerView(PortaViewModel model, AbstractViewer viewer)
41 {
42 this.portaViewModel = model;
43 this.viewer = viewer;
44 // add myself to the observer.
45 model.addObserver(this);
46 init();
47 }
48
49 /***
50 * If the displayer has no picture, returns the text that it will display. Children
51 * classes should implement this method.
52 * @return the string to display.
53 */
54 protected abstract String getNoPicText();
55
56 /***
57 * Initialize this view.
58 */
59 private void init()
60 {
61 if (needsLabel())
62 {
63 titleLabel = new JLabel();
64 titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
65 titleLabel.setBorder(BorderFactory.createEtchedBorder());
66 }
67
68 picLabel = new JLabel(getNoPicText());
69 picLabel.setHorizontalAlignment(SwingConstants.CENTER);
70 picLabel.setVerticalAlignment(SwingConstants.CENTER);
71 picLabel.setVerticalTextPosition(SwingConstants.BOTTOM);
72
73 //setBackground(Color.BLACK);
74 setLayout(new BorderLayout());
75 controlBar = getImageControls(this.viewer);
76 if (needsLabel())
77 {
78 add(titleLabel, BorderLayout.NORTH);
79 }
80 add(controlBar, BorderLayout.SOUTH);
81 /*
82 JPanel p = new JPanel(new GridBagLayout());
83 GridBagConstraints c = new GridBagConstraints();
84 c.gridheight = 1;
85 c.gridwidth = 1;
86 c.anchor = GridBagConstraints.CENTER;
87 c.weightx = 1;
88 c.weighty = 1;
89 c.gridx = 0;
90 c.gridy = 0;
91 p.add(picLabel, c);
92 */
93 /*
94 JPanel p = new JPanel();
95 add(p, BorderLayout.CENTER);
96 */
97 add(picLabel, BorderLayout.CENTER);
98 setPreferredSize(new Dimension(640, 480));
99 }
100
101 /***
102 * Returns the image control bar. Children classes decide what button bar it wants to
103 * put below the displayer.
104 * @return image control bar.
105 */
106 protected abstract ImageControlBar getImageControls(AbstractViewer viewer);
107
108 /***
109 * Children should implement this in order to set whether you need the label or not.
110 * @return true if children needs a label.
111 */
112 protected abstract boolean needsLabel();
113
114 /***
115 * @return true if the view need refreshing, false otherwise.
116 */
117 protected abstract boolean needRefresh();
118
119 /***
120 * Refreshes the tool bar if the model has changed.
121 */
122 public void refreshBar(Observable dmodel)
123 {
124 if (dmodel instanceof PortaViewModel)
125 {
126 PortaViewModel m = (PortaViewModel) dmodel;
127 // depending on the model, disable the buttons.
128 SlideSettings ss = m.getSlideSettings();
129 if (ss.getTotal() == 0)
130 {
131 // disable all the buttons
132 controlBar.getPrevButton().setEnabled(false);
133 controlBar.getPauseButton().setEnabled(false);
134 controlBar.getPlayButton().setEnabled(false);
135 controlBar.getNextButton().setEnabled(false);
136 JButton sb = controlBar.getSettingsButton();
137 if (sb != null)
138 sb.setEnabled(false);
139 }
140 else
141 {
142 JButton sb = controlBar.getSettingsButton();
143 if (ss.getPlayState() == SlideSettings.PAUSE)
144 {
145 controlBar.getPrevButton().setEnabled(true);
146 controlBar.getPauseButton().setEnabled(false);
147 controlBar.getPlayButton().setEnabled(true);
148 controlBar.getNextButton().setEnabled(true);
149 if (sb != null)
150 sb.setEnabled(true);
151 }
152 else
153 {
154 controlBar.getPrevButton().setEnabled(false);
155 controlBar.getPauseButton().setEnabled(true);
156 controlBar.getPlayButton().setEnabled(false);
157 controlBar.getNextButton().setEnabled(false);
158 if (sb != null)
159 sb.setEnabled(false);
160 }
161 }
162 }
163 }
164
165 /***
166 * Refreshes the image, resizing it to the size of the label.
167 */
168 public void refreshImage()
169 {
170 if (!needRefresh())
171 return;
172 if (needsLabel())
173 {
174 SlideSettings ss = getPortaViewModel().getSlideSettings();
175 // Note that index starts from 0, we need to increment one
176 if (ss.getTotal() != 0)
177 {
178 String txt =
179 portaViewModel.getName()
180 + " (Picture "
181 + (ss.getIndex() + 1)
182 + " of "
183 + ss.getTotal()
184 + ")";
185 if (ss.getPlayState() == SlideSettings.PLAY)
186 {
187 txt = txt + " (Refreshing every " + ss.getSpeed() + " sec)";
188 }
189 else
190 {
191 txt = txt + " (Paused)";
192 }
193 titleLabel.setText(txt);
194 }
195 else
196 {
197 titleLabel.setText(portaViewModel.getName());
198 }
199 }
200 Image img = this.portaViewModel.getImageModel().getImage();
201 BufferedImage bi = null;
202 if (img != null
203 && picLabel.getWidth() > 20
204 && picLabel.getHeight() > 20)
205 {
206 try
207 {
208 bi =
209 ImageUtils.getBufferedImageInFrame(
210 ImageUtils.getBufferedImage(img, picLabel),
211 picLabel.getWidth(),
212 picLabel.getHeight(),
213 picLabel);
214 }
215 catch (Exception e)
216 {
217 e.printStackTrace();
218 }
219 }
220 if (bi != null)
221 {
222 ImageUtils.drawBorder(portaViewModel, bi);
223 ImageIcon ii = new ImageIcon(bi);
224 picLabel.setIcon(ii);
225 picLabel.setText("");
226 picLabel.repaint();
227 }
228 else
229 {
230 picLabel.setIcon(null);
231 picLabel.setText(getNoPicText());
232 picLabel.setForeground(getNoPicTextColor());
233 picLabel.repaint();
234 }
235 }
236
237
238
239
240 /***
241 * Refresh the tool bar and the image.
242 *
243 */
244 public void refresh()
245 {
246 refreshBar(portaViewModel);
247 refreshImage();
248 }
249
250 /***
251 * The model would call this method when its state has change.
252 *
253 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
254 */
255 public void update(Observable dispModel, Object arg1)
256 {
257 refreshImage();
258 refreshBar(dispModel);
259 }
260
261 /***
262 * @return the PortaViewModel associated with this view.
263 */
264 public PortaViewModel getPortaViewModel()
265 {
266 return portaViewModel;
267 }
268
269 /***
270 * Sets the PortaViewModel
271 * @param model the model to set.
272 */
273 public void setPortaViewModel(PortaViewModel model)
274 {
275 // unregister the listener
276 if (portaViewModel != null)
277 portaViewModel.deleteObserver(this);
278 portaViewModel = model;
279 model.addObserver(this);
280 refresh();
281 }
282
283 protected Color getNoPicTextColor()
284 {
285 return Color.black;
286 }
287
288 }
This page was automatically generated by Maven