1 26 27 package swingwtx.swing.text; 28 29 import org.eclipse.swt.SWT; 30 import org.eclipse.swt.widgets.Text; 31 32 import swingwtx.swing.SwingUtilities; 33 import swingwtx.swing.SwingWTUtils; 34 35 46 public class JTextComponent extends swingwtx.swing.JComponent { 47 48 protected Text ppeer = null; 49 protected String pText = ""; 50 protected boolean pEditable = true; 51 protected boolean pWordWrap = false; 52 protected int pRows = 0; 53 protected int pCols = 0; 54 protected int selStart = -1; 55 protected int selEnd = -1; 56 protected Caret caret = new DefaultCaret(); 57 protected Document doc = null; 58 protected PlainView view = null; 59 60 61 protected String retVal = ""; 62 protected int iRetVal = 0; 63 64 public JTextComponent() { 65 doc = new PlainDocument(); 66 view = new PlainView(doc, this); 67 } 68 69 public Document getDocument() { 70 return doc; 71 } 72 73 public void setDocument(Document doc) { 74 this.doc = doc; 75 view.setDocument(doc); 76 } 77 78 public String getText() { 79 retVal=""; 80 SwingUtilities.invokeSync(new Runnable () { 81 public void run() { 82 if (SwingWTUtils.isSWTControlAvailable(ppeer)) 83 retVal = ppeer.getText(); 84 else retVal = pText; 85 } 86 }); return retVal; 87 } 88 89 public void setText(String text) { 90 if (text == null) 91 pText = ""; 92 else 93 pText = text; 94 SwingUtilities.invokeSync(new Runnable () { 95 public void run() { 96 if (SwingWTUtils.isSWTControlAvailable(ppeer)) 97 ppeer.setText(pText);} 98 }); 99 } 100 101 public void setEditable(boolean b) { pEditable = b; if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.setEditable(b); } 102 public boolean isEditable() { return pEditable; } 103 104 public int getCaretPosition() { 105 iRetVal = 0; 106 SwingUtilities.invokeSync(new Runnable () { 107 public void run() { 108 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) iRetVal = -1; else iRetVal = ppeer.getCaretPosition(); 109 } 110 }); 111 return iRetVal; 112 } 113 114 public void setCaretPosition(final int pos) { 115 SwingUtilities.invokeSync(new Runnable () { 116 public void run() { 117 if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.setSelection(pos); 118 } 119 }); 120 } 121 122 public void copy() { 123 SwingUtilities.invokeSync(new Runnable () { 124 public void run() { 125 if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.copy(); 126 } 127 }); 128 } 129 130 public void cut() { 131 SwingUtilities.invokeSync(new Runnable () { 132 public void run() { 133 if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.cut(); 134 } 135 }); 136 } 137 138 public void paste() { 139 SwingUtilities.invokeSync(new Runnable () { 140 public void run() { 141 if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.paste(); 142 } 143 }); 144 } 145 146 public Caret getCaret() { 147 return caret; 148 } 149 150 public void setCaret(Caret caret) { 151 this.caret = caret; 152 } 153 154 155 public void replaceSelection(String content) { 156 } 157 158 159 public void select(final int selectionStart, final int selectionEnd) { 160 SwingUtilities.invokeSync(new Runnable () { 161 public void run() { 162 if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.setSelection(selectionStart, selectionEnd); 163 } 164 }); 165 } 166 167 public void selectAll() { 168 SwingUtilities.invokeSync(new Runnable () { 169 public void run() { 170 if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.selectAll(); 171 } 172 }); 173 } 174 175 public void setSelectionStart(int start) { 176 selStart = start; 177 if (selStart > 0 && selEnd > 0) 178 select(selStart, selEnd); 179 } 180 181 public void setSelectionEnd(int end) { 182 selEnd = end; 183 if (selStart > 0 && selEnd > 0) 184 select(selStart, selEnd); 185 } 186 187 188 public void setSelectedTextColor(swingwt.awt.Color c) { } 189 190 public void setSelectionColor(swingwt.awt.Color c) { } 191 192 197 public void setSwingWTParent(swingwt.awt.Container parent) throws Exception { 198 ppeer = new Text(parent.getComposite(), SWT.BORDER | SWT.SINGLE); 199 ppeer.setText(pText); 200 ppeer.setEditable(pEditable); 201 peer = ppeer; 202 this.parent = parent; 203 } 204 205 public void addInputMethodListener() { 206 207 } 208 } 209 | Popular Tags |