开发者

How to avoid editing same value in a form in a Multi-user environment?

开发者 https://www.devze.com 2023-01-29 15:25 出处:网络
My Environment: Multi-user application with server and client. The server knows which field is currently in use and can’t edit from another user.

My Environment: Multi-user application with server and client. The server knows which field is currently in use and can’t edit from another user.

I have a form with several JComponents like JCheckBox, JTextField, JTextArea and JComboBox. The problem is that I want to control whether the user is allowed to edit the value of the field or not. When the user is not allowed to edit the field, the component is not allowed to go to Edit Mode.

For Example: User A is editing a value of textfield. Now User B wants to edit the same value. He clicks on the same textfield and at this point I want to make sure that the textfield ain’t switching in the Edit Mode.

Has someone a good idea where the right place is for this check? Do I have to implement a check in every single component or is there maybe a solution开发者_如何学JAVA that fit to all JComponents in my form?

Thanks


I believe that you can provide generic solution. Create listener EditabilityListener. When user changes value if component it should send signal to server and all other users should get it, so their instances of JComponent become disabled.

The problem is in concurrent access. For example what would you like to do if user A started typing in text field a second after user B but before the signal arrived to his application?


The server can support a request for component ownership, which returns a boolean used by the client. The server side implementation should maintain a synchronized data structure aware of component ownerships. You can use a ConcurrentHashMap for example (it supports putIfAbsent which performs an atomic operation that can help).

Note that you'll also need clients to report when they release the component (possibly also adding ownership timeout in server).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号