This repository was archived by the owner on Jan 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathBackAction.java
More file actions
188 lines (160 loc) · 5.84 KB
/
BackAction.java
File metadata and controls
188 lines (160 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* @(#)BackAction.java 1.4 06/10/30
*
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
package javax.help;
import java.awt.*;
import java.awt.event.*;
import javax.help.*;
import javax.help.event.*;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Stack;
import javax.swing.*;
/**
*
* @author Stepan Marek
* @version 1.4 10/30/06
*/
public class BackAction extends AbstractHelpAction implements MouseListener, HelpHistoryModelListener {
private static final String NAME = "BackAction";
private static final int DELAY = 500;
private Timer timer;
private HelpHistoryModel historyModel;
/** Creates new BackAction */
public BackAction(Object control) {
super(control, NAME);
if (control instanceof JHelp) {
JHelp help = (JHelp)control;
historyModel = help.getHistoryModel();
historyModel.addHelpHistoryModelListener(this);
setEnabled(historyModel.getIndex() > 0);
putValue("icon", UIManager.getIcon(NAME + ".icon"));
Locale locale = null;
try {
locale = help.getModel().getHelpSet().getLocale();
} catch (NullPointerException npe) {
locale = Locale.getDefault();
}
putValue("tooltip", HelpUtilities.getString(locale, "tooltip." + NAME));
putValue("access", HelpUtilities.getString(locale, "access." + NAME));
}
}
/**
* Invoked when the mouse exits a component.
*/
public void mouseExited(MouseEvent e) {
}
/**
* Invoked when a mouse button has been released on a component.
*/
public void mouseReleased(MouseEvent e) {
if (timer != null) {
timer.stop();
}
}
/**
* Invoked when a mouse button has been pressed on a component.
*/
public void mousePressed(MouseEvent e) {
timer = new Timer(DELAY, new TimeListener(e));
timer.start();
}
/**
* Invoked when the mouse has been clicked on a component.
*/
public void mouseClicked(MouseEvent e) {
if ((historyModel != null) && isEnabled()) {
historyModel.goBack();
}
}
/**
* Invoked when the mouse enters a component.
*/
public void mouseEntered(MouseEvent e) {
}
private class TimeListener implements ActionListener {
private MouseEvent e;
public TimeListener(MouseEvent e) {
this.e = e;
}
public void actionPerformed(ActionEvent evt) {
timer.stop();
if (BackAction.this.isEnabled()) {
BackAction.this.showBackwardHistory(e);
}
}
}
private class HistoryActionListener implements ActionListener {
private int index;
public HistoryActionListener(int index) {
this.index = index;
}
public void actionPerformed(java.awt.event.ActionEvent event) {
if(historyModel != null) {
historyModel.setHistoryEntry(index);
}
}
}
private void showBackwardHistory(MouseEvent e) {
JPopupMenu backwardMenu = new JPopupMenu("Backward History");
if (historyModel == null) {
return;
}
Stack menuStack = new Stack();
Locale locale = ((JHelp)getControl()).getModel().getHelpSet().getLocale();
Enumeration items = historyModel.getBackwardHistory().elements();
JMenuItem mi = null;
//while(items.hasMoreElements()) {
for (int i = 0; items.hasMoreElements(); i++) {
HelpModelEvent item = (HelpModelEvent)items.nextElement();
if (item != null) {
String title = item.getHistoryName();
if (title == null) {
title = HelpUtilities.getString(locale, "history.unknownTitle");
}
mi = new JMenuItem(title);
//mi.setToolTipText(item.getURL().getPath());
mi.addActionListener(new HistoryActionListener(i));
menuStack.push(mi);
}
}
int size = menuStack.size();
for(int j = 0; j < size; j++) {
backwardMenu.add((JMenuItem)menuStack.pop());
}
// if(e.isPopupTrigger())
backwardMenu.show(e.getComponent(),e.getX(),e.getY());
}
/**
* Tells the listener that the history has changed.
* Will enable/disable the Action depending on the events previous flag
*
* @param e The HelpHistoryModelEvent
*/
public void historyChanged(HelpHistoryModelEvent e) {
setEnabled(e.isPrevious());
}
}