Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
tp-4
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pirkl-poo-2018
tp-4
Commits
3e5c01dd
Verified
Commit
3e5c01dd
authored
6 years ago
by
Théo Pirkl
Browse files
Options
Downloads
Patches
Plain Diff
Implements events and controls reactivity.
parent
8cd9b31a
No related branches found
No related tags found
3 merge requests
!21
*poof* final version
,
!16
Gui transport
,
!11
Gui transport
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/ch/hepia/ui/MainWindowController.java
+71
-17
71 additions, 17 deletions
src/main/java/ch/hepia/ui/MainWindowController.java
src/main/resources/fxml/MainWindow.fxml
+2
-0
2 additions, 0 deletions
src/main/resources/fxml/MainWindow.fxml
with
73 additions
and
17 deletions
src/main/java/ch/hepia/ui/MainWindowController.java
+
71
−
17
View file @
3e5c01dd
package
ch.hepia.ui
;
import
ch.hepia.api.transport.Connection
;
import
ch.hepia.api.transport.LinkAPI
;
import
ch.hepia.api.transport.Section
;
import
javafx.fxml.FXML
;
import
javafx.fxml.Initializable
;
import
javafx.scene.canvas.Canvas
;
import
javafx.scene.control.Alert
;
import
javafx.scene.control.ComboBox
;
import
javafx.scene.control.Label
;
import
javafx.scene.shape.Line
;
import
javafx.stage.Stage
;
import
org.json.JSONArray
;
import
java.io.IOException
;
import
java.lang.reflect.Array
;
import
java.net.URL
;
import
java.text.ParseException
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.ResourceBundle
;
public
class
MainWindowController
implements
Initializable
{
...
...
@@ -25,35 +35,79 @@ public class MainWindowController implements Initializable {
@FXML
private
ComboBox
<
String
>
destinationComboBox
;
@Override
public
void
initialize
(
URL
url
,
ResourceBundle
resourceBundle
)
{
currentJourneyLabel
.
setText
(
"Vous n'avez prévu aucun voyage pour le moment."
);
startStopLabel
.
setText
(
""
);
// No text should be visible when no journey has been selected.
LinkAPI
transportApi
=
new
LinkAPI
();
@FXML
private
Canvas
connectionCanvas
;
originComboBox
.
valueProperty
().
addListener
(
(
observable
,
oldValue
,
newValue
)
->
{
if
(!
newValue
.
isEmpty
()
&&
newValue
.
length
()
>
3
){
originComboBox
.
getItems
().
clear
();
/**
* Shows a sad message when the API crashes.
*/
private
void
showSadMessage
(){
AlertUtils
.
dialog
(
Alert
.
AlertType
.
ERROR
,
"Erreur"
,
"Une erreur est survenue."
,
"Impossible de contacter les services de transport Suisses."
);
}
/**
* Searches stops that matches the query.
* @param newValue The stop name query.
* @param transportApi The API to connect to
* @param target The combo box where to put the results.
*/
private
void
searchStops
(
String
newValue
,
LinkAPI
transportApi
,
ComboBox
<
String
>
target
){
if
(!
newValue
.
isEmpty
()
&&
newValue
.
length
()
>
3
&&
!
target
.
getItems
().
contains
(
newValue
)){
try
{
JSONArray
a
=
transportApi
.
getStations
(
newValue
);
ArrayList
<
String
>
results
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
a
.
length
();
i
++){
originComboBox
.
getItems
().
add
(
a
.
getJSONObject
(
i
).
getString
(
"name"
));
results
.
add
(
a
.
getJSONObject
(
i
).
getString
(
"name"
));
}
target
.
getItems
().
clear
();
target
.
getItems
().
addAll
(
results
);
target
.
show
();
}
catch
(
IOException
e
)
{
showSadMessage
();
}
}
catch
(
Exception
e
)
{
AlertUtils
.
dialog
(
Alert
.
AlertType
.
ERROR
,
"Erreur"
,
"Une erreur est survenue."
,
"Impossible de contacter les services de transport Suisses."
);
}
}
});
private
void
drawConnection
(
Connection
connection
,
int
x
,
int
y
){
List
<
Section
>
sections
=
connection
.
getSections
();
for
(
int
i
=
0
;
i
<
sections
.
size
();
i
++){
connectionCanvas
.
getGraphicsContext2D
().
strokeLine
(
x
+
(
800
/
sections
.
size
())
*
i
,
y
,
800
/
sections
.
size
(),
y
);
}
}
@Override
public
void
initialize
(
URL
url
,
ResourceBundle
resourceBundle
)
{
currentJourneyLabel
.
setText
(
"Vous n'avez prévu aucun voyage pour le moment."
);
startStopLabel
.
setText
(
""
);
// No text should be visible when no journey has been selected.
LinkAPI
transportApi
=
new
LinkAPI
();
originComboBox
.
valueProperty
().
addListener
(
(
observable
,
oldValue
,
newValue
)
->
searchStops
(
newValue
,
transportApi
,
originComboBox
)
);
destinationComboBox
.
valueProperty
().
addListener
(
(
observable
,
oldValue
,
newValue
)
->
{
if
(!
newValue
.
isEmpty
()
&&
!
originComboBox
.
getValue
().
isEmpty
()){
searchStops
(
newValue
,
transportApi
,
destinationComboBox
);
if
(
destinationComboBox
.
getItems
().
contains
(
newValue
)
&&
originComboBox
.
getItems
().
contains
(
originComboBox
.
getValue
())){
// Both are autofilled, plotting route
try
{
JSONArray
connections
=
transportApi
.
getConnections
(
originComboBox
.
getValue
(),
destinationComboBox
.
getValue
());
startStopLabel
.
setText
(
originComboBox
.
getValue
()
+
" - "
+
destinationComboBox
.
getValue
());
for
(
int
i
=
0
;
i
<
connections
.
length
();
i
++){
// Now iterating over connections
Connection
c
=
new
Connection
.
ConnectionBuilder
(
connections
.
getJSONObject
(
i
)).
build
();
drawConnection
(
c
,
30
,
100
+
30
*
i
);
}
}
catch
(
IOException
|
ParseException
e
)
{
showSadMessage
();
}
}
}
);
}
...
...
This diff is collapsed.
Click to expand it.
src/main/resources/fxml/MainWindow.fxml
+
2
−
0
View file @
3e5c01dd
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.canvas.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.shape.*?>
...
...
@@ -50,6 +51,7 @@
</font>
</Label>
<Line
endX=
"100.0"
layoutX=
"115.0"
layoutY=
"49.0"
startX=
"-100.0"
stroke=
"#4c7ba8"
/>
<Canvas
id=
"connectionCanvas"
fx:id=
"connectionCanvas"
height=
"451.0"
layoutX=
"15.0"
layoutY=
"59.0"
width=
"652.0"
/>
</children>
</Pane>
</center>
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment