private void updateSelectionPosition(String newRawText) { // Adjust the selection if (isInsertingNewCharacter(newRawText) || replacedSelectedText) { // Find the position after where the new character was actually inserted int selectionDelta = editMaskParser.getNextInputPosition(oldSelection) - oldSelection; if (selectionDelta == 0) { selectionDelta = editMaskParser.getNextInputPosition(selection) - selection; } selection += selectionDelta; } // Did we just type something that was accepted by the mask? if (!newRawText.equals(oldRawText)) { // yep // If the user hits <end>, bounce them back to the end of their actual input int firstIncompletePosition = editMaskParser.getFirstIncompleteInputPosition(); if (firstIncompletePosition > 0 && selection > firstIncompletePosition) selection = firstIncompletePosition; text.setSelection(new Point(selection, selection)); } else { // nothing was accepted by the mask // Either we backspaced over a literal or we typed an illegal character if (selection > oldSelection) { // typed an illegal character; backup text.setSelection(new Point(selection - 1, selection - 1)); } else { // backspaced over a literal; don't interfere with selection position text.setSelection(new Point(selection, selection)); } } oldRawText = newRawText; }
private void selectFileName() { String text = fileText.getText(); if (text.endsWith(".zwibbler")) { int lastDot = text.lastIndexOf('.'); fileText.setSelection(0, lastDot); } }
public void setSelection(int start, int end) { m_Text.setSelection(start, end); m_Text.showSelection(); // May have scrolled, so need to redraw the icons m_IconBar.redraw(); }
@Override public void create() { super.create(); setTitle(UIText.BranchRenameDialog_Title); String oldName = branchToRename.getName(); String prefix; if (oldName.startsWith(Constants.R_HEADS)) prefix = Constants.R_HEADS; else if (oldName.startsWith(Constants.R_REMOTES)) prefix = Constants.R_REMOTES; else prefix = null; String shortName = null; if (prefix != null) { shortName = Repository.shortenRefName(branchToRename.getName()); setMessage(NLS.bind(UIText.BranchRenameDialog_Message, shortName)); } else setErrorMessage(NLS.bind(UIText.BranchRenameDialog_WrongPrefixErrorMessage, oldName)); if (shortName != null) { name.setText(shortName); name.setSelection(0, shortName.length()); } final IInputValidator inputValidator = ValidationUtils.getRefNameInputValidator(repository, prefix, true); name.addModifyListener( new ModifyListener() { public void modifyText(ModifyEvent e) { String error = inputValidator.isValid(name.getText()); setErrorMessage(error); getButton(OK).setEnabled(error == null); } }); getButton(OK).setEnabled(false); }
/** * Sets the selection to the range specified by the given point, where the x coordinate represents * the start index and the y coordinate represents the end index. * * <p>Indexing is zero based. The range of a selection is from 0..N where N is the number of * characters in the widget. * * <p>Text selections are specified in terms of caret positions. In a text widget that contains N * characters, there are N+1 caret positions, ranging from 0..N. This differs from other functions * that address character position such as getText () that use the usual array indexing rules. * * @param selection the point * @exception IllegalArgumentException * <ul> * <li>ERROR_NULL_ARGUMENT - if the point is null * </ul> * * @exception SWTException * <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver * </ul> */ public void setSelection(Point selection) { checkWidget(); if (selection == null) { error(SWT.ERROR_NULL_ARGUMENT); } setSelection(selection.x, selection.y); }
// TODO [bm] extend testcase with newline chars for SWT.MULTI @Test public void testAppend() { Text text = new Text(shell, SWT.SINGLE); try { text.append(null); fail("No exception thrown for string == null"); } catch (IllegalArgumentException e) { } text = new Text(shell, SWT.SINGLE); try { text.append(null); fail("No exception thrown on string == null"); } catch (IllegalArgumentException e) { } // tests a SINGLE line text editor text = new Text(shell, SWT.SINGLE); text.setText("01"); text.append("23"); assertEquals("0123", text.getText()); text.append("45"); assertEquals("012345", text.getText()); text.setSelection(0); text.append("67"); assertEquals("01234567", text.getText()); }
/* * @see DialogField#setFocus */ @Override public boolean setFocus() { if (isOkToUse(fTextControl)) { fTextControl.setFocus(); fTextControl.setSelection(0, fTextControl.getText().length()); } return true; }
@Override protected void doSetFocus() { super.doSetFocus(); if (!_selectAll) { final Text text = getControl(); text.setSelection(text.getText().length()); } }
@Test public void testGetCaretPosition() { Text text = new Text(shell, SWT.SINGLE); text.setText("Sample text"); assertEquals(0, text.getCaretPosition()); text.setSelection(5); assertEquals(5, text.getCaretPosition()); text.setSelection(3, 8); assertEquals(3, text.getCaretPosition()); text.setSelection(8, 5); assertEquals(5, text.getCaretPosition()); text.setText("New text"); assertEquals(0, text.getCaretPosition()); text.setSelection(3, 8); text.clearSelection(); assertEquals(new Point(8, 8), text.getSelection()); assertEquals(8, text.getCaretPosition()); }
@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); } }
// 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()); } }
private void preselectFileNameText() { fileNameText.setFocus(); if (getDefaultFileName() != null && getDefaultFileName().length() > 0) { int lastIndexOf = getDefaultFileName().indexOf("."); // $NON-NLS-1$ if (lastIndexOf == -1) { lastIndexOf = getDefaultFileName().length(); } fileNameText.setSelection(0, lastIndexOf); } }
/** * ************************************************************************* Add a text message to * the display ************************************************************************ */ protected void addDisplayMessage(String message) { String text = m_display.getText(); // Take into account wether the display is empty or not if (text.length() > 0) { text += m_display.getLineDelimiter() + message; } else { text = message; } m_display.setText(text); m_display.setSelection(text.length()); }
/** Limits the length of the SMS textarea to an appropriate value */ private void messageKeyReleased(KeyEvent evt) { int length = message.getText().length(); if (length > getMaxSMSLengthPossible(app.getMaxSMSpossible())) { message.setText( message.getText().substring(0, getMaxSMSLengthPossible(app.getMaxSMSpossible()))); message.setSelection(getMaxSMSLengthPossible(app.getMaxSMSpossible())); length = getMaxSMSLengthPossible(app.getMaxSMSpossible()); } smsCount.setText(new Integer(calculateSMSCount(length)).toString()); numberCharsLeft.setText( new Integer(getMaxSMSLengthPossible(app.getMaxSMSpossible()) - length).toString()); }
/** * Mark D I added this as I am trying to make it focus on the right item when you open the dialog */ private void setFilenameFocus() { // TODO: this works if you do a right click but not if you come here from a select your page // type page fileText.setFocus(); System.out.println("setting the focus"); int textLen = fileText.getText().length() - 4; fileText.setSelection(0, textLen); }
// 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); } }
public void keyPressed(KeyEvent event) { String value = consoleWidget.getText(); String newValue = value; if (backSpacePressed(event)) { newValue = removeLastCommandCharacter(value); } else if (anyKeyPressed(event)) { newValue = appendLastCommandCharacter(event, value); } if (!enterPressed(event)) { event.doit = false; } consoleWidget.setText(newValue); consoleWidget.setSelection(consoleWidget.getText().length()); consoleWidget.getParent().layout(true, true); }
@Override public void setVisible(boolean visible) { if (visible) { INameUpdating nameUpdating = (INameUpdating) getRefactoring().getAdapter(INameUpdating.class); if (nameUpdating != null) { String newName = getNewName(nameUpdating); if (newName != null && newName.length() > 0 && !newName.equals(getInitialValue())) { Text textField = getTextField(); textField.setText(newName); textField.setSelection(0, newName.length()); } } } super.setVisible(visible); }
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); Text text = new Text(shell, SWT.BORDER | SWT.V_SCROLL); text.setBounds(10, 10, 100, 100); for (int i = 0; i < 16; i++) { text.append("Line " + i + "\n"); } shell.open(); text.setSelection(30, 38); System.out.println(text.getCaretLocation()); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
void displayTextTypeUi() { normalizeTypes(); String value = model.getText(); if (value == null) { value = ""; // $NON-NLS-1$ } constructTextWidgets(); if (!txfValue.getText().equals(value)) { txfValue.setText(value); // causes caret to be placed at the beginning txfValue.setSelection(value.length()); // place caret at end } showControl(pnlText); }
/** * Creates the controls for chart configuration. * * @param parent The parent composite */ private void createChartConfigControls(Composite parent) { Composite composite = new Composite(parent, SWT.NONE); GridLayout layout = new GridLayout(2, false); composite.setLayout(layout); composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); new Label(composite, SWT.NONE).setText(Messages.chartTitleLabel); chartTitleText = new Text(composite, SWT.BORDER); chartTitleText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); chartTitleText.setText(chartTitle); chartTitleText.setSelection(0, chartTitle.length()); chartTitleText.addModifyListener( new ModifyListener() { @Override public void modifyText(ModifyEvent e) { validate(chartTitleText.getText()); } }); new Label(composite, SWT.NONE).setText(Messages.yAxisUnitLabel); axisUnitCombo = new Combo(composite, SWT.READ_ONLY); List<String> items = new ArrayList<String>(); int initialSelection = -1; AxisUnit[] values = AxisUnit.values(); for (int i = 0; i < values.length; i++) { items.add(values[i].name()); if (values[i] == unit) { initialSelection = i; } } axisUnitCombo.setItems(items.toArray(new String[items.size()])); if (initialSelection == -1) { initialSelection = items.size() - 1; } axisUnitCombo.select(initialSelection); axisUnitCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); }
@Test public void testSelection() { // test select all text.setText("abc"); text.selectAll(); assertEquals(new Point(0, 3), text.getSelection()); assertEquals("abc", text.getSelectionText()); // test clearSelection text.setText("abc"); text.clearSelection(); assertEquals(new Point(0, 0), text.getSelection()); assertEquals("", text.getSelectionText()); // test setSelection text.setText("abc"); text.setSelection(1); assertEquals(new Point(1, 1), text.getSelection()); assertEquals(0, text.getSelectionCount()); assertEquals("", text.getSelectionText()); text.setSelection(1000); assertEquals(new Point(3, 3), text.getSelection()); assertEquals(0, text.getSelectionCount()); assertEquals("", text.getSelectionText()); Point saveSelection = text.getSelection(); text.setSelection(-1); assertEquals(saveSelection, text.getSelection()); assertEquals(0, text.getSelectionCount()); assertEquals("", text.getSelectionText()); text.setText("abcdefg"); text.setSelection(new Point(5, 2)); assertEquals(new Point(2, 5), text.getSelection()); // test selection when changing text text.setText("abcefg"); text.setSelection(1, 2); text.setText("gfecba"); assertEquals(new Point(0, 0), text.getSelection()); // ... even setting the same text again will clear the selection text.setText("abcefg"); text.setSelection(1, 2); text.setText(text.getText()); assertEquals(new Point(0, 0), text.getSelection()); }
private void initialize() { nameText.setText(annotation.getName()); nameText.setSelection(0, nameText.getText().length()); snapToTrace.setSelection(!annotation.isFree()); xAxisLabel.setText( snapToTrace.getSelection() ? Messages.Annotation_Trace : Messages.Annotation_XAxis); xAxisOrTraceCombo.removeAll(); if (!annotation.isFree()) { for (Trace trace : xyGraph.getPlotArea().getTraceList()) xAxisOrTraceCombo.add(trace.getName()); xAxisOrTraceCombo.select(xyGraph.getPlotArea().getTraceList().indexOf(annotation.getTrace())); } else { for (Axis axis : xyGraph.getXAxisList()) xAxisOrTraceCombo.add(axis.getTitle()); xAxisOrTraceCombo.select(xyGraph.getXAxisList().indexOf(annotation.getXAxis())); } for (Axis axis : xyGraph.getYAxisList()) yAxisCombo.add(axis.getTitle()); yAxisCombo.select(xyGraph.getYAxisList().indexOf(annotation.getYAxis())); yAxisLabel.setVisible(!snapToTrace.getSelection()); yAxisCombo.setVisible(!snapToTrace.getSelection()); useDefaultColorButton.setSelection(annotation.getAnnotationColor() == null); colorLabel.setVisible(!useDefaultColorButton.getSelection()); colorSelector.getButton().setVisible(annotation.getAnnotationColor() != null); colorSelector.setColorValue( annotation.getAnnotationColor() == null ? annotation.getYAxis().getForegroundColor().getRGB() : annotation.getAnnotationColor().getRGB()); fontLabel.setText( Messages.Annotation_Font + (font == null ? Messages.Annotation_SystemDefault : font.getFontData()[0].getName())); fontLabel.setFont(font); cursorLineCombo.select(annotation.getCursorLineStyle().getIndex()); showNameButton.setSelection(annotation.isShowName()); showSampleInfoButton.setSelection(annotation.isShowSampleInfo()); showPositionButton.setSelection(annotation.isShowPosition()); }
/* * (non-Javadoc) * * @see org.eclipse.jface.fieldassist.IControlContentAdapter#setCursorPosition(org.eclipse.swt.widgets.Control, * int) */ public void setCursorPosition(Control control, int position) { ((Text) control).setSelection(new Point(position, position)); }
/** * Sets the selection. * * <p>Indexing is zero based. The range of a selection is from 0..N where N is the number of * characters in the widget. * * <p>Text selections are specified in terms of caret positions. In a text widget that contains N * characters, there are N+1 caret positions, ranging from 0..N. This differs from other functions * that address character position such as getText () that use the regular array indexing rules. * * @param start new caret position * @exception SWTException * <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver * </ul> */ public void setSelection(int start) { checkWidget(); start = Math.min(Math.max(0, start), getCharCount()); setSelection(start, start); }
@Override public void focusGained(FocusEvent e) { commandLineTxt.setSelection(commandLineTxt.getText().length()); }
/** * Init with listener * * @param azureus_core * @param parent * @param linkURL * @param referrer * @param listener */ public OpenUrlWindow( final Shell parent, String linkURL, boolean default_magnet, final String referrer, final TorrentDownloaderCallBackInterface listener) { final Shell shell = ShellFactory.createShell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.RESIZE); shell.setText(MessageText.getString("openUrl.title")); Utils.setShellIcon(shell); GridData gridData; GridLayout layout = new GridLayout(); layout.numColumns = 3; shell.setLayout(layout); // URL field Label label = new Label(shell, SWT.NULL); label.setText(MessageText.getString("openUrl.url")); gridData = new GridData(); label.setLayoutData(gridData); final Text url = new Text(shell, SWT.BORDER); gridData = new GridData(GridData.FILL_HORIZONTAL); gridData.widthHint = 400; gridData.horizontalSpan = 2; url.setLayoutData(gridData); if (linkURL == null) Utils.setTextLinkFromClipboard(shell, url, true, default_magnet); else url.setText(linkURL); url.setSelection(url.getText().length()); // help field Label help_label = new Label(shell, SWT.NULL); help_label.setText(MessageText.getString("openUrl.url.info")); gridData = new GridData(); gridData.horizontalSpan = 3; help_label.setLayoutData(gridData); Label space = new Label(shell, SWT.NULL); gridData = new GridData(); gridData.horizontalSpan = 3; space.setLayoutData(gridData); // referrer field Label referrer_label = new Label(shell, SWT.NULL); referrer_label.setText(MessageText.getString("openUrl.referrer")); gridData = new GridData(); referrer_label.setLayoutData(gridData); final Combo referrer_combo = new Combo(shell, SWT.BORDER); gridData = new GridData(GridData.FILL_HORIZONTAL); gridData.widthHint = 150; gridData.grabExcessHorizontalSpace = true; referrer_combo.setLayoutData(gridData); final StringList referrers = COConfigurationManager.getStringListParameter("url_open_referrers"); StringIterator iter = referrers.iterator(); while (iter.hasNext()) { referrer_combo.add(iter.next()); } if (referrer != null && referrer.length() > 0) { referrer_combo.setText(referrer); } else if (last_referrer != null) { referrer_combo.setText(last_referrer); } Label referrer_info = new Label(shell, SWT.NULL); referrer_info.setText(MessageText.getString("openUrl.referrer.info")); // line Label labelSeparator = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL); gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_END); gridData.horizontalSpan = 3; labelSeparator.setLayoutData(gridData); // buttons Composite panel = new Composite(shell, SWT.NULL); layout = new GridLayout(); layout.numColumns = 3; panel.setLayout(layout); gridData = new GridData( GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_END | GridData.VERTICAL_ALIGN_END); gridData.horizontalSpan = 3; gridData.grabExcessHorizontalSpace = true; panel.setLayoutData(gridData); new Label(panel, SWT.NULL); Button ok = new Button(panel, SWT.PUSH); gridData = new GridData( GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_END | GridData.VERTICAL_ALIGN_END); gridData.widthHint = 70; gridData.grabExcessHorizontalSpace = true; ok.setLayoutData(gridData); ok.setText(MessageText.getString("Button.ok")); ok.addListener( SWT.Selection, new Listener() { public void handleEvent(Event e) { last_referrer = referrer_combo.getText().trim(); if (!referrers.contains(last_referrer)) { referrers.add(last_referrer); COConfigurationManager.setParameter("url_open_referrers", referrers); COConfigurationManager.save(); } COConfigurationManager.setParameter(CONFIG_REFERRER_DEFAULT, last_referrer); COConfigurationManager.save(); String url_str = url.getText(); url_str = UrlUtils.parseTextForURL(url_str, true); if (url_str == null) { url_str = UrlUtils.parseTextForMagnets(url.getText()); } if (url_str == null) { url_str = url.getText(); } new FileDownloadWindow(parent, url_str, last_referrer, null, null, listener); shell.dispose(); } }); shell.setDefaultButton(ok); Button cancel = new Button(panel, SWT.PUSH); gridData = new GridData(GridData.HORIZONTAL_ALIGN_END); gridData.grabExcessHorizontalSpace = false; gridData.widthHint = 70; cancel.setLayoutData(gridData); cancel.setText(MessageText.getString("Button.cancel")); cancel.addListener( SWT.Selection, new Listener() { public void handleEvent(Event e) { shell.dispose(); } }); shell.addListener( SWT.Traverse, new Listener() { public void handleEvent(Event e) { if (e.character == SWT.ESC) { shell.dispose(); } } }); Point p = shell.computeSize(SWT.DEFAULT, SWT.DEFAULT); if (p.x > 800) { p.x = 800; } shell.setSize(p); Utils.createURLDropTarget(shell, url); Utils.centreWindow(shell); shell.open(); }
/* * (non-Javadoc) * * @see org.eclipse.jface.fieldassist.IControlContentAdapter#setControlContents(org.eclipse.swt.widgets.Control, * java.lang.String, int) */ public void setControlContents(Control control, String text, int cursorPosition) { ((Text) control).setText(text); ((Text) control).setSelection(cursorPosition, cursorPosition); }