/* * Markers_Dialog.bsh - a BeanShell macro for viewing and moving * the cursor to markers in a buffer. * Copyright (C) 2001 Ollie Rutherfurd, oliver@rutherfurd.net * * :mode=beanshell:tabSize=4:indentSize=4:maxLineLen=0:noTabs=false: * :indentOnTab=true:indentOnEnter=true:folding=explicit:collapseFolds=1: * * {{{ License * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with the jEdit program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * }}} * * $Id: Markers_Dialog.bsh 28 2003-03-25 22:40:28Z oliver $ */ import javax.swing.border.EmptyBorder; /* {{{ MarkerCellRenderer * I used a custom cell renderer because I couldn't figure out * how to override toString for the MarkerProxy object. */ MarkerCellRenderer() { l = new JLabel(); l.setOpaque(true); Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { l.setText(value.text); if (isSelected) { l.setBackground(list.getSelectionBackground()); l.setForeground(list.getSelectionForeground()); } else { l.setBackground(list.getBackground()); l.setForeground(list.getForeground()); } return l; } return this; } // }}} // {{{ MarkerProxy Object MarkerProxy(marker){ this.position = marker.getPosition(); lineno = buffer.getLineOfOffset(this.position); // pad line number with spaces line = lineno + 1; /* if(line < 10) line = " " + line; else if(line < 100) line = " " + line; else if(line < 1000) line = " " + line; */ line = "" + line; // prepend line number to line contents, replace tabs with » character this.text = line + ": " + buffer.getLineText(lineno).replace('\t', '»'); void scrollToMarker(){ textArea.setCaretPosition(this.position); } return this; } // }}} MarkersDialog(doModal) { // {{{ create dialog dialog = new JDialog(view, "Markers", doModal); content = new JPanel(new BorderLayout()); content.setBorder(new EmptyBorder(12,12,12,12)); dialog.setContentPane(content); markers = buffer.getMarkers(); markerProxies = new Vector(markers.size()); for(i = 0; i < markers.size(); i++) markerProxies.addElement(MarkerProxy(markers.elementAt(i))); markerList = new JList(markerProxies); markerList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); markerList.setCellRenderer(MarkerCellRenderer()); content.add(new JScrollPane(markerList), BorderLayout.CENTER); buttonPanel = new JPanel(); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS)); buttonPanel.setBorder(new EmptyBorder(12,50,0,50)); buttonPanel.add(Box.createGlue()); ok = new JButton("OK"); close = new JButton("Close"); ok.setPreferredSize(close.getPreferredSize()); dialog.getRootPane().setDefaultButton(ok); buttonPanel.add(ok); buttonPanel.add(Box.createHorizontalStrut(6)); buttonPanel.add(close); buttonPanel.add(Box.createGlue()); content.add(buttonPanel, BorderLayout.SOUTH); // }}} // {{{ gotoMarker() void gotoMarker() { m = markerList.getSelectedValue(); if(m != null) m.scrollToMarker(); } // }}} // {{{ actionPerformed void actionPerformed(evt) { if(evt.getSource() == ok) gotoMarker(); dialog.dispose(); dialog = null; } // }}} // {{{ KeyListener implementation void keyPressed(evt) { if(evt.getKeyCode() == KeyEvent.VK_ESCAPE) dialog.dispose(); else if(evt.getKeyCode() == KeyEvent.VK_SPACE) gotoMarker(); } void keyReleased(evt){} void keyTyped(evt){} // }}} // {{{ MouseListener implementation void mouseClicked(evt) { if(evt.getClickCount() > 1) { gotoMarker(); dialog.dispose(); } } void mouseEntered(evt){} void mouseExited(evt){} void mousePressed(evt){} void mouseReleased(evt){} // }}} // {{{ add listeners dialog.addKeyListener(this); markerList.addMouseListener(this); ok.addActionListener(this); close.addActionListener(this); // }}} // {{{ display dialog dialog.pack(); markerList.requestFocus(); if(markers.size() > 0) markerList.setSelectedIndex(0); dialog.setLocationRelativeTo(view); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setVisible(true); // }}} } doModal = true; MarkersDialog(doModal); /* Macro index data (in DocBook format) Markers_Dialog Displays a modal dialog with all the markers in the buffer, allowing one to move the cursor to one of markers. */