Java Code Examples for org.eclipse.swt.SWT#BS

The following examples show how to use org.eclipse.swt.SWT#BS . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
public ExitFlags doExit(LinkedModeModel model, VerifyEvent event, int offset, int length) {
	if (length == 0 && (event.character == SWT.BS || event.character == SWT.DEL)) {
		LinkedPosition position= model.findPosition(new LinkedPosition(fDocument, offset, 0, LinkedPositionGroup.NO_STOP));
		if (position != null) {
			if (event.character == SWT.BS) {
				if (offset - 1 < position.getOffset()) {
					//skip backspace at beginning of linked position
					event.doit= false;
				}
			} else /* event.character == SWT.DEL */ {
				if (offset + 1 > position.getOffset() + position.getLength()) {
					//skip delete at end of linked position
					event.doit= false;
				}
			}
		}
	}

	return null; // don't change behavior
}
 
Example 2
/**
 * Verify the entry and store the value in the field storedValue
 *
 * @param entry entry to check
 * @param keyCode code of the typed key
 * @return <code>true</code> if the entry if correct, <code>false</code>
 *         otherwise
 */
private boolean verifyEntryAndStoreValue(final String entry, final int keyCode) {
	final String work;
	if (keyCode == SWT.DEL) {
		work = StringUtil.removeCharAt(text.getText(), text.getCaretPosition());
	} else if (keyCode == SWT.BS && text.getCaretPosition() == 0) {
		work = StringUtil.removeCharAt(text.getText(), text.getCaretPosition() - 1);
	} else if (keyCode == 0) {
		work = entry;
	} else {
		work = StringUtil.insertString(text.getText(), entry, text.getCaretPosition());
	}

	try {
		final double d = Double.parseDouble(work.replace(decimalFormatSeparator, '.'));
		storedValue = (int) (d * Math.pow(10, getDigits()));
	} catch (final NumberFormatException nfe) {
		return false;
	}

	for (final SelectionListener s : selectionListeners) {
		s.widgetSelected(null);
	}

	return true;
}
 
Example 3
Source Project: nebula   File: PWFloatText.java    License: Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Check if an entry is a float
 *
 * @param entry text typed by the user
 * @param keyCode key code
 * @return true if the user typed a float value, false otherwise
 */
private boolean verifyEntry(final String entry, final int keyCode) {
	final String work;
	if (keyCode == SWT.DEL) {
		work = StringUtil.removeCharAt(text.getText(), text.getCaretPosition());
	} else if (keyCode == SWT.BS && text.getCaretPosition() == 0) {
		work = StringUtil.removeCharAt(text.getText(), text.getCaretPosition() - 1);
	} else if (keyCode == 0) {
		work = entry;
	} else {
		work = StringUtil.insertString(text.getText(), entry, text.getCaretPosition());
	}

	try {
		Double.parseDouble(work.replace(',', '.'));
	} catch (final NumberFormatException nfe) {
		return false;
	}

	return true;
}
 
Example 4
Source Project: nebula   File: PTFloatEditor.java    License: Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Check if an entry is a float
 *
 * @param entry text typed by the user
 * @param keyCode key code
 * @return true if the user typed a float value, false otherwise
 */
private boolean verifyEntry(final String entry, final int keyCode) {
	final String work;
	if (keyCode == SWT.DEL) {
		work = StringUtil.removeCharAt(text.getText(), text.getCaretPosition());
	} else if (keyCode == SWT.BS && text.getCaretPosition() == 0) {
		work = StringUtil.removeCharAt(text.getText(), text.getCaretPosition() - 1);
	} else if (keyCode == 0) {
		work = entry;
	} else {
		work = StringUtil.insertString(text.getText(), entry, text.getCaretPosition());
	}

	try {
		Double.parseDouble(work.replace(',', '.'));
	} catch (final NumberFormatException nfe) {
		return false;
	}

	return true;
}
 
Example 5
/**
 * @see org.eclipse.swt.events.VerifyListener#verifyText(org.eclipse.swt.events.VerifyEvent)
 */
