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.RegionManagerListener;
054: import edu.rice.cs.drjava.model.DocumentRegion;
055: import edu.rice.cs.drjava.model.OpenDefinitionsDocument;
056: import edu.rice.cs.drjava.config.*;
057: import edu.rice.cs.util.swing.Utilities;
058: import edu.rice.cs.util.UnexpectedException;
059:
060: /**
061: * Panel for displaying bookmarks.
062: * This class is a swing view class and hence should only be accessed from the event-handling thread.
063: * @version $Id$
064: */
065: public class BookmarksPanel extends RegionsTreePanel<DocumentRegion> {
066: protected JButton _goToButton;
067: protected JButton _removeButton;
068: protected JButton _removeAllButton;
069:
070: /** Constructs a new bookmarks panel.
071: * This is swing view class and hence should only be accessed from the event-handling thread.
072: * @param frame the MainFrame
073: */
074: public BookmarksPanel(MainFrame frame) {
075: super (frame, "Bookmarks");
076: _model.getBookmarkManager().addListener(
077: new RegionManagerListener<DocumentRegion>() {
078: public void regionAdded(DocumentRegion r, int index) {
079: addRegion(r);
080: }
081:
082: public void regionChanged(DocumentRegion r,
083: int index) {
084: regionRemoved(r);
085: regionAdded(r, index);
086: }
087:
088: public void regionRemoved(DocumentRegion r) {
089: removeRegion(r);
090: }
091: });
092: }
093:
094: /** Action performed when the Enter key is pressed. Should be overridden. */
095: protected void performDefaultAction() {
096: goToRegion();
097: }
098:
099: /** Creates the buttons for controlling the regions. Should be overridden. */
100: protected JComponent[] makeButtons() {
101: Action goToAction = new AbstractAction("Go to") {
102: public void actionPerformed(ActionEvent ae) {
103: goToRegion();
104: }
105: };
106: _goToButton = new JButton(goToAction);
107:
108: Action removeAction = new AbstractAction("Remove") {
109: public void actionPerformed(ActionEvent ae) {
110: for (DocumentRegion r : getSelectedRegions()) {
111: _model.getBookmarkManager().removeRegion(r);
112: }
113: }
114: };
115: _removeButton = new JButton(removeAction);
116:
117: Action removeAllAction = new AbstractAction("Remove All") {
118: public void actionPerformed(ActionEvent ae) {
119: _model.getBookmarkManager().clearRegions();
120: }
121: };
122: _removeAllButton = new JButton(removeAllAction);
123:
124: JComponent[] buts = new JComponent[] { _goToButton,
125: _removeButton, _removeAllButton };
126:
127: return buts;
128: }
129:
130: /** Update button state and text. */
131: protected void updateButtons() {
132: ArrayList<DocumentRegion> regs = getSelectedRegions();
133: _goToButton.setEnabled(regs.size() == 1);
134: _removeButton.setEnabled(regs.size() > 0);
135: _removeAllButton.setEnabled(_regionRootNode != null
136: && _regionRootNode.getDepth() > 0);
137: }
138:
139: /** Makes the popup menu actions. Should be overridden if additional actions besides "Go to" and "Remove" are added. */
140: protected AbstractAction[] makePopupMenuActions() {
141: AbstractAction[] acts = new AbstractAction[] {
142: new AbstractAction("Go to") {
143: public void actionPerformed(ActionEvent e) {
144: goToRegion();
145: }
146: },
147:
148: new AbstractAction("Remove") {
149: public void actionPerformed(ActionEvent e) {
150: for (DocumentRegion r : getSelectedRegions())
151: _model.getBookmarkManager().removeRegion(r);
152: }
153: } };
154: return acts;
155: }
156: }
|