public int getYValueFromOffset(int offset) { try { return textPane.modelToView(offset).y; } catch (Exception e) { System.out.println("EditorServer: DocumentPanel: getYValueFromOffset. error"); e.printStackTrace(); return 0; } }
public DocumentPanel() { super(new BorderLayout()); JLabel lblDocument = new JLabel("Document: " + document.getTitle()); lblDocument.setBorder(new EtchedBorder()); textPane = new JTextPane(document); textPane.setEditable(false); textPane.setMargin(new Insets(5, 20, 5, 5)); textPane.setMaximumSize(new Dimension(364, 1000000000)); textPane.setPreferredSize(new Dimension(364, 400)); textPane.setMinimumSize(new Dimension(364, 10)); textPane.addCaretListener( new CaretListener() { public void caretUpdate(CaretEvent e) { int length = document.getLength(); int offset = e.getDot(); if (e.getDot() == e.getMark()) textPane.getCaret().moveDot(offset + 1); Paragraph p = lockManager.getParFromOffset(offset); int pOffset = p.getOffset(); lblCursor.setText( "Document Length=" + String.valueOf(length) + ", CaretOffset=" + String.valueOf(offset) + ", Paragraph=" + p.toString() + ", Offset in Paragraph=" + String.valueOf(offset - p.getOffset())); } }); Box box = new Box(BoxLayout.X_AXIS); box.add(textPane); box.add(Box.createGlue()); box.setBackground(Color.WHITE); box.setOpaque(true); box.setPreferredSize(new Dimension(600, 10000)); lblCursor = new JLabel("Cursor"); lblCursor.setBorder(new EtchedBorder()); JPanel boxText = new JPanel(new BorderLayout()); boxText.setBorder(new EmptyBorder(5, 5, 5, 5)); boxText.add(lblDocument, BorderLayout.NORTH); boxText.add(new JScrollPane(box), BorderLayout.CENTER); boxText.add(lblCursor, BorderLayout.SOUTH); JLabel lblPars = new JLabel("Paragraphs: "); lblPars.setBorder(new EtchedBorder()); parList = new JList(); parList.setPreferredSize(new Dimension(100, 300)); parList.setEnabled(false); JPanel boxPars = new JPanel(new BorderLayout()); boxPars.setBorder(new EmptyBorder(5, 5, 5, 5)); boxPars.add(lblPars, BorderLayout.NORTH); boxPars.add(new JScrollPane(parList), BorderLayout.CENTER); add(boxText, BorderLayout.CENTER); add(boxPars, BorderLayout.EAST); }
public int getOffsetFromPoint(Point p) { return textPane.viewToModel(p); }