@Override
public void verifyText(VerifyEvent e) {

  boolean doit = true;

  // only let digits pass (and del, backspace)
  if (!(Character.isDigit(e.character) || e.character == SWT.DEL || e.character == SWT.BS)) {
    doit = false;
  }

  // check if inserted text is an integer
  if (!doit) {
    try {
      Integer.parseInt(e.text);
      doit = true;
    } catch (NumberFormatException ex) {
      doit = false;
    }
  }

  e.doit = doit;
  if (!e.doit) {
    Display.getCurrent().beep();
  }
}
 
Example 6
@Override
public ExitFlags doExit(LinkedModeModel model, VerifyEvent event, int offset, int length) {
	showPreview = (event.stateMask & SWT.CTRL) != 0 && (event.character == SWT.CR || event.character == SWT.LF);
	if (length == 0 && (event.character == SWT.BS || event.character == SWT.DEL)) {
		LinkedPosition position = model.findPosition(new LinkedPosition(document, offset, 0,
				LinkedPositionGroup.NO_STOP));
		if (position != null) {
			if (event.character == SWT.BS) {
				if (offset - 1 < position.getOffset()) {
					// skip backspace at beginning of linked position
					event.doit = false;
				}
			} else /* event.character == SWT.DEL */{
				if (offset + 1 > position.getOffset() + position.getLength()) {
					// skip delete at end of linked position
					event.doit = false;
				}
			}
		}
	}
	return null; // don't change behavior
}
 
Example 7
@Override
public ExitFlags doExit(LinkedModeModel model, VerifyEvent event, int offset, int length) {
	if (length == 0 && (event.character == SWT.BS || event.character == SWT.DEL)) {
		LinkedPosition position= model.findPosition(new LinkedPosition(fDocument, offset, 0, LinkedPositionGroup.NO_STOP));
		if (position != null) {
			if (event.character == SWT.BS) {
				if (offset - 1 < position.getOffset()) {
					//skip backspace at beginning of linked position
					event.doit= false;
				}
			} else /* event.character == SWT.DEL */ {
				if (offset + 1 > position.getOffset() + position.getLength()) {
					//skip delete at end of linked position
					event.doit= false;
				}
			}
		}
	}

	return null; // don't change behavior
}
 
Example 8
private boolean isModifierKey(int keyCode)
{
	// TODO Add more non alphanumeric keys we should skip!
	switch (keyCode)
	{
		case SWT.SHIFT:
		case SWT.BS:
		case SWT.CR:
		case SWT.DEL:
		case SWT.ESC:
		case SWT.LF:
		case SWT.TAB:
		case SWT.CTRL:
		case SWT.COMMAND:
		case SWT.ALT:
		case SWT.ARROW_DOWN:
		case SWT.ARROW_LEFT:
		case SWT.ARROW_RIGHT:
		case SWT.ARROW_UP:
			return true;
	}
	return false;
}
 
Example 9
public ExitFlags doExit(LinkedModeModel model, VerifyEvent event, int offset, int length) {
	if (length == 0 && (event.character == SWT.BS || event.character == SWT.DEL)) {
		LinkedPosition position= model.findPosition(new LinkedPosition(fDocument, offset, 0, LinkedPositionGroup.NO_STOP));
		if (position != null) {
			if (event.character == SWT.BS) {
				if (offset - 1 < position.getOffset()) {
					//skip backspace at beginning of linked position
					event.doit= false;
				}
			} else /* event.character == SWT.DEL */ {
				if (offset + 1 > position.getOffset() + position.getLength()) {
					//skip delete at end of linked position
					event.doit= false;
				}
			}
		}
	}

	return null; // don't change behavior
}
 
Example 10
protected KeyCommand toKeyCommand(VerifyEvent event) {
	switch(event.character) {
	case SWT.BS: return KeyCommand.BACKSPACE; 
	case SWT.DEL: return KeyCommand.DELETE;
	case SWT.CR: return KeyCommand.ENTER;
	default: return KeyCommand.OTHER;
	}
}
 
