KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > ui > FindResultsPanel


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.ui;
35
36 import java.util.Vector JavaDoc;
37 import java.util.LinkedList JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.Enumeration JavaDoc;
40 import java.io.File JavaDoc;
41
42 import javax.swing.*;
43 import javax.swing.event.*;
44 import javax.swing.tree.*;
45 import javax.swing.table.*;
46 import javax.swing.text.BadLocationException JavaDoc;
47 import java.awt.event.*;
48 import java.awt.*;
49 import javax.swing.text.BadLocationException JavaDoc;
50 import javax.swing.text.Position JavaDoc;
51
52 import javax.swing.border.CompoundBorder JavaDoc;
53 import javax.swing.border.LineBorder JavaDoc;
54 import javax.swing.border.MatteBorder JavaDoc;
55
56 import edu.rice.cs.drjava.model.RegionManagerListener;
57 import edu.rice.cs.drjava.model.DocumentRegion;
58 import edu.rice.cs.drjava.model.MovingDocumentRegion;
59 import edu.rice.cs.drjava.model.SimpleDocumentRegion;
60 import edu.rice.cs.drjava.model.OpenDefinitionsDocument;
61 import edu.rice.cs.drjava.model.FileMovedException;
62 import edu.rice.cs.drjava.model.RegionManager;
63 import edu.rice.cs.drjava.config.*;
64 import edu.rice.cs.drjava.DrJava;
65 import edu.rice.cs.plt.tuple.Pair;
66 import edu.rice.cs.util.swing.Utilities;
67 import edu.rice.cs.util.UnexpectedException;
68 import edu.rice.cs.util.StringOps;
69 import edu.rice.cs.drjava.config.OptionConstants;
70
71 /** Panel for displaying find results. This class is a swing class which should only be accessed from the event thread.
72   * @version $Id$
73   */

