001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.ui;
038:
039: import java.util.Vector;
040: import java.util.ArrayList;
041: import java.util.Enumeration;
042:
043: import javax.swing.*;
044: import javax.swing.event.*;
045: import javax.swing.tree.*;
046: import javax.swing.table.*;
047: import javax.swing.text.BadLocationException;
048: import java.awt.event.*;
049: import java.awt.*;
050: import javax.swing.text.BadLocationException;
051: import javax.swing.text.Position;
052:
053: import edu.rice.cs.drjava.model.RegionManager;
054: import edu.rice.cs.drjava.model.RegionManagerListener;
055: import edu.rice.cs.drjava.model.DocumentRegion;
056: import edu.rice.cs.drjava.model.OpenDefinitionsDocument;
057: import edu.rice.cs.drjava.config.*;
058: import edu.rice.cs.util.swing.Utilities;
059: import edu.rice.cs.util.UnexpectedException;
060:
061: /**
062: * Panel for displaying browser history.
063: * Currently not used because of synchronization problems.
064: * This class is a swing view class and hence should only be accessed from the event-handling thread.
065: * @version $Id$
066: */
067: public class BrowserHistoryPanel extends
068: RegionsListPanel<DocumentRegion> {
069: protected JButton _backButton;
070: protected JButton _forwardButton;
071: protected JButton _goToButton;
072: protected JButton _removeButton;
073: protected JButton _removeAllButton;
074: protected AbstractAction _backAction;
075: protected AbstractAction _forwardAction;
076:
077: /** Constructs a new browser history panel.
078: * This is swing view class and hence should only be accessed from the event-handling thread.
079: * @param frame the MainFrame
080: */
081: public BrowserHistoryPanel(MainFrame frame) {
082: super (frame, "Browser History");
083: _model.getBrowserHistoryManager().addListener(
084: new RegionManagerListener<DocumentRegion>() {
085: public void regionAdded(DocumentRegion r, int index) {
086: addRegion(r, index);
087: _list.ensureIndexIsVisible(index);
088: }
089:
090: public void regionChanged(DocumentRegion r,
091: int index) {
092: regionRemoved(r);
093: regionAdded(r, index);
094: }
095:
096: public void regionRemoved(DocumentRegion r) {
097: removeRegion(r);
098: }
099: });
100: }
101:
102: /** Action performed when the Enter key is pressed. Should be overridden. */
103: protected void performDefaultAction() {
104: goToRegion();
105: }
106:
107: /** Go to region. */
108: protected void goToRegion() {
109: ArrayList<DocumentRegion> r = getSelectedRegions();
110: if (r.size() == 1) {
111: _model.getBrowserHistoryManager()
112: .setCurrentRegion(r.get(0));
113: updateButtons();
114: RegionListUserObj<DocumentRegion> userObj = getUserObjForRegion(r
115: .get(0));
116: if (userObj != null) {
117: _list.ensureIndexIsVisible(_listModel.indexOf(userObj));
118: }
119: _frame.scrollToDocumentAndOffset(r.get(0).getDocument(), r
120: .get(0).getStartOffset(), false, false);
121: }
122: }
123:
124: /** Go to the previous region. */
125: protected void backRegion() {
126: RegionManager rm = _model.getBrowserHistoryManager();
127:
128: // add current location to history
129: _frame.addToBrowserHistory();
130:
131: // then move back
132: DocumentRegion r = rm.prevCurrentRegion();
133: updateButtons();
134: RegionListUserObj<DocumentRegion> userObj = getUserObjForRegion(r);
135: if (userObj != null) {
136: _list.ensureIndexIsVisible(_listModel.indexOf(userObj));
137: }
138: _frame.scrollToDocumentAndOffset(r.getDocument(), r
139: .getStartOffset(), false, false);
140: }
141:
142: /** Go to the next region. */
143: protected void forwardRegion() {
144: RegionManager rm = _model.getBrowserHistoryManager();
145:
146: // add current location to history
147: _frame.addToBrowserHistory();
148:
149: // then move forward
150: DocumentRegion r = rm.nextCurrentRegion();
151: updateButtons();
152: RegionListUserObj<DocumentRegion> userObj = getUserObjForRegion(r);
153: if (userObj != null) {
154: _list.ensureIndexIsVisible(_listModel.indexOf(userObj));
155: }
156: _frame.scrollToDocumentAndOffset(r.getDocument(), r
157: .getStartOffset(), false, false);
158: }
159:
160: /** @return the action to go back in the browser history. */
161: AbstractAction getBackAction() {
162: return _backAction;
163: }
164:
165: /** @return the action to go forwardin the browser history. */
166: AbstractAction getForwardAction() {
167: return _forwardAction;
168: }
169:
170: /** Creates the buttons for controlling the regions. Should be overridden. */
171: protected JComponent[] makeButtons() {
172: _backAction = new AbstractAction("Back") {
173: public void actionPerformed(ActionEvent ae) {
174: backRegion();
175: }
176: };
177: _backButton = new JButton(_backAction);
178:
179: _forwardAction = new AbstractAction("Forward") {
180: public void actionPerformed(ActionEvent ae) {
181: forwardRegion();
182: }
183: };
184: _forwardButton = new JButton(_forwardAction);
185:
186: Action goToAction = new AbstractAction("Go to") {
187: public void actionPerformed(ActionEvent ae) {
188: goToRegion();
189: }
190: };
191: _goToButton = new JButton(goToAction);
192:
193: Action removeAction = new AbstractAction("Remove") {
194: public void actionPerformed(ActionEvent ae) {
195: for (DocumentRegion r : getSelectedRegions()) {
196: _model.getBrowserHistoryManager().removeRegion(r);
197: }
198: }
199: };
200: _removeButton = new JButton(removeAction);
201:
202: Action removeAllAction = new AbstractAction("Remove All") {
203: public void actionPerformed(ActionEvent ae) {
204: _model.getBrowserHistoryManager().clearRegions();
205: }
206: };
207: _removeAllButton = new JButton(removeAllAction);
208:
209: JComponent[] buts = new JComponent[] { _backButton,
210: _forwardButton, _goToButton, _removeButton,
211: _removeAllButton };
212:
213: return buts;
214: }
215:
216: /** Update button state and text. */
217: protected void updateButtons() {
218: ArrayList<DocumentRegion> regs = getSelectedRegions();
219: _goToButton.setEnabled(regs.size() == 1);
220: _removeButton.setEnabled(regs.size() > 0);
221: _removeAllButton.setEnabled(_listModel.size() > 0);
222: _backAction.setEnabled((_listModel.size() > 0)
223: && (!_model.getBrowserHistoryManager()
224: .isCurrentRegionFirst()));
225: _forwardAction.setEnabled((_listModel.size() > 0)
226: && (!_model.getBrowserHistoryManager()
227: .isCurrentRegionLast()));
228: }
229:
230: /** Makes the popup menu actions. Should be overridden if additional actions besides "Go to" and "Remove" are added. */
231: protected AbstractAction[] makePopupMenuActions() {
232: AbstractAction[] acts = new AbstractAction[] {
233: new AbstractAction("Go to") {
234: public void actionPerformed(ActionEvent e) {
235: goToRegion();
236: }
237: },
238:
239: new AbstractAction("Remove") {
240: public void actionPerformed(ActionEvent e) {
241: for (DocumentRegion r : getSelectedRegions()) {
242: _model.getBrowserHistoryManager()
243: .removeRegion(r);
244: }
245: }
246: } };
247: return acts;
248: }
249:
250: /** @return the usser object in the list associated with the region, or null if not found */
251: protected RegionListUserObj<DocumentRegion> getUserObjForRegion(
252: DocumentRegion r) {
253: for (int i = 0; i < _listModel.size(); ++i) {
254: @SuppressWarnings("unchecked")
255: RegionListUserObj<DocumentRegion> userObj = (RegionListUserObj<DocumentRegion>) _listModel
256: .get(i);
257: if (userObj.region() == r) {
258: return userObj;
259: }
260: }
261: return null;
262: }
263:
264: /** Factory method to create user objects put in the tree.
265: * If subclasses extend RegionListUserObj, they need to override this method. */
266: protected RegionListUserObj<DocumentRegion> makeRegionListUserObj(
267: DocumentRegion r) {
268: return new BrowserHistoryListUserObj(r);
269: }
270:
271: /** Class that gets put into the tree. The toString() method determines what's displayed in the three. */
272: protected class BrowserHistoryListUserObj extends
273: RegionListUserObj<DocumentRegion> {
274: public BrowserHistoryListUserObj(DocumentRegion r) {
275: super (r);
276: }
277:
278: public String toString() {
279: final StringBuilder sb = new StringBuilder();
280: _region.getDocument().acquireReadLock();
281: try {
282: sb.append("<html>");
283: if (_region == _model.getBrowserHistoryManager()
284: .getCurrentRegion()) {
285: sb.append("<font color=\"red\">");
286: }
287: sb.append(_region.getDocument().toString());
288: sb.append(':');
289: sb.append(lineNumber());
290: try {
291: sb.append(": ");
292: int length = Math.min(120, _region.getEndOffset()
293: - _region.getStartOffset());
294: sb.append(_region.getDocument().getText(
295: _region.getStartOffset(), length).trim());
296: } catch (BadLocationException bpe) { /* ignore, just don't display line */
297: }
298: if (_region.equals(_model.getBrowserHistoryManager()
299: .getCurrentRegion())) {
300: sb.append("</font>");
301: }
302: sb.append("</html>");
303: } finally {
304: _region.getDocument().releaseReadLock();
305: }
306: return sb.toString();
307: }
308:
309: public boolean equals(Object other) {
310: @SuppressWarnings("unchecked")
311: BrowserHistoryListUserObj o = (BrowserHistoryListUserObj) other;
312: return (o.region().getDocument().equals(region()
313: .getDocument()))
314: && (o.region().getStartOffset() == region()
315: .getStartOffset())
316: && (o.region().getEndOffset() == region()
317: .getEndOffset());
318: }
319: }
320: }
|