use of javax.swing.text.Element in project java-swing-tips by aterai.
the class NoWrapEditorKit method processChangedLines.
private void processChangedLines(int offset, int length) throws BadLocationException {
Element root = getDefaultRootElement();
String content = getText(0, getLength());
int startLine = root.getElementIndex(offset);
int endLine = root.getElementIndex(offset + length);
for (int i = startLine; i <= endLine; i++) {
applyHighlighting(content, i);
}
}
use of javax.swing.text.Element in project vcell by virtualcell.
the class BNGLDebugger method initialize.
@Deprecated
public void initialize() {
// -------------------------------------------------- bngl panel
JScrollPane bnglPanel = new JScrollPane();
bnglTextArea = new JTextArea();
bnglTextArea.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
// bnglTextArea.setFont(new Font("monospaced", Font.PLAIN, 14));
lineNumberArea = new JTextArea("1");
lineNumberArea.setBackground(Color.LIGHT_GRAY);
lineNumberArea.setEditable(false);
lineNumberArea.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
bnglTextArea.getDocument().addDocumentListener(new DocumentListener() {
public String getNr() {
lines = 1;
int caretPosition = bnglTextArea.getDocument().getLength();
Element root = bnglTextArea.getDocument().getDefaultRootElement();
String nr = "1" + System.getProperty("line.separator");
for (int i = 2; i < root.getElementIndex(caretPosition) + 2; i++) {
nr += i + System.getProperty("line.separator");
lines++;
}
return nr;
}
@Override
public void changedUpdate(DocumentEvent de) {
int oldLines = lines;
String numbers = getNr();
if (oldLines != lines) {
lineNumberArea.setText(numbers);
}
lineNumberArea.getHighlighter().removeAllHighlights();
}
@Override
public void insertUpdate(DocumentEvent de) {
int oldLines = lines;
String numbers = getNr();
if (oldLines != lines) {
lineNumberArea.setText(numbers);
}
lineNumberArea.getHighlighter().removeAllHighlights();
}
@Override
public void removeUpdate(DocumentEvent de) {
int oldLines = lines;
String numbers = getNr();
if (oldLines != lines) {
lineNumberArea.setText(numbers);
}
lineNumberArea.getHighlighter().removeAllHighlights();
}
});
bnglPanel.getViewport().add(bnglTextArea);
bnglPanel.setRowHeaderView(lineNumberArea);
bnglPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel upperPanel = new JPanel();
upperPanel.setLayout(new BorderLayout());
upperPanel.add(bnglPanel, BorderLayout.CENTER);
// ---------------------------------------------------- exception panel
JScrollPane exceptionPanel = new JScrollPane();
exceptionTextArea = new JTextArea();
exceptionTextArea.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
exceptionTextArea.setFont(new Font("monospaced", Font.PLAIN, 14));
exceptionPanel.getViewport().add(exceptionTextArea);
exceptionPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel lowerPanel = new JPanel();
lowerPanel.setLayout(new BorderLayout());
lowerPanel.add(exceptionPanel, BorderLayout.CENTER);
// ---------------------------------------------------- all together now :)
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(350);
splitPane.setResizeWeight(0.9);
splitPane.setTopComponent(upperPanel);
splitPane.setBottomComponent(lowerPanel);
// ... Create menubar
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = menuBar.add(new JMenu("File"));
// fileMenu.setMnemonic('F');
// fileMenu.add(openAction); // Note use of actions, not text.
// fileMenu.add(saveAction);
// fileMenu.addSeparator();
fileMenu.add(exitAction);
JPanel buttonPane = new JPanel();
buttonParse.addActionListener(this);
buttonSave.addActionListener(this);
buttonExit.addActionListener(this);
buttonPane.add(buttonParse);
buttonPane.add(buttonSave);
buttonPane.add(buttonExit);
JPanel framePanel = new JPanel();
framePanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
framePanel.add(splitPane, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 0;
gbc.weighty = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
framePanel.add(buttonPane, gbc);
frame.add(framePanel);
frame.setJMenuBar(menuBar);
frame.pack();
frame.setName("BnglDebugger");
frame.setSize(900, 650);
frame.setVisible(true);
}
use of javax.swing.text.Element in project sonarqube by SonarSource.
the class TextLineNumber method getOffsetY.
/*
* Determine the Y offset for the current row
*/
private int getOffsetY(int rowStartOffset, FontMetrics fontMetrics) throws BadLocationException {
// Get the bounding rectangle of the row
Rectangle r = component.modelToView(rowStartOffset);
int lineHeight = fontMetrics.getHeight();
int y = r.y + r.height;
int descent = 0;
if (// default font is being used
r.height == lineHeight) {
descent = fontMetrics.getDescent();
} else // We need to check all the attributes for font changes
{
if (fonts == null)
fonts = new HashMap<>();
Element root = component.getDocument().getDefaultRootElement();
int index = root.getElementIndex(rowStartOffset);
Element line = root.getElement(index);
for (int i = 0; i < line.getElementCount(); i++) {
Element child = line.getElement(i);
AttributeSet as = child.getAttributes();
String fontFamily = (String) as.getAttribute(StyleConstants.FontFamily);
Integer fontSize = (Integer) as.getAttribute(StyleConstants.FontSize);
String key = fontFamily + fontSize;
FontMetrics fm = fonts.get(key);
if (fm == null) {
Font font = new Font(fontFamily, Font.PLAIN, fontSize);
fm = component.getFontMetrics(font);
fonts.put(key, fm);
}
descent = Math.max(descent, fm.getDescent());
}
}
return y - descent;
}
use of javax.swing.text.Element in project sonarqube by SonarSource.
the class TextLineNumber method isCurrentLine.
/*
* We need to know if the caret is currently positioned on the line we
* are about to paint so the line number can be highlighted.
*/
private boolean isCurrentLine(int rowStartOffset) {
int caretPosition = component.getCaretPosition();
Element root = component.getDocument().getDefaultRootElement();
return root.getElementIndex(rowStartOffset) == root.getElementIndex(caretPosition);
}
use of javax.swing.text.Element in project sonarqube by SonarSource.
the class TextLineNumber method getTextLineNumber.
/*
* Get the line number to be drawn. The empty string will be returned
* when a line of text has wrapped.
*/
protected String getTextLineNumber(int rowStartOffset) {
Element root = component.getDocument().getDefaultRootElement();
int index = root.getElementIndex(rowStartOffset);
Element line = root.getElement(index);
if (line.getStartOffset() == rowStartOffset)
return String.valueOf(index + 1);
else
return "";
}
Aggregations