Example 11
Source Project: nebula   File: MaskFormatter.java    License: Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Handles a <code>VerifyEvent</code> sent when the text is about to be
 * modified. This method is the entry point of all operations of formatting.
 *
 * @see org.eclipse.swt.events.VerifyListener#verifyText(org.eclipse.swt.events.VerifyEvent)
 */
public void verifyText(VerifyEvent e) {
	if (ignore) {
		return;
	}
	e.doit = false;
	if (e.keyCode == SWT.BS || e.keyCode == SWT.DEL) {
		// Clears
		clearText(e.start, (e.end > e.start) ? e.end - e.start : 1);
		updateText(editValue.toString(), e.start);
	} else {
		// Inserts
		int p;
		try {
			p = insertText(e.text, e.start);
			if (e.end - e.start > e.text.length()) {
				clearText(e.start + e.text.length(), e.end - e.start - e.text.length());
			}
		} catch (IllegalArgumentException iae) {
			beep();
			p = e.start;
		}

		// Computes new position of the cursor
		char c;
		while (p < editPattern.length()
				&& (c = editPattern.charAt(p)) != P_DIGIT
				&& c != P_ALPHANUM && c != P_UPPERCASE && c != P_LOWERCASE
				&& c != P_UHEXDIGIT && c != P_LHEXDIGIT) {
			p++;
		}

		updateText(editValue.toString(), p);
	}
}
 
Example 12
/**
 * @param offset
 * @param document
 */
private boolean isValidAutoAssistLocation(KeyEvent e, StyledText styledText)
{
	// Don't pop up CA if we pressed a Ctrl or Command character. On Linux, Unicode characters can be inserted with
	// Ctrl + Shift + u + key sequence, but at this point, all we get is the character, no modifiers.
	if (e.stateMask == SWT.MOD1)
	{
		return false;
	}

	int keyCode = e.keyCode;
	if (keyCode == SWT.ESC || keyCode == SWT.BS || keyCode == SWT.DEL || keyCode == SWT.ARROW
			|| (keyCode & SWT.KEYCODE_BIT) != 0)
	{
		return false;
	}

	int offset = styledText.getCaretOffset();
	IContentAssistProcessor processor = getProcessor(fContentAssistSubjectControlAdapter, offset);
	if (processor instanceof ICommonContentAssistProcessor)
	{
		ICommonContentAssistProcessor cp = (ICommonContentAssistProcessor) processor;
		// are we typing a valid identifier, and the previous "location" (character or lexeme) should pop up CA
		return cp.isValidIdentifier(e.character, keyCode)
				&& isAutoActivationLocation(cp, styledText, e.character, keyCode);
	}
	else
	{
		return false;
	}
}
 
Example 13
void handleKeyEvent(KeyEvent event) {
	if (event.stateMask != 0)
		return;

	if (event.keyCode == SWT.BS) {
		if (fUpAction != null && fUpAction.isEnabled()) {
			fUpAction.run();
			event.doit= false;
		}
	}
}
 
Example 14
/**
 * @see com.mulgasoft.emacsplus.minibuffer.KeyHandlerMinibuffer#getResult(Binding, KeySequence, String)
 */
@Override
protected IBindingResult getResult(final Binding binding, final KeySequence trigger, String triggerString) {

	// key character is only > 0 if it is stand alone
	int charpoint = getKeyCharacter();
	String character = null;
	if (binding == null && charpoint > 0 && triggerCount < 2) {
		if (charpoint == SWT.CR || charpoint == SWT.LF) {
			character = getEol();
		} else if (charpoint == SWT.BS) {
			character = new String(Character.toChars(charpoint));
		} else if ((Character.isWhitespace(charpoint)) || (charpoint > ' ')) {
			character = new String(Character.toChars(charpoint));
		}
	}
	if (countBuf.length() > 0) {
		try {
			if (countBuf.length() == 1 && countBuf.charAt(0)== '-') {
				;	// just use argument Count
			} else {
				setArgumentCount(Integer.parseInt(countBuf.toString()));
			}
		} catch (NumberFormatException e) {
				// bad count
				setArgumentCount(1);
		}
	}
	final String key = character;
	final boolean notNumeric = countBuf.length() == 0 && argumentCount == 4;	// flag whether a number was entered into the minibuffer

	return new IUniversalResult() {
		public Binding getKeyBinding() { return binding; }
		public String getKeyString() { return key; }
		public int getCount() { return argumentCount; }
		public boolean isNumeric() { return !notNumeric; }
		public KeySequence getTrigger() { return trigger; }
	};
}
 
