KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > gui > MarkerViewer


1 /*
2  * MarkerViewer.java - Dockable view of markers in the current buffer
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2004 Nicholas O'Leary
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.gui;
24
25 //{{{ Imports
26
import java.awt.*;
27 import java.awt.event.*;
28 import javax.swing.*;
29 import javax.swing.border.EmptyBorder JavaDoc;
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 //}}}
39

40 public class MarkerViewer extends JPanel implements EBComponent, ActionListener
41 {
42     //{{{ MarkerViewer constructor
43
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     } //}}}
95

96     //{{{ requestDefaultFocus() method
97
public boolean requestDefaultFocus()
98     {
99         markerList.requestFocus();
100         return true;
101     } //}}}
102

103     //{{{ actionPerformed() method
104
public void actionPerformed(ActionEvent evt)
105     {
106         String JavaDoc 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     } //}}}
122

123     //{{{ handleMessage() method
124
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     }//}}}
149

150     //{{{ addNotify() method
151
public void addNotify()
152     {
153         super.addNotify();
154         EditBus.addToBus(this);
155     } //}}}
156

157     //{{{ removeNotify() method
158
public void removeNotify()
159     {
160         super.removeNotify();
161         EditBus.removeFromBus(this);
162     } //}}}
163

164     //{{{ Private members
165

166     //{{{ Instance variables
167
private JList markerList;
168     private View view;
169     private RolloverButton previous;
170     private RolloverButton next;
171     private RolloverButton clear;
172     //}}}
173

174     //{{{ refreshList() method
175
private void refreshList()
176     {
177         java.util.Vector JavaDoc 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 JavaDoc[] {
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     } //}}}
197

198     //{{{ goToSelectedMarker() method
199
private void goToSelectedMarker()
200     {
201         Object JavaDoc 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     } //}}}
211

212     //{{{ updateSelection() method
213
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 JavaDoc 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     } //}}}
233

234     //}}}
235

236     //{{{ Inner classes
237

238     //{{{ Renderer Class
239
class Renderer extends DefaultListCellRenderer
240     {
241         public Component getListCellRendererComponent(
242             JList list, Object JavaDoc 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 JavaDoc txt = view.getTextArea().getLineText(pos);
254                 if (txt.equals(""))
255                     txt = jEdit.getProperty("markers.blank-line");
256                 char shortcut_char = mark.getShortcut();
257                 String JavaDoc shortcut = "";
258                 if (shortcut_char > 0)
259                     shortcut = "["+shortcut_char+"]";
260                 setText((pos+1)+" "+shortcut+": "+txt);
261             }
262             return this;
263         }
264     } //}}}
265

266     //{{{ MouseHandler Class
267
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     } //}}}
280

281     //}}}
282
}
283
Popular Tags