Question: You are not allowed to use ArrayList, Vector or any other Java API Data Structure classes to implement this assignment except where noted. You may
You are not allowed to use ArrayList, Vector or any other Java API Data Structure classes to implement this assignment except where noted.
You may use Scanner, InputStreamReader, or any other class that you wish for keyboard input.
Trees are a fairly common woodland species. They are found throughout the world, sometimes even in places you wouldn't expect them. One of these places happens to be GUIs for applications, including our very own JavaFX. When we design applications using JavaFX or any other modern application framework, these applications must often work on different screens with varying sizes and resolutions. Specifying absolute dimensions for these applications in terms of pixels would make scaling the applications to different size screens be very awkward. Therefore, most frameworks (including Android, JavaFX, and HTML - with the DOM) use a tree structure to represent their components. In this tree structure, there are two kinds of elements. In JavaFx, these are called controls and containers. Controls are used to interact with the user (by getting input and/or displaying output). Things like text boxes, buttons, images, and videos are usually represented as controls in the JavaFX framework. Containers control how the controls will be displayed. In the example below, HBox is used to specify that a certain list of controls will be displayed inline horizontally with regard to each other, and VBox is used to specify that a certain list of controls will be displayed inline vertically in line with each other. Some other JavaFX containers include the Grid (however, this can be recreated by using a set of VBoxes and HBoxes) and the AnchorPane which is used to attach all other controls and containers to a window.
In this homework we will be creating a tree representation of a JavaFX GUI similar to that of SceneBuilder (which you may use for your extra credits). We will only consider a restricted subset of JavaFX components: Buttons, TextAreas (text input), Labels (text output), HBoxes and VBoxes. Our buttons and labels will have text, and our text areas will have prompt text (that gray text that goes away once you start typing). You must be able to read a GUI input file, modify the tree, print the results, and write to a new file. For extra credit, you may choose to export the file as valid FXML that can be opened in SceneBuilder.
We have included an image representation for the GUI File we represent in the Sample I/O for your reference. Note: the anchor pane is the root of the tree. When you read from the file, you can always assume that the parent of a node is listed before a child. If the software is started without loading a file, an empty tree with just an AnchorPane is created. When you save the file, you should save it in preorder.
Here is a sample text file (format: position in tree, space, component type, a space character, text of component (if applicable), newline).:
0 AnchorPane
0-0 VBox
0-0-0 HBox
0-0-0-0 Label DogFolio, the Dog Name Management App
0-0-0-1 Button Create New Dog
0-0-0-2 Button Save Dog
0-0-0-3 Button Quit And Get A Cat
0-0-1 TextArea Fido... Or Nancy?
0-0-2 HBox
0-0-2-0 Button Get a Pro Bone-o Dog Lawyer
0-0-2-1 Button Random Generate A Dog Name
0-0-2-2 Button Just Make It Fido
0-0-2-3 VBox
0-0-2-3-0 Button Bark
0-0-2-3-1 Button Roll
0-0-2-3-2 Button Treat!
Here is what this file looks like in Scene Builder:
NOTE: All exceptions explicitly thrown in Required Classes except for IllegalArgumentException are custom exceptions that need to be made by you.
Required Classes
ComponentType
Write a fully documented enum called ComponentType. It should contain the values Button, Label, TextArea, HBox, VBox, and AnchorPane. This will help your program operate as desired (as Containers have no text, and Controls have no children). A toString() method may be helpful.
FXTreeNode
Write a fully documented class called FXTreeNode which holds the type of component being represented, an array of children (null if this will be a Control), and string for the text (null if this is a Container). Be sure to include all getters and setters. You may find it helpful to write helper methods, especially to check input before adding a child to the tree. Defining custom exceptions for actions like trying to add too many children to a node, or adding a child in an invalid position may also be desirable.
private String text
private ComponentType type
private FXTreeNode parent
private FXTreeNode[] children //Just like in hw 1, there should be no holes in the array.
final int maxChildren=10 //A full node exception may be desirable.
FXComponentTree
Write a fully documented class called FXComponentTree which will serve as the tree manager for your FXComponentTree. This must hold references into a tree (the root and cursor), as well as be able to generate and save the tree to and from a file. Again, defining some custom exceptions may be helpful for code readability.
private FXTreeNode root
private FXTreeNode cursor
public void cursorToRoot()
Sets the cursor to the root of the FXComponentTree
public void deleteChild(int index)
Removes the child at the specified index of the FXComponentTree, as well as all of its children.
public void addChild(int index, FXTreeNode node)
Adds the given node to the corresponding index of the children array.
Should throw an exception if adding the node at the specified index makes a hole in the array.
public void setTextAtCursor(String text)
Sets the current nodes text to the specified text.
public void cursorToChild(int index)
Moves the cursor to the child node of the of the cursor corresponding to the specified index.
public void cursorToParent()
Moves the cursor to the parent of the current node.
public static FXComponentTree readFromFile(String filename)
Generates the FXComponentTree based on the file name that is passed in.
public static void writeToFile(FXComponentTree tree)
Generates a text file that reflects the structure of the FXComponentTree.
The format of the tree of the file should match the format of the input file.
public static void exportToFXML(FXComponentTree tree) // Extra Credit
Postconditions:
A valid FXML file that can be opened in SceneBuilder has been created.
FXGuiMaker
Write a fully-documented class named FXGuiMaker that takes in a text file, generates a FXComponentTree and provides an interface for a user to manipulate the tree. Please see the UI required functions for the required functionality
public static void main(String[] args)
The main method runs a menu driven application which first creates an FXComponentTree based on the passed in file and then prompts the user for a menu command selecting the operation. The required information is then requested from the user based on the selected operation. You can find the list of menu options in the UI required functions section.
FXComponentTree tree;
Note on File I/O:
It is possible to read from a file using Scanner, which you should already be familiar with. Here's an example: https://stackoverflow.com/questions/13185727/reading-a-txt-file-using-scanner-class-in-java. To write to a file, you may use printwriter, with an example here: https://stackoverflow.com/questions/25298582/how-to-use-printwriter-to-create-and-write-to-a-file. Using next() is the easiest way to get the space separated position in the tree and component type, and then nextLine() will extract the text for the component.
Note on Exceptions: all exceptions should be handled gracefully - they should be caught in the main, and the user should be notified by a nice printout. Your messages should clearly indicate what the problem is (bad index, full list, negative number, etc.). The program should continue to run normally after an exception is encountered. We will not be checking Input Mismatch cases.
General Recommendations
You can feel free to add extra methods and variables if you need.
UI Required Functions
L- Load from file
P- Print
C- Cursor to child (index number)
A- Add child (index, type, prompt for text)
U- Cursor up (to parent)
E- Edit Text
D- Delete child (index number)
R- Cursor to root
S- Save to Text File
X- Export to FXML file (Extra Credit)
Q - Quit
Note: please make sure that the menu is NOT case sensitive (so selecting A should be the same as selecting a).
Program Sample
Welcome to counterfeit SceneBuilder.
Menu:
L) Load from file
P) Print tree
C) Move cursor to a child node
R) Move cursor to root
A) Add a child
E) Edit text of cursor
D) Delete child
S) Save to file
X) Export FXML //Works the same as save, extra credit
Q) Quit
Please select an option: L
Please enter filename: ashrubbery.txt
ashrubbery.txt not found.
Please select an option: L
Please enter filename: sampletree.txt
sampletree.txt loaded
Please select an option: p
==>AnchorPane
--+VBox
+--HBox
+--Label: DogFolio, the Dog Name Management App
+--Button: Create New Dog
+--Button: Save Dog
+--Button: Quit And Get A Cat
+--TextArea: Fido... Or Nancy?
+--HBox
+--Button: Get a Pro Bone-o Dog Lawyer
+--Button: Random Generate A Dog Name
+--Button: Just Make It Fido
+--VBox
+--Button: Bark
+--Button: Roll
+--Button: Treat!
Please select an option: C
Please enter number of child (starting with 1): 1 //Note: root can only have one child
Cursor Moved to VBox.
Please select an option: C
Please enter number of child (starting with 1): 3
Cursor Moved to HBox.
Please select an option: C
Please enter number of child (starting with 1): 3
Cursor Moved to Button: Just Make It Fido.
Please select an option: E
Please enter new text: Make it Ruff Randy Retriever
Text Edited.
Please select an option: p
AnchorPane
--+VBox
+--HBox
+--Label: DogFolio, the Dog Name Management App
+--Button: Create New Dog
+--Button: Save Dog
+--Button: Quit And Get A Cat
+--TextArea: Fido... Or Nancy?
+--HBox
+--Button: Get a Pro Bone-o Dog Lawyer
+--Button: Random Generate A Dog Name
==>Button: Make it Ruff Randy Retriever
+--VBox
+--Button: Bark
+--Button: Roll
+--Button: Treat!
Please select an option: U
Cursor Moved to HBox.
Please select an option: A
Select component type (H - HBox, V - VBox, T - TextArea, B - Button, L - Label): B
Please enter text: FEED SPIKE CHOCOLATE!?!?
Please enter an index: 2
Please select an option: p
AnchorPane
--+VBox
+--HBox
+--Label: DogFolio, the Dog Name Management App
+--Button: Create New Dog
+--Button: Save Dog
+--Button: Quit And Get A Cat
+--TextArea: Fido... Or Nancy?
+--HBox
+--Button: Get a Pro Bone-o Dog Lawyer
==>FEED SPIKE CHOCOLATE!?!?
+--Button: Random Generate A Dog Name
+--Button: Make it Ruff Randy Retriever
+--VBox
+--Button: Bark
+--Button: Roll
+--Button: Treat!
Please select an option: R
Cursor is at root.
Please select an option: C
Please enter number of child (starting with 1): 1
Cursor Moved to VBox.
Please select an option: R
Please enter number of child (starting with 1): 1
HBox removed.
Please select an option: p
AnchorPane
==>VBox
+--TextArea: Fido... Or Nancy?
+--HBox
+--Button: Get a Pro Bone-o Dog Lawyer
+--Button: Random Generate A Dog Name
+--Button: Just Make It Fido
+--VBox
+--Button: Bark
+--Button: Roll
+--Button: Treat!
Please select an option: S
Please enter a filename: theGivingTree.txt // A good way to test your program is to load the saved file
File saved.
Please select an option: Q
Make like a tree and leave!
hw5.fxml File Edit View Insert Modify Arrange Preview Window Help Library a *' | No Selection Inspector Custom Properties Containers No Selection Controls CKI Button CheckBox ChoiceBox ColorPicker Menu Miscellaneous Shapes Charts 3D DogFolio, the Dog Name Management App Create New Dog Save Dog Load Dog Quit and Get A Cat Document Fido... Or Nancy? Hierarchy - El AnchorPane Label DogFolio, the Dog Name Management App Button Create New Dog O Button Save Dog O Button Load Dog CK Button Quit and Get A Cat Get a Pro Bone-o Dog Lawyer Random Generate A Dog Name Just Make It Fido Bark Roll TextArea Treat! Button Get a Pro Bone-o Dog Lawyer Button Random Generate A Dog Name Button Just Make It Fido Button Bark OK Button Roll Button Treat! Layout Controller Code hw5.fxml File Edit View Insert Modify Arrange Preview Window Help Library a *' | No Selection Inspector Custom Properties Containers No Selection Controls CKI Button CheckBox ChoiceBox ColorPicker Menu Miscellaneous Shapes Charts 3D DogFolio, the Dog Name Management App Create New Dog Save Dog Load Dog Quit and Get A Cat Document Fido... Or Nancy? Hierarchy - El AnchorPane Label DogFolio, the Dog Name Management App Button Create New Dog O Button Save Dog O Button Load Dog CK Button Quit and Get A Cat Get a Pro Bone-o Dog Lawyer Random Generate A Dog Name Just Make It Fido Bark Roll TextArea Treat! Button Get a Pro Bone-o Dog Lawyer Button Random Generate A Dog Name Button Just Make It Fido Button Bark OK Button Roll Button Treat! Layout Controller CodeStep by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