Example 15
Source Project: hop   File: GuiMenuWidgets.java    License: Apache License 2.0 4 votes vote down vote up
public static String getShortcutString( KeyboardShortcut shortcut ) {
  String s = shortcut.toString();
  if ( StringUtils.isEmpty( s ) || s.endsWith( "-" ) ) {
    // Unknown characters from the SWT library
    // We'll handle the special cases here.
    //
    int keyCode = shortcut.getKeyCode();
    if ( keyCode == SWT.BS ) {
      return s + "Backspace";
    }
    if ( keyCode == SWT.ESC ) {
      return s + "Esc";
    }
    if ( keyCode == SWT.ARROW_LEFT ) {
      return s + "LEFT";
    }
    if ( keyCode == SWT.ARROW_RIGHT ) {
      return s + "RIGHT";
    }
    if ( keyCode == SWT.ARROW_UP ) {
      return s + "UP";
    }
    if ( keyCode == SWT.ARROW_DOWN ) {
      return s + "DOWN";
    }
    if ( keyCode == SWT.HOME ) {
      return s + "HOME";
    }
    if ( keyCode == SWT.F1 ) {
      return s + "F1";
    }
    if ( keyCode == SWT.F2 ) {
      return s + "F2";
    }
    if ( keyCode == SWT.F3 ) {
      return s + "F3";
    }
    if ( keyCode == SWT.F4 ) {
      return s + "F4";
    }
    if ( keyCode == SWT.F5 ) {
      return s + "F5";
    }
    if ( keyCode == SWT.F6 ) {
      return s + "F6";
    }
    if ( keyCode == SWT.F7 ) {
      return s + "F7";
    }
    if ( keyCode == SWT.F8 ) {
      return s + "F8";
    }
    if ( keyCode == SWT.F9 ) {
      return s + "F9";
    }
    if ( keyCode == SWT.F10 ) {
      return s + "F10";
    }
    if ( keyCode == SWT.F11 ) {
      return s + "F11";
    }
    if ( keyCode == SWT.F12 ) {
      return s + "F12";
    }
  }
  return s;
}
 
Example 16
/**
 * Add key listeners
 */
