001: /*
002: * MarkerViewer.java - Dockable view of markers in the current buffer
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2004 Nicholas O'Leary
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.gui;
024:
025: //{{{ Imports
026: import java.awt.*;
027: import java.awt.event.*;
028: import javax.swing.*;
029: import javax.swing.border.EmptyBorder;
030: import javax.swing.event.*;
031: import org.gjt.sp.jedit.*;
032: import org.gjt.sp.jedit.gui.RolloverButton;
033: import org.gjt.sp.jedit.textarea.JEditTextArea;
034: import org.gjt.sp.jedit.msg.BufferUpdate;
035: import org.gjt.sp.jedit.msg.EditPaneUpdate;
036: import org.gjt.sp.jedit.msg.ViewUpdate;
037: import org.gjt.sp.util.Log;
038:
039: //}}}
040:
041: public class MarkerViewer extends JPanel implements EBComponent,
042: ActionListener {
043: //{{{ MarkerViewer constructor
044: public MarkerViewer(View view) {
045: super (new BorderLayout());
046: this .view = view;
047: Box toolBar = new Box(BoxLayout.X_AXIS);
048:
049: toolBar.add(new JLabel(GUIUtilities.prettifyMenuLabel(jEdit
050: .getProperty("markers.label"))));
051:
052: toolBar.add(Box.createGlue());
053:
054: RolloverButton addMarker = new RolloverButton(GUIUtilities
055: .loadIcon("Plus.png"));
056: addMarker.setToolTipText(GUIUtilities.prettifyMenuLabel(jEdit
057: .getProperty("add-marker.label")));
058: addMarker.addActionListener(this );
059: addMarker.setActionCommand("add-marker");
060: toolBar.add(addMarker);
061:
062: previous = new RolloverButton(GUIUtilities
063: .loadIcon("ArrowL.png"));
064: previous.setToolTipText(GUIUtilities.prettifyMenuLabel(jEdit
065: .getProperty("prev-marker.label")));
066: previous.addActionListener(this );
067: previous.setActionCommand("prev-marker");
068: toolBar.add(previous);
069:
070: next = new RolloverButton(GUIUtilities.loadIcon("ArrowR.png"));
071: next.setToolTipText(GUIUtilities.prettifyMenuLabel(jEdit
072: .getProperty("next-marker.label")));
073: next.addActionListener(this );
074: next.setActionCommand("next-marker");
075: toolBar.add(next);
076:
077: clear = new RolloverButton(GUIUtilities.loadIcon("Clear.png"));
078: clear.setToolTipText(GUIUtilities.prettifyMenuLabel(jEdit
079: .getProperty("remove-all-markers.label")));
080: clear.addActionListener(this );
081: clear.setActionCommand("clear");
082: toolBar.add(clear);
083:
084: add(BorderLayout.NORTH, toolBar);
085:
086: markerList = new JList();
087: markerList
088: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
089: markerList.setCellRenderer(new Renderer());
090: markerList.addMouseListener(new MouseHandler());
091:
092: add(BorderLayout.CENTER, new JScrollPane(markerList));
093:
094: refreshList();
095: } //}}}
096:
097: //{{{ requestDefaultFocus() method
098: public boolean requestDefaultFocus() {
099: markerList.requestFocus();
100: return true;
101: } //}}}
102:
103: //{{{ actionPerformed() method
104: public void actionPerformed(ActionEvent evt) {
105: String cmd = evt.getActionCommand();
106: if (cmd.equals("clear"))
107: view.getBuffer().removeAllMarkers();
108: else if (cmd.equals("add-marker"))
109: view.getEditPane().addMarker();
110: else if (cmd.equals("next-marker")) {
111: view.getEditPane().goToNextMarker(false);
112: updateSelection();
113: } else if (cmd.equals("prev-marker")) {
114: view.getEditPane().goToPrevMarker(false);
115: updateSelection();
116: }
117: } //}}}
118:
119: //{{{ handleMessage() method
120: public void handleMessage(EBMessage msg) {
121: if (msg instanceof EditPaneUpdate) {
122: EditPaneUpdate epu = (EditPaneUpdate) msg;
123: if (epu.getEditPane().getView().equals(view)
124: && epu.getWhat().equals(
125: EditPaneUpdate.BUFFER_CHANGED))
126: refreshList();
127: }
128: if (msg instanceof ViewUpdate) {
129: ViewUpdate vu = (ViewUpdate) msg;
130: if (vu.getView().equals(view)
131: && vu.getWhat()
132: .equals(ViewUpdate.EDIT_PANE_CHANGED))
133: refreshList();
134: }
135: if (msg instanceof BufferUpdate) {
136: BufferUpdate bu = (BufferUpdate) msg;
137: if (view.getBuffer().equals(bu.getBuffer())
138: && (bu.getWhat().equals(
139: BufferUpdate.MARKERS_CHANGED) || bu
140: .getWhat().equals(BufferUpdate.LOADED)))
141: refreshList();
142: }
143: }//}}}
144:
145: //{{{ addNotify() method
146: public void addNotify() {
147: super .addNotify();
148: EditBus.addToBus(this );
149: } //}}}
150:
151: //{{{ removeNotify() method
152: public void removeNotify() {
153: super .removeNotify();
154: EditBus.removeFromBus(this );
155: } //}}}
156:
157: //{{{ Private members
158:
159: //{{{ Instance variables
160: private JList markerList;
161: private View view;
162: private RolloverButton previous;
163: private RolloverButton next;
164: private RolloverButton clear;
165:
166: //}}}
167:
168: //{{{ refreshList() method
169: private void refreshList() {
170: java.util.Vector markers = view.getBuffer().getMarkers();
171: if (markers.size() > 0) {
172: markerList.setListData(markers);
173: markerList.setEnabled(true);
174: next.setEnabled(true);
175: previous.setEnabled(true);
176: clear.setEnabled(true);
177: } else {
178: markerList.setListData(new Object[] { jEdit
179: .getProperty("no-markers.label") });
180: markerList.setEnabled(false);
181: next.setEnabled(false);
182: previous.setEnabled(false);
183: clear.setEnabled(false);
184: }
185:
186: } //}}}
187:
188: //{{{ goToSelectedMarker() method
189: private void goToSelectedMarker() {
190: Object value = markerList.getSelectedValue();
191: if (!(value instanceof Marker))
192: return;
193:
194: Marker mark = (Marker) value;
195: view.getTextArea().setCaretPosition(mark.getPosition());
196: view.toFront();
197: view.requestFocus();
198: view.getTextArea().requestFocus();
199: } //}}}
200:
201: //{{{ updateSelection() method
202: private void updateSelection() {
203: ListModel model = markerList.getModel();
204: int currentLine = view.getTextArea().getCaretLine();
205: Buffer buffer = view.getBuffer();
206: for (int i = 0; i < model.getSize(); i++) {
207: Object o = model.getElementAt(i);
208: if (o instanceof Marker) {
209: Marker mark = (Marker) model.getElementAt(i);
210: if (buffer.getLineOfOffset(mark.getPosition()) == currentLine) {
211: markerList.setSelectedIndex(i);
212: break;
213: }
214: }
215: }
216:
217: } //}}}
218:
219: //}}}
220:
221: //{{{ Inner classes
222:
223: //{{{ Renderer Class
224: class Renderer extends DefaultListCellRenderer {
225: public Component getListCellRendererComponent(JList list,
226: Object value, int index, boolean isSelected,
227: boolean cellHasFocus) {
228: super .getListCellRendererComponent(list, value, index,
229: isSelected, cellHasFocus);
230:
231: if (value instanceof Marker) {
232: Marker mark = (Marker) value;
233: JEditTextArea textArea = view.getTextArea();
234: int pos = textArea.getLineOfOffset(mark.getPosition());
235: String txt = view.getTextArea().getLineText(pos);
236: if (txt.equals(""))
237: txt = jEdit.getProperty("markers.blank-line");
238: char shortcut_char = mark.getShortcut();
239: String shortcut = "";
240: if (shortcut_char > 0)
241: shortcut = "[" + shortcut_char + "]";
242: setText((pos + 1) + " " + shortcut + ": " + txt);
243: }
244: return this ;
245: }
246: } //}}}
247:
248: //{{{ MouseHandler Class
249: class MouseHandler extends MouseAdapter {
250: public void mousePressed(MouseEvent evt) {
251: if (evt.isConsumed())
252: return;
253:
254: int index = markerList.locationToIndex(evt.getPoint());
255: markerList.setSelectedIndex(index);
256:
257: goToSelectedMarker();
258: }
259: } //}}}
260:
261: //}}}
262: }
|