KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
38 import java.util.Enumeration JavaDoc;
39
40 import javax.swing.*;
41 import javax.swing.event.*;
42 import javax.swing.tree.*;
43 import javax.swing.table.*;
44 import javax.swing.text.BadLocationException JavaDoc;
45 import java.awt.event.*;
46 import java.awt.*;
47 import javax.swing.text.BadLocationException JavaDoc;
48 import javax.swing.text.Position JavaDoc;
49
50 import edu.rice.cs.drjava.model.RegionManager;
51 import edu.rice.cs.drjava.model.RegionManagerListener;
52 import edu.rice.cs.drjava.model.DocumentRegion;
53 import edu.rice.cs.drjava.model.OpenDefinitionsDocument;
54 import edu.rice.cs.drjava.config.*;
55 import edu.rice.cs.util.swing.Utilities;
56 import edu.rice.cs.util.UnexpectedException;
57
58 /**
59  * Panel for displaying browser history.
60  * Currently not used because of synchronization problems.
61  * This class is a swing view class and hence should only be accessed from the event-handling thread.
62  * @version $Id$
63  */

64 public class BrowserHistoryPanel extends RegionsListPanel<DocumentRegion> {
65   protected JButton _backButton;
66   protected JButton _forwardButton;
67   protected JButton _goToButton;
68   protected JButton _removeButton;
69   protected JButton _removeAllButton;
70   protected AbstractAction _backAction;
71   protected AbstractAction _forwardAction;
72   
73   /** Constructs a new browser history panel.
74    * This is swing view class and hence should only be accessed from the event-handling thread.
75    * @param frame the MainFrame
76    */

77   public BrowserHistoryPanel(MainFrame frame) {
78     super(frame, "Browser History");
79     _model.getBrowserHistoryManager().addListener(new RegionManagerListener<DocumentRegion>() {
80       public void regionAdded(DocumentRegion r, int index) {
81         addRegion(r, index);
82         _list.ensureIndexIsVisible(index);
83       }
84       public void regionChanged(DocumentRegion r, int index) {
85         regionRemoved(r);
86         regionAdded(r, index);
87       }
88       public void regionRemoved(DocumentRegion r) {
89         removeRegion(r);
90       }
91     });
92   }
93   
94   /** Action performed when the Enter key is pressed. Should be overridden. */
95   protected void performDefaultAction() {
96     goToRegion();
97   }
98   
99   /** Go to region. */
100   protected void goToRegion() {
101     ArrayList JavaDoc<DocumentRegion> r = getSelectedRegions();
102     if (r.size() == 1) {
103       _model.getBrowserHistoryManager().setCurrentRegion(r.get(0));
104       updateButtons();
105       RegionListUserObj<DocumentRegion> userObj = getUserObjForRegion(r.get(0));
106       if (userObj!=null) { _list.ensureIndexIsVisible(_listModel.indexOf(userObj)); }
107       _frame.scrollToDocumentAndOffset(r.get(0).getDocument(), r.get(0).getStartOffset(), false, false);
108     }
109   }
110   
111   /** Go to the previous region. */
112   protected void backRegion() {
113     RegionManager rm = _model.getBrowserHistoryManager();
114     
115     // add current location to history
116
_frame.addToBrowserHistory();
117     
118     // then move back
119
DocumentRegion r = rm.prevCurrentRegion();
120     updateButtons();
121     RegionListUserObj<DocumentRegion> userObj = getUserObjForRegion(r);
122     if (userObj!=null) { _list.ensureIndexIsVisible(_listModel.indexOf(userObj)); }
123     _frame.scrollToDocumentAndOffset(r.getDocument(), r.getStartOffset(), false, false);
124   }
125   
126   /** Go to the next region. */
127   protected void forwardRegion() {
128     RegionManager rm = _model.getBrowserHistoryManager();
129     
130     // add current location to history
131
_frame.addToBrowserHistory();
132     
133     // then move forward
134
DocumentRegion r = rm.nextCurrentRegion();
135     updateButtons();
136     RegionListUserObj<DocumentRegion> userObj = getUserObjForRegion(r);
137     if (userObj!=null) { _list.ensureIndexIsVisible(_listModel.indexOf(userObj)); }
138     _frame.scrollToDocumentAndOffset(r.getDocument(), r.getStartOffset(), false, false);
139   }
140   
141   /** @return the action to go back in the browser history. */
142   AbstractAction getBackAction() {
143     return _backAction;
144   }
145   
146   /** @return the action to go forwardin the browser history. */
147   AbstractAction getForwardAction() {
148     return _forwardAction;
149   }
150   
151   /** Creates the buttons for controlling the regions. Should be overridden. */
152   protected JComponent[] makeButtons() {
153     _backAction = new AbstractAction("Back") {
154       public void actionPerformed(ActionEvent ae) {
155         backRegion();
156       }
157     };
158     _backButton = new JButton(_backAction);
159
160     _forwardAction = new AbstractAction("Forward") {
161       public void actionPerformed(ActionEvent ae) {
162         forwardRegion();
163       }
164     };
165     _forwardButton = new JButton(_forwardAction);
166
167     Action goToAction = new AbstractAction("Go to") {
168       public void actionPerformed(ActionEvent ae) {
169         goToRegion();
170       }
171     };
172     _goToButton = new JButton(goToAction);
173
174     Action removeAction = new AbstractAction("Remove") {
175       public void actionPerformed(ActionEvent ae) {
176         for (DocumentRegion r: getSelectedRegions()) {
177           _model.getBrowserHistoryManager().removeRegion(r);
178         }
179       }
180     };
181     _removeButton = new JButton(removeAction);
182     
183     Action removeAllAction = new AbstractAction("Remove All") {
184       public void actionPerformed(ActionEvent ae) {
185         _model.getBrowserHistoryManager().clearRegions();
186       }
187     };
188     _removeAllButton = new JButton(removeAllAction);
189     
190     JComponent[] buts = new JComponent[] {
191       _backButton,
192         _forwardButton,
193         _goToButton,
194         _removeButton,
195         _removeAllButton
196     };
197     
198     return buts;
199   }
200
201   /** Update button state and text. */
202   protected void updateButtons() {
203     ArrayList JavaDoc<DocumentRegion> regs = getSelectedRegions();
204     _goToButton.setEnabled(regs.size()==1);
205     _removeButton.setEnabled(regs.size()>0);
206     _removeAllButton.setEnabled(_listModel.size()>0);
207     _backAction.setEnabled((_listModel.size()>0) && (!_model.getBrowserHistoryManager().isCurrentRegionFirst()));
208     _forwardAction.setEnabled((_listModel.size()>0) && (!_model.getBrowserHistoryManager().isCurrentRegionLast()));
209   }
210   
211   /** Makes the popup menu actions. Should be overridden if additional actions besides "Go to" and "Remove" are added. */
212   protected AbstractAction[] makePopupMenuActions() {
213     AbstractAction[] acts = new AbstractAction[] {
214       new AbstractAction("Go to") {
215         public void actionPerformed(ActionEvent e) {
216           goToRegion();
217         }
218       },
219         
220         new AbstractAction("Remove") {
221           public void actionPerformed(ActionEvent e) {
222             for (DocumentRegion r: getSelectedRegions()) {
223               _model.getBrowserHistoryManager().removeRegion(r);
224             }
225           }
226         }
227     };
228     return acts;
229   }
230   
231   /** @return the usser object in the list associated with the region, or null if not found */
232   protected RegionListUserObj<DocumentRegion> getUserObjForRegion(DocumentRegion r) {
233     for(int i=0; i<_listModel.size(); ++i) {
234       @SuppressWarnings JavaDoc("unchecked") RegionListUserObj<DocumentRegion> userObj = (RegionListUserObj<DocumentRegion>)_listModel.get(i);
235       if (userObj.region()==r) {
236         return userObj;
237       }
238     }
239     return null;
240   }
241   
242   /** Factory method to create user objects put in the tree.
243    * If subclasses extend RegionListUserObj, they need to override this method. */

244   protected RegionListUserObj<DocumentRegion> makeRegionListUserObj(DocumentRegion r) {
245     return new BrowserHistoryListUserObj(r);
246   }
247
248   /** Class that gets put into the tree. The toString() method determines what's displayed in the three. */
249   protected class BrowserHistoryListUserObj extends RegionListUserObj<DocumentRegion> {
250     public BrowserHistoryListUserObj(DocumentRegion r) { super(r); }
251     public String JavaDoc toString() {
252       final StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
253       _region.getDocument().acquireReadLock();
254       try {
255         sb.append("<html>");
256         if (_region==_model.getBrowserHistoryManager().getCurrentRegion()) {
257           sb.append("<font color=\"red\">");
258         }
259         sb.append(_region.getDocument().toString());
260         sb.append(':');
261         sb.append(lineNumber());
262         try {
263           sb.append(": ");
264           int length = Math.min(120, _region.getEndOffset()-_region.getStartOffset());
265           sb.append(_region.getDocument().getText(_region.getStartOffset(), length).trim());
266         } catch(BadLocationException JavaDoc bpe) { /* ignore, just don't display line */ }
267         if (_region.equals(_model.getBrowserHistoryManager().getCurrentRegion())) {
268           sb.append("</font>");
269         }
270         sb.append("</html>");
271       } finally { _region.getDocument().releaseReadLock(); }
272       return sb.toString();
273     }
274     public boolean equals(Object JavaDoc other) {
275       @SuppressWarnings JavaDoc("unchecked") BrowserHistoryListUserObj o = (BrowserHistoryListUserObj)other;
276       return (o.region().getDocument().equals(region().getDocument())) &&
277         (o.region().getStartOffset()==region().getStartOffset()) &&
278         (o.region().getEndOffset()==region().getEndOffset());
279     }
280   }
281 }
282
Popular Tags