• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to determine best program structure  RSS feed

 
Brett Phagan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to Java Programming and trying to figure out the best way to implement my app.
I have it working except for USB listener. I have to check for the USB device each time to see if connected or disconnected.
I have tried reading and searching but can't quite grasp this concept.

I have a GUI Class that has some button listeners. I am able to get this to work.
I have a USB Class (hid4java) that has listeners for devices connect and disconnect. This works as a separate app but not sure how to implement this into app.
I have my own Board Class that determines which device is connected and what data to send and how to process the received data to be displayed.

What works:
main in GUI, creates instance of Board and Board creates an instance of USB.
When app launched, opens gui and if board there, gets data and displays. Any button presses are handled properly, but USB needs to be tested every time since I currently don't have a listener running.

I want to add the USB listener.

I want the GUI to update itself with Board data whenever a USB device is connected or disconnected.
I want the same GUI to have buttons to trigger Board to retrieve data from USB device.

My dilemma is how to instantiate so each listener in USB and GUI Classes can respond and access the Board Class.
 
I am new to OO programming and figured out some of the basics. Not clear how I can have 3 objects that can access methods within each other.

Hopefully I explain correctly.

Regards,
Brett
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch, Brett!

You don't want your USB class to have dependencies on the other classes. You can use the observer pattern to get around this:

Create an UsbListener interface that contains methods that handle UsbEvent. Your USB class should then contain addUsbListener() and removeUsbListener() methods so you can allow your Board class to subscribe for event notifications.

You can use the same pattern to let your GUI react to changes in the Board class, because you don't want your Board to have dependencies on the GUI.
 
Brett Phagan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for pointing me in the direction.
I had run across a Lynda training "Foundations of Programming: Design Patterns" last night. It looked like what I wanted but wasn't sure until your reply. I will go through training and see if this works for me.
I will report back when I get through it.
Many thanks.
Brett
 
Brett Phagan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did some reading and searching to try to get my head around this concept.
Most examples are 1 observable to many observers. I seem to need multiple observables with multiple observers and Board to be both.
I can get part of it when I have one Observable. I get twisted around when I try to use a second Observable.

I think I have the USB part. The hid4java comes with a HidServicesListener interface.

Here's where I confuse myself:
I can setup Board to listen for USB hid connect/disconnect.
I can setup Board to listen for Button presses on GUI.
So far so good.
Some button presses requires data to be sent/received from the USB device by Board. Can Board access the USB hid object from the button listener?
This last part is what I am unsure about.
And, Board needs to notify GUI whenever hid device is connected or disconnected to update GUI to reflect current connect status.
Can Board access the GUI from the USB Listener.

Board seems to be the traffic cop for everything. Listeners for USB and GUI. And be able to access the USB from GUI Listener and GUI from USB Listener.
Hope I am making sense.

I am trying to understand and use these properly without falling back to tightly coupling them like I did initially. Trying to do it better this time.

Thinking out loud here:
My thought is to separate the GUI into 2 pieces where JFrame is the Observable with buttons and the JPanels text areas are setup as Observers and added to the JFrame.
Then break up the Board class with an Observer as the starting point and create an Observable object within to send data to GUI Text areas. The object would also instantiate a USB hid object to allow talking to the device.
Unless it's possible for an object to be obseravle and observer at the same time.
Does that make sense or am I off base?

Regards,
Brett




 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know about MVC architecture? It sounds like your board is a Controller. Board can have a direct dependency on your USB and GUI. Whenever your Board is notified of a change to the USB, it can call the appropriate methods in both the USB and the GUI from its event handler methods.

Alternatively, make your Board observable as well: when something in the Board changes, the GUI is notified through a custom listener interface. You will have a daisy chain of observables. Board is both an observable and an observer.

Which approach is correct depends on the responsibilities of your classes. Is Board responsible for updating the UI or not?
 
Brett Phagan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. So I have been on and off this app the past couple weeks and have been trying to do some reading.

I have made progress on the USB side. I now have the pieces working that will montior USB HID connects and disconnects, and notify my main Class when it's one of our devices.
I have been able to read a lot of data from the hid device and process it into Strings, String Array[]s and String ArrayLists.

All the Strings are displaying nicely in console.

Now I want to display in gui. I have created a gui that has 1 Jlabel and 2 JTextAreas.
I modify the JLabel depending on if a board is connected or not.

Some info goes to one JTextArea, some info goes to the other.

I have the gui up, but not able to update the gui.

Not sure if it is some kind of threading issue or just not able to the gui.

Below is the code for my main class (I tried to remove unneed stuff) and the gui class.
At some point I will want to use the buttons in the gui, but not initially.

Any pointers would be greatly appreciated.

Regards,
Brett






 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this code you are creating two JFrame objects, one in the constructor implicitly because it extends JFrame, and one explicitly in initialize(). you should choose one or the other.
 
Brett Phagan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help. I managed to get everything working fine when running the app from Eclipse.

I created an runnable jar file from this and it is not quite working.

The gui comes up but no usb connection.
When I run from cmd line with console enabled, I get a resource not found error.
See console text below.

I tried exporting several ways but similar issue.
When I open the jar file, I see a resources\win32-x86-64 folder with the hidapi.dll in it.
I am sure it is something simple I am overlooking, but I can't find it.
FYI, the jar file is on my desktop.

Manifest
Manifest-Version: 1.0
Class-Path: .
Main-Class: mxUSB.MXBootTool

"
C:\Users\r12528\Desktop>java -jar iMX_Boot_Utility.jar
Launching application...
Operating System : Windows 7 6.1
Java Version: 1.8.0_91
Java Home: C:\Program Files\Java\jre1.8.0_91
Showing GUI.
Loading hidapi...
Exception in thread "AWT-EventQueue-0" org.hid4java.HidException: Hidapi did not
initialise: Unable to load library 'hidapi': Native library (win32-x86-64/hidap
i.dll) not found in resource path ([file:/C:/Users/r12528/Desktop/iMX_Boot_Utili
ty.jar])
        at org.hid4java.HidDeviceManager.<init>(HidDeviceManager.java:89)
        at org.hid4java.HidServices.<init>(HidServices.java:88)
        at org.hid4java.HidServices.<init>(HidServices.java:76)
        at org.hid4java.HidServices.<init>(HidServices.java:63)
        at org.hid4java.HidManager.getHidServices(HidManager.java:50)
        at mxUSB.MXConnectUSB.<init>(MXConnectUSB.java:57)
        at mxUSB.MXBootTool.createAndShowGUI(MXBootTool.java:92)
        at mxUSB.MXBootTool$1.run(MXBootTool.java:85)
        at java.awt.event.InvocationEvent.dispatch(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$500(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionP
rivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)


 
Brett Phagan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevermind, as I suspected it was a simple fix.
After some testing, I was able to get it working by adding the resources folder to the MANIFEST.MF.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!