added ant build file -> build/build.xml
[jalview-fx.git] / src / jalviewfx / controllers / Desktop.java
1 package jalviewfx.controllers;
2
3 import jalview.bin.Cache;
4 import jalview.datamodel.AlignmentI;
5 import jalview.gui.AlignFrame;
6 import jalview.gui.AlignmentPanel;
7 import jalview.io.AppletFormatAdapter;
8 import jalview.util.MessageManager;
9 import jalviewfx.controllers.engine.ControlledScreen;
10 import jalviewfx.controllers.engine.ScreensController;
11
12 import java.awt.print.PrinterException;
13 import java.io.IOException;
14 import java.net.URL;
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.ResourceBundle;
18
19 import javafx.application.Platform;
20 import javafx.embed.swing.SwingNode;
21 import javafx.fxml.FXML;
22 import javafx.fxml.Initializable;
23 import javafx.scene.Node;
24 import javafx.scene.control.TreeCell;
25 import javafx.scene.control.TreeItem;
26 import javafx.scene.control.TreeView;
27 import javafx.scene.image.Image;
28 import javafx.scene.image.ImageView;
29 import javafx.util.Callback;
30
31 import javax.swing.JComponent;
32
33 import application.Project;
34 import application.TextFieldTreeCellImpl;
35
36 public class Desktop implements Initializable, ControlledScreen {
37         @FXML
38         private MainMenuController mainMenuController;
39         @FXML
40         private AlignmentViewController alignmentViewController;
41         @FXML
42         private TreeView<String> projectExplorer;
43         // @FXML private Canvas allignmentCanvas;
44         @FXML
45         private SwingNode jalviewNode;
46
47         private final Node rootIcon = new ImageView(new Image(getClass().getResourceAsStream("/images/Jalview_Logo_small.png")));
48         private final Image depIcon = new Image(getClass().getResourceAsStream("/images/Jalview_Logo_small.png"));
49         List<Project> projects = Arrays.<Project> asList(new Project("Spinach Ferodoxin", "exampleFile_2_3"), new Project("FE2S2 Representation", "exampleFile_2_3"), new Project("Mafft Alignment Ordering", "exampleFile_2_3"), new Project("Phosporylation SP", "exampleFile_2_3"), new Project("Mafft example", "exampleFile_2_3"), new Project("FS3ES", "globins"), new Project("Mafft2", "globins"), new Project("Judy", "plantfdx"), new Project("Gregory", "plantfdx"), new Project("Jacob", "plantfdx"), new Project("Isabella", "globins"));
50
51         ScreensController myController;
52
53         @Override
54         public void setScreenParent(ScreensController screenParent) {
55                 myController = screenParent;
56         }
57
58         @Override
59         public void initialize(URL paramURL, ResourceBundle paramResourceBundle) {
60                 loadProjectExplorer();
61                 try {
62                         loadJalview();
63                 } catch (IOException | PrinterException e) {
64                         e.printStackTrace();
65                 }
66         }
67         final static String TEST_DATA = ">FER_CAPAA/13-56 Ferredoxin\nJRFIJOIERFEOIJOIREFOIJETBGNJEWRPOKPWEOIJWEW----X-\n"
68                         + ">FER_CAPAN/13-51 Ferredoxin, chloroplast precursor\nWFEXLFWEOPKOWEFPO--EWF-FWE-WEF-WFW-BRWGEWF---FWE-\n"
69                         + ">FER1_SOLLC/13-56 Ferredoxin-1, chloroplast precursor\n-WEFWEFWFW3FLIKFOILKWFEIOEWFJIJWEFLIKWE-EWF--WEF-\n";
70         
71         AlignmentI alignment;
72         AlignmentPanel parentPanel;
73         AlignFrame af;
74
75         public void loadJalview() throws IOException, PrinterException {
76                 System.out.println("--------------> Loading jalview");
77                 alignment = new jalview.io.FormatAdapter().readFile(TEST_DATA, AppletFormatAdapter.PASTE, "FASTA");
78                 af = new AlignFrame(alignment, AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
79
80                 // parentPanel = new AlignmentPanel(af, af.getViewport());
81                 // System.out.println("alignment Canvas : "+ allignmentCanvas);
82                 // FXGraphics2D g = new
83                 // FXGraphics2D(allignmentCanvas.getGraphicsContext2D());
84                 createSwingContent(jalviewNode, af.alignPanel);
85                 // parentPanel.paintComponent(g);
86 //              startDesktop();
87
88         }
89
90         private void startDesktop() {
91                 Platform.runLater(new Runnable() {
92                         @Override
93                         public void run() {
94                                 try {
95                                     Cache.initLogger();
96                                         jalview.gui.Desktop desktop = new jalview.gui.Desktop();
97                                         desktop.setInBatchMode(true); // indicate we are starting up
98                                         desktop.setVisible(true);
99                                         desktop.startServiceDiscovery();
100
101                                         jalview.gui.Desktop.addInternalFrame(af, MessageManager.formatMessage("label.input_cut_paste_params", new String[] { "paste" }), AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
102                                         af.statusBar.setText(MessageManager.getString("label.successfully_pasted_alignment_file"));
103
104                                         //
105 //                                      af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN", true));
106                                 } catch (Exception ex) {
107                                         ex.printStackTrace();
108                                 }
109
110                         }
111                 });
112         }
113
114         private void createSwingContent(final SwingNode swingNode, final JComponent swingComp) {
115                 Platform.runLater(new Runnable() {
116                         @Override
117                         public void run() {
118                                 swingNode.setContent(swingComp);
119                         }
120                 });
121         }
122
123         public void loadProjectExplorer() {
124                 rootIcon.setScaleX(.5);
125                 rootIcon.setScaleY(.5);
126                 TreeItem<String> rootNode = new TreeItem<String>("Project Explorer", rootIcon);
127                 projectExplorer.setRoot(rootNode);
128                 projectExplorer.setEditable(true);
129                 rootNode.setExpanded(true);
130                 for (Project project : projects) {
131                         TreeItem<String> empLeaf = new TreeItem<String>(project.getName());
132                         boolean found = false;
133                         for (TreeItem<String> typeNode : rootNode.getChildren()) {
134                                 if (typeNode.getValue().contentEquals(project.getType())) {
135                                         typeNode.getChildren().add(empLeaf);
136                                         found = true;
137                                         break;
138                                 }
139                         }
140                         ImageView imgView = new ImageView(depIcon);
141                         imgView.setScaleX(.5);
142                         imgView.setScaleY(.5);
143                         if (!found) {
144                                 TreeItem<String> typeNode = new TreeItem<String>(project.getType(), imgView);
145                                 rootNode.getChildren().add(typeNode);
146                                 typeNode.getChildren().add(empLeaf);
147                         }
148                 }
149
150                 projectExplorer.setCellFactory(new Callback<TreeView<String>, TreeCell<String>>() {
151                         @Override
152                         public TreeCell<String> call(TreeView<String> p) {
153                                 return new TextFieldTreeCellImpl();
154                         }
155                 });
156         }
157
158 }