Commit 3c9a0849 authored by Robert Zenz's avatar Robert Zenz

More work on the UI rework.

parent b94b3c05
......@@ -53,16 +53,15 @@ public final class Main {
ui = new Tui();
}
if (ui != null) {
try {
ui.start(arguments);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return;
if (arguments.useSwt() || ui == null) {
ui = new Swt();
}
Swt.run(arguments);
try {
ui.start(arguments);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
......@@ -19,11 +19,10 @@ package org.bonsaimind.jmathpaper.cli;
import java.io.IOException;
import org.bonsaimind.jmathpaper.Configuration;
import org.bonsaimind.jmathpaper.core.EvaluatedExpression;
import org.bonsaimind.jmathpaper.core.ui.AbstractPapersUi;
public final class Cli extends AbstractPapersUi {
public class Cli extends AbstractPapersUi {
public Cli() {
super();
}
......@@ -50,11 +49,6 @@ public final class Cli extends AbstractPapersUi {
return true;
}
@Override
protected void initDefaultPaper() throws IOException {
open(Configuration.getGlobalPaperFile());
}
@Override
protected void internalStart() throws Exception {
if (arguments.getExpression() != null) {
......
......@@ -23,6 +23,7 @@ import java.util.ArrayList;
import java.util.List;
import org.bonsaimind.jmathpaper.Arguments;
import org.bonsaimind.jmathpaper.Configuration;
import org.bonsaimind.jmathpaper.core.Paper;
public abstract class AbstractPapersUi implements Ui {
......@@ -151,7 +152,7 @@ public abstract class AbstractPapersUi implements Ui {
}
protected void initDefaultPaper() throws IOException {
new_();
open(Configuration.getGlobalPaperFile());
}
protected abstract void internalStart() throws Exception;
......
......@@ -31,7 +31,7 @@ public enum Command {
LOAD("load"),
/** Start a new paper. */
NEW(),
NEW("new"),
/** Opens the given paper. */
OPEN("open", ":e"),
......
......@@ -101,10 +101,10 @@ public class MainComposite extends Composite {
new MenuItem(fileMenu, SWT.SEPARATOR);
saveAsMenuItem = new MenuItem(fileMenu, SWT.PUSH);
saveAsMenuItem.setText("Clea&r");
saveAsMenuItem.setToolTipText("Clears the current paper.");
saveAsMenuItem.addListener(SWT.Selection, this::onClearPushed);
clearMenuItem = new MenuItem(fileMenu, SWT.PUSH);
clearMenuItem.setText("Clea&r");
clearMenuItem.setToolTipText("Clears the current paper.");
clearMenuItem.addListener(SWT.Selection, this::onClearPushed);
new MenuItem(fileMenu, SWT.SEPARATOR);
......@@ -209,6 +209,7 @@ public class MainComposite extends Composite {
saveMenuItem.setEnabled(hasItems);
saveAsMenuItem.setEnabled(hasItems);
clearMenuItem.setEnabled(hasItems);
boolean hasManyItems = cTabFolder.getItemCount() > 1;
......
......@@ -17,37 +17,47 @@
package org.bonsaimind.jmathpaper.swt;
import org.bonsaimind.jmathpaper.Arguments;
import org.bonsaimind.jmathpaper.core.ui.AbstractPapersUi;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public final class Swt {
private Swt() {
// No instancing required.
public class Swt extends AbstractPapersUi {
protected MainComposite mainComposite = null;
protected Shell shell = null;
public Swt() {
super();
}
@Override
public void quit() {
shell.dispose();
}
public static final void run(Arguments arguments) {
@Override
protected void internalStart() throws Exception {
Display display = new Display();
GridLayout mainLayout = new GridLayout(1, true);
Shell mainWindow = new Shell();
mainWindow.setLayout(mainLayout);
mainWindow.setSize(720, 480);
mainWindow.setText("jMathPaper");
shell = new Shell();
shell.setLayout(mainLayout);
shell.setSize(720, 480);
shell.setText("jMathPaper");
MainComposite mainContent = new MainComposite(mainWindow, SWT.NONE);
mainContent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
mainContent.setVisible(true);
mainComposite = new MainComposite(shell, SWT.NONE);
mainComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
mainComposite.setVisible(true);
mainWindow.open();
shell.open();
mainContent.init(arguments);
mainComposite.init(arguments);
while (!mainWindow.isDisposed()) {
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
......
......@@ -17,6 +17,10 @@
package org.bonsaimind.jmathpaper.tui;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Path;
import org.bonsaimind.jmathpaper.core.EvaluatedExpression;
import org.bonsaimind.jmathpaper.core.ui.AbstractPapersUi;
import org.bonsaimind.jmathpaper.core.ui.CommandProcessor;
......@@ -27,33 +31,54 @@ import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
public class Tui extends AbstractPapersUi {
private boolean running = true;
protected boolean running = true;
protected PrintWriter writer = null;
public Tui() {
super();
}
@Override
public void close() {
super.close();
if (paper == null) {
try {
initDefaultPaper();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
printPaper();
}
}
@Override
public void open(Path file) throws IOException {
super.open(file);
printPaper();
}
@Override
public void quit() {
running = false;
}
@Override
public void reload() throws IOException {
super.reload();
printPaper();
}
@Override
protected void internalStart() throws Exception {
try (Terminal terminal = TerminalBuilder.terminal()) {
if (paper.getNotes() != null && paper.getNotes().trim().length() > 0) {
terminal.writer().write(paper.getNotes());
terminal.writer().write("\n");
terminal.writer().write("\n");
}
writer = terminal.writer();
for (EvaluatedExpression evaluatedExpression : paper.getEvaluatedExpressions()) {
terminal.writer().write(evaluatedExpression.toString(
paper.getIdColumnSize(),
paper.getExpressionColumnSize(),
paper.getResultColumnSize()));
terminal.writer().write("\n");
}
printPaper();
LineReader reader = LineReaderBuilder.builder().terminal(terminal).build();
......@@ -64,11 +89,11 @@ public class Tui extends AbstractPapersUi {
if (!CommandProcessor.applyCommand(this, input)) {
EvaluatedExpression evaluatedExpression = paper.evaluate(input);
terminal.writer().write(evaluatedExpression.toString(
writer.write(evaluatedExpression.toString(
paper.getIdColumnSize(),
paper.getExpressionColumnSize(),
paper.getResultColumnSize()));
terminal.writer().write("\n");
writer.write("\n");
terminal.flush();
}
}
......@@ -78,4 +103,35 @@ public class Tui extends AbstractPapersUi {
return;
}
}
protected void printPaper() {
if (writer != null && paper != null) {
int paperWidth = paper.getIdColumnSize() + paper.getExpressionColumnSize() + paper.getResultColumnSize() + 4;
for (int counter = 0; counter < paperWidth; counter++) {
writer.write("-");
}
writer.write("\n");
if (paper.getFile() != null) {
writer.write(paper.getFile().toAbsolutePath().toString());
writer.write("\n");
writer.write("\n");
}
if (paper.getNotes() != null && paper.getNotes().trim().length() > 0) {
writer.write(paper.getNotes());
writer.write("\n");
writer.write("\n");
}
for (EvaluatedExpression evaluatedExpression : paper.getEvaluatedExpressions()) {
writer.write(evaluatedExpression.toString(
paper.getIdColumnSize(),
paper.getExpressionColumnSize(),
paper.getResultColumnSize()));
writer.write("\n");
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment