View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2001 Bartek Teodorczyk <barteo@barteo.net>
4    *
5    *  This library is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU Lesser General Public
7    *  License as published by the Free Software Foundation; either
8    *  version 2.1 of the License, or (at your option) any later version.
9    *
10   *  This library is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *  Lesser General Public License for more details.
14   *
15   *  You should have received a copy of the GNU Lesser General Public
16   *  License along with this library; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package javax.microedition.lcdui;
21  
22  
23  // TODO implement pointer events
24  public abstract class Screen extends Displayable
25  {
26  	
27      Screen(String title)
28      {
29          super(title);
30      }
31  
32      
33      void scroll(int gameKeyCode) {
34      	viewPortY += traverse(gameKeyCode, viewPortY, viewPortY + viewPortHeight);
35      	repaint();
36      }
37      
38  	
39  	abstract int traverse(int gameKeyCode, int top, int bottom);
40  
41  	
42  	void keyPressed(int keyCode) 
43  	{
44  		int gameKeyCode = Display.getGameAction(keyCode);
45  
46  		if (gameKeyCode == Canvas.UP || gameKeyCode == Canvas.DOWN) {
47  			viewPortY += traverse(gameKeyCode, viewPortY, viewPortY + viewPortHeight);
48  			repaint();
49  		}
50  	}
51  
52  	
53  	void hideNotify() 
54  	{
55  		super.hideNotify();
56  	}
57  
58  	
59  	void keyRepeated(int keyCode) 
60  	{
61  		keyPressed(keyCode);
62  	}
63  
64  	
65  	final void paint(Graphics g) 
66  	{
67  		int contentHeight = 0;
68  		int translatedY;
69  
70  		if (viewPortY == 0) {
71  			currentDisplay.setScrollUp(false);
72  		} else {
73  			currentDisplay.setScrollUp(true);
74  		}
75  
76  		g.setGrayScale(255);
77  		g.fillRect(0, 0, getWidth(), getHeight());
78  
79  		g.setGrayScale(0);
80  
81          // TODO move to Displayable
82  		if (getTicker() != null) {
83  			contentHeight += getTicker().paintContent(g);
84  		}
85  
86  		g.translate(0, contentHeight);
87  		translatedY = contentHeight;
88  
89          // TODO move to Displayable
90          // TODO remove this StringComponent object when native UI is completed
91          StringComponent title = new StringComponent(getTitle());
92  		contentHeight += title.paint(g);
93  		g.drawLine(0, title.getHeight(), getWidth(), title.getHeight());
94  		contentHeight += 1;
95  
96  		g.translate(0, contentHeight - translatedY);
97  		translatedY = contentHeight;
98  
99  		g.setClip(0, 0, getWidth(), getHeight() - contentHeight);
100 		g.translate(0, -viewPortY);
101 		contentHeight += paintContent(g);
102 		g.translate(0, viewPortY);
103 
104 		if (contentHeight - viewPortY > getHeight()) {
105 			currentDisplay.setScrollDown(true);
106 		} else {
107 			currentDisplay.setScrollDown(false);
108 		}
109 		g.translate(0, -translatedY);
110 	}
111 	
112 
113 	abstract int paintContent(Graphics g);
114 
115 	
116 	void repaint() 
117 	{
118 		super.repaint();
119 	}
120 
121 	
122 	void showNotify() 
123 	{
124 		viewPortY = 0;
125 
126 		super.showNotify();
127 	}
128 
129 }