Add a filter by category feature to the
ProductViewer dialog. This will require you to:
Add another method to the DAO for performing the filtering. Think about the signature for this method yourself. What are the inputs (parameters)? What is the output (return type)? Is the output going to be a single value or a collection?
Name the method
Implement the method so that it returns all products in the specified category.
This is an example of a one-to-many relationship. There are many products per category. We should use a collection class that allows us to represent the one-to-many relationships between the categories and the products. The best type of collection for doing this is known as a multimap. Java does not have a built-in multimap, but there are a several third-party libraries that provide a multimap collection.
The Google Guava library has a very good multimap implementation. You will need to add Guava as a dependency to your
build.gradle file as shown in the reference document in the Java/Guava Multimap section. Adding this dependency will cause Gradle to download the Guava JAR file and add it to the project.
Refer to the reference section for an example of how to use Guava multimaps.
Hint: Your keys are categories (String) and the values are products.
Add components to the
ProductViewer dialog for facilitating the filter feature if you haven't already done so (refer to the project mock-ups if you are not sure what is needed).
Add code to the dialog to make the categories appear in the combobox (if you haven't done so already).
This is the same code that you used in the
ProductEditor to display the categories in the combobox on that dialog.
Add code to the filter combobox's click handler that gets the selected category and uses it to call the new DAO method to find the associated products.
You can then call the
updateItems method on the JList's model to display the filtered products.