use of org.apache.pivot.wtk.TextInput in project pivot by apache.
the class TerraTextInputSkin method getPreferredWidth.
@Override
public int getPreferredWidth(int height) {
TextInput textInput = (TextInput) getComponent();
int textSize = textInput.getTextSize();
return averageCharacterSize.width * textSize + padding.getWidth() + 2;
}
use of org.apache.pivot.wtk.TextInput in project pivot by apache.
the class TerraTextInputSkin method layout.
@Override
public void layout() {
TextInput textInput = (TextInput) getComponent();
textLayout = null;
int n = textInput.getCharacterCount();
AttributedStringCharacterIterator composedText = textInput.getComposedText();
if (n > 0 || composedText != null) {
AttributedCharacterIterator text = null;
if (n > 0) {
int insertPos = textInput.getSelectionStart();
if (composedText == null) {
text = getCharIterator(textInput, 0, n);
} else if (insertPos == n) {
// The composed text position is the end of the committed text
text = new CompositeIterator(getCharIterator(textInput, 0, n), composedText);
} else if (insertPos == 0) {
text = new CompositeIterator(composedText, getCharIterator(textInput, 0, n));
} else {
// The composed text is somewhere in the middle of the text
text = new CompositeIterator(getCharIterator(textInput, 0, insertPos), composedText, getCharIterator(textInput, insertPos, n));
}
} else {
text = composedText;
}
FontRenderContext fontRenderContext = Platform.getFontRenderContext();
textLayout = new TextLayout(text, fontRenderContext);
int textWidth = getTextWidth(textLayout);
int width = getWidth();
if (textWidth - scrollLeft + padding.left + 1 < width - padding.right - 1) {
// The right edge of the text is less than the right inset;
// align the text's right edge with the inset
scrollLeft = Math.max(textWidth + padding.getWidth() + 2 - width, 0);
} else {
// Scroll selection start to visible
int selectionStart = textInput.getSelectionStart();
if (textInput.isFocused()) {
if (composedVisiblePosition != null) {
scrollCharacterToVisible(selectionStart + composedVisiblePosition.getInsertionIndex());
} else {
if (selectionStart <= n) {
scrollCharacterToVisible(selectionStart);
}
}
}
}
} else {
scrollLeft = 0;
}
updateSelection();
showCaret(textInput.isFocused() && textInput.getSelectionLength() == 0);
}
use of org.apache.pivot.wtk.TextInput in project pivot by apache.
the class TerraTextInputSkin method mouseClick.
@Override
public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
if (button == Mouse.Button.LEFT && count > 1) {
TextInput textInput = (TextInput) getComponent();
if (count == 2) {
CharSpan charSpan = CharUtils.selectWord(textInput.getCharacters(), getInsertionPoint(x));
if (charSpan != null) {
textInput.setSelection(charSpan);
}
} else if (count == 3) {
textInput.selectAll();
}
selectDirection = null;
}
return super.mouseClick(component, button, x, y, count);
}
use of org.apache.pivot.wtk.TextInput in project pivot by apache.
the class TerraTextInputSkin method updateSelection.
private void updateSelection() {
TextInput textInput = (TextInput) getComponent();
int height = getHeight();
int selectionStart = textInput.getSelectionStart();
int selectionLength = textInput.getSelectionLength();
int n = textInput.getCharacterCount();
Bounds leadingSelectionBounds;
if (selectionStart < n) {
leadingSelectionBounds = getCharacterBounds(selectionStart);
} else {
// The insertion point is after the last character
int x = padding.left + 1 - scrollLeft + getAlignmentDeltaX(textLayout);
if (n > 0) {
x += getTextWidth(textLayout);
}
int y = padding.top + 1;
leadingSelectionBounds = new Bounds(x, y, 0, height - (padding.top + padding.bottom + 2));
}
if (composedTextCaret != null) {
caret = getCaretRectangle(composedTextCaret);
} else {
caret = leadingSelectionBounds.toRectangle();
}
caret.width = 1;
if (selectionLength > 0) {
Bounds trailingSelectionBounds = getCharacterBounds(selectionStart + selectionLength - 1);
selection = new Rectangle(leadingSelectionBounds.x, leadingSelectionBounds.y, trailingSelectionBounds.x + trailingSelectionBounds.width - leadingSelectionBounds.x, trailingSelectionBounds.y + trailingSelectionBounds.height - leadingSelectionBounds.y);
} else {
selection = null;
}
}
use of org.apache.pivot.wtk.TextInput in project pivot by apache.
the class TerraFileBrowserSheetSkin method install.
@Override
public void install(Component component) {
super.install(component);
final FileBrowserSheet fileBrowserSheet = (FileBrowserSheet) component;
fileBrowserSheet.setMinimumWidth(360);
fileBrowserSheet.setMinimumHeight(180);
// Load the sheet content
BXMLSerializer bxmlSerializer = new BXMLSerializer();
Component content;
try {
content = (Component) bxmlSerializer.readObject(TerraFileBrowserSheetSkin.class, "terra_file_browser_sheet_skin.bxml", true);
} catch (IOException exception) {
throw new RuntimeException(exception);
} catch (SerializationException exception) {
throw new RuntimeException(exception);
}
fileBrowserSheet.setContent(content);
bxmlSerializer.bind(this, TerraFileBrowserSheetSkin.class);
// set the same rootDirectory to fileBrowser
fileBrowser.setRootDirectory(fileBrowserSheet.getRootDirectory());
saveAsTextInput.getTextInputContentListeners().add(new TextInputContentListener() {
@Override
public void textChanged(TextInput textInput) {
Form.clearFlag(saveAsBoxPane);
updateOKButtonState();
}
});
fileBrowser.getFileBrowserListeners().add(new FileBrowserListener() {
@Override
public void rootDirectoryChanged(FileBrowser fileBrowserArgument, File previousRootDirectory) {
updatingSelection = true;
fileBrowserSheet.setRootDirectory(fileBrowserArgument.getRootDirectory());
updatingSelection = false;
selectedDirectoryCount = 0;
updateOKButtonState();
}
@Override
public void selectedFileAdded(FileBrowser fileBrowserArgument, File file) {
if (file.isDirectory()) {
selectedDirectoryCount++;
}
updateOKButtonState();
}
@Override
public void selectedFileRemoved(FileBrowser fileBrowserArgument, File file) {
if (file.isDirectory()) {
selectedDirectoryCount--;
}
updateOKButtonState();
}
@Override
public void selectedFilesChanged(FileBrowser fileBrowserArgument, Sequence<File> previousSelectedFiles) {
selectedDirectoryCount = 0;
Sequence<File> selectedFiles = fileBrowserArgument.getSelectedFiles();
for (int i = 0, n = selectedFiles.getLength(); i < n; i++) {
File selectedFile = selectedFiles.get(i);
if (selectedFile.isDirectory()) {
selectedDirectoryCount++;
}
}
if (!fileBrowserArgument.isMultiSelect()) {
File selectedFile = fileBrowserArgument.getSelectedFile();
if (selectedFile != null && !selectedFile.isDirectory()) {
saveAsTextInput.setText(selectedFile.getName());
}
}
updateOKButtonState();
}
});
fileBrowser.getComponentMouseButtonListeners().add(new ComponentMouseButtonListener() {
private File file = null;
@Override
public boolean mouseClick(Component componentArgument, Mouse.Button button, int x, int y, int count) {
boolean consumed = false;
FileBrowserSheet.Mode mode = fileBrowserSheet.getMode();
if (count == 1) {
file = fileBrowser.getFileAt(x, y);
} else if (count == 2) {
File fileLocal = fileBrowser.getFileAt(x, y);
if (fileLocal != null && this.file != null && fileLocal.equals(this.file) && fileBrowser.isFileSelected(fileLocal)) {
if (mode == FileBrowserSheet.Mode.OPEN || mode == FileBrowserSheet.Mode.OPEN_MULTIPLE) {
if (!fileLocal.isDirectory()) {
fileBrowserSheet.close(true);
consumed = true;
}
}
}
}
return consumed;
}
});
okButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
fileBrowserSheet.close(true);
}
});
cancelButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
fileBrowserSheet.close(false);
}
});
// Add this as a file browser sheet listener
fileBrowserSheet.getFileBrowserSheetListeners().add(this);
modeChanged(fileBrowserSheet, null);
rootDirectoryChanged(fileBrowserSheet, null);
selectedFilesChanged(fileBrowserSheet, null);
}
Aggregations