2015-10-20 14:26:58 +00:00
|
|
|
package com.coder.client.gui.editor;
|
2015-10-20 06:56:14 +00:00
|
|
|
|
2015-10-20 14:26:58 +00:00
|
|
|
import java.io.IOException;
|
2015-10-20 06:56:14 +00:00
|
|
|
import java.net.URL;
|
|
|
|
|
import java.util.ArrayList;
|
2015-11-13 11:58:23 +01:00
|
|
|
import java.util.Enumeration;
|
2015-11-11 16:13:01 +01:00
|
|
|
import java.util.List;
|
2015-10-20 06:56:14 +00:00
|
|
|
import java.util.ResourceBundle;
|
2015-11-13 11:58:23 +01:00
|
|
|
import java.util.logging.Level;
|
2015-11-11 16:13:01 +01:00
|
|
|
import java.util.logging.Logger;
|
2015-10-20 06:56:14 +00:00
|
|
|
|
|
|
|
|
import org.controlsfx.control.PropertySheet;
|
|
|
|
|
import org.controlsfx.property.editor.PropertyEditor;
|
|
|
|
|
|
2015-11-11 16:13:01 +01:00
|
|
|
import zutil.log.LogUtil;
|
|
|
|
|
|
|
|
|
|
import com.coder.client.CoderClient;
|
2015-10-20 14:26:58 +00:00
|
|
|
import com.coder.client.gui.GuiWindow;
|
2015-11-13 15:44:47 +01:00
|
|
|
import com.coder.client.project.OpenProjectEventHandler;
|
2015-10-20 06:56:14 +00:00
|
|
|
import com.coder.client.property.CheckBoxProperty;
|
|
|
|
|
import com.coder.client.property.CoderClientProperty;
|
|
|
|
|
import com.coder.client.property.ComboBoxProperty;
|
2015-11-11 16:13:01 +01:00
|
|
|
import com.coder.client.session.ProjectRspMsgListener;
|
|
|
|
|
import com.coder.client.session.ProjectTypeRspMsgListener;
|
2015-11-13 11:58:23 +01:00
|
|
|
import com.coder.server.message.CoderMessage;
|
2015-11-13 13:25:36 +01:00
|
|
|
import com.coder.server.message.ProjectReqMsg;
|
2015-11-11 16:13:01 +01:00
|
|
|
import com.coder.server.message.ProjectRspMsg;
|
2015-11-13 11:58:23 +01:00
|
|
|
import com.coder.server.message.ProjectTypeReqMsg;
|
2015-11-11 16:13:01 +01:00
|
|
|
import com.coder.server.message.ProjectTypeRspMsg;
|
2015-10-20 06:56:14 +00:00
|
|
|
|
|
|
|
|
import javafx.beans.value.ChangeListener;
|
|
|
|
|
import javafx.beans.value.ObservableValue;
|
|
|
|
|
import javafx.event.ActionEvent;
|
|
|
|
|
import javafx.fxml.FXML;
|
|
|
|
|
import javafx.scene.control.Button;
|
|
|
|
|
import javafx.scene.control.TextArea;
|
|
|
|
|
import javafx.scene.control.TreeItem;
|
|
|
|
|
import javafx.scene.control.TreeView;
|
|
|
|
|
import javafx.util.Callback;
|
|
|
|
|
|
2015-10-20 14:26:58 +00:00
|
|
|
public class EditorWindow extends GuiWindow {
|
2015-11-11 16:13:01 +01:00
|
|
|
public static final Logger logger = LogUtil.getLogger();
|
|
|
|
|
private CoderClient client;
|
|
|
|
|
|
2015-10-20 06:56:14 +00:00
|
|
|
@FXML private TreeView<String> fileTreeView;
|
|
|
|
|
@FXML private TextArea editTextArea;
|
|
|
|
|
@FXML private PropertySheet propertySheet;
|
|
|
|
|
@FXML private Button compileButton;
|
|
|
|
|
@FXML private Button runButton;
|
2015-11-05 14:42:40 +01:00
|
|
|
@FXML private Button exitButton;
|
|
|
|
|
@FXML private Button changeProjectButton;
|
2015-10-20 06:56:14 +00:00
|
|
|
|
2015-11-13 11:58:23 +01:00
|
|
|
private String projectType = null;
|
|
|
|
|
|
|
|
|
|
public EditorWindow(final CoderClient client) throws IOException{
|
2015-10-20 14:26:58 +00:00
|
|
|
super(EditorWindow.class.getResource("EditorWindow.fxml"));
|
2015-11-11 16:13:01 +01:00
|
|
|
this.client = client;
|
2015-11-13 13:25:36 +01:00
|
|
|
|
2015-11-11 16:13:01 +01:00
|
|
|
client.getSessionHandler().addMessageListener(new ProjectRspMsgListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void messageReceived(final ProjectRspMsg msg) {
|
2015-11-13 11:58:23 +01:00
|
|
|
try{
|
|
|
|
|
logger.fine("a ProjectRspMsg received");
|
|
|
|
|
if(msg.error != null){
|
|
|
|
|
logger.severe("Server responded on the project request with the following error message: " + msg.error);
|
|
|
|
|
client.getProjectHandler().setProject(null);
|
|
|
|
|
return;
|
|
|
|
|
}else{
|
|
|
|
|
client.getProjectHandler().setProject(msg.name);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-13 15:44:47 +01:00
|
|
|
logger.info("loading project \""+msg.name+"\"");
|
|
|
|
|
client.showOnStage(EditorWindow.this); //TODO: show "loading project" popup instead
|
|
|
|
|
|
2015-11-13 11:58:23 +01:00
|
|
|
//handle name and description
|
|
|
|
|
fileTreeView.getRoot().setValue(msg.name); //set file tree root name to the project name
|
|
|
|
|
String projectDescription = msg.description;
|
|
|
|
|
|
|
|
|
|
//handle config
|
|
|
|
|
propertySheet.getItems().clear();
|
2015-11-13 16:12:33 +01:00
|
|
|
if(msg.config != null){
|
|
|
|
|
logger.fine("the project has a configuration - populating property sheet");
|
|
|
|
|
Enumeration<String> propertyNames = (Enumeration<String>) msg.config.propertyNames();
|
|
|
|
|
while(propertyNames.hasMoreElements()){ //populate propertySheet with all config elements
|
|
|
|
|
String propertyName = propertyNames.nextElement();
|
|
|
|
|
String propertyValue = msg.config.getProperty(propertyName);
|
|
|
|
|
ComboBoxProperty comboProperty = new ComboBoxProperty(propertyName, propertyValue, null);
|
|
|
|
|
propertySheet.getItems().add(comboProperty);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//request alternative values for config of this project type
|
|
|
|
|
projectType = msg.type;
|
|
|
|
|
sendProjectTypeReqMsg(projectType);
|
|
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
logger.fine("the project has no configuration");
|
2015-11-13 11:58:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//handle file list
|
|
|
|
|
List<String> fileList = msg.fileList;
|
|
|
|
|
fileTreeView.getRoot().getChildren().clear();
|
|
|
|
|
for(String filePath : fileList){
|
2015-11-13 15:44:47 +01:00
|
|
|
logger.finer("adding file \""+filePath+"\" to the file tree");
|
2015-11-13 11:58:23 +01:00
|
|
|
if(filePath.endsWith("/")){
|
|
|
|
|
logger.warning("file: \"" + filePath + "\" in file list is a directory and not a file. Currently not supported. Ignoring entry");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if(filePath.startsWith("/")){
|
|
|
|
|
filePath = filePath.substring(1, filePath.length());
|
|
|
|
|
}
|
|
|
|
|
TreeItem<String> tmpParent = fileTreeView.getRoot();
|
2015-11-13 15:44:47 +01:00
|
|
|
String[] filePathSpilt = filePath.split("/");
|
|
|
|
|
for(int i = 0; i < filePathSpilt.length; ++i){
|
|
|
|
|
if(i < filePathSpilt.length-1){
|
|
|
|
|
String directoryName = filePathSpilt[i];
|
|
|
|
|
if(!tmpParent.getChildren().contains(directoryName)){
|
|
|
|
|
if(tmpParent == fileTreeView.getRoot()){
|
|
|
|
|
logger.finer("adding directory \""+directoryName+"\" to directory \"/\"");
|
|
|
|
|
}else{
|
|
|
|
|
logger.finer("adding directory \""+directoryName+"\" to directory \""+tmpParent+"\"");
|
|
|
|
|
}
|
|
|
|
|
TreeItem<String> tmpChild = new TreeItem<String>(directoryName);
|
|
|
|
|
tmpParent.getChildren().add(tmpChild);
|
|
|
|
|
tmpParent = tmpChild;
|
|
|
|
|
}else{
|
|
|
|
|
int index = tmpParent.getChildren().indexOf(directoryName);
|
|
|
|
|
tmpParent = tmpParent.getChildren().get(index);
|
|
|
|
|
}
|
2015-11-13 11:58:23 +01:00
|
|
|
}else{
|
2015-11-13 15:44:47 +01:00
|
|
|
String fileName = filePathSpilt[i];
|
|
|
|
|
if(!tmpParent.getChildren().contains(fileName)){
|
|
|
|
|
if(tmpParent == fileTreeView.getRoot()){
|
|
|
|
|
logger.finer("adding file \""+fileName+"\" to directory \"/\"");
|
|
|
|
|
}else{
|
|
|
|
|
logger.finer("adding file \""+fileName+"\" to directory \""+tmpParent+"\"");
|
|
|
|
|
}
|
|
|
|
|
TreeItem<String> tmpChild = new TreeItem<String>(fileName);
|
|
|
|
|
tmpParent.getChildren().add(tmpChild);
|
|
|
|
|
tmpParent = tmpChild;
|
|
|
|
|
}else{
|
|
|
|
|
int index = tmpParent.getChildren().indexOf(fileName);
|
|
|
|
|
tmpParent = tmpParent.getChildren().get(index);
|
|
|
|
|
}
|
2015-11-13 11:58:23 +01:00
|
|
|
}
|
|
|
|
|
}
|
2015-11-13 15:44:47 +01:00
|
|
|
|
2015-11-13 11:58:23 +01:00
|
|
|
}
|
|
|
|
|
}catch(Exception e){
|
2015-11-13 15:44:47 +01:00
|
|
|
logger.log(Level.SEVERE, "exception while load the project", e);
|
|
|
|
|
client.getProjectHandler().triggerOpenProjectFailureEvent("ERROR: failed to loading project");
|
2015-11-11 16:13:01 +01:00
|
|
|
}
|
2015-11-13 15:44:47 +01:00
|
|
|
|
|
|
|
|
client.showOnStage(EditorWindow.this);
|
2015-11-11 16:13:01 +01:00
|
|
|
}
|
|
|
|
|
});
|
2015-11-13 13:25:36 +01:00
|
|
|
|
2015-11-11 16:13:01 +01:00
|
|
|
client.getSessionHandler().addMessageListener(new ProjectTypeRspMsgListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void messageReceived(ProjectTypeRspMsg msg) {
|
2015-11-13 11:58:23 +01:00
|
|
|
for(String type : msg.keySet()){
|
|
|
|
|
if(type.equals(projectType)){ //the current project type matches the project type in the message
|
|
|
|
|
//TODO: handle type specific configurations
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-11 16:13:01 +01:00
|
|
|
}
|
|
|
|
|
});
|
2015-11-13 13:25:36 +01:00
|
|
|
|
2015-11-13 15:44:47 +01:00
|
|
|
client.getProjectHandler().addprojectEventHandler(new OpenProjectEventHandler() {
|
2015-11-13 13:25:36 +01:00
|
|
|
@Override
|
|
|
|
|
public void openProject(String projectName) {
|
|
|
|
|
CoderMessage msg = new CoderMessage();
|
|
|
|
|
msg.ProjectReq = new ProjectReqMsg();
|
|
|
|
|
msg.ProjectReq.name = projectName;
|
|
|
|
|
client.getSessionHandler().sendMessage(msg);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2015-10-20 06:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
|
2015-11-13 11:58:23 +01:00
|
|
|
setErrorMessage("");
|
2015-10-20 06:56:14 +00:00
|
|
|
setupPropertySheet();
|
|
|
|
|
setupFileTreeView();
|
2015-10-20 14:26:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2015-11-13 13:25:36 +01:00
|
|
|
public void willShow(){
|
2015-11-11 16:13:01 +01:00
|
|
|
|
2015-10-20 06:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@FXML
|
2015-11-11 16:13:01 +01:00
|
|
|
protected void run(ActionEvent event){
|
|
|
|
|
//TODO
|
2015-10-20 06:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@FXML
|
2015-11-11 16:13:01 +01:00
|
|
|
protected void compile(ActionEvent event){
|
|
|
|
|
//TODO
|
2015-10-20 06:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
2015-10-23 13:44:11 +00:00
|
|
|
@FXML
|
2015-11-11 16:13:01 +01:00
|
|
|
protected void changeProject(ActionEvent event){
|
|
|
|
|
client.getProjectHandler().setProject(null);
|
2015-11-13 15:44:47 +01:00
|
|
|
client.getProjectHandler().triggerSelectProjectEvent();
|
2015-10-23 13:44:11 +00:00
|
|
|
}
|
|
|
|
|
|
2015-11-05 14:42:40 +01:00
|
|
|
@FXML
|
2015-11-11 16:13:01 +01:00
|
|
|
protected void exit(ActionEvent event){
|
|
|
|
|
client.exit();
|
2015-11-05 14:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
2015-11-13 11:58:23 +01:00
|
|
|
private void sendProjectTypeReqMsg(String type){
|
|
|
|
|
CoderMessage msg = new CoderMessage();
|
|
|
|
|
msg.ProjectTypeReq = new ProjectTypeReqMsg();
|
|
|
|
|
msg.ProjectTypeReq.type = type;
|
|
|
|
|
client.getSessionHandler().sendMessage(msg);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-20 06:56:14 +00:00
|
|
|
private void setupFileTreeView(){
|
|
|
|
|
fileTreeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem<String>>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void changed(ObservableValue<? extends TreeItem<String>> item, TreeItem<String> oldValue, TreeItem<String> newValue) {
|
|
|
|
|
if(newValue != fileTreeView.getRoot()){
|
|
|
|
|
System.out.println("INFO: item " + newValue + " selected");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2015-11-13 15:44:47 +01:00
|
|
|
TreeItem<String> root = new TreeItem<String>("/");
|
2015-10-20 06:56:14 +00:00
|
|
|
root.setExpanded(true);
|
|
|
|
|
fileTreeView.setRoot(root);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setupPropertySheet(){
|
|
|
|
|
//handle custom ProperySheet.Item's editor
|
|
|
|
|
propertySheet.setPropertyEditorFactory(new Callback<PropertySheet.Item, PropertyEditor<?>>(){
|
|
|
|
|
@Override
|
|
|
|
|
public PropertyEditor<?> call(PropertySheet.Item param) {
|
|
|
|
|
//only support internal types
|
|
|
|
|
if(param instanceof CoderClientProperty){
|
|
|
|
|
return ((CoderClientProperty<?>)param).getEditor();
|
|
|
|
|
}
|
|
|
|
|
//not a internal property type
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
propertySheet.getItems().clear();
|
|
|
|
|
|
|
|
|
|
ArrayList<String> boards = new ArrayList<String>();
|
|
|
|
|
boards.add("UNO");
|
|
|
|
|
boards.add("Mega");
|
|
|
|
|
ComboBoxProperty p1 = new ComboBoxProperty("Port", null, boards);
|
|
|
|
|
propertySheet.getItems().add(p1);
|
|
|
|
|
|
|
|
|
|
CheckBoxProperty p2 = new CheckBoxProperty("Melt?", false);
|
|
|
|
|
propertySheet.getItems().add(p2);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-21 16:13:45 +00:00
|
|
|
@Override
|
2015-11-13 13:25:36 +01:00
|
|
|
public String getTitle() {
|
2015-10-21 16:13:45 +00:00
|
|
|
return "Coder Client";
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 16:13:01 +01:00
|
|
|
private void setErrorMessage(String msg) {
|
2015-10-23 13:44:11 +00:00
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 16:13:01 +01:00
|
|
|
@Override
|
2015-11-13 13:25:36 +01:00
|
|
|
public String getDescriptiveName() {
|
2015-11-11 16:13:01 +01:00
|
|
|
return "Editor Window";
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-20 06:56:14 +00:00
|
|
|
}
|