use of javax.swing.text.DocumentFilter in project JMRI by JMRI.
the class AbstractMonPane method initComponents.
@Override
public void initComponents() throws Exception {
pm = jmri.InstanceManager.getNullableDefault(jmri.UserPreferencesManager.class);
// the following code sets the frame's initial state
// NOI18N
clearButton.setText(Bundle.getMessage("ButtonClearScreen"));
clearButton.setVisible(true);
// NOI18N
clearButton.setToolTipText(Bundle.getMessage("TooltipClearMonHistory"));
// NOI18N
freezeButton.setText(Bundle.getMessage("ButtonFreezeScreen"));
freezeButton.setVisible(true);
// NOI18N
freezeButton.setToolTipText(Bundle.getMessage("TooltipStopScroll"));
// NOI18N
enterButton.setText(Bundle.getMessage("ButtonAddMessage"));
enterButton.setVisible(true);
// NOI18N
enterButton.setToolTipText(Bundle.getMessage("TooltipAddMessage"));
createDataPanes();
// NOI18N
entryField.setToolTipText(Bundle.getMessage("TooltipEntryPane"));
// cap vertical size to avoid over-growth
Dimension currentPreferredSize = entryField.getPreferredSize();
Dimension currentMaximumSize = entryField.getMaximumSize();
currentMaximumSize.height = currentPreferredSize.height;
entryField.setMaximumSize(currentMaximumSize);
//setup filterField
// NOI18N
filterField.setToolTipText(Bundle.getMessage("TooltipFilter"));
filterField.setMaximumSize(currentMaximumSize);
try {
//restore prev values
filterField.setText(pm.getProperty(filterFieldCheck, filterFieldCheck).toString());
} catch (Exception e1) {
//leave blank if previous value not retrieved
}
//automatically uppercase input in filterField, and only accept spaces and valid hex characters
((AbstractDocument) filterField.getDocument()).setDocumentFilter(new DocumentFilter() {
// typing inserts individual characters
static final String pattern = "[0-9a-fA-F ]*+";
@Override
public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attrs) throws BadLocationException {
if (text.matches(pattern)) {
// NOI18N
fb.insertString(offset, text.toUpperCase(), attrs);
} else {
fb.insertString(offset, "", attrs);
}
}
@Override
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
if (text.matches(pattern)) {
// NOI18N
fb.replace(offset, length, text.toUpperCase(), attrs);
} else {
fb.replace(offset, length, "", attrs);
}
}
});
// NOI18N
startLogButton.setText(Bundle.getMessage("ButtonStartLogging"));
startLogButton.setVisible(true);
// NOI18N
startLogButton.setToolTipText(Bundle.getMessage("TooltipStartLogging"));
// NOI18N
stopLogButton.setText(Bundle.getMessage("ButtonStopLogging"));
stopLogButton.setVisible(true);
// NOI18N
stopLogButton.setToolTipText(Bundle.getMessage("TooltipStopLogging"));
// NOI18N
rawCheckBox.setText(Bundle.getMessage("ButtonShowRaw"));
rawCheckBox.setVisible(true);
// NOI18N
rawCheckBox.setToolTipText(Bundle.getMessage("TooltipShowRaw"));
if (pm != null)
rawCheckBox.setSelected(pm.getSimplePreferenceState(rawDataCheck));
// NOI18N
timeCheckBox.setText(Bundle.getMessage("ButtonShowTimestamps"));
timeCheckBox.setVisible(true);
// NOI18N
timeCheckBox.setToolTipText(Bundle.getMessage("TooltipShowTimestamps"));
if (pm != null)
timeCheckBox.setSelected(pm.getSimplePreferenceState(timeStampCheck));
// NOI18N
alwaysOnTopCheckBox.setText(Bundle.getMessage("ButtonWindowOnTop"));
alwaysOnTopCheckBox.setVisible(true);
// NOI18N
alwaysOnTopCheckBox.setToolTipText(Bundle.getMessage("TooltipWindowOnTop"));
if (pm != null)
alwaysOnTopCheckBox.setSelected(pm.getSimplePreferenceState(alwaysOnTopCheck));
if (getTopLevelAncestor() != null) {
((jmri.util.JmriJFrame) getTopLevelAncestor()).setAlwaysOnTop(alwaysOnTopCheckBox.isSelected());
} else {
// which can be normal, but
if (alwaysOnTopCheckBox.isSelected()) {
// in this case we want to access the enclosing frame to setAlwaysOnTop. So defer for a bit....
log.debug("Cannot set Always On Top from preferences due to no Top Level Ancestor");
timerCount = 0;
timer = new javax.swing.Timer(20, (java.awt.event.ActionEvent evt) -> {
if (getTopLevelAncestor() != null && timerCount > 3) {
timer.stop();
((jmri.util.JmriJFrame) getTopLevelAncestor()).setAlwaysOnTop(alwaysOnTopCheckBox.isSelected());
log.debug("set Always On Top");
} else {
log.debug("Have to repeat attempt to set Always on Top");
timerCount++;
if (timerCount > 50) {
log.debug("Set Always on Top failed");
timer.stop();
}
}
});
timer.start();
}
}
// NOI18N
autoScrollCheckBox.setText(Bundle.getMessage("ButtonAutoScroll"));
autoScrollCheckBox.setVisible(true);
// NOI18N
autoScrollCheckBox.setToolTipText(Bundle.getMessage("TooltipAutoScroll"));
if (pm != null)
autoScrollCheckBox.setSelected(!pm.getSimplePreferenceState(autoScrollCheck));
// NOI18N
openFileChooserButton.setText(Bundle.getMessage("ButtonChooseLogFile"));
openFileChooserButton.setVisible(true);
// NOI18N
openFileChooserButton.setToolTipText(Bundle.getMessage("TooltipChooseLogFile"));
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
// add items to GUI
addDataPanes();
JPanel paneA = new JPanel();
paneA.setLayout(new BoxLayout(paneA, BoxLayout.Y_AXIS));
JPanel pane1 = new JPanel();
pane1.setLayout(new BoxLayout(pane1, BoxLayout.X_AXIS));
pane1.add(clearButton);
pane1.add(freezeButton);
pane1.add(rawCheckBox);
pane1.add(timeCheckBox);
pane1.add(alwaysOnTopCheckBox);
pane1.add(autoScrollCheckBox);
paneA.add(pane1);
JPanel pane2 = new JPanel();
pane2.setLayout(new BoxLayout(pane2, BoxLayout.X_AXIS));
pane2.add(filterLabel);
pane2.add(filterField);
pane2.add(openFileChooserButton);
pane2.add(startLogButton);
pane2.add(stopLogButton);
paneA.add(pane2);
JPanel pane3 = new JPanel();
pane3.setLayout(new BoxLayout(pane3, BoxLayout.X_AXIS));
pane3.add(enterButton);
pane3.add(entryField);
paneA.add(pane3);
add(paneA);
// connect actions to buttons
clearButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
clearButtonActionPerformed(e);
}
});
startLogButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
startLogButtonActionPerformed(e);
}
});
stopLogButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
stopLogButtonActionPerformed(e);
}
});
openFileChooserButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
openFileChooserButtonActionPerformed(e);
}
});
enterButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
enterButtonActionPerformed(e);
}
});
alwaysOnTopCheckBox.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
if (getTopLevelAncestor() != null) {
((jmri.util.JmriJFrame) getTopLevelAncestor()).setAlwaysOnTop(alwaysOnTopCheckBox.isSelected());
}
}
});
autoScrollCheckBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
doAutoScroll(monTextPane, autoScrollCheckBox.isSelected());
}
});
// set file chooser to a default
logFileChooser.setSelectedFile(new File("monitorLog.txt"));
// connect to data source
init();
}
Aggregations