33import java .io .File ;
44import java .io .IOException ;
55import java .net .URL ;
6- import java .util .ArrayList ;
7- import java .util .Arrays ;
8- import java .util .HashSet ;
9- import java .util .List ;
10- import java .util .Map ;
116import java .util .ResourceBundle ;
12- import java .util .Set ;
137
148import javafx .event .ActionEvent ;
159import javafx .fxml .FXML ;
2115import javafx .stage .Window ;
2216import nl .tudelft .lifetiles .core .util .FileUtils ;
2317import nl .tudelft .lifetiles .core .util .Message ;
24- import nl .tudelft .lifetiles .graph .controller .GraphController ;
25- import nl .tudelft .lifetiles .graph .model .Graph ;
2618import nl .tudelft .lifetiles .notification .controller .NotificationController ;
2719import nl .tudelft .lifetiles .notification .model .AbstractNotification ;
2820import nl .tudelft .lifetiles .notification .model .NotificationFactory ;
29- import nl .tudelft .lifetiles .sequence .model .Sequence ;
3021
3122/**
3223 * The controller of the menu bar.
@@ -40,6 +31,22 @@ public class MenuController extends AbstractController {
4031 * Constant annotation extension, currently as defined by client: '.txt'.
4132 */
4233 private static final String ANNOTATION_EXTENSION = ".txt" ;
34+ /**
35+ * Extension of the node file.
36+ */
37+ private static final String NODE_EXTENSION = ".node.graph" ;
38+ /**
39+ * Extension of the edge file.
40+ */
41+ private static final String EDGE_EXTENSION = ".edge.graph" ;
42+ /**
43+ * Extension of the tree file.
44+ */
45+ private static final String TREE_EXTENSION = ".nwk" ;
46+ /**
47+ * Extension of the sequence meta data file.
48+ */
49+ private static final String META_EXTENSION = ".meta" ;
4350
4451 /**
4552 * The initial x-coordinate of the window.
@@ -62,11 +69,6 @@ public class MenuController extends AbstractController {
6269 */
6370 private NotificationFactory nf ;
6471
65- /**
66- * all sequences, to reset the filters.
67- */
68- private Set <Sequence > sequences ;
69-
7072 /**
7173 * Handle action related to "Open" menu item.
7274 *
@@ -79,7 +81,7 @@ private void openAction(final ActionEvent event) {
7981 loadDataFiles ();
8082 } catch (IOException e ) {
8183 AbstractNotification notification = nf .getNotification (e );
82- shout (NotificationController .NOTIFY , notification );
84+ shout (NotificationController .NOTIFY , "" , notification );
8385 }
8486 }
8587
@@ -91,9 +93,7 @@ private void openAction(final ActionEvent event) {
9193 */
9294 @ FXML
9395 private void resetAction (final ActionEvent event ) {
94- if (sequences != null ) {
95- shout (Message .FILTERED , sequences );
96- }
96+ shout (Message .RESET , "" );
9797 }
9898
9999 /**
@@ -113,31 +113,95 @@ private void loadDataFiles() throws IOException {
113113 if (directory == null ) {
114114 return ;
115115 }
116+ loadGraph (directory );
117+ loadTree (directory );
118+ loadAnnotations (directory );
119+ loadMetaData (directory );
120+ }
116121
117- List <File > dataFiles = new ArrayList <>();
118- List <File > annotations = FileUtils .findByExtension (directory ,
119- ANNOTATION_EXTENSION );
122+ /**
123+ * Loads the graph from files in the specified directory.
124+ *
125+ * @param directory
126+ * The directory in which to locate the files.
127+ * @throws IOException
128+ * When the directory does not contain exactly one graph file
129+ * and one node file.
130+ */
131+ private void loadGraph (final File directory ) throws IOException {
132+ File nodeFile = FileUtils .getSingleFileByExtension (directory ,
133+ NODE_EXTENSION );
134+ File edgeFile = FileUtils .getSingleFileByExtension (directory ,
135+ EDGE_EXTENSION );
136+ shout (Message .OPENED , "graph" , nodeFile , edgeFile );
137+ }
120138
121- List <String > exts = Arrays .asList (".node.graph" , ".edge.graph" , ".nwk" );
122- for (String ext : exts ) {
123- List <File > hits = FileUtils .findByExtension (directory , ext );
124- if (hits .size () != 1 ) {
125- throw new IOException ("Expected 1 " + ext + " file intead of "
126- + hits .size ());
127- }
139+ /**
140+ * Loads the sequence meta-data file in the specified directory.
141+ *
142+ * @param directory
143+ * The directory in which to locate the files.
144+ * @throws IOException
145+ * When the directory does not contain exactly one graph file
146+ * and one node file.
147+ */
148+ private void loadMetaData (final File directory ) throws IOException {
149+ File metaDataFile = loadOrWarn (directory , META_EXTENSION );
150+ if (metaDataFile != null ) {
151+ shout (Message .OPENED , "meta" , metaDataFile );
152+ }
153+ }
154+
155+ /**
156+ * Loads the annotations from a file in the specified directory.
157+ *
158+ * @param directory
159+ * The directory from which to load annotations.
160+ */
161+ private void loadAnnotations (final File directory ) {
162+ File annotationFile = loadOrWarn (directory , ANNOTATION_EXTENSION );
163+ if (annotationFile != null ) {
164+ shout (Message .OPENED , "annotations" , annotationFile );
165+ }
166+ }
128167
129- dataFiles .add (hits .get (0 ));
168+ /**
169+ * Loads the tree from a file in the specified directory.
170+ *
171+ * @param directory
172+ * The directory in which to search for the tree file.
173+ */
174+ private void loadTree (final File directory ) {
175+ File treeFile = loadOrWarn (directory , TREE_EXTENSION );
176+ if (treeFile != null ) {
177+ shout (Message .OPENED , "tree" , treeFile );
130178 }
179+ }
131180
132- shout (Message .OPENED , dataFiles .get (0 ), dataFiles .get (1 ),
133- dataFiles .get (2 ));
134- if (annotations == null || annotations .isEmpty ()) {
135- shout (NotificationController .NOTIFY , nf .getNotification (
136- "Annotation file (" + ANNOTATION_EXTENSION
137- + ") can't be found" , NotificationFactory .WARNING ));
138- } else {
139- shout (GraphController .ANNOTATIONS , annotations .get (0 ));
181+ /**
182+ * Loads a file from the specified directory, with the specified extension,
183+ * and give a warning.
184+ *
185+ * @param directory
186+ * The directory in which to search for the file.
187+ * @param extension
188+ * The extension to search for.
189+ * @return The found file.
190+ */
191+ private File loadOrWarn (final File directory , final String extension ) {
192+ try {
193+ File file = FileUtils
194+ .getSingleFileByExtension (directory , extension );
195+ return file ;
196+ } catch (IOException e ) {
197+ shout (NotificationController .NOTIFY ,
198+ "" ,
199+ nf .getNotification (
200+ extension
201+ + " file could not be found or multiple files found " ,
202+ NotificationFactory .WARNING ));
140203 }
204+ return null ;
141205 }
142206
143207 /**
@@ -152,7 +216,7 @@ private void insertAnnotationsAction(final ActionEvent event) {
152216 loadAnnotationsFile ();
153217 } catch (IOException e ) {
154218 AbstractNotification notification = nf .getNotification (e );
155- shout (NotificationController .NOTIFY , notification );
219+ shout (NotificationController .NOTIFY , "" , notification );
156220 }
157221 }
158222
@@ -175,7 +239,7 @@ private void loadAnnotationsFile() throws IOException {
175239 return ;
176240 }
177241
178- shout (GraphController . ANNOTATIONS , file );
242+ shout (Message . OPENED , "annotations" , file );
179243 }
180244
181245 /**
@@ -210,15 +274,5 @@ public final void initialize(final URL location,
210274 final ResourceBundle resources ) {
211275 addDraggableNode (menuBar );
212276 nf = new NotificationFactory ();
213- // listen to loaded to get the sequence list
214- listen (Message .LOADED ,
215- (controller , args ) -> {
216- if (controller instanceof GraphController ) {
217- assert args [0 ] instanceof Graph ;
218- assert (args [1 ] instanceof Map <?, ?>);
219- Map <String , Sequence > sequenceMap = (Map <String , Sequence >) args [1 ];
220- sequences = new HashSet <Sequence >(sequenceMap .values ());
221- }
222- });
223277 }
224278}
0 commit comments