package com.github.swapii.condi;
import com.googlecode.lanterna.gui.*;
import com.googlecode.lanterna.gui.component.Button;
import com.googlecode.lanterna.gui.component.Panel;
import com.googlecode.lanterna.gui.component.Table;
import com.googlecode.lanterna.gui.dialog.DialogButtons;
import com.googlecode.lanterna.gui.dialog.DialogResult;
import com.googlecode.lanterna.gui.dialog.MessageBox;
import com.googlecode.lanterna.gui.layout.HorisontalLayout;
import com.googlecode.lanterna.input.Key;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Pavel Savinov
*/
public class CondiWindow extends Window {
private static final Logger logger = LoggerFactory.getLogger(CondiWindow.class);
private final Button scanButton;
private GUIScreen screen;
public CondiWindow(GUIScreen screen) {
super("");
this.screen = screen;
logger.debug("Main window activated");
Panel toolbar = new Panel(Panel.Orientation.HORISONTAL);
toolbar.setLayoutManager(new HorisontalLayout());
scanButton = new Button("Scan folder", new Action() {
@Override
public void doAction() {
scanFolder();
}
});
scanButton.setAlignment(Component.Alignment.FILL);
toolbar.addComponent(scanButton);
Button aboutButton = new Button("About", new Action() {
@Override
public void doAction() {
new AboutDialog(CondiWindow.this.screen);
}
});
toolbar.addComponent(aboutButton);
addComponent(toolbar);
Table table = new Table(2);
addComponent(table);
setFocus(scanButton);
screen.showWindow(this, GUIScreen.Position.FULL_SCREEN);
}
@Override
public void onKeyPressed(Key key) {
super.onKeyPressed(key);
if (key.getKind() == Key.Kind.Escape) {
askExit();
}
if ('s' == key.getCharacter() && key.isCtrlPressed()) scanFolder();
if ('a' == key.getCharacter() && key.isCtrlPressed()) new AboutDialog(screen);
}
private void scanFolder() {
new FolderChooser(screen);
}
private void askExit() {
logger.trace("Exit from app request");
if (DialogResult.YES == MessageBox.showMessageBox(getOwner(), "Exit?", "Sure exit?", DialogButtons.YES_NO))
System.exit(0);
}
}