-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrowserHistory.java
More file actions
145 lines (125 loc) · 3.81 KB
/
Copy pathBrowserHistory.java
File metadata and controls
145 lines (125 loc) · 3.81 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
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.HBox;
import javafx.scene.web.WebHistory;
import javafx.scene.web.WebHistory.Entry;
import javafx.scene.web.WebView;
import javafx.util.Callback;
public class BrowserHistory extends HBox
{
public BrowserHistory(WebView webView)
{
// Set Spacing
this.setSpacing(4);
// Set the Style-properties of the Navigation Bar
this.setStyle("-fx-padding: 10;" +
"-fx-border-style: solid inside;" +
"-fx-border-width: 2;" +
"-fx-border-insets: 5;" +
"-fx-border-radius: 5;" +
"-fx-border-color: blue;");
// Create the WebHistory
WebHistory history = webView.getEngine().getHistory();
// Create the Label
Label label = new Label("History:");
// Create the Buttons
Button backButton = new Button("Back");
backButton.setDisable(true);
Button forwardButton = new Button("Forward");
forwardButton.setDisable(true);
// Add an ActionListener to the Back and Forward Buttons
backButton.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
history.go(-1);
}
});
forwardButton.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
history.go(1);
}
});
// Add an ChangeListener to the currentIndex property
history.currentIndexProperty().addListener(new ChangeListener<Number>()
{
public void changed(ObservableValue<? extends Number> ov,
final Number oldvalue, final Number newvalue)
{
int currentIndex = newvalue.intValue();
if (currentIndex <= 0)
{
backButton.setDisable(true);
}
else
{
backButton.setDisable(false);
}
if (currentIndex >= history.getEntries().size())
{
forwardButton.setDisable(true);
}
else
{
forwardButton.setDisable(false);
}
}
});
// Create the ComboBox for the History List
ComboBox<Entry> historyList = new ComboBox<>();
historyList.setPrefWidth(150);
historyList.setItems(history.getEntries());
// Set a cell factory to to show only the page title in the history list
historyList.setCellFactory(new Callback<ListView<WebHistory.Entry>, ListCell<WebHistory.Entry>>()
{
@Override public ListCell<WebHistory.Entry> call(ListView<WebHistory.Entry> list)
{
ListCell<Entry> cell = new ListCell<Entry>()
{
@Override
public void updateItem(Entry item, boolean empty)
{
super.updateItem(item, empty);
if (empty)
{
this.setText(null);
this.setGraphic(null);
}
else
{
String pageTitle = item.getTitle();
this.setText(pageTitle);
}
}
};
return cell;
}
});
// Let the user navigate to a page using the history list
historyList.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
int currentIndex = history.getCurrentIndex();
Entry selectedEntry = historyList.getValue();
int selectedIndex = historyList.getItems().indexOf(selectedEntry);
int offset = selectedIndex - currentIndex;
history.go(offset);
}
});
// Add the Children to the BrowserHistory
this.getChildren().addAll(backButton, forwardButton, label,historyList);
}
}