Skip to content
This repository has been archived by the owner on Jan 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #34 from TjeuKayim/develop
Browse files Browse the repository at this point in the history
Version 4.1
  • Loading branch information
TjeuKayim authored Jun 25, 2018
2 parents e9621ec + 3976d2e commit e2df393
Show file tree
Hide file tree
Showing 36 changed files with 627 additions and 251 deletions.
31 changes: 27 additions & 4 deletions client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,21 @@
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!-- Icons http://aalmiray.github.io/ikonli/cheat-sheet-fontawesome5.html -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!-- Icons http://aalmiray.github.io/ikonli/cheat-sheet-fontawesome5.html -->
<dependency>
<!-- Icons -->
<groupId>org.kordamp.ikonli</groupId>
<artifactId>ikonli-javafx</artifactId>
<version>2.1.1</version>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.kordamp.ikonli</groupId>
<artifactId>ikonli-fontawesome5-pack</artifactId>
<version>2.1.1</version>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.websocket</groupId>
Expand All @@ -58,6 +57,30 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>com.github.coldab.client.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
Expand Down
15 changes: 9 additions & 6 deletions client/src/main/java/com/github/coldab/client/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
public class Main {

private static final Logger LOGGER = Logger.getLogger(Main.class.getName());
private static String webSocketEndpoint = "ws://localhost:8080/ws/";
private static String restEndpoint = "http://localhost:8080";
private static String webSocketEndpoint;
private static String restEndpoint;

public static void main(String[] args) {
// Set log level
Expand All @@ -25,17 +25,20 @@ public static void main(String[] args) {
// Parse args
Options options = new Options();
options.addOption("debugwebsockets", "Set log level");
options.addOption("k", "insecure", false, "Use TLS (HTTP and WSS)");
options.addOption("h", "host", true, "Hosts ip or domain name");
try {
CommandLine commandLine = new DefaultParser().parse(options, args);
if (commandLine.hasOption("debugwebsockets")) {
MessageEncoder.enableDebugging();
}
String host = commandLine.getOptionValue("h");
if (host != null) {
webSocketEndpoint = "ws://" + host + ":8080/ws/";
restEndpoint = "http://" + host + ":8080";
String host = commandLine.getOptionValue("host");
if (host == null) {
host = "coldab.herokuapp.com";
}
boolean secure = !commandLine.hasOption("insecure");
webSocketEndpoint = (secure ? "wss" : "ws") + "://" + host + "/ws/";
restEndpoint = (secure ? "https" : "http") + "://" + host;
} catch (Exception exception) {
LOGGER.severe("Error while parsing CLI arguments:" + exception.getMessage());
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class ColdabApplication extends Application {
private RestClient restClient = new RestClient();

private static final Logger LOGGER = Logger.getLogger(ColdabApplication.class.getName());
private Stage editorStage;

/**
* open the login/register screen, if te login/regiser is successful, open the project chooser.
Expand Down Expand Up @@ -79,11 +80,11 @@ private void startEditor(Project project, Account account) {
} catch (IOException e) {
throw new IllegalStateException("Could not load FXML");
}
Stage stage = new Stage();
stage.setTitle(String.format("%s - Coldab text", project.getName()));
editorStage = new Stage();
editorStage.setTitle(String.format("%s - Coldab text", project.getName()));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
editorStage.setScene(scene);
editorStage.show();
WebSocketConnection webSocketConnection = new WebSocketConnection(
project,
restClient.getSessionId(),
Expand All @@ -94,13 +95,24 @@ private void startEditor(Project project, Account account) {
Platform.runLater(() ->
controller.afterConnectionEstablished(chatComponent, projectComponent));
return new WebSocketEndpoint(chatComponent, projectComponent);
});
stage.setOnCloseRequest(event -> {
}, this::webSocketDisconnected);
editorStage.setOnCloseRequest(event -> {
webSocketConnection.disconnect();
projectChooserStage.show();
});
}

/**
* Close editor when webSocket disconnects.
*/
private void webSocketDisconnected() {
Platform.runLater(() -> {
LOGGER.warning("Close editor due to network error");
editorStage.close();
projectChooserStage.show();
});
}

public static void main(String[] args) {
launch(args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,6 @@ private void addNodesToFileTree(TreeItem<FileTree> treeItem, DirectoryNode fileT
}
}

/**
* refresh the list of collaborators on the project.
*/
@Override
public void updateCollaborators() {

}

/**
* create a new file inside the project
*/
Expand All @@ -211,19 +203,6 @@ public void newFile(ActionEvent actionEvent) {
});
}

/**
* create a new project.
*/
@FXML
private void newProject(ActionEvent actionEvent) {
TextInputDialog dialog = new TextInputDialog("");
dialog.setTitle("New Project");
dialog.setHeaderText("New Project");
dialog.setContentText("ProjectName");
Optional<String> result = dialog.showAndWait();
// todo
}

/**
* share the current project with a diffrent user using there email address.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,6 @@ public void initialize(URL location, ResourceBundle resources) {
refreshProjects();
}

/**
* open the editor with the selected project.
*/
private void openProject(Project project) {
resultCallback.accept(project);
}

/**
* create a new project, where you are the admin.
*/
Expand Down Expand Up @@ -91,13 +84,14 @@ private class ProjectRow extends ListCell<Project> {
private final HBox box;
private final Label label;
private final Button open;
private final Button remove;

ProjectRow(ListView<Project> listView) {
label = new Label();
Pane pane = new Pane();
HBox.setHgrow(pane, Priority.ALWAYS);
open = new Button("Open");
Button remove = new Button(null, new FontIcon(FontAwesomeRegular.TRASH_ALT));
remove = new Button(null, new FontIcon(FontAwesomeRegular.TRASH_ALT));
box = new HBox(label, pane, open, remove);
}

Expand All @@ -108,9 +102,22 @@ protected void updateItem(Project project, boolean empty) {
setText(null);
label.setText(project.getName());
open.setOnAction(event -> openProject(project));
remove.setOnAction(event -> removeProject(project));
setGraphic(box);
}
}

/**
* open the editor with the selected project.
*/
private void openProject(Project project) {
resultCallback.accept(project);
}

private void removeProject(Project project) {
if (accountServer.removeProject(project)) {
projectsListView.getItems().remove(project);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class TabController implements TextFileObserver {
private SuspendableEventStream<List<PlainTextChange>> eventStream;
private static final Logger LOGGER = Logger.getLogger(TabController.class.getName());


public TabController(TextFile file, Tab tab, ProjectController projectController) {
this.file = file;
this.tab = tab;
Expand Down Expand Up @@ -74,6 +75,7 @@ private void closeTab() {

/**
* this methode gets called if the are changes made to the file.
*
* @param changes list of changes that are made to the file
*/
private void textChanged(List<PlainTextChange> changes) {
Expand Down Expand Up @@ -106,21 +108,40 @@ public void updateText(String text) {
}

/**
*this methode gets called if there are remote changes made to the file.
* Called if there are remote changes made to the file.
*/
@Override
public void remoteEdits(Collection<RemoteDeletion> deletions,
Collection<RemoteAddition> additions) {
int caret = codeArea.getCaretPosition();
eventStream.suspendWhile(() -> {
int caret = codeArea.getCaretPosition();
int position = 0;
MultiChangeBuilder<Collection<String>, String, Collection<String>> builder = codeArea
.createMultiChange();
deletions.forEach(d -> builder
.deleteTextAbsolutely(d.getStart() + 1, d.getStart() + d.getLength() + 1));
additions.forEach(a -> builder
.insertTextAbsolutely(a.getStart() + 1, a.getText()));
for (RemoteDeletion d : deletions) {
builder.deleteTextAbsolutely(d.getStart() + 1, d.getStart() + d.getLength() + 1);
int index = d.getStart();
int length = d.getLength();
if (index < caret) {
position -= length;
}
}
for (RemoteAddition a : additions) {
builder.insertTextAbsolutely(a.getStart() + 1, a.getText());
int index = a.getStart();
int length = a.getText().length();
if (index < caret) {
position += length;
}
}
builder.commit();
codeArea.moveTo(caret + 1);
caret += position;
if (caret > codeArea.getLength()) {
caret = codeArea.getLength();
} else if (caret < 0) {
caret = 0;
}
codeArea.moveTo(caret);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.List;
import java.util.stream.Stream;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;

public class ProjectComponent implements ProjectClient, ProjectController {

Expand All @@ -41,7 +43,19 @@ public void collaborators(List<Account> admins, List<Account> collaborators) {
project.getAdmins().addAll(admins);
project.getCollaborators().clear();
project.getCollaborators().addAll(collaborators);
projectObserver.updateCollaborators();
}

/**
* Display error to user.
*/
@Override
public void error(String message) {
Platform.runLater(() -> {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error");
alert.setContentText(message);
alert.show();
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,4 @@ public interface ProjectObserver {
* Files are added or removed.
*/
void updateFiles();

/**
* collaborators are added or removed.
*/
void updateCollaborators();
}
25 changes: 13 additions & 12 deletions client/src/main/java/com/github/coldab/client/rest/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
import java.util.List;
import java.util.logging.Logger;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriBuilderFactory;

Expand All @@ -24,7 +23,6 @@ public class RestClient implements AccountServer {
private String sessionId;

public RestClient() {
restTemplate.setErrorHandler(new ErrorHandler());
restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory(Main.getRestEndpoint()));
restTemplate.setInterceptors(Collections.singletonList((request, body, execution) -> {
request.getHeaders().add("Session", getSessionId());
Expand Down Expand Up @@ -67,6 +65,9 @@ public boolean createProject(String projectName) {
public Account register(Credentials credentials) {
Account account = restTemplate
.postForObject("/account/register", credentials, Account.class);
if (account == null) {
return null;
}
setSessionId(account.getSessionId());
return account;
}
Expand All @@ -75,6 +76,9 @@ public Account register(Credentials credentials) {
public Account login(Credentials credentials) {
Account account = restTemplate
.postForObject("/account/login", credentials, Account.class);
if (account == null) {
return null;
}
setSessionId(account.getSessionId());
return account;
}
Expand All @@ -85,15 +89,12 @@ public void logout(String sessionId) {
.postForEntity("/account/logout", sessionId, Boolean.TYPE);
}

private class ErrorHandler implements ResponseErrorHandler {

@Override
public void handleError(ClientHttpResponse response) {
LOGGER.severe("HTTP error");
}

@Override
public boolean hasError(ClientHttpResponse response) {
@Override
public boolean removeProject(Project project) {
try {
restTemplate.delete("/account/project/{id}", project.getId());
return true;
} catch (HttpServerErrorException e) {
return false;
}
}
Expand Down
Loading

0 comments on commit e2df393

Please sign in to comment.