private void addKeyListeners() {
	keyListener = new KeyAdapter() {

		/**
		 * @see org.eclipse.swt.events.KeyAdapter#keyPressed(org.eclipse.swt.events.KeyEvent)
		 */
		@Override
		public void keyPressed(final KeyEvent e) {
			switch (e.character) {
				case '0':
				case '1':
				case '2':
				case '3':
				case '4':
				case '5':
				case '6':
				case '7':
				case '8':
				case '9':
					behaviourEngine.addDigitToDisplay(Integer.parseInt(String.valueOf(e.character)));
					return;
				case '.':
					behaviourEngine.addDecimalPoint();
					return;
				case '+':
					engine.processOperation(CalculatorEngine.OPERATOR_PLUS);
					return;
				case '-':
					engine.processOperation(CalculatorEngine.OPERATOR_MINUS);
					return;
				case '*':
					engine.processOperation(CalculatorEngine.OPERATOR_MULTIPLY);
					return;
				case '/':
					engine.processOperation(CalculatorEngine.OPERATOR_DIVIDE);
					return;
				case '=':
					engine.processEquals();
					return;
				case '%':
					engine.processPerCentageOperation();
					return;

			}

			switch (e.keyCode) {
				case SWT.KEYPAD_0:
				case SWT.KEYPAD_1:
				case SWT.KEYPAD_2:
				case SWT.KEYPAD_3:
				case SWT.KEYPAD_4:
				case SWT.KEYPAD_5:
				case SWT.KEYPAD_6:
				case SWT.KEYPAD_7:
				case SWT.KEYPAD_8:
				case SWT.KEYPAD_9:
					final int digit = e.keyCode - SWT.KEYCODE_BIT - 47;
					behaviourEngine.addDigitToDisplay(digit);
					return;
				case SWT.KEYPAD_ADD:
					engine.processOperation(CalculatorEngine.OPERATOR_PLUS);
					return;
				case SWT.KEYPAD_SUBTRACT:
					engine.processOperation(CalculatorEngine.OPERATOR_MINUS);
					return;
				case SWT.KEYPAD_DIVIDE:
					engine.processOperation(CalculatorEngine.OPERATOR_DIVIDE);
					return;
				case SWT.KEYPAD_MULTIPLY:
					engine.processOperation(CalculatorEngine.OPERATOR_MULTIPLY);
					return;
				case SWT.KEYPAD_CR:
				case SWT.KEYPAD_EQUAL:
				case SWT.CR:
					engine.processEquals();
					return;
				case SWT.BS:
					behaviourEngine.processBackSpace();
					return;
				case SWT.ESC:
					behaviourEngine.clearResult();
					engine.cancel();
					return;
			}
		}
	};

	for (final Control control : getChildren()) {
		control.addKeyListener(keyListener);
	}
	addKeyListener(keyListener);

}
 
Example 17
Source Project: uima-uimaj   File: CommonInputDialog.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public boolean verifyKeyChecks(VerifyEvent event) {
  char ch = event.character;

  boolean validateDottedName = ((validation & DOTTED_NAME) == DOTTED_NAME);
  boolean validateSpaces = ((validation & SPACED_NAMES) == SPACED_NAMES);
  boolean validateLanguage = ((validation & LANGUAGE) == LANGUAGE);
  boolean validateAllOK = ((validation & ALLOK) == ALLOK);
  boolean validateTrueFalse = ((validation & TRUE_FALSE) == TRUE_FALSE);
  boolean validateInteger = ((validation & INTEGER) == INTEGER);
  boolean validateFloat = ((validation & FLOAT) == FLOAT);

  if (event.keyCode == SWT.CR || event.keyCode == SWT.TAB || event.keyCode == SWT.BS)
    return true;

  if (validateTrueFalse) {
    return ("truefalse".indexOf(ch) >= 0);
  }

  if (validateSpaces && ch == ' ')
    return true;

  if (validateDottedName && ch == '.')
    return true;

  if ((!validateTrueFalse) && (!validateInteger) && (!validateFloat)
          && Character.isJavaIdentifierPart(ch))
    return true;

  if (validateLanguage && ch == '-')
    return true;

  if (validateAllOK)
    return true;

  if (validateInteger)
    if (Character.isDigit(ch) || ch == '-')
      return true;

  if (validateFloat) {
    if (Character.isDigit(ch) || ch == '-' || ch == 'E' || ch == 'e' || ch == '.')
      return true;
  }
  return false;
}
 
