| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
package palmed.edit.view; |
| 32 |
|
|
| 33 |
|
import java.io.IOException; |
| 34 |
|
import java.io.InputStream; |
| 35 |
|
import java.io.OutputStream; |
| 36 |
|
import java.util.Enumeration; |
| 37 |
|
import java.util.Vector; |
| 38 |
|
import palmed.edit.selection.ILineVisitor; |
| 39 |
|
import palmed.edit.util.Coordinate; |
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
public final class Viewport implements IViewport, IContent, IView |
| 48 |
|
{ |
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
private final Coordinate scroll_; |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
private int visibleColumns_, visibleLines_; |
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
private int maxColumns_, maxLines_; |
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
private final Coordinate cursorPosition_; |
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
private final IContent content_; |
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
private IView view_; |
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
private final Vector views_; |
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
public Viewport( final IContent content ) |
| 84 |
35 |
{ |
| 85 |
35 |
if( content == null ) |
| 86 |
0 |
throw new IllegalArgumentException( "parameter 'content' is null" ); |
| 87 |
35 |
content_ = content; |
| 88 |
35 |
scroll_ = new Coordinate(); |
| 89 |
35 |
view_ = new NullView(); |
| 90 |
35 |
cursorPosition_ = new Coordinate(); |
| 91 |
35 |
views_ = new Vector(); |
| 92 |
35 |
content_.register( this ); |
| 93 |
35 |
} |
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
public void type( final int keyCode ) |
| 99 |
|
{ |
| 100 |
10 |
content_.type( keyCode ); |
| 101 |
10 |
showCursor(); |
| 102 |
10 |
} |
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
public void drag( final int x, final int y ) |
| 108 |
|
{ |
| 109 |
0 |
content_.drag( x + scroll_.x_, y + scroll_.y_ ); |
| 110 |
0 |
showCursor(); |
| 111 |
0 |
} |
| 112 |
|
|
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| 116 |
|
public void click( final int x, final int y ) |
| 117 |
|
{ |
| 118 |
0 |
content_.click( x + scroll_.x_, y + scroll_.y_ ); |
| 119 |
0 |
showCursor(); |
| 120 |
0 |
} |
| 121 |
|
|
| 122 |
|
|
| 123 |
|
|
| 124 |
|
|
| 125 |
|
public void enter() |
| 126 |
|
{ |
| 127 |
0 |
content_.enter(); |
| 128 |
0 |
showCursor(); |
| 129 |
0 |
} |
| 130 |
|
|
| 131 |
|
|
| 132 |
|
|
| 133 |
|
|
| 134 |
|
public void backspace() |
| 135 |
|
{ |
| 136 |
0 |
content_.backspace(); |
| 137 |
0 |
showCursor(); |
| 138 |
0 |
} |
| 139 |
|
|
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
|
public void accept( final ILineVisitor visitor, final int y ) |
| 144 |
|
{ |
| 145 |
0 |
content_.accept( new TranslatedLineVisitor( visitor, scroll_.x_ ), y + scroll_.y_ ); |
| 146 |
0 |
} |
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
| 150 |
|
|
| 151 |
|
public void register( final IView view ) |
| 152 |
|
{ |
| 153 |
35 |
if( view == null ) |
| 154 |
0 |
throw new IllegalArgumentException( "parameter 'view' is null" ); |
| 155 |
35 |
view_ = view; |
| 156 |
35 |
} |
| 157 |
|
|
| 158 |
|
|
| 159 |
|
|
| 160 |
|
|
| 161 |
|
public void scroll( final int columns, final int lines ) |
| 162 |
|
{ |
| 163 |
30 |
scroll_.x_ += columns; |
| 164 |
30 |
scroll_.y_ += lines; |
| 165 |
30 |
center(); |
| 166 |
30 |
} |
| 167 |
|
|
| 168 |
|
|
| 169 |
|
|
| 170 |
|
|
| 171 |
|
public void update( final Coordinate position ) |
| 172 |
|
{ |
| 173 |
10 |
cursorPosition_.x_ = position.x_; |
| 174 |
10 |
cursorPosition_.y_ = position.y_; |
| 175 |
10 |
view_.update( new Coordinate( cursorPosition_.x_ - scroll_.x_, cursorPosition_.y_ - scroll_.y_ ) ); |
| 176 |
10 |
} |
| 177 |
|
|
| 178 |
|
|
| 179 |
|
|
| 180 |
|
|
| 181 |
|
public void update( final int columns, final int lines ) |
| 182 |
|
{ |
| 183 |
35 |
maxColumns_ = columns; |
| 184 |
35 |
maxLines_ = lines; |
| 185 |
35 |
center(); |
| 186 |
35 |
} |
| 187 |
|
|
| 188 |
|
|
| 189 |
|
|
| 190 |
|
|
| 191 |
|
public void modified( final boolean status ) |
| 192 |
|
{ |
| 193 |
0 |
view_.modified( status ); |
| 194 |
0 |
} |
| 195 |
|
|
| 196 |
|
|
| 197 |
|
|
| 198 |
|
|
| 199 |
|
public void resize( final int columns, final int lines ) |
| 200 |
|
{ |
| 201 |
35 |
visibleColumns_ = columns; |
| 202 |
35 |
visibleLines_ = lines; |
| 203 |
35 |
center(); |
| 204 |
35 |
} |
| 205 |
|
|
| 206 |
|
private void center() |
| 207 |
|
{ |
| 208 |
100 |
scroll_.x_ = Math.max( 0, Math.min( scroll_.x_, maxColumns_ - visibleColumns_ ) ); |
| 209 |
100 |
scroll_.y_ = Math.max( 0, Math.min( scroll_.y_, maxLines_ - visibleLines_ ) ); |
| 210 |
100 |
view_.update( new Coordinate( cursorPosition_.x_ - scroll_.x_, cursorPosition_.y_ - scroll_.y_ ) ); |
| 211 |
100 |
view_.update( maxColumns_, maxLines_ ); |
| 212 |
100 |
final Enumeration e = views_.elements(); |
| 213 |
200 |
while( e.hasMoreElements() ) |
| 214 |
100 |
((IScrollView)e.nextElement()).scrolled( scroll_ ); |
| 215 |
100 |
} |
| 216 |
|
|
| 217 |
|
|
| 218 |
|
|
| 219 |
|
|
| 220 |
|
public void register( final IScrollView view ) |
| 221 |
|
{ |
| 222 |
35 |
if( view == null ) |
| 223 |
0 |
throw new IllegalArgumentException( "parameter 'view' is null" ); |
| 224 |
35 |
views_.addElement( view ); |
| 225 |
35 |
view.scrolled( scroll_ ); |
| 226 |
35 |
} |
| 227 |
|
|
| 228 |
|
|
| 229 |
|
|
| 230 |
|
|
| 231 |
|
public void unmarshall( final InputStream stream ) throws IOException |
| 232 |
|
{ |
| 233 |
0 |
scroll_.unmarshall( stream ); |
| 234 |
0 |
content_.unmarshall( stream ); |
| 235 |
0 |
} |
| 236 |
|
|
| 237 |
|
|
| 238 |
|
|
| 239 |
|
|
| 240 |
|
public void marshall( final OutputStream stream ) throws IOException |
| 241 |
|
{ |
| 242 |
0 |
scroll_.marshall( stream ); |
| 243 |
0 |
content_.marshall( stream ); |
| 244 |
0 |
} |
| 245 |
|
|
| 246 |
|
|
| 247 |
|
|
| 248 |
|
|
| 249 |
|
public void clear() |
| 250 |
|
{ |
| 251 |
0 |
content_.clear(); |
| 252 |
0 |
} |
| 253 |
|
|
| 254 |
|
private void showCursor() |
| 255 |
|
{ |
| 256 |
10 |
scroll( nearestColumn( cursorPosition_.x_ - scroll_.x_ ), nearestLine( cursorPosition_.y_ - scroll_.y_ ) ); |
| 257 |
10 |
} |
| 258 |
|
|
| 259 |
|
private int nearestLine( final int y ) |
| 260 |
|
{ |
| 261 |
10 |
if( visibleLines_ > 0 ) |
| 262 |
|
{ |
| 263 |
10 |
if( y >= visibleLines_ ) |
| 264 |
5 |
return y - visibleLines_ + 1; |
| 265 |
5 |
else if( y < 0 ) |
| 266 |
5 |
return y; |
| 267 |
|
} |
| 268 |
0 |
return 0; |
| 269 |
|
} |
| 270 |
|
|
| 271 |
|
private int nearestColumn( final int x ) |
| 272 |
|
{ |
| 273 |
10 |
if( visibleColumns_ > 0 ) |
| 274 |
|
{ |
| 275 |
10 |
if( x > visibleColumns_ ) |
| 276 |
5 |
return x - visibleColumns_; |
| 277 |
5 |
else if( x <= 0 ) |
| 278 |
5 |
return x - 1; |
| 279 |
|
} |
| 280 |
0 |
return 0; |
| 281 |
|
} |
| 282 |
|
|
| 283 |
|
|
| 284 |
|
|
| 285 |
|
|
| 286 |
|
public void copy( final OutputStream stream ) throws IOException |
| 287 |
|
{ |
| 288 |
0 |
content_.copy( stream ); |
| 289 |
0 |
} |
| 290 |
|
|
| 291 |
|
|
| 292 |
|
|
| 293 |
|
|
| 294 |
|
public void cut( final OutputStream stream ) throws IOException |
| 295 |
|
{ |
| 296 |
0 |
content_.cut( stream ); |
| 297 |
0 |
showCursor(); |
| 298 |
0 |
} |
| 299 |
|
|
| 300 |
|
|
| 301 |
|
|
| 302 |
|
|
| 303 |
|
public void paste( final InputStream stream ) throws IOException |
| 304 |
|
{ |
| 305 |
0 |
content_.paste( stream ); |
| 306 |
0 |
showCursor(); |
| 307 |
0 |
} |
| 308 |
|
|
| 309 |
|
|
| 310 |
|
|
| 311 |
|
|
| 312 |
|
public void undo() |
| 313 |
|
{ |
| 314 |
0 |
content_.undo(); |
| 315 |
0 |
showCursor(); |
| 316 |
0 |
} |
| 317 |
|
|
| 318 |
|
|
| 319 |
|
|
| 320 |
|
|
| 321 |
|
public void redo() |
| 322 |
|
{ |
| 323 |
0 |
content_.redo(); |
| 324 |
0 |
showCursor(); |
| 325 |
0 |
} |
| 326 |
|
} |