如何避免在多用户环境中编辑表单中的相同值?
我的环境:带有服务器和客户端的多用户应用程序。服务器知道当前正在使用哪个字段,并且无法从其他用户处进行编辑。
我有一个包含多个 JComponent 的表单,例如 JCheckBox、JTextField、JTextArea 和 JComboBox。问题是我想控制是否允许用户编辑字段的值。当不允许用户编辑该字段时,组件将不允许进入编辑模式。
例如: 用户 A 正在编辑文本字段的值。 现在用户 B 想要编辑相同的值。他单击同一个文本字段,此时我想确保文本字段不会在编辑模式下切换。
有人知道进行此检查的正确位置在哪里吗?我是否必须在每个组件中实施检查,或者是否有适合我表单中所有 JComponent 的解决方案?
谢谢
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 that fit to all JComponents in my form?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您可以提供通用的解决方案。创建监听器
EditabilityListener
。当用户更改组件的值时,它应该向服务器发送信号,并且所有其他用户都应该收到它,因此他们的 JComponent 实例将被禁用。问题出在并发访问上。例如,如果用户 A 在用户 B 之后一秒但在信号到达其应用程序之前开始在文本字段中键入内容,您会怎么做?
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?
服务器可以支持组件所有权请求,该请求返回客户端使用的布尔值。服务器端实现应该维护一个了解组件所有权的同步数据结构。例如,您可以使用 ConcurrentHashMap(它支持 putIfAbsent,它执行可以提供帮助的原子操作)。
请注意,您还需要客户端在释放组件时进行报告(可能还需要在服务器中添加所有权超时)。
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).