Hopefully on right path to choices, also switched to english

This commit is contained in:
Thastertyn 2023-04-25 21:57:35 +02:00
parent a633fa6490
commit 06183dd59c
11 changed files with 238 additions and 121 deletions

View File

@ -1,9 +1,5 @@
package xyz.thastertyn; package xyz.thastertyn;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.plaf.FontUIResource;
import xyz.thastertyn.UserInterface.MainWindow; import xyz.thastertyn.UserInterface.MainWindow;
/** /**

View File

@ -65,7 +65,7 @@ public class Login {
lastCheck = start; lastCheck = start;
} }
/*public void loginJidelna() throws UnknownHostException, IOException public void loginJidelna(String user, String pass) throws UnknownHostException, IOException
{ {
//#region JSESSIONID //#region JSESSIONID
Connection.Response jidelna = Jsoup.connect("https://objednavky.jidelnasokolska.cz/") Connection.Response jidelna = Jsoup.connect("https://objednavky.jidelnasokolska.cz/")
@ -94,13 +94,13 @@ public class Login {
//#endregion //#endregion
//#region Login //#region Login
Connection.Response jidelnaLogin = Jsoup.connect("https://objednavky.jidelnasokolska.cz/j_spring_security_check") Jsoup.connect("https://objednavky.jidelnasokolska.cz/j_spring_security_check")
.header("Connection", "keep-alive") .header("Connection", "keep-alive")
.header("Content-Type", "application/x-www-form-urlencoded") .header("Content-Type", "application/x-www-form-urlencoded")
.cookie("XSRF-TOKEN", XSRF_TOKEN) .cookie("XSRF-TOKEN", XSRF_TOKEN)
.cookie("JSESSIONID", jidelnaJSESSIONID) .cookie("JSESSIONID", jidelnaJSESSIONID)
.data("j_username", Credentials.user) .data("j_username", user)
.data("j_password", Credentials.pass) .data("j_password", pass)
.data("terminal", "false") .data("terminal", "false")
.data("type", "web") .data("type", "web")
.data("_csrf", XSRF_TOKEN) .data("_csrf", XSRF_TOKEN)
@ -108,5 +108,5 @@ public class Login {
.method(Method.POST) .method(Method.POST)
.execute(); .execute();
//#endregion //#endregion
}*/ }
} }

View File