74 public class FindResultsPanel extends RegionsTreePanel<MovingDocumentRegion> {
75   protected JButton _goToButton;
76   protected JButton _bookmarkButton;
77   protected JButton _removeButton;
78   protected JComboBox _colorBox;
79   protected RegionManager<MovingDocumentRegion> _regionManager;
80   protected int _lastIndex;
81   
82   /** Saved option listeners kept in this field so they can be removed for garbage collection */
83   private LinkedList JavaDoc<Pair<Option<Color>, OptionListener<Color>>> _colorOptionListeners =
84     new LinkedList JavaDoc<Pair<Option<Color>, OptionListener<Color>>>();
85   
86   /** Constructs a new find results panel. This is swing class which should only be accessed from the event thread.
87     * @param frame the MainFrame
88     * @param rm the region manager associated with this panel
89     * @param title for the panel
90     */

91   public FindResultsPanel(MainFrame frame, RegionManager<MovingDocumentRegion> rm, String JavaDoc title) {
92     super(frame, title);
93     _regionManager = rm;
94     _regionManager.addListener(new RegionManagerListener<MovingDocumentRegion>() {
95       public void regionAdded(MovingDocumentRegion r, int index) { addRegion(r); }
96       public void regionChanged(MovingDocumentRegion r, int index) {
97         regionRemoved(r);
98         regionAdded(r, index);
99       }
100       public void regionRemoved(MovingDocumentRegion r) { removeRegion(r); }
101     });
102     
103     OptionListener<Color> temp;
104     Pair<Option<Color>, OptionListener<Color>> pair;
105     for(int i=0; i<OptionConstants.FIND_RESULTS_COLORS.length; ++i) {
106       temp = new FindResultsColorOptionListener(i);
107       pair = new Pair<Option<Color>, OptionListener<Color>>(OptionConstants.FIND_RESULTS_COLORS[i], temp);
108       _colorOptionListeners.add(pair);
109       DrJava.getConfig().addOptionListener( OptionConstants.FIND_RESULTS_COLORS[i], temp);
110     }
111   }
112   
113   static class ColorComboRenderer extends JPanel implements ListCellRenderer {
114     private Color m_c = Color.black;
115     private DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();
116     private final static Dimension preferredSize = new Dimension(0, 20);
117     
118     public ColorComboRenderer() {
119       super();
120       setBorder(new CompoundBorder JavaDoc(new MatteBorder JavaDoc(2, 10, 2, 10, Color.white), new LineBorder JavaDoc(Color.black)));
121     }
122     
123     public Component getListCellRendererComponent(JList list, Object JavaDoc value,
124                                                   int row, boolean sel, boolean hasFocus) {
125       Component renderer;
126       if (value instanceof Color) {
127         m_c = (Color) value;
128         renderer = this;
129       }
130       else {
131         JLabel l = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, row, sel, hasFocus);
132         l.setHorizontalAlignment(JLabel.CENTER);
133         renderer = l;
134       }
135       // Taken out because this is a 1.5 method; not sure if it's necessary
136
//renderer.setPreferredSize(preferredSize);
137
return renderer;
138     }
139     
140     public void paint(Graphics g) {
141       setBackground(m_c);
142       super.paint(g);
143     }
144   }
145   
146   /** Creates the buttons for controlling the regions. Should be overridden. */
147   protected JComponent[] makeButtons() {
148     Action goToAction = new AbstractAction("Go to") {
149       public void actionPerformed(ActionEvent ae) { goToRegion(); }
150     };
151     _goToButton = new JButton(goToAction);
152
153     Action bookmarkAction = new AbstractAction("Bookmark") {
154       public void actionPerformed(ActionEvent ae) { _bookmark(); }
155     };
156     _bookmarkButton = new JButton(bookmarkAction);
157
158     Action removeAction = new AbstractAction("Remove") {
159       public void actionPerformed(ActionEvent ae) { _remove(); }
160     };
161     _removeButton = new JButton(removeAction);
162
163     // "Highlight" label panel
164
final JPanel highlightPanel = new JPanel();
165     final Color normalColor = highlightPanel.getBackground();
166     highlightPanel.add(new JLabel("Highlight:"));
167     
168     _colorBox = new JComboBox();
169     for(int i=0; i<OptionConstants.FIND_RESULTS_COLORS.length; ++i) {
170       _colorBox.addItem(DrJava.getConfig().getSetting(OptionConstants.FIND_RESULTS_COLORS[i]));
171     }
172     _colorBox.addItem("None");
173     _colorBox.setRenderer(new ColorComboRenderer());
174     _colorBox.addActionListener(new ActionListener() {
175       public void actionPerformed(ActionEvent e) {
176         if (_lastIndex<OptionConstants.FIND_RESULTS_COLORS.length) {
177           --DefinitionsPane.FIND_RESULTS_PAINTERS_USAGE[_lastIndex];
178         }
179         _lastIndex = _colorBox.getSelectedIndex();
180         if (_lastIndex<OptionConstants.FIND_RESULTS_COLORS.length) {
181           ++DefinitionsPane.FIND_RESULTS_PAINTERS_USAGE[_lastIndex];
182           highlightPanel.setBackground(DrJava.getConfig().getSetting(OptionConstants.FIND_RESULTS_COLORS[_lastIndex]));
183         }
184         else highlightPanel.setBackground(normalColor);
185
186         _frame.refreshFindResultsHighlightPainter(FindResultsPanel.this,
187                                                   DefinitionsPane.FIND_RESULTS_PAINTERS[_lastIndex]);
188       }
189     });
190     // find the first available color, or choose "None"
191
for(_lastIndex=0; _lastIndex<OptionConstants.FIND_RESULTS_COLORS.length; ++_lastIndex) {
192       if (DefinitionsPane.FIND_RESULTS_PAINTERS_USAGE[_lastIndex]==0) break;
193     }
194     if (_lastIndex<OptionConstants.FIND_RESULTS_COLORS.length) {
195       ++DefinitionsPane.FIND_RESULTS_PAINTERS_USAGE[_lastIndex];
196     }
197     _colorBox.setSelectedIndex(_lastIndex);
198     _frame.refreshFindResultsHighlightPainter(FindResultsPanel.this,
199                                               DefinitionsPane.FIND_RESULTS_PAINTERS[_lastIndex]);
200     
201     return new JComponent[] { _goToButton, _bookmarkButton, _removeButton, highlightPanel, _colorBox};
202   }
203   
204   /** @return the selected painter for these find results. */
205   public ReverseHighlighter.DefaultUnderlineHighlightPainter getSelectedPainter() {
206     return DefinitionsPane.FIND_RESULTS_PAINTERS[_lastIndex];
207   }
208   
209   /** Turn the selected regions into bookmarks. */
210   private void _bookmark() {
211     for (final MovingDocumentRegion r: getSelectedRegions()) {
212       DocumentRegion bookmark =
213         _model.getBookmarkManager().getRegionOverlapping(r.getDocument(), r.getStartOffset(), r.getEndOffset());
214       if (bookmark == null) {
215         try {
216           DocumentRegion newR =
217             new SimpleDocumentRegion(r.getDocument(), r.getDocument().getFile(), r.getStartOffset(), r.getEndOffset());
218           _model.getBookmarkManager().addRegion(newR);
219         }
220         catch (FileMovedException fme) { throw new UnexpectedException(fme); }
221       }
222     }
223   }
224   
225   /** Action performed when the Enter key is pressed. Should be overridden. */
226   protected void performDefaultAction() { goToRegion(); }
227   
228   /** Remove the selected regions. */
229   private void _remove() {
230     for (MovingDocumentRegion r: getSelectedRegions()) _regionManager.removeRegion(r);
231     if (_regionManager.getRegions().size()==0) { _close(); }
232   }
233
234   /** Update button state and text. */
235   protected void updateButtons() {
236     ArrayList JavaDoc<MovingDocumentRegion> regs = getSelectedRegions();
237     _goToButton.setEnabled(regs.size()==1);
238     _bookmarkButton.setEnabled(regs.size()>0);
239     _removeButton.setEnabled(regs.size()>0);
240   }
241   
242   /** Makes the popup menu actions. Should be overridden if additional actions besides "Go to" and "Remove" are added. */
243   protected AbstractAction[] makePopupMenuActions() {
244     AbstractAction[] acts = new AbstractAction[] {
245       new AbstractAction("Go to") { public void actionPerformed(ActionEvent e) { goToRegion(); } },
246         
247         new AbstractAction("Bookmark") { public void actionPerformed(ActionEvent e) { _bookmark(); } },
248           
249           new AbstractAction("Remove") { public void actionPerformed(ActionEvent e) { _remove(); } }
250     };
251     return acts;
252   }
253   
254   /** Close the pane. */
255   public void _close() {
256     super._close();
257     freeResources();
258   }
259   
260   /** Free the resources; this can be used if the panel was never actually displayed. */
261   public void freeResources() {
262     _regionManager.clearRegions();
263     _model.disposeFindResultsManager(_regionManager);
264     for (Pair<Option<Color>, OptionListener<Color>> p: _colorOptionListeners) {
265       DrJava.getConfig().removeOptionListener(p.first(), p.second());
266     }
267     if (_lastIndex<OptionConstants.FIND_RESULTS_COLORS.length) {
268       --DefinitionsPane.FIND_RESULTS_PAINTERS_USAGE[_lastIndex];
269     }
270   }
271
272   /** Factory method to create user objects put in the tree.
273    * If subclasses extend RegionTreeUserObj, they need to override this method. */