Example 18
private void handleSearchKeyPress(KeyEvent e) {
		TableViewSWTFilter<?> filter = tv.getSWTFilter();
		if (filter == null || e.widget == filter.widget) {
			return;
		}

		String newText = null;

		// normal character: jump to next item with a name beginning with this character
		if (ASYOUTYPE_MODE == ASYOUTYPE_MODE_FIND) {
			if (System.currentTimeMillis() - filter.lastFilterTime > 3000)
				newText = "";
		}

		if (e.keyCode == SWT.BS) {
			if (e.stateMask == SWT.CONTROL) {
				newText = "";
			} else if (filter.nextText.length() > 0) {
				newText = filter.nextText.substring(0, filter.nextText.length() - 1);
			}
		} else if ((e.stateMask & ~SWT.SHIFT) == 0 && e.character > 32 && e.character != SWT.DEL) {
			newText = filter.nextText + String.valueOf(e.character);
		}

		if (newText == null) {
			return;
		}

		if (ASYOUTYPE_MODE == ASYOUTYPE_MODE_FILTER) {
			if (filter != null && filter.widget != null && !filter.widget.isDisposed()) {
				filter.widget.setFocus();
			}
			tv.setFilterText(newText);
//		} else {
//			TableCellCore[] cells = getColumnCells("name");
//
//			//System.out.println(sLastSearch);
//
//			Arrays.sort(cells, TableCellImpl.TEXT_COMPARATOR);
//			int index = Arrays.binarySearch(cells, filter.text,
//					TableCellImpl.TEXT_COMPARATOR);
//			if (index < 0) {
//
//				int iEarliest = -1;
//				String s = filter.regex ? filter.text : "\\Q" + filter.text + "\\E";
//				Pattern pattern = Pattern.compile(s, Pattern.CASE_INSENSITIVE);
//				for (int i = 0; i < cells.length; i++) {
//					Matcher m = pattern.matcher(cells[i].getText());
//					if (m.find() && (m.start() < iEarliest || iEarliest == -1)) {
//						iEarliest = m.start();
//						index = i;
//					}
//				}
//
//				if (index < 0)
//					// Insertion Point (best guess)
//					index = -1 * index - 1;
//			}
//
//			if (index >= 0) {
//				if (index >= cells.length)
//					index = cells.length - 1;
//				TableRowCore row = cells[index].getTableRowCore();
//				int iTableIndex = row.getIndex();
//				if (iTableIndex >= 0) {
//					setSelectedRows(new TableRowCore[] {
//						row
//					});
//				}
//			}
//			filter.lastFilterTime = System.currentTimeMillis();
		}
		e.doit = false;
	}
 
Example 19
private boolean isBackspace(VerifyEvent event) {
	return event.doit == true && event.character == SWT.BS && event.stateMask == 0;
}
 
Example 20
Source Project: Pydev   File: PyBackspace.java    License: Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Creates a handler that will properly treat backspaces considering python code.
 */
public static VerifyKeyListener createVerifyKeyListener(final TextViewer viewer, final PyEdit edit) {
    return new VerifyKeyListener() {

        @Override
        public void verifyKey(VerifyEvent event) {
            if ((event.doit && event.character == SWT.BS && event.stateMask == 0 && viewer != null && viewer
                    .isEditable())) { //isBackspace
                boolean blockSelection = false;
                try {
                    blockSelection = viewer.getTextWidget().getBlockSelection();
                } catch (Throwable e) {
                    //that's OK (only available in eclipse 3.5)
                }
                if (!blockSelection) {
                    if (viewer instanceof ITextViewerExtensionAutoEditions) {
                        ITextViewerExtensionAutoEditions autoEditions = (ITextViewerExtensionAutoEditions) viewer;
                        if (!autoEditions.getAutoEditionsEnabled()) {
                            return;
                        }
                    }

                    ISelection selection = viewer.getSelection();
                    if (selection instanceof ITextSelection) {
                        //Only do our custom backspace if we're not in block selection mode.
                        PyBackspace pyBackspace = new PyBackspace();
                        if (edit != null) {
                            pyBackspace.setEditor(edit);
                        } else {
                            IAdaptable adaptable;
                            if (viewer instanceof IAdaptable) {
                                adaptable = (IAdaptable) viewer;
                            } else {
                                adaptable = new IAdaptable() {

                                    @Override
                                    public <T> T getAdapter(Class<T> adapter) {
                                        return null;
                                    }
                                };
                            }
                            pyBackspace.setIndentPrefs(new DefaultIndentPrefs(adaptable));
                        }
                        PySelection ps = PySelectionFromEditor.createPySelectionFromEditor(viewer,
                                (ITextSelection) selection);
                        pyBackspace.perform(ps);
                        event.doit = false;
                    }
                }
            }
        }
    };
}