Panel reloading upon choice works

This commit is contained in:
Thastertyn 2023-04-28 12:56:14 +02:00
parent 06183dd59c
commit 41553eb50e
12 changed files with 240 additions and 71 deletions

View File

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

View File

@ -22,6 +22,6 @@ public class Option {
} }
@Override @Override
public String toString() { public String toString() {
return text; return value;
} }
} }

View File

@ -14,12 +14,18 @@ public class Predmet {
this.jmenoPredmetu = jmenoPredmetu; this.jmenoPredmetu = jmenoPredmetu;
} }
public Predmet(String jmenoPredmetu, int vyslednaZnamka)
{
this.jmenoPredmetu = jmenoPredmetu;
this.vyslednaZnamka = vyslednaZnamka;
}
public void addZnamka(Znamka novaZnamka) public void addZnamka(Znamka novaZnamka)
{ {
znamky.add(novaZnamka); znamky.add(novaZnamka);
} }
public double getPrumer() public double getVyslednaZnamka()
{ {
if(vyslednaZnamka != 0) if(vyslednaZnamka != 0)
{ {

View File

@ -1,11 +1,14 @@
package xyz.thastertyn.UserInterface.Content; package xyz.thastertyn.UserInterface.Content;
import java.io.IOException;
import com.googlecode.lanterna.gui2.Label;
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.Choice;
import xyz.thastertyn.UserInterface.UpdateListener;
import com.googlecode.lanterna.gui2.Panel;
import com.googlecode.lanterna.gui2.Label;
/** /**
* Wrapper to include all classes that deal with content in some way * Wrapper to include all classes that deal with content in some way
@ -13,10 +16,11 @@ 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 downloadDefault(); public abstract void downloadDefault() throws IOException;
protected abstract void download(Choice choice); protected abstract void download(Choice choice) throws IOException;
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) throws IOException;
protected abstract void setGUI(); protected abstract void setGUI();
public abstract void setUpdateListener(UpdateListener updateListener);
} }

View File

@ -10,6 +10,7 @@ import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
import xyz.thastertyn.Tuples.Pair; import xyz.thastertyn.Tuples.Pair;
import xyz.thastertyn.Types.Choice; import xyz.thastertyn.Types.Choice;
import xyz.thastertyn.UserInterface.UpdateListener;
public class OmluvnyList extends JecnaContent{ public class OmluvnyList extends JecnaContent{
@ -17,6 +18,8 @@ public class OmluvnyList extends JecnaContent{
private Label borderLabel = new Label("Omluvny L."); private Label borderLabel = new Label("Omluvny L.");
private xyz.thastertyn.Scrape.OmluvnyList omluvnyList = new xyz.thastertyn.Scrape.OmluvnyList(); private xyz.thastertyn.Scrape.OmluvnyList omluvnyList = new xyz.thastertyn.Scrape.OmluvnyList();
private UpdateListener listener;
private boolean hasStarted = false; private boolean hasStarted = false;
@Override @Override
@ -73,4 +76,9 @@ public class OmluvnyList extends JecnaContent{
// TODO Auto-generated method stub // TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getOptions'"); throw new UnsupportedOperationException("Unimplemented method 'getOptions'");
} }
@Override
public void setUpdateListener(UpdateListener updateListener) {
this.listener = updateListener;
}
} }

View File

@ -9,6 +9,7 @@ import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
import com.googlecode.lanterna.gui2.table.Table; import com.googlecode.lanterna.gui2.table.Table;
import xyz.thastertyn.Types.Choice; import xyz.thastertyn.Types.Choice;
import xyz.thastertyn.UserInterface.UpdateListener;
public class Rozvrh extends JecnaContent { public class Rozvrh extends JecnaContent {
@ -16,6 +17,8 @@ public class Rozvrh extends JecnaContent {
private Label borderLabel = new Label("Rozvrh"); private Label borderLabel = new Label("Rozvrh");
private xyz.thastertyn.Scrape.Rozvrh rozvrh = new xyz.thastertyn.Scrape.Rozvrh(); private xyz.thastertyn.Scrape.Rozvrh rozvrh = new xyz.thastertyn.Scrape.Rozvrh();
private UpdateListener listener;
private boolean hasStarted = false; 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", "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"};
@ -91,4 +94,9 @@ public class Rozvrh extends JecnaContent {
// TODO Auto-generated method stub // TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getOptions'"); throw new UnsupportedOperationException("Unimplemented method 'getOptions'");
} }
@Override
public void setUpdateListener(UpdateListener updateListener) {
this.listener = updateListener;
}
} }

View File

@ -11,6 +11,7 @@ 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.Choice;
import xyz.thastertyn.UserInterface.UpdateListener;
public class Sdeleni extends JecnaContent { public class Sdeleni extends JecnaContent {
@ -19,6 +20,8 @@ public class Sdeleni extends JecnaContent {
private xyz.thastertyn.Scrape.Sdeleni sdeleni = new xyz.thastertyn.Scrape.Sdeleni(); private xyz.thastertyn.Scrape.Sdeleni sdeleni = new xyz.thastertyn.Scrape.Sdeleni();
private UpdateListener listener;
private boolean hasStarted = false; private boolean hasStarted = false;
@Override @Override
@ -78,4 +81,9 @@ public class Sdeleni extends JecnaContent {
// TODO Auto-generated method stub // TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getOptions'"); throw new UnsupportedOperationException("Unimplemented method 'getOptions'");
} }
@Override
public void setUpdateListener(UpdateListener updateListener) {
this.listener = updateListener;
}
} }

View File

@ -18,6 +18,7 @@ import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
import xyz.thastertyn.Types.Choice; 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.UpdateListener;
import xyz.thastertyn.UserInterface.Dialogs.OptionsDialog; import xyz.thastertyn.UserInterface.Dialogs.OptionsDialog;
public class Znamky extends JecnaContent { public class Znamky extends JecnaContent {
@ -41,24 +42,24 @@ public class Znamky extends JecnaContent {
true, true,
false); false);
private Panel mainPanel = new Panel() private Panel mainPanel;
.setLayoutManager(new GridLayout(3));
private Label borderLabel = new Label("Znamky"); private Label borderLabel = new Label("Znamky");
private xyz.thastertyn.Scrape.Znamky znamky = new xyz.thastertyn.Scrape.Znamky(); private xyz.thastertyn.Scrape.Znamky znamky;
private UpdateListener listener;
private boolean hasStarted = false; private boolean hasStarted = false;
@Override @Override
public void downloadDefault() public void downloadDefault() throws IOException
{ {
download(null); download(null);
} }
@Override @Override
protected void download(Choice choice) protected void download(Choice choice) throws IOException
{ {
try{ znamky = new xyz.thastertyn.Scrape.Znamky();
if(choice != null) if(choice != null)
{ {
znamky.downloadZnamky(choice); znamky.downloadZnamky(choice);
@ -67,26 +68,26 @@ public class Znamky extends JecnaContent {
} }
setGUI(); setGUI();
listener.updatePanel();
hasStarted = true; hasStarted = true;
}catch(IOException e)
{
mainPanel.addComponent(new Label("An error has occured"));
}
} }
@Override @Override
protected void setGUI() protected void setGUI()
{ {
ArrayList<Predmet> predmety = znamky.getPredmety(); mainPanel = new Panel()
.setLayoutManager(new GridLayout(3));
HashMap<Integer, SimpleTheme> barvy = new HashMap<>(); ArrayList<Predmet> subjects = znamky.getPredmety();
barvy.put(1, new SimpleTheme(ANSI.BLACK, VYBORNY));
barvy.put(2, new SimpleTheme(ANSI.BLACK, CHVALITEBNY)); HashMap<Integer, SimpleTheme> colors = new HashMap<>();
barvy.put(3, new SimpleTheme(ANSI.BLACK, DOBRY)); colors.put(1, new SimpleTheme(ANSI.BLACK, VYBORNY));
barvy.put(4, new SimpleTheme(ANSI.BLACK, DOSTATECNY)); colors.put(2, new SimpleTheme(ANSI.BLACK, CHVALITEBNY));
barvy.put(5, new SimpleTheme(ANSI.BLACK, NEDOSTATECNY)); colors.put(3, new SimpleTheme(ANSI.BLACK, DOBRY));
barvy.put(-1, new SimpleTheme(ANSI.WHITE, NEHODNOCEN)); colors.put(4, new SimpleTheme(ANSI.BLACK, DOSTATECNY));
colors.put(5, new SimpleTheme(ANSI.BLACK, NEDOSTATECNY));
colors.put(-1, new SimpleTheme(ANSI.WHITE, NEHODNOCEN));
// Sloupec pro jmena predmetu // Sloupec pro jmena predmetu
Panel jmemaPredmetu = new Panel() Panel jmemaPredmetu = new Panel()
@ -106,7 +107,7 @@ public class Znamky extends JecnaContent {
.setLayoutData(ALIGN_RIGHT) .setLayoutData(ALIGN_RIGHT)
.addTo(mainPanel); .addTo(mainPanel);
for(Predmet predmet : predmety) for(Predmet predmet : subjects)
{ {
Panel jednotliveZnamky = new Panel() Panel jednotliveZnamky = new Panel()
.setLayoutManager(new LinearLayout(Direction.HORIZONTAL)) .setLayoutManager(new LinearLayout(Direction.HORIZONTAL))
@ -120,19 +121,20 @@ public class Znamky extends JecnaContent {
for(Znamka znamka : predmet.getZnamky()) for(Znamka znamka : predmet.getZnamky())
{ {
Label znamkaLabel = new Label(znamka.getText()); Label znamkaLabel = new Label(znamka.getText());
znamkaLabel.setTheme(barvy.get(znamka.getZnamka())); znamkaLabel.setTheme(colors.get(znamka.getZnamka()));
jednotliveZnamky.addComponent(znamkaLabel); jednotliveZnamky.addComponent(znamkaLabel);
} }
double prumer = predmet.getPrumer(); double prumer = predmet.getVyslednaZnamka();
Label vysl = new Label(String.format("%.2f", prumer)); Label vysl = new Label(String.format("%.2f", prumer));
vysl.setTheme(barvy.get((int) Math.round(prumer))); vysl.setTheme(colors.get((int) Math.round(prumer)));
jmemaPredmetu.addComponent(new Label(predmet.getJmenoPredmetu())); jmemaPredmetu.addComponent(new Label(predmet.getJmenoPredmetu()));
vysledneZnamky.addComponent(vysl); vysledneZnamky.addComponent(vysl);
} }
mainPanel.invalidate();
} }
@ -155,7 +157,7 @@ public class Znamky extends JecnaContent {
} }
@Override @Override
public void showOptions(WindowBasedTextGUI textGUI) { public void showOptions(WindowBasedTextGUI textGUI) throws IOException {
OptionsDialog d = new OptionsDialog(znamky.getOptions().getValue0(), znamky.getOptions().getValue1()); OptionsDialog d = new OptionsDialog(znamky.getOptions().getValue0(), znamky.getOptions().getValue1());
Choice c = d.showDialog(textGUI); Choice c = d.showDialog(textGUI);
@ -165,4 +167,9 @@ public class Znamky extends JecnaContent {
download(c); download(c);
} }
} }
@Override
public void setUpdateListener(UpdateListener updateListener) {
this.listener = updateListener;
}
} }

View File

@ -0,0 +1,59 @@
package xyz.thastertyn.UserInterface.Dialogs;
import java.util.Arrays;
import com.googlecode.lanterna.gui2.Button;
import com.googlecode.lanterna.gui2.GridLayout;
import com.googlecode.lanterna.gui2.LocalizedString;
import com.googlecode.lanterna.gui2.Panel;
import com.googlecode.lanterna.gui2.Window;
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
import com.googlecode.lanterna.gui2.dialogs.DialogWindow;
public class EscapeDialog extends DialogWindow {
public EscapeDialog()
{
super("Choose from below");
Panel mainPanel = new Panel()
.setLayoutManager(new GridLayout(1)
.setLeftMarginSize(1)
.setRightMarginSize(1));
new Panel()
.setLayoutManager(
new GridLayout(2).setHorizontalSpacing(1))
.addComponent(
new Button(LocalizedString.OK.toString(), this::onOK)
.setLayoutData(GridLayout.createLayoutData(
GridLayout.Alignment.CENTER,
GridLayout.Alignment.CENTER,
true,
false)))
.addComponent(
new Button(LocalizedString.Cancel.toString(), this::onCancel))
.addTo(mainPanel);
setHints(Arrays.asList(Window.Hint.CENTERED));
setComponent(mainPanel);
}
public void onOK()
{
close();
}
public void onCancel()
{
close();
}
@Override
public Object showDialog(WindowBasedTextGUI textGUI) {
super.showDialog(textGUI);
return null;
}
}

View File

@ -90,11 +90,20 @@ public class OptionsDialog extends DialogWindow {
public Choice showDialog(WindowBasedTextGUI textGUI) { public Choice showDialog(WindowBasedTextGUI textGUI) {
super.showDialog(textGUI); super.showDialog(textGUI);
Choice c;
ArrayList<String> s = new ArrayList<>();
for(ComboBox<Option> b : boxs)
{
s.add(b.getSelectedItem().getValue());
}
c = new Choice(s);
return (useData) ? return (useData) ?
new Choice( // User pressed ok c
boxs.stream()
.map(b -> b.getSelectedItem().toString())
.toArray(String[]::new))
: :
null; // User pressed Cancel null; // User pressed Cancel
} }

View File

@ -0,0 +1,6 @@
package xyz.thastertyn.UserInterface;
public interface UpdateListener {
public void updatePanel();
}

View File

@ -1,5 +1,6 @@
package xyz.thastertyn.UserInterface; package xyz.thastertyn.UserInterface;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import com.googlecode.lanterna.TerminalPosition; import com.googlecode.lanterna.TerminalPosition;
@ -23,8 +24,7 @@ import xyz.thastertyn.UserInterface.Content.Rozvrh;
import xyz.thastertyn.UserInterface.Content.Sdeleni; import xyz.thastertyn.UserInterface.Content.Sdeleni;
import xyz.thastertyn.UserInterface.Content.Znamky; import xyz.thastertyn.UserInterface.Content.Znamky;
public class WindowSwitchListener implements WindowListener { public class WindowSwitchListener implements WindowListener, UpdateListener {
private WindowBasedTextGUI textGUI; private WindowBasedTextGUI textGUI;
private JecnaContent[] contents = { private JecnaContent[] contents = {
@ -39,8 +39,6 @@ public class WindowSwitchListener implements WindowListener {
private Panel tabsPanel = new Panel().setLayoutManager(new LinearLayout(Direction.HORIZONTAL)); private Panel tabsPanel = new Panel().setLayoutManager(new LinearLayout(Direction.HORIZONTAL));
private Panel holderPanel = new Panel(); private Panel holderPanel = new Panel();
private Panel errorPanel = new Panel().addComponent(new Label("Something went wrong"));
int current = contents.length - 1; int current = contents.length - 1;
public WindowSwitchListener(Panel holder, String title, WindowBasedTextGUI textGUI) public WindowSwitchListener(Panel holder, String title, WindowBasedTextGUI textGUI)
@ -55,6 +53,11 @@ public class WindowSwitchListener implements WindowListener {
tabsPanel.addComponent(this.tabs[i]); tabsPanel.addComponent(this.tabs[i]);
} }
for(JecnaContent c : contents)
{
c.setUpdateListener(this);
}
next(); next();
} }
@ -67,15 +70,45 @@ public class WindowSwitchListener implements WindowListener {
current++; current++;
} }
if(!contents[current].hasStarted()) loadPanel();
{
contents[current].downloadDefault();
} }
private void previous()
{
if(current - 1 == -1)
{
current = contents.length - 1;
}else{
current--;
}
loadPanel();
}
private void loadPanel()
{
checkIfDownloadedCorrectly();
holderPanel.removeAllComponents(); holderPanel.removeAllComponents();
tabsPanel.removeAllComponents(); tabsPanel.removeAllComponents();
if(contents[current].hasStarted()) setPanelPreferedSize();
updateLabels();
}
private void checkIfDownloadedCorrectly()
{
if(!contents[current].hasStarted())
{
try{
contents[current].downloadDefault();
}catch(IOException e)
{
MessageDialog.showMessageDialog(textGUI, "Something failed", "The content failed to download", MessageDialogButton.OK);
}
}
}
private void setPanelPreferedSize()
{ {
int tabColumns = tabsPanel.getSize().getColumns(); int tabColumns = tabsPanel.getSize().getColumns();
@ -88,10 +121,10 @@ public class WindowSwitchListener implements WindowListener {
}else{ }else{
holderPanel.addComponent(contents[current].getPanel()); holderPanel.addComponent(contents[current].getPanel());
} }
}else{
holderPanel.addComponent(errorPanel);
} }
private void updateLabels()
{
for(int i = 0; i < tabs.length; i++) for(int i = 0; i < tabs.length; i++)
{ {
if(i == current) if(i == current)
@ -103,6 +136,16 @@ public class WindowSwitchListener implements WindowListener {
} }
} }
private void showOptions()
{
try{
contents[current].showOptions(textGUI);
}catch(IOException e)
{
MessageDialog.showMessageDialog(textGUI, "Something failed", "An error with connection has occured, either no connection at all, or connection timed out", MessageDialogButton.OK);
}
}
@Override @Override
public void onInput(Window basePane, KeyStroke keyStroke, AtomicBoolean deliverEvent) { public void onInput(Window basePane, KeyStroke keyStroke, AtomicBoolean deliverEvent) {
KeyType type = keyStroke.getKeyType(); KeyType type = keyStroke.getKeyType();
@ -110,12 +153,17 @@ public class WindowSwitchListener implements WindowListener {
switch(type) switch(type)
{ {
case Tab: case Tab:
if(keyStroke.isShiftDown())
{
previous();
}else{
next(); next();
}
break; break;
case Character: case Character:
if(keyStroke.getCharacter() == ' ') if(keyStroke.getCharacter() == ' ')
{ {
contents[current].showOptions(textGUI); showOptions();
} }
break; break;
case Escape: case Escape:
@ -141,4 +189,10 @@ public class WindowSwitchListener implements WindowListener {
// TODO Auto-generated method stub // TODO Auto-generated method stub
} }
@Override
public void updatePanel() {
holderPanel.removeAllComponents();
holderPanel.addComponent(contents[current].getPanel());
}
} }