diff --git a/src/main/java/xyz/thastertyn/Login/LoginController.java b/src/main/java/xyz/thastertyn/Login/LoginController.java index c1bdc87..1613061 100644 --- a/src/main/java/xyz/thastertyn/Login/LoginController.java +++ b/src/main/java/xyz/thastertyn/Login/LoginController.java @@ -55,19 +55,19 @@ public class LoginController { MessageDialogButton.Abort); } catch (UnknownHostException e) { MessageDialog.showMessageDialog(textGUI, "No Internet connection", - "There seems to be a problem with your internet connection", + e.getMessage(), MessageDialogButton.OK); login(); } catch (CredentialException e) { MessageDialog.showMessageDialog(textGUI, "Incorrect username or password", - "The username or password you entered is incorrect", + e.getMessage(), MessageDialogButton.OK); login(); } catch (IOException e) { MessageDialog.showMessageDialog(textGUI, "There was an error", - "Maybe try again and it will go away", + e.getMessage(), MessageDialogButton.Retry); login(); } diff --git a/src/main/java/xyz/thastertyn/Scrape/Jidelna.java b/src/main/java/xyz/thastertyn/Scrape/Jidelna.java index d939b45..2a754e8 100644 --- a/src/main/java/xyz/thastertyn/Scrape/Jidelna.java +++ b/src/main/java/xyz/thastertyn/Scrape/Jidelna.java @@ -9,19 +9,16 @@ public class Jidelna extends JecnaScrape { @Override public Options[] getOptions() { - // TODO Auto-generated method stub throw new UnsupportedOperationException("Unimplemented method 'getOptions'"); } @Override public void download(Choice choice) throws IOException { - // TODO Auto-generated method stub throw new UnsupportedOperationException("Unimplemented method 'download'"); } @Override public void download() throws IOException { - // TODO Auto-generated method stub throw new UnsupportedOperationException("Unimplemented method 'download'"); } diff --git a/src/main/java/xyz/thastertyn/Scrape/OmluvnyList.java b/src/main/java/xyz/thastertyn/Scrape/OmluvnyList.java index 884f5b7..3d21313 100644 --- a/src/main/java/xyz/thastertyn/Scrape/OmluvnyList.java +++ b/src/main/java/xyz/thastertyn/Scrape/OmluvnyList.java @@ -9,18 +9,35 @@ import org.jsoup.select.Elements; import xyz.thastertyn.Tuples.Pair; import xyz.thastertyn.Types.Choice; +import xyz.thastertyn.Types.Option; import xyz.thastertyn.Types.Options; public class OmluvnyList extends JecnaScrape { - private ArrayList> data = new ArrayList<>(); + private ArrayList> data; + + private Options schoolYearOptions; @Override public void download() throws IOException { - Document omluvnyListDokumentHTML = Downloader.download("https://www.spsejecna.cz/absence/student").get(); + download("https://www.spsejecna.cz/absence/student"); + } - Elements omluvneListy = omluvnyListDokumentHTML.select("table.absence-list").select("tr"); + @Override + public void download(Choice choice) throws IOException { + download(String.format( + "https://www.spsejecna.cz/absence/student?schoolYearId=%s", + choice.getChoices().get(0))); + } + + private void download(String url) throws IOException + { + data = new ArrayList<>(); + schoolYearOptions = new Options("Skolni R."); + Document omluvnyListDokumentHTML = Downloader.download(url).get(); + + Elements omluvneListy = omluvnyListDokumentHTML.select("table.absence-list").select("tbody").select("tr"); for(Element e : omluvneListy) { @@ -29,6 +46,18 @@ public class OmluvnyList extends JecnaScrape { data.add(new Pair(date, text)); } + + Elements options = omluvnyListDokumentHTML.select("form.listConfigure").select("select[id=schoolYearId]").select("option"); + + for(Element e : options) + { + schoolYearOptions.addOption(new Option(e.text(), e.attr("value"))); + } + } + + @Override + public Options[] getOptions() { + return new Options[] {schoolYearOptions}; } public ArrayList> getData() @@ -42,15 +71,4 @@ public class OmluvnyList extends JecnaScrape { return (!data.isEmpty()) ? data.toString() : null; } - @Override - public Options[] getOptions() { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'getOptions'"); - } - - @Override - public void download(Choice choice) throws IOException { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'download'"); - } } diff --git a/src/main/java/xyz/thastertyn/Scrape/Rozvrh.java b/src/main/java/xyz/thastertyn/Scrape/Rozvrh.java index 1054233..b9744dc 100644 --- a/src/main/java/xyz/thastertyn/Scrape/Rozvrh.java +++ b/src/main/java/xyz/thastertyn/Scrape/Rozvrh.java @@ -4,12 +4,14 @@ import java.io.IOException; import java.net.UnknownHostException; import java.util.Arrays; import java.util.HashSet; +import java.util.regex.Pattern; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import xyz.thastertyn.Types.Choice; +import xyz.thastertyn.Types.Option; import xyz.thastertyn.Types.Options; import xyz.thastertyn.Types.Timetable; @@ -19,6 +21,10 @@ import xyz.thastertyn.Types.Timetable; public class Rozvrh extends JecnaScrape { private Timetable timetable; + + private Options schoolYearOptions; + private Options timetableOptions; + /** * Stahne rozvrh z www.spsejecna.cz a dale ho zpracuje do formy * se kterou da pracovat @@ -29,8 +35,24 @@ public class Rozvrh extends JecnaScrape { @Override public void download() throws IOException { + download("https://www.spsejecna.cz/timetable/class"); + } + + @Override + public void download(Choice choice) throws IOException { + download(String + .format("https://www.spsejecna.cz/timetable/class?schoolYearId=%s&timetableId=%s", + choice.getChoices().get(0), + choice.getChoices().get(1))); + } + + private void download(String url) throws IOException + { + schoolYearOptions = new Options("Skolni R."); + timetableOptions = new Options("Obdobi"); + timetable = new Timetable(); - Document rozvrhDokumentHTML = Downloader.download("https://www.spsejecna.cz/timetable/class").get(); + Document rozvrhDokumentHTML = Downloader.download(url).get(); Elements[] radkyRozvrhuHTML = rozvrhDokumentHTML .select("table.timetable") @@ -53,6 +75,28 @@ public class Rozvrh extends JecnaScrape { timetable.get(i).set(j, pr); } } + + Element optionsPanel = rozvrhDokumentHTML.selectFirst("form.listConfigure"); + + Elements schoolYear = optionsPanel.select("select[id=schoolYearId]").select("option"); + Elements timetableId = optionsPanel.select("select[id=timetableId]").select("option"); + + for(Element e : schoolYear) + { + schoolYearOptions.addOption(new Option(e.text(), e.attr("value"))); + } + + for(Element e : timetableId) + { + String text = Pattern + .compile("(Od .* do .*)") + .matcher(e.text()) + .results() + .findFirst() + .map(m -> m.group(1)) + .orElse(e.text()); + timetableOptions.addOption(new Option(text, e.attr("value"))); + } } public Timetable getRozvrh() @@ -71,13 +115,6 @@ public class Rozvrh extends JecnaScrape { @Override public Options[] getOptions() { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'getOptions'"); - } - - @Override - public void download(Choice choice) throws IOException { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'download'"); + return new Options[] {schoolYearOptions, timetableOptions}; } } diff --git a/src/main/java/xyz/thastertyn/Scrape/Sdeleni.java b/src/main/java/xyz/thastertyn/Scrape/Sdeleni.java index 640f43b..03b4784 100644 --- a/src/main/java/xyz/thastertyn/Scrape/Sdeleni.java +++ b/src/main/java/xyz/thastertyn/Scrape/Sdeleni.java @@ -49,13 +49,11 @@ public class Sdeleni extends JecnaScrape { @Override public Options[] getOptions() { - // TODO Auto-generated method stub throw new UnsupportedOperationException("Unimplemented method 'getOptions'"); } @Override public void download(Choice choice) throws IOException { - // TODO Auto-generated method stub throw new UnsupportedOperationException("Unimplemented method 'download'"); } } diff --git a/src/main/java/xyz/thastertyn/Scrape/Znamky.java b/src/main/java/xyz/thastertyn/Scrape/Znamky.java index de45d14..63eff0f 100644 --- a/src/main/java/xyz/thastertyn/Scrape/Znamky.java +++ b/src/main/java/xyz/thastertyn/Scrape/Znamky.java @@ -22,8 +22,8 @@ public class Znamky extends JecnaScrape { private ArrayList predmety; // int znaci id roku, boolean jestli je jen prvni nebo i druhe pololeti - private Options schoolYearOptions = new Options("Skolni R."); - private Options schoolHalfYearOptions = new Options("Pololeti"); + private Options schoolYearOptions; + private Options schoolHalfYearOptions; public void download() throws IOException { @@ -41,6 +41,9 @@ public class Znamky extends JecnaScrape { private void download(String url) throws IOException { + schoolHalfYearOptions = new Options("Pololeti"); + schoolYearOptions = new Options("Skolni R."); + predmety = new ArrayList<>(); Document znamkyDokumentHTML = Downloader.download(url).get(); @@ -100,6 +103,7 @@ public class Znamky extends JecnaScrape { Elements skolniRoky = optionsPanel.select("select[id=schoolYearId]").select("option"); Elements pololeti = optionsPanel.select("select[id=schoolYearHalfId]").select("option"); + for(Element e : skolniRoky) { schoolYearOptions.addOption(new Option(e.text(), e.attr("value"))); diff --git a/src/main/java/xyz/thastertyn/UserInterface/Content/JecnaContent.java b/src/main/java/xyz/thastertyn/UserInterface/Content/JecnaContent.java index 906fee5..e760330 100644 --- a/src/main/java/xyz/thastertyn/UserInterface/Content/JecnaContent.java +++ b/src/main/java/xyz/thastertyn/UserInterface/Content/JecnaContent.java @@ -40,6 +40,7 @@ public abstract class JecnaContent { }else{ scraper.download(); setGUI(); + listener.updatePanel(); } hasStarted = true; diff --git a/src/main/java/xyz/thastertyn/UserInterface/Content/OmluvnyList.java b/src/main/java/xyz/thastertyn/UserInterface/Content/OmluvnyList.java index 303a908..e981f3c 100644 --- a/src/main/java/xyz/thastertyn/UserInterface/Content/OmluvnyList.java +++ b/src/main/java/xyz/thastertyn/UserInterface/Content/OmluvnyList.java @@ -1,81 +1,42 @@ package xyz.thastertyn.UserInterface.Content; -import java.io.IOException; import java.util.ArrayList; +import com.googlecode.lanterna.gui2.Direction; +import com.googlecode.lanterna.gui2.GridLayout; import com.googlecode.lanterna.gui2.Label; +import com.googlecode.lanterna.gui2.LinearLayout; import com.googlecode.lanterna.gui2.Panel; -import com.googlecode.lanterna.gui2.WindowBasedTextGUI; import xyz.thastertyn.Tuples.Pair; -import xyz.thastertyn.Types.Choice; import xyz.thastertyn.UserInterface.UpdateListener; public class OmluvnyList extends JecnaContent{ - private Panel omluvnyPanel = new Panel(); - private Label borderLabel = new Label("Omluvny L."); private xyz.thastertyn.Scrape.OmluvnyList omluvnyList = new xyz.thastertyn.Scrape.OmluvnyList(); - private boolean hasStarted = false; - public OmluvnyList(UpdateListener listener) { super(listener); - } - - @Override - public void downloadDefault() throws IOException - { - download(null); - } - - @Override - protected void download(Choice choice) throws IOException - { - omluvnyList = new xyz.thastertyn.Scrape.OmluvnyList(); - if(choice != null) - { - omluvnyList.download(choice); - setGUI(); - listener.updatePanel(); - }else{ - omluvnyList.download(); - setGUI(); - } - - super.hasStarted = true; + this.mainPanel = new Panel() + .setLayoutManager(new GridLayout(1)); + this.borderLabel = new Label("Omluvny L."); + super.scraper = omluvnyList; } @Override protected void setGUI() { + this.mainPanel.removeAllComponents(); ArrayList> a = omluvnyList.getData(); + Panel content = new Panel() + .setLayoutManager(new LinearLayout(Direction.VERTICAL)) + .addTo(mainPanel); + for(Pair p : a) { - omluvnyPanel.addComponent(new Label(p.getValue0() + " - " + p.getValue1())); + content.addComponent(new Label(p.getValue0() + " - " + p.getValue1())); } } - - @Override - public Panel getPanel() { - return omluvnyPanel; - } - - @Override - public boolean hasStarted() { - return hasStarted; - } - - @Override - public Label getLabel() { - return borderLabel; - } - - @Override - public void showOptions(WindowBasedTextGUI textGUI) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'getOptions'"); - } } diff --git a/src/main/java/xyz/thastertyn/UserInterface/Content/Rozvrh.java b/src/main/java/xyz/thastertyn/UserInterface/Content/Rozvrh.java index 7b33a34..fe5b46d 100644 --- a/src/main/java/xyz/thastertyn/UserInterface/Content/Rozvrh.java +++ b/src/main/java/xyz/thastertyn/UserInterface/Content/Rozvrh.java @@ -1,6 +1,5 @@ package xyz.thastertyn.UserInterface.Content; -import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -8,14 +7,11 @@ import com.googlecode.lanterna.gui2.Label; import com.googlecode.lanterna.gui2.Panel; import com.googlecode.lanterna.gui2.table.Table; -import xyz.thastertyn.Types.Choice; import xyz.thastertyn.Types.Timetable; import xyz.thastertyn.UserInterface.UpdateListener; public class Rozvrh extends JecnaContent { - private Panel rozvrhPanel = new Panel(); - private Label borderLabel = new Label("Rozvrh"); private xyz.thastertyn.Scrape.Rozvrh rozvrh = new xyz.thastertyn.Scrape.Rozvrh(); private String[] labels = {"Den", "7:30-8:15", "8:25-9:10", "9:20-10:05", "10:20-11:05", "11:15-12:00", "12:10-12:55", "13:05-13:50", "14:00-14:45", "14:55-15:40", "15:50-16:35"}; @@ -23,35 +19,21 @@ public class Rozvrh extends JecnaContent { String[] daysLabels = {"PO", "UT", "ST", "CT", "PA"}; - Table table = new Table<>(labels); + Table table; public Rozvrh(UpdateListener listener) { super(listener); - super.mainPanel = this.mainPanel; - super.borderLabel = this.borderLabel; + this.mainPanel = new Panel(); + this.borderLabel = new Label("Rozvrh"); super.scraper = this.rozvrh; } - @Override - protected void download(Choice choice) throws IOException - { - if(choice != null) - { - scraper.download(choice); - setGUI(); - listener.updatePanel(); - }else{ - scraper.download(); - setGUI(); - } - - hasStarted = true; - } - @Override protected void setGUI() { + mainPanel.removeAllComponents(); + table = new Table<>(labels); Timetable timetable = rozvrh.getRozvrh(); for(int day = 0; day < 5; day++) @@ -72,6 +54,6 @@ public class Rozvrh extends JecnaContent { table.getTableModel().addRow(currentRow); } - rozvrhPanel.addComponent(table); + mainPanel.addComponent(table); } } diff --git a/src/main/java/xyz/thastertyn/UserInterface/Content/Sdeleni.java b/src/main/java/xyz/thastertyn/UserInterface/Content/Sdeleni.java index a5c4876..166af7c 100644 --- a/src/main/java/xyz/thastertyn/UserInterface/Content/Sdeleni.java +++ b/src/main/java/xyz/thastertyn/UserInterface/Content/Sdeleni.java @@ -1,63 +1,34 @@ package xyz.thastertyn.UserInterface.Content; - -import java.io.IOException; import java.util.ArrayList; import com.googlecode.lanterna.gui2.GridLayout; import com.googlecode.lanterna.gui2.Label; import com.googlecode.lanterna.gui2.Panel; -import xyz.thastertyn.Types.Choice; import xyz.thastertyn.UserInterface.UpdateListener; public class Sdeleni extends JecnaContent { - private Panel sdeleniPanel = new Panel(); - private Label borderLabel = new Label("Sdeleni R."); - private xyz.thastertyn.Scrape.Sdeleni sdeleni = new xyz.thastertyn.Scrape.Sdeleni(); public Sdeleni(UpdateListener listener) { super(listener); - super.mainPanel = this.mainPanel; - super.borderLabel = this.borderLabel; + this.mainPanel = new Panel().setLayoutManager(new GridLayout(1) + .setLeftMarginSize(1) + .setRightMarginSize(1)); + this.borderLabel = new Label("Sdeleni R."); super.scraper = this.sdeleni; } - @Override - public void downloadDefault() throws IOException - { - download(null); - } - - @Override - protected void download(Choice choice) throws IOException - { - sdeleni = new xyz.thastertyn.Scrape.Sdeleni(); - if(choice != null) - { - sdeleni.download(choice); - setGUI(); - listener.updatePanel(); - }else{ - sdeleni.download(); - setGUI(); - } - - super.hasStarted = true; - } - @Override protected void setGUI() { - sdeleniPanel.setLayoutManager(new GridLayout(1) - .setLeftMarginSize(1) - .setRightMarginSize(1)); + mainPanel.removeAllComponents(); ArrayList sdeleniList = sdeleni.getSdeleni(); - sdeleniList.forEach(s -> sdeleniPanel.addComponent(new Label(s))); + sdeleniList.forEach(sdeleni -> mainPanel.addComponent(new Label(sdeleni))); } } diff --git a/src/main/java/xyz/thastertyn/UserInterface/Content/Znamky.java b/src/main/java/xyz/thastertyn/UserInterface/Content/Znamky.java index 58eb873..aef3a48 100644 --- a/src/main/java/xyz/thastertyn/UserInterface/Content/Znamky.java +++ b/src/main/java/xyz/thastertyn/UserInterface/Content/Znamky.java @@ -38,18 +38,15 @@ public class Znamky extends JecnaContent { true, false); - private Panel mainPanel = new Panel() - .setLayoutManager(new GridLayout(3)); - - private Label borderLabel = new Label("Znamky"); - private xyz.thastertyn.Scrape.Znamky znamky = new xyz.thastertyn.Scrape.Znamky(); public Znamky(UpdateListener listener) { super(listener); - super.mainPanel = this.mainPanel; - super.borderLabel = this.borderLabel; + + this.mainPanel = new Panel() + .setLayoutManager(new GridLayout(3)); + this.borderLabel = new Label("Znamky"); super.scraper = this.znamky; } @@ -59,7 +56,7 @@ public class Znamky extends JecnaContent { mainPanel.removeAllComponents(); ArrayList subjects = znamky.getPredmety(); - + HashMap colors = new HashMap<>(); colors.put(1, new SimpleTheme(ANSI.BLACK, VYBORNY)); colors.put(2, new SimpleTheme(ANSI.BLACK, CHVALITEBNY)); diff --git a/src/main/java/xyz/thastertyn/UserInterface/WindowSwitchListener.java b/src/main/java/xyz/thastertyn/UserInterface/WindowSwitchListener.java index e8cba70..1011535 100644 --- a/src/main/java/xyz/thastertyn/UserInterface/WindowSwitchListener.java +++ b/src/main/java/xyz/thastertyn/UserInterface/WindowSwitchListener.java @@ -29,8 +29,8 @@ public class WindowSwitchListener implements WindowListener, UpdateListener { private WindowBasedTextGUI textGUI; private JecnaContent[] contents = { - new Znamky(this), new Rozvrh(this), + new Znamky(this), // This being first doesn't resize properly for some reason new Sdeleni(this), new OmluvnyList(this) }; @@ -99,7 +99,7 @@ public class WindowSwitchListener implements WindowListener, UpdateListener { contents[current].downloadDefault(); }catch(IOException e) { - MessageDialog.showMessageDialog(textGUI, "Something failed", "The content failed to download", MessageDialogButton.OK); + MessageDialog.showMessageDialog(textGUI, "Something failed", e.getMessage(), MessageDialogButton.OK); } } } @@ -110,20 +110,21 @@ public class WindowSwitchListener implements WindowListener, UpdateListener { .getSize() .getColumns(); - int currentSize = contents[current] + int currentColumns = contents[current] .getPanel() .getSize() .getColumns(); - if(currentSize < tabColumns) + if(currentColumns < tabColumns) { holderPanel.addComponent( - contents[current].getPanel().setPreferredSize(new TerminalSize(tabColumns, - contents[current].getPanel().getPreferredSize().getRows()))); - - }else{ - holderPanel.addComponent(contents[current].getPanel()); + contents[current].getPanel().setPreferredSize(new TerminalSize( + tabColumns - 2, + contents[current].getPanel().getPreferredSize().getRows()))); + return; } + + holderPanel.addComponent(contents[current].getPanel()); } private void updateLabels() @@ -180,7 +181,6 @@ public class WindowSwitchListener implements WindowListener, UpdateListener { @Override public void onUnhandledInput(Window basePane, KeyStroke keyStroke, AtomicBoolean hasBeenHandled) { - // TODO Auto-generated method stub } @Override @@ -190,12 +190,12 @@ public class WindowSwitchListener implements WindowListener, UpdateListener { @Override public void onMoved(Window window, TerminalPosition oldPosition, TerminalPosition newPosition) { - // TODO Auto-generated method stub } @Override public void updatePanel() { holderPanel.removeAllComponents(); + // Todo find a way to resize the panel on screen holderPanel.addComponent(contents[current].getPanel()); }