@ -10,6 +10,7 @@ import org.jsoup.nodes.Element;
import org.jsoup.select.Elements; import org.jsoup.select.Elements;
import xyz.thastertyn.Tuples.Pair; import xyz.thastertyn.Tuples.Pair;
import xyz.thastertyn.Types.Choice;
import xyz.thastertyn.Types.Option; import xyz.thastertyn.Types.Option;
import xyz.thastertyn.Types.Options; import xyz.thastertyn.Types.Options;
import xyz.thastertyn.Types.Predmet; import xyz.thastertyn.Types.Predmet;
@ -26,11 +27,24 @@ public class Znamky {
private Options schoolYearOptions = new Options("Skolni R."); private Options schoolYearOptions = new Options("Skolni R.");
private Options schoolHalfYearOptions = new Options("Pololeti"); private Options schoolHalfYearOptions = new Options("Pololeti");
public void downloadZnamky(/*, int schoolYearId, int schoolYearHalfId*/) throws UnknownHostException, IOException public void downloadZnamky() throws UnknownHostException, IOException
{ {
//String url = String.format("https://www.spsejecna.cz/score/student?schoolYearId=%d&schoolYearHalfId=%d", schoolYearId, schoolYearHalfId); download("https://www.spsejecna.cz/score/student");
Document znamkyDokumentHTML = Downloader.download("https://www.spsejecna.cz/score/student").get(); }
public void downloadZnamky(Choice choice) throws UnknownHostException, IOException
{
download(String.format(
"https://www.spsejecna.cz/score/student?schoolYearId=%s&schoolYearHalfId=%s",
choice.getChoices().get(0),
choice.getChoices().get(1)));
}
private void download(String url) throws UnknownHostException, IOException
{
//String url =
Document znamkyDokumentHTML = Downloader.download(url).get();
// Predmety ulozene v <tr> // Predmety ulozene v <tr>
Elements[] radkyPredmetuHTML = znamkyDokumentHTML Elements[] radkyPredmetuHTML = znamkyDokumentHTML
.select("table.score") .select("table.score")

View File

@ -0,0 +1,18 @@
package xyz.thastertyn.Types;
import java.util.ArrayList;
import java.util.Arrays;
public class Choice {
private ArrayList<String> choices;
public Choice(String... choices)
{
this.choices.addAll(Arrays.asList(choices));
}
public ArrayList<String> getChoices()
{
return choices;
}
}

View File

@ -1,7 +1,9 @@
package xyz.thastertyn.UserInterface.Content; package xyz.thastertyn.UserInterface.Content;
import com.googlecode.lanterna.gui2.WindowBasedTextGUI; import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
import com.googlecode.lanterna.gui2.dialogs.MessageDialog;
import xyz.thastertyn.Types.Choice;
import com.googlecode.lanterna.gui2.Panel; import com.googlecode.lanterna.gui2.Panel;
import com.googlecode.lanterna.gui2.Label; import com.googlecode.lanterna.gui2.Label;
@ -11,8 +13,10 @@ import com.googlecode.lanterna.gui2.Label;
public abstract class JecnaContent { public abstract class JecnaContent {
public abstract Panel getPanel(); public abstract Panel getPanel();
public abstract void download(); public abstract void downloadDefault();
protected abstract void download(Choice choice);
public abstract boolean hasStarted(); public abstract boolean hasStarted();
public abstract Label getLabel(); public abstract Label getLabel();
public abstract void showOptions(final WindowBasedTextGUI textGUI); public abstract void showOptions(final WindowBasedTextGUI textGUI);
protected abstract void setGUI();
} }

View File

@ -9,6 +9,7 @@ import com.googlecode.lanterna.gui2.Panel;
import com.googlecode.lanterna.gui2.WindowBasedTextGUI; import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
import xyz.thastertyn.Tuples.Pair; import xyz.thastertyn.Tuples.Pair;
import xyz.thastertyn.Types.Choice;
public class OmluvnyList extends JecnaContent{ public class OmluvnyList extends JecnaContent{
@ -19,16 +20,18 @@ public class OmluvnyList extends JecnaContent{
private boolean hasStarted = false; private boolean hasStarted = false;
@Override @Override
public void download() { public void downloadDefault()
{
download(null);
}
@Override
protected void download(Choice choice)
{
try{ try{
omluvnyList.downloadOmluvnyList(); omluvnyList.downloadOmluvnyList();
setGUI();
ArrayList<Pair<String, String>> a = omluvnyList.getData();
for(Pair<String, String> p : a)
{
omluvnyPanel.addComponent(new Label(p.getValue0() + " - " + p.getValue1()));
}
hasStarted = true; hasStarted = true;
}catch(UnknownHostException e) }catch(UnknownHostException e)
{ {
@ -39,6 +42,17 @@ public class OmluvnyList extends JecnaContent{
} }
} }
@Override
protected void setGUI()
{
ArrayList<Pair<String, String>> a = omluvnyList.getData();
for(Pair<String, String> p : a)
{
omluvnyPanel.addComponent(new Label(p.getValue0() + " - " + p.getValue1()));
}
}
@Override @Override
public Panel getPanel() { public Panel getPanel() {
return omluvnyPanel; return omluvnyPanel;

View File

@ -6,9 +6,10 @@ import java.net.UnknownHostException;
import com.googlecode.lanterna.gui2.Label; import com.googlecode.lanterna.gui2.Label;
import com.googlecode.lanterna.gui2.Panel; import com.googlecode.lanterna.gui2.Panel;
import com.googlecode.lanterna.gui2.WindowBasedTextGUI; import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
import com.googlecode.lanterna.gui2.dialogs.MessageDialog;
import com.googlecode.lanterna.gui2.table.Table; import com.googlecode.lanterna.gui2.table.Table;
import xyz.thastertyn.Types.Choice;
public class Rozvrh extends JecnaContent { public class Rozvrh extends JecnaContent {
private Panel rozvrhPanel = new Panel(); private Panel rozvrhPanel = new Panel();
@ -25,26 +26,18 @@ public class Rozvrh extends JecnaContent {
Table<String> t = new Table<>(labels); Table<String> t = new Table<>(labels);
@Override @Override
public void download() public void downloadDefault()
{
download(null);
}
@Override
protected void download(Choice choice)
{ {
try{ try{
rozvrh.downloadRozvrh(); rozvrh.downloadRozvrh();
setGUI();
String[][] rozvrhArray = rozvrh.getRozvrh();
String[] den = new String[11];
for(int i = 0; i < rozvrhArray.length; i++)
{
den[0] = daysLabels[i];
for(int j = 1; j < rozvrhArray[i].length + 1; j++)
{
den[j] = (rozvrhArray[i][j - 1].isBlank()) ? " - " : rozvrhArray[i][j - 1];
}
t.getTableModel().addRow(den);
}
rozvrhPanel.addComponent(t);
hasStarted = true; hasStarted = true;
}catch(UnknownHostException e) }catch(UnknownHostException e)
{ {
@ -55,6 +48,26 @@ public class Rozvrh extends JecnaContent {
} }
} }
@Override
protected void setGUI()
{
String[][] rozvrhArray = rozvrh.getRozvrh();
String[] den = new String[11];
for(int i = 0; i < rozvrhArray.length; i++)
{
den[0] = daysLabels[i];
for(int j = 1; j < rozvrhArray[i].length + 1; j++)
{
den[j] = (rozvrhArray[i][j - 1].isBlank()) ? " - " : rozvrhArray[i][j - 1];
}
t.getTableModel().addRow(den);
}
rozvrhPanel.addComponent(t);
}
@Override @Override
public boolean hasStarted() public boolean hasStarted()
{ {

View File

@ -9,7 +9,8 @@ import com.googlecode.lanterna.gui2.GridLayout;
import com.googlecode.lanterna.gui2.Label; import com.googlecode.lanterna.gui2.Label;
import com.googlecode.lanterna.gui2.Panel; import com.googlecode.lanterna.gui2.Panel;
import com.googlecode.lanterna.gui2.WindowBasedTextGUI; import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
import com.googlecode.lanterna.gui2.dialogs.MessageDialog;
import xyz.thastertyn.Types.Choice;
public class Sdeleni extends JecnaContent { public class Sdeleni extends JecnaContent {
@ -20,33 +21,40 @@ public class Sdeleni extends JecnaContent {
private boolean hasStarted = false; private boolean hasStarted = false;
@Override
public void downloadDefault()
{
download(null);
}
@Override @Override
public void download() protected void download(Choice choice)
{ {
if(!hasStarted) try{
sdeleni.downloadSdeleni();
hasStarted = true;
}catch(UnknownHostException e)
{ {
try{
sdeleniPanel.setLayoutManager(new GridLayout(1)
.setLeftMarginSize(1)
.setRightMarginSize(1));
sdeleni.downloadSdeleni(); }catch(IOException e)
{
}
}
ArrayList<String> sdeleniList = sdeleni.getSdeleni(); @Override
protected void setGUI()
{
sdeleniPanel.setLayoutManager(new GridLayout(1)
.setLeftMarginSize(1)
.setRightMarginSize(1));
for(String s : sdeleniList) ArrayList<String> sdeleniList = sdeleni.getSdeleni();
{
sdeleniPanel.addComponent(new Label(s));
}
hasStarted = true;
}catch(UnknownHostException e)
{
}catch(IOException e) for(String s : sdeleniList)
{ {
sdeleniPanel.addComponent(new Label(s));
}
} }
} }

View File

@ -15,6 +15,7 @@ import com.googlecode.lanterna.gui2.LinearLayout;
import com.googlecode.lanterna.gui2.Panel; import com.googlecode.lanterna.gui2.Panel;
import com.googlecode.lanterna.gui2.WindowBasedTextGUI; import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
import xyz.thastertyn.Types.Choice;
import xyz.thastertyn.Types.Predmet; import xyz.thastertyn.Types.Predmet;
import xyz.thastertyn.Types.Znamka; import xyz.thastertyn.Types.Znamka;
import xyz.thastertyn.UserInterface.Dialogs.OptionsDialog; import xyz.thastertyn.UserInterface.Dialogs.OptionsDialog;
@ -49,66 +50,23 @@ public class Znamky extends JecnaContent {
private boolean hasStarted = false; private boolean hasStarted = false;
@Override @Override
public void download() public void downloadDefault()
{
download(null);
}
@Override
protected void download(Choice choice)
{ {
try{ try{
znamky.downloadZnamky(); if(choice != null)
ArrayList<Predmet> predmety = znamky.getPredmety();
HashMap<Integer, SimpleTheme> barvy = new HashMap<>();
barvy.put(1, new SimpleTheme(ANSI.BLACK, VYBORNY));
barvy.put(2, new SimpleTheme(ANSI.BLACK, CHVALITEBNY));
barvy.put(3, new SimpleTheme(ANSI.BLACK, DOBRY));
barvy.put(4, new SimpleTheme(ANSI.BLACK, DOSTATECNY));
barvy.put(5, new SimpleTheme(ANSI.BLACK, NEDOSTATECNY));
barvy.put(-1, new SimpleTheme(ANSI.WHITE, NEHODNOCEN));
// Sloupec pro jmena predmetu
Panel jmemaPredmetu = new Panel()
.setLayoutManager(new LinearLayout(Direction.VERTICAL))
.setLayoutData(ALIGN_LEFT)
.addTo(mainPanel);
// Sloupec pro znamky z predmetu
Panel znamky = new Panel()
.setLayoutManager(new LinearLayout(Direction.VERTICAL))
.setLayoutData(ALIGN_LEFT)
.addTo(mainPanel);
// Sloupec pro vyslednou znamku
Panel vysledneZnamky = new Panel()
.setLayoutManager(new LinearLayout(Direction.VERTICAL))
.setLayoutData(ALIGN_RIGHT)
.addTo(mainPanel);
for(Predmet predmet : predmety)
{ {
Panel jednotliveZnamky = new Panel() znamky.downloadZnamky(choice);
.setLayoutManager(new LinearLayout(Direction.HORIZONTAL)) }else{
.addTo(znamky); znamky.downloadZnamky();
if(predmet.getZnamky().isEmpty())
{
jednotliveZnamky.addComponent(new Label(""));
}
for(Znamka znamka : predmet.getZnamky())
{
Label znamkaLabel = new Label(znamka.getText());
znamkaLabel.setTheme(barvy.get(znamka.getZnamka()));
jednotliveZnamky.addComponent(znamkaLabel);
}
double prumer = predmet.getPrumer();
Label vysl = new Label(String.format("%.2f", prumer));
vysl.setTheme(barvy.get((int) Math.round(prumer)));
jmemaPredmetu.addComponent(new Label(predmet.getJmenoPredmetu()));
vysledneZnamky.addComponent(vysl);
} }
setGUI();
hasStarted = true; hasStarted = true;
}catch(IOException e) }catch(IOException e)
@ -117,6 +75,67 @@ public class Znamky extends JecnaContent {
} }
} }
@Override
protected void setGUI()
{
ArrayList<Predmet> predmety = znamky.getPredmety();
HashMap<Integer, SimpleTheme> barvy = new HashMap<>();
barvy.put(1, new SimpleTheme(ANSI.BLACK, VYBORNY));
barvy.put(2, new SimpleTheme(ANSI.BLACK, CHVALITEBNY));
barvy.put(3, new SimpleTheme(ANSI.BLACK, DOBRY));
barvy.put(4, new SimpleTheme(ANSI.BLACK, DOSTATECNY));
barvy.put(5, new SimpleTheme(ANSI.BLACK, NEDOSTATECNY));
barvy.put(-1, new SimpleTheme(ANSI.WHITE, NEHODNOCEN));
// Sloupec pro jmena predmetu
Panel jmemaPredmetu = new Panel()
.setLayoutManager(new LinearLayout(Direction.VERTICAL))
.setLayoutData(ALIGN_LEFT)
.addTo(mainPanel);
// Sloupec pro znamky z predmetu
Panel znamky = new Panel()
.setLayoutManager(new LinearLayout(Direction.VERTICAL))
.setLayoutData(ALIGN_LEFT)
.addTo(mainPanel);
// Sloupec pro vyslednou znamku
Panel vysledneZnamky = new Panel()
.setLayoutManager(new LinearLayout(Direction.VERTICAL))
.setLayoutData(ALIGN_RIGHT)
.addTo(mainPanel);
for(Predmet predmet : predmety)
{
Panel jednotliveZnamky = new Panel()
.setLayoutManager(new LinearLayout(Direction.HORIZONTAL))
.addTo(znamky);
if(predmet.getZnamky().isEmpty())
{
jednotliveZnamky.addComponent(new Label(""));
}
for(Znamka znamka : predmet.getZnamky())
{
Label znamkaLabel = new Label(znamka.getText());
znamkaLabel.setTheme(barvy.get(znamka.getZnamka()));
jednotliveZnamky.addComponent(znamkaLabel);
}
double prumer = predmet.getPrumer();
Label vysl = new Label(String.format("%.2f", prumer));
vysl.setTheme(barvy.get((int) Math.round(prumer)));
jmemaPredmetu.addComponent(new Label(predmet.getJmenoPredmetu()));
vysledneZnamky.addComponent(vysl);
}
}
@Override @Override
public Panel getPanel() public Panel getPanel()
{ {
@ -139,6 +158,11 @@ public class Znamky extends JecnaContent {
public void showOptions(WindowBasedTextGUI textGUI) { public void showOptions(WindowBasedTextGUI textGUI) {
OptionsDialog d = new OptionsDialog(znamky.getOptions().getValue0(), znamky.getOptions().getValue1()); OptionsDialog d = new OptionsDialog(znamky.getOptions().getValue0(), znamky.getOptions().getValue1());
d.showDialog(textGUI); Choice c = d.showDialog(textGUI);
if(c != null)
{
download(c);
}
} }
} }

View File

@ -1,6 +1,9 @@
package xyz.thastertyn.UserInterface.Dialogs; package xyz.thastertyn.UserInterface.Dialogs;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import com.googlecode.lanterna.gui2.Button; import com.googlecode.lanterna.gui2.Button;
import com.googlecode.lanterna.gui2.ComboBox; import com.googlecode.lanterna.gui2.ComboBox;
@ -12,14 +15,27 @@ import com.googlecode.lanterna.gui2.Window;
import com.googlecode.lanterna.gui2.WindowBasedTextGUI; import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
import com.googlecode.lanterna.gui2.dialogs.DialogWindow; import com.googlecode.lanterna.gui2.dialogs.DialogWindow;
import xyz.thastertyn.Types.Choice;
import xyz.thastertyn.Types.Option;
import xyz.thastertyn.Types.Options; import xyz.thastertyn.Types.Options;
public class OptionsDialog extends DialogWindow { public class OptionsDialog extends DialogWindow {
private List<ComboBox<Option>> boxs;
private boolean useData = false;
public OptionsDialog(Options... options) public OptionsDialog(Options... options)
{ {
super("Choose from below"); super("Choose from below");
boxs = new ArrayList<>();
for(Options o : options)
{
boxs.add(new ComboBox<Option>(o.getOptions()));
}
Panel mainPanel = new Panel() Panel mainPanel = new Panel()
.setLayoutManager(new GridLayout(1) .setLayoutManager(new GridLayout(1)
.setLeftMarginSize(1) .setLeftMarginSize(1)
@ -28,9 +44,11 @@ public class OptionsDialog extends DialogWindow {
Panel optionPanel = new Panel().setLayoutManager(new GridLayout(3)) Panel optionPanel = new Panel().setLayoutManager(new GridLayout(3))
.addTo(mainPanel); .addTo(mainPanel);
for(int i = 0; i < options.length; i++) for(int i = 0; i < options.length; i++)
{ {
optionPanel.addComponent(new ComboBox<>(options[i].getOptions())); optionPanel.addComponent(boxs.get(i));
if(i != options.length - 1) if(i != options.length - 1)
{ {
@ -60,6 +78,7 @@ public class OptionsDialog extends DialogWindow {
public void onOK() public void onOK()
{ {
close(); close();
useData = true;
} }
public void onCancel() public void onCancel()
@ -68,8 +87,15 @@ public class OptionsDialog extends DialogWindow {
} }
@Override @Override
public String showDialog(WindowBasedTextGUI textGUI) { public Choice showDialog(WindowBasedTextGUI textGUI) {
super.showDialog(textGUI); super.showDialog(textGUI);
return "";
return (useData) ?
new Choice( // User pressed ok
boxs.stream()
.map(b -> b.getSelectedItem().toString())
.toArray(String[]::new))
:
null; // User pressed Cancel
} }
} }

View File

@ -69,7 +69,7 @@ public class WindowSwitchListener implements WindowListener {
if(!contents[current].hasStarted()) if(!contents[current].hasStarted())
{ {
contents[current].download(); contents[current].downloadDefault();
} }
holderPanel.removeAllComponents(); holderPanel.removeAllComponents();