KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > help > HistoryButton


1 /*
2  * HistoryButton.java - History Button
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2005 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.help;
24
25 //{{{ Imports
26
import org.gjt.sp.jedit.*;
27 import org.gjt.sp.jedit.gui.RolloverButton;
28 import java.awt.*;
29 import java.awt.event.*;
30 import javax.swing.*;
31 import javax.swing.event.*;
32 //}}}
33

34 /**
35  * History Button
36  * @author Nicholas O'Leary
37  * @version $Id: HistoryButton.java 5295 2005-11-05 06:42:54Z ezust $
38  */

39 public class HistoryButton extends JPanel implements ActionListener
40 {
41     public static final int BACK = 0;
42     public static final int FORWARD = 1;
43
44     //{{{ Private Members
45
private int type;
46     private HelpHistoryModel history;
47     private RolloverButton arrow_button;
48     private RolloverButton drop_button;
49     private JPopupMenu historyList;
50     private ActionListener arrowActionListener;
51     //}}}
52

53     //{{{ HistoryButton
54
public HistoryButton(int type, HelpHistoryModel model)
55     {
56         super();
57         arrow_button = new RolloverButton(GUIUtilities.loadIcon(
58             jEdit.getProperty(
59                 (type==BACK?
60                     "helpviewer.back.icon"
61                     :
62                     "helpviewer.forward.icon"
63                     ))));
64         arrow_button.setToolTipText(
65             jEdit.getProperty((type==BACK?
66                 "helpviewer.back.label"
67                 :
68                 "helpviewer.forward.label"
69                 )));
70         Box box = new Box(BoxLayout.X_AXIS);
71         drop_button = new RolloverButton(GUIUtilities.loadIcon("ToolbarMenu.gif"));
72         drop_button.addActionListener(new DropActionHandler());
73         box.add(arrow_button);
74         box.add(drop_button);
75         this.setMaximumSize(new Dimension(
76             drop_button.getPreferredSize().width +
77             arrow_button.getPreferredSize().width +
78             5,
79             arrow_button.getPreferredSize().height+10)
80             );
81         this.add(box);
82         this.type = type;
83         this.history = model;
84     } //}}}
85

86     //{{{ setEnabled
87
public void setEnabled(boolean state)
88     {
89         super.setEnabled(state);
90         drop_button.setEnabled(state);
91         arrow_button.setEnabled(state);
92     } //}}}
93

94     //{{{ addActionListener
95
public void addActionListener(ActionListener al)
96     {
97         arrow_button.addActionListener(this);
98         arrowActionListener = al;
99     } //}}}
100

101     //{{{ actionPerformed
102
public void actionPerformed(ActionEvent evt)
103     {
104         arrowActionListener.actionPerformed(
105             new ActionEvent(this,
106                 ActionEvent.ACTION_PERFORMED,
107                 evt.getActionCommand(),
108                 evt.getWhen(),
109                 evt.getModifiers()
110                 )
111             );
112     } //}}}
113

114     //{{{ getParentHistoryButton
115
private HistoryButton getParentHistoryButton()
116     {
117         return this;
118     } //}}}
119

120     //{{{ Inner Classes
121

122     //{{{ DropActionHandler
123
class DropActionHandler implements ActionListener
124     {
125         //{{{ actionPerformed
126
public void actionPerformed(ActionEvent evt)
127         {
128             historyList = new JPopupMenu();
129             HelpHistoryModel.HistoryEntry[] urls;
130             if (type == BACK)
131                 urls = history.getPreviousURLs();
132             else
133                 urls = history.getNextURLs();
134             if (urls != null)
135             {
136                 if (type==BACK) {
137                     for (int i=urls.length-1;i>=0;i--)
138                         if (urls[i]!=null)
139                             historyList.add(new HistoryListActionHandler(urls[i]));
140                 }
141                 else
142                 {
143                     for (int i=0;i<urls.length;i++)
144                         if (urls[i]!=null)
145                             historyList.add(new HistoryListActionHandler(urls[i]));
146                 }
147
148                     
149                 historyList.show((JComponent)evt.getSource(),0,0);
150             }
151         } //}}}
152
} //}}}
153

154     //{{{ HistoryListActionHandler
155
class HistoryListActionHandler extends AbstractAction
156     {
157         HelpHistoryModel.HistoryEntry entry;
158         
159         //{{{ HistoryListActionHandler
160
HistoryListActionHandler(HelpHistoryModel.HistoryEntry entry)
161         {
162             super(entry.title);
163             this.entry = entry;
164             this.putValue(Action.ACTION_COMMAND_KEY,entry.url);
165         } //}}}
166

167         //{{{ actionPerformed
168
public void actionPerformed(ActionEvent ae)
169         {
170             history.setCurrentEntry(entry);
171             getParentHistoryButton().actionPerformed(ae);
172         } //}}}
173
} //}}}
174

175     //}}}
176
}
177
178
Popular Tags