Working on using abstract class more, a lot of stuff doesn't work rn

This commit is contained in:
Thastertyn 2023-05-02 22:16:34 +02:00
parent b4c1e53c83
commit 447561c95c
12 changed files with 139 additions and 133 deletions

View File

@ -49,6 +49,7 @@ public class LocalCredentials {
public String[] getCredentialsFile()
{
// TODO Use hashmap instead of array
String[] result = new String[2];
if(credentialsFile == null)
{

View File

@ -1,9 +1,7 @@
package xyz.thastertyn.Login;
import java.io.FileWriter;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Properties;
import java.util.concurrent.TimeoutException;
import javax.security.auth.login.CredentialException;

View File

@ -11,14 +11,14 @@ import org.jsoup.select.Elements;
import xyz.thastertyn.Types.Choice;
import xyz.thastertyn.Types.Options;
import xyz.thastertyn.Types.Timetable;
/**
* Jeden radek v rozvrhu
*/
public class Rozvrh extends JecnaScrape {
private String[][] rozvrh = new String[5][10];
private Timetable timetable;
/**
* Stahne rozvrh z www.spsejecna.cz a dale ho zpracuje do formy
* se kterou da pracovat
@ -29,6 +29,7 @@ public class Rozvrh extends JecnaScrape {
@Override
public void download() throws IOException
{
timetable = new Timetable();
Document rozvrhDokumentHTML = Downloader.download("https://www.spsejecna.cz/timetable/class").get();
Elements[] radkyRozvrhuHTML = rozvrhDokumentHTML
@ -38,42 +39,34 @@ public class Rozvrh extends JecnaScrape {
.map(Element::children)
.toArray(Elements[]::new);
for(int i = 1; i < 6; i++)
for(int i = 0; i < 5; i++)
{
for(int j = 1; j < 11; j++)
for(int j = 0; j < 10; j++)
{
String predmet = radkyRozvrhuHTML[i].get(j).select("span.subject").text();
String predmet = radkyRozvrhuHTML[i+1].get(j+1).select("span.subject").text();
// Predmety jako CEL jsou trikrat, staci ale jen jednou
String[] split = predmet.split(" ");
HashSet<String> set = new HashSet<>(Arrays.asList(split));
String pr = String.join("/", set);
rozvrh[i-1][j-1] = pr;
timetable.get(i).set(j, pr);
}
}
}
public String[][] getRozvrh()
public Timetable getRozvrh()
{
return rozvrh;
return timetable;
}
@Override
public String toString()
{
String s = "";
for(String[] st : rozvrh)
{
s += ("| ");
for(String str : st)
{
s += String.format("%-5s", str) + " | ";
}
s += "\n";
}
return s;
return (timetable == null) ?
"Nothing downloaded yet"
:
"All up and ready";
}
@Override

View File

@ -1,8 +1,6 @@
package xyz.thastertyn.Scrape;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.regex.Pattern;
@ -21,13 +19,13 @@ public class Znamky extends JecnaScrape {
private boolean wasDownloaded;
// schoolYear, schoolYearId
private ArrayList<Predmet> predmety = new ArrayList<>();
private ArrayList<Predmet> 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");
public void download() throws UnknownHostException, IOException
public void download() throws IOException
{
download("https://www.spsejecna.cz/score/student");
}
@ -41,9 +39,9 @@ public class Znamky extends JecnaScrape {
choice.getChoices().get(1)));
}
private void download(String url) throws UnknownHostException, IOException
private void download(String url) throws IOException
{
//String url =
predmety = new ArrayList<>();
Document znamkyDokumentHTML = Downloader.download(url).get();
// Predmety ulozene v <tr>

View File

@ -0,0 +1,16 @@
package xyz.thastertyn.Types;
public class DayOfTimetable {
private String[] subjects = new String[10];
public String get(int index)
{
return subjects[index];
}
public void set(int index, String subject)
{
subjects[index] = subject;
}
}

View File

@ -0,0 +1,19 @@
package xyz.thastertyn.Types;
public class Timetable {
private DayOfTimetable[] timetable = new DayOfTimetable[5];
public Timetable()
{
for(int i = 0; i < timetable.length; i++)
{
timetable[i] = new DayOfTimetable();
}
}
public DayOfTimetable get(int day)
{
return timetable[day];
}
}

View File

@ -21,35 +21,58 @@ public abstract class JecnaContent {
protected boolean hasStarted = false;
protected Label borderLabel;
protected UpdateListener listener;
protected abstract void setGUI();
protected abstract void download(Choice choice) throws IOException;
public abstract void downloadDefault() throws IOException;
protected JecnaScrape scraper;
public void showOptions(WindowBasedTextGUI textGUI) throws IOException {
OptionsDialog d = new OptionsDialog(scraper.getOptions());
protected abstract void setGUI();
Choice c = d.showDialog(textGUI);
public JecnaContent(UpdateListener listener)
{
this.listener = listener;
}
if(c != null)
protected void download(Choice choice) throws IOException
{
if(choice != null)
{
download(c);
scraper.download(choice);
setGUI();
listener.updatePanel();
}else{
scraper.download();
setGUI();
}
hasStarted = true;
}
public void downloadDefault() throws IOException
{
download(null);
}
public void showOptions(WindowBasedTextGUI textGUI) throws IOException {
OptionsDialog dialog = new OptionsDialog(scraper.getOptions());
Choice choice = dialog.showDialog(textGUI);
if(choice != null)
{
download(choice);
}
}
public Panel getPanel()
{
return mainPanel;
return this.mainPanel;
}
public boolean hasStarted()
{
return hasStarted;
return this.hasStarted;
}
public Label getLabel()
{
return borderLabel;
return this.borderLabel;
}
}

View File

@ -21,7 +21,7 @@ public class OmluvnyList extends JecnaContent{
public OmluvnyList(UpdateListener listener)
{
this.listener = listener;
super(listener);
}
@Override

View File

@ -1,14 +1,15 @@
package xyz.thastertyn.UserInterface.Content;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import com.googlecode.lanterna.gui2.Label;
import com.googlecode.lanterna.gui2.Panel;
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
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 {
@ -17,84 +18,60 @@ public class Rozvrh extends JecnaContent {
private Label borderLabel = new Label("Rozvrh");
private xyz.thastertyn.Scrape.Rozvrh rozvrh = new xyz.thastertyn.Scrape.Rozvrh();
private boolean hasStarted = false;
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"};
//private String[] labels = {"Den", "1.", "2.", "3.", "4.", "5.", "6.", "7.", "8.", "9.", "10."};
String[] daysLabels = {"PO", "UT", "ST", "CT", "PA"};
Table<String> t = new Table<>(labels);
Table<String> table = new Table<>(labels);
public Rozvrh(UpdateListener listener)
{
this.listener = listener;
super(listener);
super.mainPanel = this.mainPanel;
super.borderLabel = this.borderLabel;
super.scraper = this.rozvrh;
}
@Override
public void downloadDefault()
protected void download(Choice choice) throws IOException
{
download(null);
}
@Override
protected void download(Choice choice)
{
try{
rozvrh.download();
if(choice != null)
{
scraper.download(choice);
setGUI();
listener.updatePanel();
}else{
scraper.download();
setGUI();
hasStarted = true;
}catch(UnknownHostException e)
{
rozvrhPanel.addComponent(new Label("A connection error occurred"));
}catch(IOException e)
{
rozvrhPanel.addComponent(new Label("An error occurred"));
}
hasStarted = true;
}
@Override
protected void setGUI()
{
String[][] rozvrhArray = rozvrh.getRozvrh();
Timetable timetable = rozvrh.getRozvrh();
String[] den = new String[11];
for(int i = 0; i < rozvrhArray.length; i++)
for(int day = 0; day < 5; day++)
{
den[0] = daysLabels[i];
for(int j = 1; j < rozvrhArray[i].length + 1; j++)
List<String> currentRow = new ArrayList<>();
// Set the day (Po, Ut, St,...)
currentRow.add(daysLabels[day]);
// Add the classes
for(int hour = 0; hour < 10; hour++)
{
den[j] = (rozvrhArray[i][j - 1].isBlank()) ? " - " : rozvrhArray[i][j - 1];
currentRow.add((timetable.get(day).get(hour).isBlank()) ?
" - " // Empty class, nothing taught at that moment
:
timetable.get(day).get(hour)); // Get class
}
t.getTableModel().addRow(den);
table.getTableModel().addRow(currentRow);
}
rozvrhPanel.addComponent(t);
}
@Override
public boolean hasStarted()
{
return hasStarted;
}
@Override
public Panel getPanel()
{
return rozvrhPanel;
}
@Override
public Label getLabel()
{
return borderLabel;
}
@Override
public void showOptions(WindowBasedTextGUI textGUI) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getOptions'");
rozvrhPanel.addComponent(table);
}
}

View File

@ -2,7 +2,6 @@ package xyz.thastertyn.UserInterface.Content;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import com.googlecode.lanterna.gui2.GridLayout;
@ -21,10 +20,10 @@ public class Sdeleni extends JecnaContent {
public Sdeleni(UpdateListener listener)
{
super(listener);
super.mainPanel = this.mainPanel;
super.borderLabel = this.borderLabel;
super.scraper = this.sdeleni;
this.listener = listener;
}
@Override

View File

@ -1,6 +1,5 @@
package xyz.thastertyn.UserInterface.Content;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
@ -14,7 +13,6 @@ import com.googlecode.lanterna.gui2.LayoutData;
import com.googlecode.lanterna.gui2.LinearLayout;
import com.googlecode.lanterna.gui2.Panel;
import xyz.thastertyn.Types.Choice;
import xyz.thastertyn.Types.Predmet;
import xyz.thastertyn.Types.Znamka;
import xyz.thastertyn.UserInterface.UpdateListener;
@ -41,41 +39,18 @@ public class Znamky extends JecnaContent {
false);
private Panel mainPanel = new Panel()
.setLayoutManager(new GridLayout(3));
private Label borderLabel = new Label("Znamky");
private xyz.thastertyn.Scrape.Znamky znamky;
.setLayoutManager(new GridLayout(3));
private UpdateListener listener;
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;
super.scraper = this.znamky;
this.listener = listener;
}
@Override
public void downloadDefault() throws IOException
{
download(null);
}
@Override
protected void download(Choice choice) throws IOException
{
znamky = new xyz.thastertyn.Scrape.Znamky();
if(choice != null)
{
znamky.download(choice);
setGUI();
listener.updatePanel();
}else{
znamky.download();
setGUI();
}
super.hasStarted = true;
}
@Override

View File

@ -29,8 +29,8 @@ public class WindowSwitchListener implements WindowListener, UpdateListener {
private WindowBasedTextGUI textGUI;
private JecnaContent[] contents = {
new Rozvrh(this),
new Znamky(this),
new Rozvrh(this),
new Sdeleni(this),
new OmluvnyList(this)
};
@ -106,9 +106,16 @@ public class WindowSwitchListener implements WindowListener, UpdateListener {
private void setPanelPreferedSize()
{
int tabColumns = tabsPanel.getSize().getColumns();
int tabColumns = tabsPanel
.getSize()
.getColumns();
if(contents[current].getPanel().getSize().getColumns() < tabColumns)
int currentSize = contents[current]
.getPanel()
.getSize()
.getColumns();
if(currentSize < tabColumns)
{
holderPanel.addComponent(
contents[current].getPanel().setPreferredSize(new TerminalSize(tabColumns,