use of javax.swing.text.DefaultCaret in project myrobotlab by MyRobotLab.
the class SerialGui method autoScroll.
public void autoScroll(boolean b) {
DefaultCaret caret = (DefaultCaret) rx.getCaret();
if (b) {
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
} else {
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
}
DefaultCaret caretTX = (DefaultCaret) tx.getCaret();
if (b) {
caretTX.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
} else {
caretTX.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
}
}
use of javax.swing.text.DefaultCaret in project Arduino by arduino.
the class AbstractTextMonitor method onCreateWindow.
@Override
protected void onCreateWindow(Container mainPane) {
mainPane.setLayout(new BorderLayout());
textArea = new TextAreaFIFO(8_000_000);
textArea.setRows(16);
textArea.setColumns(40);
textArea.setEditable(false);
// don't automatically update the caret. that way we can manually decide
// whether or not to do so based on the autoscroll checkbox.
((DefaultCaret) textArea.getCaret()).setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
scrollPane = new JScrollPane(textArea);
mainPane.add(scrollPane, BorderLayout.CENTER);
JPanel upperPane = new JPanel();
upperPane.setLayout(new BoxLayout(upperPane, BoxLayout.X_AXIS));
upperPane.setBorder(new EmptyBorder(4, 4, 4, 4));
textField = new JTextField(40);
// textField is selected every time the window is focused
addWindowFocusListener(new WindowAdapter() {
@Override
public void windowGainedFocus(WindowEvent e) {
textField.requestFocusInWindow();
}
});
// Add cut/copy/paste contextual menu to the text input field.
JPopupMenu menu = new JPopupMenu();
Action cut = new DefaultEditorKit.CutAction();
cut.putValue(Action.NAME, tr("Cut"));
menu.add(cut);
Action copy = new DefaultEditorKit.CopyAction();
copy.putValue(Action.NAME, tr("Copy"));
menu.add(copy);
Action paste = new DefaultEditorKit.PasteAction();
paste.putValue(Action.NAME, tr("Paste"));
menu.add(paste);
textField.setComponentPopupMenu(menu);
sendButton = new JButton(tr("Send"));
clearButton = new JButton(tr("Clear output"));
upperPane.add(textField);
upperPane.add(Box.createRigidArea(new Dimension(4, 0)));
upperPane.add(sendButton);
mainPane.add(upperPane, BorderLayout.NORTH);
final JPanel pane = new JPanel();
pane.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS));
pane.setBorder(new EmptyBorder(4, 4, 4, 4));
autoscrollBox = new JCheckBox(tr("Autoscroll"), true);
addTimeStampBox = new JCheckBox(tr("Show timestamp"), false);
noLineEndingAlert = new JLabel(I18n.format(tr("You've pressed {0} but nothing was sent. Should you select a line ending?"), tr("Send")));
noLineEndingAlert.setToolTipText(noLineEndingAlert.getText());
noLineEndingAlert.setForeground(pane.getBackground());
Dimension minimumSize = new Dimension(noLineEndingAlert.getMinimumSize());
minimumSize.setSize(minimumSize.getWidth() / 3, minimumSize.getHeight());
noLineEndingAlert.setMinimumSize(minimumSize);
lineEndings = new JComboBox<>(new String[] { tr("No line ending"), tr("Newline"), tr("Carriage return"), tr("Both NL & CR") });
lineEndings.addActionListener((ActionEvent event) -> {
PreferencesData.setInteger("serial.line_ending", lineEndings.getSelectedIndex());
noLineEndingAlert.setForeground(pane.getBackground());
});
addTimeStampBox.addActionListener((ActionEvent event) -> PreferencesData.setBoolean("serial.show_timestamp", addTimeStampBox.isSelected()));
lineEndings.setMaximumSize(lineEndings.getMinimumSize());
serialRates = new JComboBox<>();
for (String rate : serialRateStrings) {
serialRates.addItem(rate + " " + tr("baud"));
}
serialRates.setMaximumSize(serialRates.getMinimumSize());
pane.add(autoscrollBox);
pane.add(addTimeStampBox);
pane.add(Box.createHorizontalGlue());
pane.add(noLineEndingAlert);
pane.add(Box.createRigidArea(new Dimension(8, 0)));
pane.add(lineEndings);
pane.add(Box.createRigidArea(new Dimension(8, 0)));
pane.add(serialRates);
pane.add(Box.createRigidArea(new Dimension(8, 0)));
pane.add(clearButton);
applyPreferences();
mainPane.add(pane, BorderLayout.SOUTH);
}
use of javax.swing.text.DefaultCaret in project Arduino by arduino.
the class EditorTab method setText.
/**
* Replace the entire contents of this tab.
*/
public void setText(String what) {
// Remove all highlights, since these will all end up at the start of the
// text otherwise. Preserving them is tricky, so better just remove them.
textarea.removeAllLineHighlights();
// Set the caret update policy to NEVER_UPDATE while completely replacing
// the current text. Normally, the caret tracks inserts and deletions, but
// replacing the entire text will always make the caret end up at the end,
// which isn't really useful. With NEVER_UPDATE, the caret will just keep
// its absolute position (number of characters from the start), which isn't
// always perfect, but the best we can do without making a diff of the old
// and new text and some guesswork.
// Note that we cannot use textarea.setText() here, since that first removes
// text and then inserts the new text. Even with NEVER_UPDATE, the caret
// always makes sure to stay valid, so first removing all text makes it
// reset to 0. Also note that simply saving and restoring the caret position
// will work, but then the scroll position might change in response to the
// caret position.
DefaultCaret caret = (DefaultCaret) textarea.getCaret();
int policy = caret.getUpdatePolicy();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
try {
Document doc = textarea.getDocument();
int oldLength = doc.getLength();
// The undo manager already seems to group the insert and remove together
// automatically, but better be explicit about it.
textarea.beginAtomicEdit();
try {
doc.insertString(oldLength, what, null);
doc.remove(0, oldLength);
} catch (BadLocationException e) {
System.err.println("Unexpected failure replacing text");
} finally {
textarea.endAtomicEdit();
}
} finally {
caret.setUpdatePolicy(policy);
}
// A trick to force textarea to recalculate the bracket matching rectangle.
// In the worst case scenario, this should be ineffective.
textarea.setLineWrap(textarea.getLineWrap());
}
use of javax.swing.text.DefaultCaret in project sdrtrunk by DSheirer.
the class ChannelDetailPanel method init.
private void init() {
setLayout(new MigLayout("insets 0 0 0 0", "[grow,fill]", "[]0[grow,fill]"));
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new MigLayout("insets 1 1 1 1", "[][grow,fill][][grow,fill][][grow,fill][]", ""));
buttonPanel.add(new JLabel("System:"));
mSystemLabel = new JLabel(" ");
buttonPanel.add(mSystemLabel);
buttonPanel.add(new JLabel("Site:"));
mSiteLabel = new JLabel(" ");
buttonPanel.add(mSiteLabel);
buttonPanel.add(new JLabel("Channel Name:"));
mNameLabel = new JLabel(" ");
buttonPanel.add(mNameLabel);
JButton refreshButton = new JButton("Refresh");
refreshButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
receive(mProcessingChain);
}
});
buttonPanel.add(refreshButton);
add(buttonPanel, "wrap");
mDetailTextPane = new JTextArea(EMPTY_DETAILS);
DefaultCaret caret = (DefaultCaret) mDetailTextPane.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
add(new JScrollPane(mDetailTextPane));
}
use of javax.swing.text.DefaultCaret in project snowblossom by snowblossomcoin.
the class BasePanel method makeNotViewReset.
public static void makeNotViewReset(JTextArea a) {
DefaultCaret caret = (DefaultCaret) a.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
}
Aggregations