274   protected RegionTreeUserObj<MovingDocumentRegion> makeRegionTreeUserObj(MovingDocumentRegion r) {
275     return new FindResultsRegionTreeUserObj(r);
276   }
277
278   /** Class that gets put into the tree. The toString() method determines what's displayed in the three. */
279   protected static class FindResultsRegionTreeUserObj extends RegionTreeUserObj<MovingDocumentRegion> {
280     protected int _lineNumber;
281     public FindResultsRegionTreeUserObj(MovingDocumentRegion r) {
282       super(r);
283       _lineNumber = _region.getDocument().getLineOfOffset(_region.getStartOffset())+1;
284     }
285 // public int lineNumber() {
286
// return _lineNumber;
287
// }
288
public String JavaDoc toString() {
289       final StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
290       sb.append("<html>");
291       sb.append(lineNumber());
292       sb.append(": ");
293       sb.append(_region.getString());
294       sb.append("</html>");
295       return sb.toString();
296     }
297   }
298   
299   /** The OptionListener for FIND_RESULTS_COLOR. */
300   private class FindResultsColorOptionListener implements OptionListener<Color> {
301     private int _index;
302     public FindResultsColorOptionListener(int i) { _index = i; }
303     public void optionChanged(OptionEvent<Color> oce) {
304       int pos = _colorBox.getSelectedIndex();
305       _colorBox.removeItemAt(_index);
306       _colorBox.insertItemAt(oce.value, _index);
307       _colorBox.setSelectedIndex(pos);
308       if (pos==_index) {
309         _frame.refreshFindResultsHighlightPainter(FindResultsPanel.this, DefinitionsPane.FIND_RESULTS_PAINTERS[_index]);
310       }
311     }
312   }
313 }
314
Popular Tags