@Test public void testInsertWithModifyListener() { final java.util.List<ModifyEvent> log = new ArrayList<ModifyEvent>(); Text text = new Text(shell, SWT.SINGLE); text.setBounds(0, 0, 100, 20); text.addModifyListener( new ModifyListener() { public void modifyText(ModifyEvent event) { log.add(event); } }); // Test that event is fired when correctly using insert log.clear(); text.insert("abc"); assertEquals(1, log.size()); // Test that event is *not* fired when passing illegal argument to insert log.clear(); text = new Text(shell, SWT.SINGLE); try { text.insert(null); fail("No exception thrown on string == null"); } catch (IllegalArgumentException e) { } assertEquals(0, log.size()); }
protected void onInsertVariable() { StringVariableSelectionDialog dlg = new StringVariableSelectionDialog(getShell()); if (dlg.open() == Window.OK) { String var = dlg.getVariableExpression(); fDestinationField.insert(var); } }
/** The working dir variables button has been selected */ private void handleWorkingDirVariablesButtonSelected() { StringVariableSelectionDialog dialog = new StringVariableSelectionDialog(getShell()); dialog.open(); String variableText = dialog.getVariableExpression(); if (variableText != null) { fOtherWorkingText.insert(variableText); } }
@Test public void testSelectionAfterInsertText() { text.setText("foobar"); text.setSelection(3); text.insert("xxx"); assertEquals(new Point(6, 6), text.getSelection()); }
/* * (non-Javadoc) * * @see org.eclipse.jface.fieldassist.IControlContentAdapter#insertControlContents(org.eclipse.swt.widgets.Control, * java.lang.String, int) */ public void insertControlContents(Control control, String text, int cursorPosition) { Point selection = ((Text) control).getSelection(); ((Text) control).insert(text); // Insert will leave the cursor at the end of the inserted text. If this // is not what we wanted, reset the selection. if (cursorPosition < text.length()) { ((Text) control).setSelection(selection.x + cursorPosition, selection.x + cursorPosition); } }
public void widgetSelected(SelectionEvent e) { // String fileName = Util.substituteVar(fPomDirName.getText()); // if(!isDirectoryExist(fileName)) { // MessageDialog.openError(getShell(), Messages.getString("launch.errorPomMissing"), // Messages.getString("launch.errorSelectPom")); //$NON-NLS-1$ //$NON-NLS-2$ // return; // } MavenGoalSelectionDialog dialog = new MavenGoalSelectionDialog(shell); int rc = dialog.open(); if (rc == IDialogConstants.OK_ID) { text.insert(""); // clear selected text String txt = text.getText(); int len = txt.length(); int pos = text.getCaretPosition(); StringBuffer sb = new StringBuffer(); if ((pos > 0 && txt.charAt(pos - 1) != ' ')) { sb.append(' '); } String sep = ""; Object[] o = dialog.getResult(); for (int i = 0; i < o.length; i++) { if (o[i] instanceof MavenGoalSelectionDialog.Entry) { if (dialog.isQualifiedName()) { sb.append(sep).append(((MavenGoalSelectionDialog.Entry) o[i]).getQualifiedName()); } else { sb.append(sep).append(((MavenGoalSelectionDialog.Entry) o[i]).getName()); } } sep = " "; } if (pos < len && txt.charAt(pos) != ' ') { sb.append(' '); } text.insert(sb.toString()); text.setFocus(); } }
// TODO [bm] extend testcase with newline chars and getLineCount @Test public void testInsert() { // Test insert on multi-line Text Text text = new Text(shell, SWT.MULTI); text.setBounds(0, 0, 500, 500); // Ensure initial state assertEquals("", text.getText()); // Test with allowed arguments text.insert(""); assertEquals("", text.getText()); text.insert("fred"); assertEquals("fred", text.getText()); text.setSelection(2); text.insert("helmut"); assertEquals("frhelmuted", text.getText()); // Test with illegal argument try { text.setText("oldText"); text.insert(null); fail("No exception thrown on string == null"); } catch (IllegalArgumentException e) { assertEquals("oldText", text.getText()); } // Test insert on single-line Text text = new Text(shell, SWT.SINGLE); assertEquals("", text.getText()); text.insert(""); assertEquals("", text.getText()); text.insert("fred"); assertEquals("fred", text.getText()); text.setSelection(2); text.insert("helmut"); assertEquals("frhelmuted", text.getText()); // Test with illegal arguments text = new Text(shell, SWT.SINGLE); try { text.setText("oldText"); text.insert(null); fail("No exception thrown on string == null"); } catch (IllegalArgumentException e) { assertEquals("oldText", text.getText()); } }
// Separating out the call to append text to a widget so we can work on its // behavior. // We'd like this to not scroll if the cursor is not at the bottom of the // window // and SWT's append method always scrolls. private void appendTextToWidget(Text widget, String text) { // If set this to true we just use standard append // and always scroll. boolean kUseAppend = false; if (kUseAppend) { widget.append(text); } else { // A more sophisticated routine for inserting text // at the end of the widget without scrolling unless // current selection is already at the end. Point currentSelection = widget.getSelection(); int length = widget.getCharCount(); if (currentSelection.x == length) widget.append(text); else { // The calls to setRedraw are needed because there's a bug // on SWT in Windows where setSelection() calls scroll caret // always // which it really shouldn't. So we need to suppress that // behavior by // turning redraw on and off. However that incurs the cost of // redrawing the // entire widget in the setRedraw(true) call which shouldn't be // needed. // Because of this we only use this code if the selection isn't // at the end // of the widget. widget.setRedraw(false); widget.setSelection(length, length); widget.insert(text); widget.setSelection(currentSelection); widget.setRedraw(true); if (currentSelection.x == length) widget.showSelection(); } } }
public void insertControlContents(Control control, String text, int cursorPosition) { Point selection = ((Text) control).getSelection(); int posMarker = selection.y - 1; if (cursorPosition != 0) { String constraintText = ((Text) control).getText(); while (posMarker >= 0 && (constraintText.charAt(posMarker) != ' ' && constraintText.charAt(posMarker) != ')' && constraintText.charAt(posMarker) != '(')) { posMarker--; } } selection.x = posMarker + 1; ((Text) control).setSelection(selection); ((Text) control).insert(text); // Insert will leave the cursor at the end of the inserted text. If this // is not what we wanted, reset the selection. if (cursorPosition < text.length()) { ((Text) control).setSelection(selection.x + cursorPosition, selection.x + cursorPosition); } }