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;
import java.util.ArrayList;
import java.util.Arrays;
public class Choice {
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()

View File

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

View File

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

View File

@ -1,11 +1,14 @@
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 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
@ -13,10 +16,11 @@ import com.googlecode.lanterna.gui2.Label;
public abstract class JecnaContent {
public abstract Panel getPanel();
public abstract void downloadDefault();
protected abstract void download(Choice choice);
public abstract void downloadDefault() throws IOException;
protected abstract void download(Choice choice) throws IOException;
public abstract boolean hasStarted();
public abstract Label getLabel();
public abstract void showOptions(final WindowBasedTextGUI textGUI);
public abstract void showOptions(final WindowBasedTextGUI textGUI) throws IOException;
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.Types.Choice;
import xyz.thastertyn.UserInterface.UpdateListener;
public class OmluvnyList extends JecnaContent{
@ -17,6 +18,8 @@ public class OmluvnyList extends JecnaContent{
private Label borderLabel = new Label("Omluvny L.");
private xyz.thastertyn.Scrape.OmluvnyList omluvnyList = new xyz.thastertyn.Scrape.OmluvnyList();
private UpdateListener listener;
private boolean hasStarted = false;
@Override
@ -73,4 +76,9 @@ public class OmluvnyList extends JecnaContent{
// TODO Auto-generated method stub
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 xyz.thastertyn.Types.Choice;
import xyz.thastertyn.UserInterface.UpdateListener;
public class Rozvrh extends JecnaContent {
@ -16,6 +17,8 @@ public class Rozvrh extends JecnaContent {
private Label borderLabel = new Label("Rozvrh");
private xyz.thastertyn.Scrape.Rozvrh rozvrh = new xyz.thastertyn.Scrape.Rozvrh();
private UpdateListener listener;
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"};
@ -91,4 +94,9 @@ public class Rozvrh extends JecnaContent {
// TODO Auto-generated method stub
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 xyz.thastertyn.Types.Choice;
import xyz.thastertyn.UserInterface.UpdateListener;
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 UpdateListener listener;
private boolean hasStarted = false;
@Override
@ -78,4 +81,9 @@ public class Sdeleni extends JecnaContent {
// TODO Auto-generated method stub
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.Predmet;
import xyz.thastertyn.Types.Znamka;
import xyz.thastertyn.UserInterface.UpdateListener;
import xyz.thastertyn.UserInterface.Dialogs.OptionsDialog;
public class Znamky extends JecnaContent {
@ -41,52 +42,52 @@ public class Znamky extends JecnaContent {
true,
false);
private Panel mainPanel = new Panel()
.setLayoutManager(new GridLayout(3));
private Panel mainPanel;
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;
@Override
public void downloadDefault()
public void downloadDefault() throws IOException
{
download(null);
}
@Override
protected void download(Choice choice)
protected void download(Choice choice) throws IOException
{
try{
if(choice != null)
{
znamky.downloadZnamky(choice);
}else{
znamky.downloadZnamky();
}
setGUI();
hasStarted = true;
}catch(IOException e)
znamky = new xyz.thastertyn.Scrape.Znamky();
if(choice != null)
{
mainPanel.addComponent(new Label("An error has occured"));
znamky.downloadZnamky(choice);
}else{
znamky.downloadZnamky();
}
setGUI();
listener.updatePanel();
hasStarted = true;
}
@Override
protected void setGUI()
{
ArrayList<Predmet> predmety = znamky.getPredmety();
mainPanel = new Panel()
.setLayoutManager(new GridLayout(3));
ArrayList<Predmet> subjects = 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));
HashMap<Integer, SimpleTheme> colors = new HashMap<>();
colors.put(1, new SimpleTheme(ANSI.BLACK, VYBORNY));
colors.put(2, new SimpleTheme(ANSI.BLACK, CHVALITEBNY));
colors.put(3, new SimpleTheme(ANSI.BLACK, DOBRY));
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
Panel jmemaPredmetu = new Panel()
@ -106,7 +107,7 @@ public class Znamky extends JecnaContent {
.setLayoutData(ALIGN_RIGHT)
.addTo(mainPanel);
for(Predmet predmet : predmety)
for(Predmet predmet : subjects)
{
Panel jednotliveZnamky = new Panel()
.setLayoutManager(new LinearLayout(Direction.HORIZONTAL))
@ -120,19 +121,20 @@ public class Znamky extends JecnaContent {
for(Znamka znamka : predmet.getZnamky())
{
Label znamkaLabel = new Label(znamka.getText());
znamkaLabel.setTheme(barvy.get(znamka.getZnamka()));
znamkaLabel.setTheme(colors.get(znamka.getZnamka()));
jednotliveZnamky.addComponent(znamkaLabel);
}
double prumer = predmet.getPrumer();
double prumer = predmet.getVyslednaZnamka();
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()));
vysledneZnamky.addComponent(vysl);
}
mainPanel.invalidate();
}
@ -155,7 +157,7 @@ public class Znamky extends JecnaContent {
}
@Override
public void showOptions(WindowBasedTextGUI textGUI) {
public void showOptions(WindowBasedTextGUI textGUI) throws IOException {
OptionsDialog d = new OptionsDialog(znamky.getOptions().getValue0(), znamky.getOptions().getValue1());
Choice c = d.showDialog(textGUI);
@ -165,4 +167,9 @@ public class Znamky extends JecnaContent {
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) {
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) ?
new Choice( // User pressed ok
boxs.stream()
.map(b -> b.getSelectedItem().toString())
.toArray(String[]::new))
c
:
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;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
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.Znamky;
public class WindowSwitchListener implements WindowListener {
public class WindowSwitchListener implements WindowListener, UpdateListener {
private WindowBasedTextGUI textGUI;
private JecnaContent[] contents = {
@ -39,8 +39,6 @@ public class WindowSwitchListener implements WindowListener {
private Panel tabsPanel = new Panel().setLayoutManager(new LinearLayout(Direction.HORIZONTAL));
private Panel holderPanel = new Panel();
private Panel errorPanel = new Panel().addComponent(new Label("Something went wrong"));
int current = contents.length - 1;
public WindowSwitchListener(Panel holder, String title, WindowBasedTextGUI textGUI)
@ -55,6 +53,11 @@ public class WindowSwitchListener implements WindowListener {
tabsPanel.addComponent(this.tabs[i]);
}
for(JecnaContent c : contents)
{
c.setUpdateListener(this);
}
next();
}
@ -66,32 +69,62 @@ public class WindowSwitchListener implements WindowListener {
}else{
current++;
}
loadPanel();
}
if(!contents[current].hasStarted())
private void previous()
{
if(current - 1 == -1)
{
contents[current].downloadDefault();
current = contents.length - 1;
}else{
current--;
}
loadPanel();
}
private void loadPanel()
{
checkIfDownloadedCorrectly();
holderPanel.removeAllComponents();
tabsPanel.removeAllComponents();
if(contents[current].hasStarted())
{
int tabColumns = tabsPanel.getSize().getColumns();
if(contents[current].getPanel().getSize().getColumns() < tabColumns)
{
holderPanel.addComponent(
contents[current].getPanel().setPreferredSize(new TerminalSize(tabColumns,
contents[current].getPanel().getPreferredSize().getRows())));
}else{
holderPanel.addComponent(contents[current].getPanel());
}
}else{
holderPanel.addComponent(errorPanel);
}
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();
if(contents[current].getPanel().getSize().getColumns() < tabColumns)
{
holderPanel.addComponent(
contents[current].getPanel().setPreferredSize(new TerminalSize(tabColumns,
contents[current].getPanel().getPreferredSize().getRows())));
}else{
holderPanel.addComponent(contents[current].getPanel());
}
}
private void updateLabels()
{
for(int i = 0; i < tabs.length; i++)
{
if(i == current)
@ -102,7 +135,17 @@ 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
public void onInput(Window basePane, KeyStroke keyStroke, AtomicBoolean deliverEvent) {
KeyType type = keyStroke.getKeyType();
@ -110,12 +153,17 @@ public class WindowSwitchListener implements WindowListener {
switch(type)
{
case Tab:
next();
if(keyStroke.isShiftDown())
{
previous();
}else{
next();
}
break;
case Character:
if(keyStroke.getCharacter() == ' ')
{
contents[current].showOptions(textGUI);
showOptions();
}
break;
case Escape:
@ -140,5 +188,11 @@ public class WindowSwitchListener implements WindowListener {
public void onMoved(Window window, TerminalPosition oldPosition, TerminalPosition newPosition) {
// TODO Auto-generated method stub
}
@Override
public void updatePanel() {
holderPanel.removeAllComponents();
holderPanel.addComponent(contents[current].getPanel());
}
}