1 22 23 27 28 package org.netbeans.lib.terminalemulator; 29 30 import java.awt.*; 31 import java.awt.event.*; 32 33 import javax.swing.*; 34 import javax.accessibility.*; 36 import javax.swing.text.AttributeSet ; 37 import javax.swing.text.SimpleAttributeSet ; 38 import javax.swing.text.MutableAttributeSet ; 39 import javax.swing.text.StyleConstants ; 40 41 49 50 class Screen extends JComponent implements Accessible { 51 private Term term; 53 private static final boolean debug = false; 54 55 public Screen(Term term, int dx, int dy) { 56 this.term = term; 57 Dimension dim = new Dimension(dx, dy); 58 setSize(dim); 59 setPreferredSize(dim); 60 62 setGrabTab(true); 63 64 if (debug) { 65 RepaintManager repaintManager = RepaintManager.currentManager(this); 68 repaintManager.setDoubleBufferingEnabled(false); 69 70 setDebugGraphicsOptions(DebugGraphics.FLASH_OPTION | 71 DebugGraphics.BUFFERED_OPTION | 72 DebugGraphics.LOG_OPTION); 73 } 74 } 75 76 78 86 87 92 public void setBounds(int x, int y, int width, int height) { 93 if (width <= 0 || height <= 0) 94 return; 95 super.setBounds(x, y, width, height); 96 } 97 98 public void setBounds(Rectangle r) { 99 super.setBounds(r); 100 } 101 102 108 public boolean OLD_isManagingFocus() { 109 return true; 110 } 111 112 128 129 private java.util.Set original_fwd_keys = null; 130 private java.util.Set original_bwd_keys = null; 131 132 133 private void setGrabTab(boolean grabTab) { 134 135 if (original_fwd_keys == null) { 136 original_fwd_keys = new java.util.HashSet (); 137 original_fwd_keys.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, 138 InputEvent.CTRL_MASK)); 139 original_bwd_keys = new java.util.HashSet (); 140 original_bwd_keys.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, 141 InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK)); 142 } 143 144 if (grabTab) { 145 setFocusTraversalKeys( 147 KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, original_fwd_keys); 148 setFocusTraversalKeys( 149 KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, original_bwd_keys); 150 151 } else { 152 setFocusTraversalKeys( 154 KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, null); 155 setFocusTraversalKeys( 156 KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, null); 157 } 158 } 159 160 public void paint(Graphics g) { 161 162 165 if (debug) { 166 g = new DebugGraphics(g, this); 171 } 172 173 term.do_paint(g); 175 } 176 177 public Dimension getMaximumSize() { 178 return new Dimension(1000, 1000); 179 } 180 181 185 private AccessibleContext accessible_context; 186 187 private AccessibleScreenText accessible_screen_text; 188 189 public AccessibleContext getAccessibleContext() { 190 if (accessible_context == null) { 191 accessible_context = new AccessibleScreen(); 192 } 193 return accessible_context; 194 } 195 196 protected class AccessibleScreenText implements AccessibleText { 197 AccessibleScreenText() { 198 } 199 200 public int getCaretPosition() { 201 return term.CoordToPosition(term.getCursorCoord()); 202 } 203 204 private int last_attr; 206 private MutableAttributeSet last_as; 207 208 public AttributeSet getCharacterAttribute(int index) { 209 Coord c = term.PositionToCoord(index); 210 if (c == null) 211 return null; 212 BCoord b = c.toBCoord(term.firsta); 213 int attr = 0; 214 try { 215 Line l = term.buf.lineAt(b.row); 216 int [] attrs = l.attrArray(); 217 attr = attrs[b.col]; 218 } catch (Exception x) { 219 ; 220 } 221 222 if (attr == last_attr) 223 return last_as; 224 225 MutableAttributeSet as = new SimpleAttributeSet (); 226 227 if ((attr & Attr.UNDERSCORE) == Attr.UNDERSCORE) 228 as.addAttribute(StyleConstants.Underline, Boolean.TRUE); 229 if ((attr & Attr.BRIGHT) == Attr.BRIGHT) 230 as.addAttribute(StyleConstants.Bold, Boolean.TRUE); 231 232 boolean reverse = ((attr & Attr.REVERSE) == Attr.REVERSE); 233 234 Color color; 235 if ((color = term.foregroundColor(reverse, attr)) != Color.black) 236 as.addAttribute(StyleConstants.Foreground, color); 237 238 if ((color = term.backgroundColor(reverse, attr)) != null) 239 as.addAttribute(StyleConstants.Background, color); 240 241 last_attr = attr; 242 last_as = as; 243 244 return as; 245 } 246 247 public Rectangle getCharacterBounds(int index) { 248 return term.getCharacterBounds(term.PositionToCoord(index)); 249 } 250 251 public int getCharCount() { 252 return term.getCharCount(); 253 } 254 255 public int getSelectionStart() { 256 Extent x = term.getSelectionExtent(); 257 if (x == null) 258 return getCaretPosition(); 259 return term.CoordToPosition(x.begin); 260 } 261 262 public int getSelectionEnd() { 263 Extent x = term.getSelectionExtent(); 264 if (x == null) 265 return getCaretPosition(); 266 return term.CoordToPosition(x.end); 267 } 268 269 public String getSelectedText() { 270 return term.getSelectedText(); 271 } 272 273 274 private String getHelper(int part, BCoord b) { 275 if (b == null) 276 return null; 277 278 Line l = term.buf.lineAt(b.row); 279 280 switch (part) { 281 case CHARACTER: 282 return new String (l.charArray(), b.col, 1); 283 case WORD: 284 BExtent bword = term.buf.find_word(term.word_delineator, b); 285 Extent word = bword.toExtent(term.firsta); 286 return term.textWithin(word.begin, word.end); 287 case SENTENCE: 288 return new String (l.charArray()); 289 } 290 return null; 291 } 292 293 public String getAfterIndex(int part, int index) { 294 Coord c = term.PositionToCoord(index); 295 if (c == null) 296 return null; 297 BCoord b = c.toBCoord(term.firsta); 298 b = term.buf.advance(b); 299 return getHelper(part, b); 300 } 301 302 public String getAtIndex(int part, int index) { 303 Coord c = term.PositionToCoord(index); 304 if (c == null) 305 return null; 306 BCoord b = c.toBCoord(term.firsta); 307 return getHelper(part, b); 308 } 309 310 public String getBeforeIndex(int part, int index) { 311 Coord c = term.PositionToCoord(index); 312 if (c == null) 313 return null; 314 BCoord b = c.toBCoord(term.firsta); 315 b = term.buf.backup(b); 316 return getHelper(part, b); 317 } 318 319 public int getIndexAtPoint(Point p) { 320 BCoord v = term.toViewCoord(p); 321 BCoord b = term.toBufCoords(v); 322 return term.CoordToPosition(new Coord(b, term.firsta)); 323 } 324 } 325 326 327 protected class AccessibleScreen extends AccessibleJComponent { 328 public String getAccessibleDescription() { 329 return "Terminal emulator"; 330 } 331 public AccessibleRole getAccessibleRole() { 332 return AccessibleRole.TEXT; 333 } 334 public AccessibleText getAccessibleText() { 335 if (accessible_screen_text == null) 336 accessible_screen_text = new AccessibleScreenText(); 337 return accessible_screen_text; 338 } 339 } 340 341 private int oldPos; 342 343 void possiblyUpdateCaretText() { 344 351 352 if (accessible_screen_text == null) 354 return; 355 356 int pos = term.CoordToPosition(term.getCursorCoord()); 357 358 accessible_context.firePropertyChange( 359 AccessibleContext.ACCESSIBLE_TEXT_PROPERTY, 360 null, new Integer (pos)); 361 363 accessible_context.firePropertyChange( 364 AccessibleContext.ACCESSIBLE_CARET_PROPERTY, 365 new Integer (pos), new Integer (oldPos)); 366 367 oldPos = pos; 368 } 369 } 370 | Popular Tags |