1 22 23 package org.gjt.sp.jedit.gui; 24 25 import java.awt.*; 27 import java.awt.event.*; 28 import javax.swing.*; 29 import javax.swing.border.EmptyBorder ; 30 import javax.swing.event.*; 31 import org.gjt.sp.jedit.*; 32 import org.gjt.sp.jedit.gui.RolloverButton; 33 import org.gjt.sp.jedit.textarea.JEditTextArea; 34 import org.gjt.sp.jedit.msg.BufferUpdate; 35 import org.gjt.sp.jedit.msg.EditPaneUpdate; 36 import org.gjt.sp.jedit.msg.ViewUpdate; 37 import org.gjt.sp.util.Log; 38 40 public class MarkerViewer extends JPanel implements EBComponent, ActionListener 41 { 42 public MarkerViewer(View view) 44 { 45 super(new BorderLayout()); 46 this.view = view; 47 Box toolBar = new Box(BoxLayout.X_AXIS); 48 49 toolBar.add(new JLabel(GUIUtilities.prettifyMenuLabel( 50 jEdit.getProperty("markers.label")))); 51 52 toolBar.add(Box.createGlue()); 53 54 RolloverButton addMarker = new RolloverButton( 55 GUIUtilities.loadIcon("Plus.png")); 56 addMarker.setToolTipText(GUIUtilities.prettifyMenuLabel( 57 jEdit.getProperty("add-marker.label"))); 58 addMarker.addActionListener(this); 59 addMarker.setActionCommand("add-marker"); 60 toolBar.add(addMarker); 61 62 previous = new RolloverButton(GUIUtilities.loadIcon("ArrowL.png")); 63 previous.setToolTipText(GUIUtilities.prettifyMenuLabel( 64 jEdit.getProperty("prev-marker.label"))); 65 previous.addActionListener(this); 66 previous.setActionCommand("prev-marker"); 67 toolBar.add(previous); 68 69 next = new RolloverButton(GUIUtilities.loadIcon("ArrowR.png")); 70 next.setToolTipText(GUIUtilities.prettifyMenuLabel( 71 jEdit.getProperty("next-marker.label"))); 72 next.addActionListener(this); 73 next.setActionCommand("next-marker"); 74 toolBar.add(next); 75 76 clear = new RolloverButton(GUIUtilities.loadIcon("Clear.png")); 77 clear.setToolTipText(GUIUtilities.prettifyMenuLabel( 78 jEdit.getProperty("remove-all-markers.label"))); 79 clear.addActionListener(this); 80 clear.setActionCommand("clear"); 81 toolBar.add(clear); 82 83 84 add(BorderLayout.NORTH, toolBar); 85 86 markerList = new JList(); 87 markerList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 88 markerList.setCellRenderer(new Renderer()); 89 markerList.addMouseListener(new MouseHandler()); 90 91 add(BorderLayout.CENTER,new JScrollPane(markerList)); 92 93 refreshList(); 94 } 96 public boolean requestDefaultFocus() 98 { 99 markerList.requestFocus(); 100 return true; 101 } 103 public void actionPerformed(ActionEvent evt) 105 { 106 String cmd = evt.getActionCommand(); 107 if (cmd.equals("clear")) 108 view.getBuffer().removeAllMarkers(); 109 else if (cmd.equals("add-marker")) 110 view.getEditPane().addMarker(); 111 else if (cmd.equals("next-marker")) 112 { 113 view.getEditPane().goToNextMarker(false); 114 updateSelection(); 115 } 116 else if (cmd.equals("prev-marker")) 117 { 118 view.getEditPane().goToPrevMarker(false); 119 updateSelection(); 120 } 121 } 123 public void handleMessage(EBMessage msg) 125 { 126 if (msg instanceof EditPaneUpdate) 127 { 128 EditPaneUpdate epu = (EditPaneUpdate)msg; 129 if (epu.getEditPane().getView().equals(view) && 130 epu.getWhat().equals(EditPaneUpdate.BUFFER_CHANGED)) 131 refreshList(); 132 } 133 if (msg instanceof ViewUpdate) 134 { 135 ViewUpdate vu = (ViewUpdate)msg; 136 if (vu.getView().equals(view) && 137 vu.getWhat().equals(ViewUpdate.EDIT_PANE_CHANGED)) 138 refreshList(); 139 } 140 if (msg instanceof BufferUpdate) 141 { 142 BufferUpdate bu = (BufferUpdate)msg; 143 if (view.getBuffer().equals(bu.getBuffer()) 144 && 145 (bu.getWhat().equals(BufferUpdate.MARKERS_CHANGED) || bu.getWhat().equals(BufferUpdate.LOADED))) 146 refreshList(); 147 } 148 } 150 public void addNotify() 152 { 153 super.addNotify(); 154 EditBus.addToBus(this); 155 } 157 public void removeNotify() 159 { 160 super.removeNotify(); 161 EditBus.removeFromBus(this); 162 } 164 166 private JList markerList; 168 private View view; 169 private RolloverButton previous; 170 private RolloverButton next; 171 private RolloverButton clear; 172 174 private void refreshList() 176 { 177 java.util.Vector markers = view.getBuffer().getMarkers(); 178 if (markers.size() > 0) 179 { 180 markerList.setListData(markers); 181 markerList.setEnabled(true); 182 next.setEnabled(true); 183 previous.setEnabled(true); 184 clear.setEnabled(true); 185 } 186 else 187 { 188 markerList.setListData(new Object [] { 189 jEdit.getProperty("no-markers.label") }); 190 markerList.setEnabled(false); 191 next.setEnabled(false); 192 previous.setEnabled(false); 193 clear.setEnabled(false); 194 } 195 196 } 198 private void goToSelectedMarker() 200 { 201 Object value = markerList.getSelectedValue(); 202 if(!(value instanceof Marker)) 203 return; 204 205 Marker mark = (Marker)value; 206 view.getTextArea().setCaretPosition(mark.getPosition()); 207 view.toFront(); 208 view.requestFocus(); 209 view.getTextArea().requestFocus(); 210 } 212 private void updateSelection() 214 { 215 ListModel model = markerList.getModel(); 216 int currentLine = view.getTextArea().getCaretLine(); 217 Buffer buffer = view.getBuffer(); 218 for (int i = 0; i < model.getSize(); i++) 219 { 220 Object o = model.getElementAt(i); 221 if (o instanceof Marker) 222 { 223 Marker mark = (Marker)model.getElementAt(i); 224 if (buffer.getLineOfOffset(mark.getPosition()) == currentLine) 225 { 226 markerList.setSelectedIndex(i); 227 break; 228 } 229 } 230 } 231 232 } 234 236 238 class Renderer extends DefaultListCellRenderer 240 { 241 public Component getListCellRendererComponent( 242 JList list, Object value, int index, 243 boolean isSelected, boolean cellHasFocus) 244 { 245 super.getListCellRendererComponent(list,value, 246 index,isSelected,cellHasFocus); 247 248 if(value instanceof Marker) 249 { 250 Marker mark = (Marker)value; 251 JEditTextArea textArea = view.getTextArea(); 252 int pos = textArea.getLineOfOffset(mark.getPosition()); 253 String txt = view.getTextArea().getLineText(pos); 254 if (txt.equals("")) 255 txt = jEdit.getProperty("markers.blank-line"); 256 char shortcut_char = mark.getShortcut(); 257 String shortcut = ""; 258 if (shortcut_char > 0) 259 shortcut = "["+shortcut_char+"]"; 260 setText((pos+1)+" "+shortcut+": "+txt); 261 } 262 return this; 263 } 264 } 266 class MouseHandler extends MouseAdapter 268 { 269 public void mousePressed(MouseEvent evt) 270 { 271 if(evt.isConsumed()) 272 return; 273 274 int index = markerList.locationToIndex(evt.getPoint()); 275 markerList.setSelectedIndex(index); 276 277 goToSelectedMarker(); 278 } 279 } 281 } 283 | Popular Tags |