1 package fr.ifremer.dali.ui.swing.content.manage.rule.controlrule;
2
3 import fr.ifremer.dali.ui.swing.DaliHelpBroker;
4 import fr.ifremer.dali.ui.swing.DaliUIContext;
5 import fr.ifremer.dali.ui.swing.util.DaliUI;
6 import fr.ifremer.quadrige3.ui.swing.ApplicationUI;
7 import fr.ifremer.quadrige3.ui.swing.ApplicationUIUtil;
8 import fr.ifremer.quadrige3.ui.swing.table.SwingTable;
9 import java.awt.BorderLayout;
10 import java.awt.Component;
11 import java.awt.Container;
12 import java.awt.LayoutManager;
13 import java.awt.event.ActionEvent;
14 import java.awt.event.ActionListener;
15 import java.awt.event.MouseEvent;
16 import java.awt.event.MouseListener;
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.TreeMap;
22 import javax.swing.BorderFactory;
23 import javax.swing.Box;
24 import javax.swing.BoxLayout;
25 import javax.swing.JButton;
26 import javax.swing.JMenuItem;
27 import javax.swing.JPanel;
28 import javax.swing.JPopupMenu;
29 import javax.swing.JScrollPane;
30 import jaxx.runtime.JAXXBinding;
31 import jaxx.runtime.JAXXContext;
32 import jaxx.runtime.JAXXObject;
33 import jaxx.runtime.JAXXObjectDescriptor;
34 import jaxx.runtime.JAXXUtil;
35 import jaxx.runtime.SwingUtil;
36 import jaxx.runtime.binding.DefaultJAXXBinding;
37 import jaxx.runtime.context.DefaultJAXXContext;
38 import jaxx.runtime.swing.help.JAXXHelpUI;
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41 import static org.nuiton.i18n.I18n.t;
42
43 public class ControlRuleTableUI extends JPanel implements JAXXHelpUI<DaliHelpBroker>, DaliUI<ControlRuleTableUIModel, ControlRuleTableUIHandler>, JAXXObject {
44
45
46
47
48
49 public static final String BINDING_EDIT_RULE_CODE_ENABLED = "editRuleCode.enabled";
50 public static final String BINDING_REMOVE_CONTROL_RULE_BUTTON_ENABLED = "removeControlRuleButton.enabled";
51
52
53
54
55
56 private static final String $j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VSokDxzvIPY9CDPK2MQFX75AxvTUpq9KZrCowIrnmSWJzw4EHZ9ncciHTXKItUGH35KxX1Ak+gomIy5XV2u3btSUz7GmH6/jD4/a+6gUrqwh0PX0co7pM2gKGl8XkpomSBZ/meTJubPxXW0o4whNTbuZfZMTEIz5r0Jh7P4BDA3jDhu2fpHbAPBTDdYITS+/VZCgEOnIeDrx7wHuoP8aazLfSJD8KcDMEO1ZgHnn+XWhsVms70oaw749oF/BZnaiI2xY+7HyFVy+QUxWM7UgXfgYcnqN060K5uOco3LlALiXHir+OxmgsXCiLo2eu4zwFrOMpHamGrDYLAAA=";
57 private static final Log log = LogFactory.getLog(ControlRuleTableUI.class);
58 private static final long serialVersionUID = 1L;
59
60
61
62
63
64 protected List<Object> $activeBindings = new ArrayList<Object>();
65 protected Map<String, Object> $bindingSources = new HashMap<String, Object>();
66 protected final Map<String, JAXXBinding> $bindings = new TreeMap<String, JAXXBinding>();
67 protected Map<String, Object> $objectMap = new HashMap<String, Object>();
68 protected Map<?, ?> $previousValues = new HashMap<Object, Object>();
69 private boolean allComponentsCreated;
70 protected final JAXXContext delegateContext = new DefaultJAXXContext();
71
72
73
74
75
76 protected JButton addControlRuleButton;
77 protected DaliHelpBroker broker;
78 protected SwingTable controlRuleTable;
79 protected ControlRuleTableUI controlRuleTableUI = this;
80 protected JScrollPane controlRuleTableUIPanel;
81 protected JMenuItem editRuleCode;
82 protected final ControlRuleTableUIHandler handler = createHandler();
83 protected ControlRuleTableUIModel model;
84 protected JButton removeControlRuleButton;
85 protected JPopupMenu tablePopup;
86
87
88
89
90
91 private JPanel $JPanel0;
92 private JPanel $JPanel1;
93
94
95
96
97
98 public ControlRuleTableUI(ApplicationUI parentUI) {
99 ApplicationUIUtil.setParentUI(this, parentUI);
100 $initialize();
101 }
102
103 public ControlRuleTableUI() {
104 $initialize();
105 }
106
107 public ControlRuleTableUI(JAXXContext parentContext) {
108 JAXXUtil.initContext(this, parentContext);
109 $initialize();
110 }
111
112 public ControlRuleTableUI(boolean param0) {
113 super(param0);
114 $initialize();
115 }
116
117 public ControlRuleTableUI(JAXXContext parentContext, boolean param1) {
118 super(param1);
119 JAXXUtil.initContext(this, parentContext);
120 $initialize();
121 }
122
123 public ControlRuleTableUI(LayoutManager param0) {
124 super(param0);
125 $initialize();
126 }
127
128 public ControlRuleTableUI(JAXXContext parentContext, LayoutManager param1) {
129 super(param1);
130 JAXXUtil.initContext(this, parentContext);
131 $initialize();
132 }
133
134 public ControlRuleTableUI(LayoutManager param0, boolean param1) {
135 super(param0 ,param1);
136 $initialize();
137 }
138
139 public ControlRuleTableUI(JAXXContext parentContext, LayoutManager param1, boolean param2) {
140 super(param1 ,param2);
141 JAXXUtil.initContext(this, parentContext);
142 $initialize();
143 }
144
145
146
147
148
149 public static JAXXObjectDescriptor $getJAXXObjectDescriptor() {
150 return JAXXUtil.decodeCompressedJAXXObjectDescriptor($jaxxObjectDescriptor);
151 }
152
153
154
155
156
157 @Override
158 public void applyDataBinding(String $binding) {
159 if (allComponentsCreated && $bindings.containsKey($binding)) {
160 getDataBinding($binding).applyDataBinding();
161 }
162 processDataBinding($binding);
163 }
164
165 @Override
166 public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
167 super.firePropertyChange(propertyName, oldValue, newValue);
168 }
169
170 @Override
171 public Map<String, Object> get$objectMap() {
172 return $objectMap;
173 }
174
175 @Override
176 public JAXXBinding getDataBinding(String bindingId) {
177 return $bindings.get(bindingId);
178 }
179
180 @Override
181 public JAXXBinding[] getDataBindings() {
182 return $bindings.values().toArray(new JAXXBinding[$bindings.size()]);
183 }
184
185 @Override
186 public Object getObjectById(String id) {
187 return $objectMap.get(id);
188 }
189
190 @Override
191 public void processDataBinding(String $binding, boolean $force) {
192 if (!$force && $activeBindings.contains($binding)) {
193 return;
194 }
195 $activeBindings.add($binding);
196 try {
197 if (allComponentsCreated && $bindings.containsKey($binding)) {
198 getDataBinding($binding).processDataBinding();
199 }
200 } finally {
201 $activeBindings.remove($binding);
202 }
203 }
204
205 @Override
206 public void processDataBinding(String $binding) {
207 processDataBinding($binding, false);
208 }
209
210 @Override
211 public void registerDataBinding(JAXXBinding binding) {
212 $bindings.put(binding.getId(), binding);
213 }
214
215 @Override
216 public void removeDataBinding(String $binding) {
217 if (allComponentsCreated && $bindings.containsKey($binding)) {
218 getDataBinding($binding).removeDataBinding();
219 }
220 }
221
222
223
224
225
226 @Override
227 public <T> T getContextValue(Class<T> clazz) {
228 return delegateContext.getContextValue(clazz, null);
229 }
230
231 @Override
232 public <T> T getContextValue(Class<T> clazz, String name) {
233 return delegateContext.getContextValue(clazz, name);
234 }
235
236 @Override
237 public JAXXContext getDelegateContext() {
238 return delegateContext;
239 }
240
241 @Override
242 public <O extends Container> O getParentContainer(Class<O> clazz) {
243 return SwingUtil.getParentContainer(this, clazz);
244 }
245
246 @Override
247 public <O extends Container> O getParentContainer(Object source, Class<O> clazz) {
248 return SwingUtil.getParentContainer(source, clazz);
249 }
250
251 @Override
252 public <T> void removeContextValue(Class<T> clazz) {
253 delegateContext.removeContextValue(clazz, null);
254 }
255
256 @Override
257 public <T> void removeContextValue(Class<T> clazz, String name) {
258 delegateContext.removeContextValue(clazz, name);
259 }
260
261 @Override
262 public <T> void setContextValue(T o) {
263 delegateContext.setContextValue(o, null);
264 }
265
266 @Override
267 public <T> void setContextValue(T o, String name) {
268 delegateContext.setContextValue(o, name);
269 }
270
271
272
273
274
275 public void doActionPerformed__on__addControlRuleButton(ActionEvent event) {
276 if (log.isDebugEnabled()) {
277 log.debug(event);
278 }
279 model.addNewRow();
280 }
281
282 public void doActionPerformed__on__editRuleCode(ActionEvent event) {
283 if (log.isDebugEnabled()) {
284 log.debug(event);
285 }
286 handler.editRuleCode();
287 }
288
289 public void doActionPerformed__on__removeControlRuleButton(ActionEvent event) {
290 if (log.isDebugEnabled()) {
291 log.debug(event);
292 }
293 handler.removeRules();
294 }
295
296 public void doMouseClicked__on__controlRuleTable(MouseEvent event) {
297 if (log.isDebugEnabled()) {
298 log.debug(event);
299 }
300 handler.autoSelectRowInTable(event, tablePopup);
301 }
302
303
304
305
306
307 public JButton getAddControlRuleButton() {
308 return addControlRuleButton;
309 }
310
311 public DaliHelpBroker getBroker() {
312 return broker;
313 }
314
315 public SwingTable getControlRuleTable() {
316 return controlRuleTable;
317 }
318
319 public JScrollPane getControlRuleTableUIPanel() {
320 return controlRuleTableUIPanel;
321 }
322
323 public JMenuItem getEditRuleCode() {
324 return editRuleCode;
325 }
326
327 public ControlRuleTableUIHandler getHandler() {
328 return handler;
329 }
330
331 public ControlRuleTableUIModel getModel() {
332 return model;
333 }
334
335 public JButton getRemoveControlRuleButton() {
336 return removeControlRuleButton;
337 }
338
339 public JPopupMenu getTablePopup() {
340 return tablePopup;
341 }
342
343
344
345
346
347 @Override
348 public void registerHelpId(DaliHelpBroker broker, Component component, String helpId) {
349 broker.installUI(component, helpId);
350 }
351
352 @Override
353 public void showHelp(String helpId) {
354 getBroker().showHelp(this, helpId);
355 }
356
357
358
359
360
361 protected JPanel get$JPanel0() {
362 return $JPanel0;
363 }
364
365 protected JPanel get$JPanel1() {
366 return $JPanel1;
367 }
368
369
370
371
372
373 protected void addChildrenToControlRuleTableUI() {
374 if (!allComponentsCreated) {
375 return;
376 }
377 add(controlRuleTableUIPanel);
378 add($JPanel0, BorderLayout.PAGE_END);
379 }
380
381 protected void addChildrenToControlRuleTableUIPanel() {
382 if (!allComponentsCreated) {
383 return;
384 }
385 controlRuleTableUIPanel.getViewport().add(controlRuleTable);
386 }
387
388 protected void addChildrenToTablePopup() {
389 if (!allComponentsCreated) {
390 return;
391 }
392 tablePopup.add(editRuleCode);
393 }
394
395 protected void createAddControlRuleButton() {
396 $objectMap.put("addControlRuleButton", addControlRuleButton = new JButton());
397
398 addControlRuleButton.setName("addControlRuleButton");
399 addControlRuleButton.setText(t("dali.common.new"));
400 addControlRuleButton.setToolTipText(t("dali.common.new"));
401 addControlRuleButton.addActionListener(JAXXUtil.getEventListener(ActionListener.class, "actionPerformed", this, "doActionPerformed__on__addControlRuleButton"));
402 }
403
404 protected void createBroker() {
405 $objectMap.put("broker", broker = new DaliHelpBroker("dali.home.help"));
406 }
407
408 protected void createControlRuleTable() {
409 $objectMap.put("controlRuleTable", controlRuleTable = new SwingTable());
410
411 controlRuleTable.setName("controlRuleTable");
412 controlRuleTable.addMouseListener(JAXXUtil.getEventListener(MouseListener.class, "mouseClicked", this, "doMouseClicked__on__controlRuleTable"));
413 }
414
415 protected void createControlRuleTableUIPanel() {
416 $objectMap.put("controlRuleTableUIPanel", controlRuleTableUIPanel = new JScrollPane());
417
418 controlRuleTableUIPanel.setName("controlRuleTableUIPanel");
419 }
420
421 protected void createEditRuleCode() {
422 $objectMap.put("editRuleCode", editRuleCode = new JMenuItem());
423
424 editRuleCode.setName("editRuleCode");
425 editRuleCode.setText(t("dali.rule.controlRule.editCode"));
426 editRuleCode.setToolTipText(t("dali.rule.controlRule.editCode.tip"));
427 editRuleCode.addActionListener(JAXXUtil.getEventListener(ActionListener.class, "actionPerformed", this, "doActionPerformed__on__editRuleCode"));
428 }
429
430 protected ControlRuleTableUIHandler createHandler() {
431 return new ControlRuleTableUIHandler();
432 }
433
434 protected void createModel() {
435 $objectMap.put("model", model = getContextValue(ControlRuleTableUIModel.class));
436 }
437
438 protected void createRemoveControlRuleButton() {
439 $objectMap.put("removeControlRuleButton", removeControlRuleButton = new JButton());
440
441 removeControlRuleButton.setName("removeControlRuleButton");
442 removeControlRuleButton.setText(t("dali.common.delete"));
443 removeControlRuleButton.setToolTipText(t("dali.common.delete"));
444 removeControlRuleButton.addActionListener(JAXXUtil.getEventListener(ActionListener.class, "actionPerformed", this, "doActionPerformed__on__removeControlRuleButton"));
445 }
446
447 protected void createTablePopup() {
448 $objectMap.put("tablePopup", tablePopup = new JPopupMenu());
449
450 tablePopup.setName("tablePopup");
451 }
452
453
454
455
456
457 private void $completeSetup() {
458 allComponentsCreated = true;
459 if (log.isDebugEnabled()) {
460 log.debug(this);
461 }
462 addChildrenToControlRuleTableUI();
463 addChildrenToTablePopup();
464 addChildrenToControlRuleTableUIPanel();
465
466 $JPanel0.add($JPanel1, BorderLayout.LINE_START);
467
468 $JPanel1.add(addControlRuleButton);
469 $JPanel1.add(removeControlRuleButton);
470
471
472 JAXXUtil.applyDataBinding(this, $bindings.keySet());
473
474
475 addControlRuleButton.setAlignmentX(Component.CENTER_ALIGNMENT);
476 removeControlRuleButton.setAlignmentX(Component.CENTER_ALIGNMENT);
477 setBorder(BorderFactory.createTitledBorder(t("dali.rule.controlRule.title")));
478 editRuleCode.setIcon(SwingUtil.createActionIcon("edit"));
479 addControlRuleButton.setIcon(SwingUtil.createActionIcon("add"));
480 removeControlRuleButton.setIcon(SwingUtil.createActionIcon("delete"));
481 }
482
483 private void $initialize() {
484 if (allComponentsCreated) {
485 return;
486 }
487 if (log.isDebugEnabled()) {
488 log.debug(this);
489 }
490 handler.beforeInit(this);
491 $objectMap.put("controlRuleTableUI", controlRuleTableUI);
492 createModel();
493 createBroker();
494 createTablePopup();
495 createEditRuleCode();
496 createControlRuleTableUIPanel();
497 createControlRuleTable();
498
499 $objectMap.put("$JPanel0", $JPanel0 = new JPanel());
500
501 $JPanel0.setName("$JPanel0");
502 $JPanel0.setLayout(new BorderLayout());
503
504 $objectMap.put("$JPanel1", $JPanel1 = new JPanel());
505
506 $JPanel1.setName("$JPanel1");
507 createAddControlRuleButton();
508 createRemoveControlRuleButton();
509
510 setName("controlRuleTableUI");
511 setLayout(new BorderLayout());
512 controlRuleTableUI.putClientProperty("validatorLabel", t("dali.rule.controlRule.title"));
513
514
515 $registerDefaultBindings();
516 $completeSetup();
517 handler.afterInit(this);
518 }
519
520 private void $registerDefaultBindings() {
521
522 registerDataBinding(new DefaultJAXXBinding(this, BINDING_EDIT_RULE_CODE_ENABLED, true) {
523
524 @Override
525 public void applyDataBinding() {
526 if (model != null) {
527 model.addPropertyChangeListener("singleSelectedRow", this);
528 }
529 }
530
531 @Override
532 public void processDataBinding() {
533 if (model != null && model.getSingleSelectedRow() != null) {
534 editRuleCode.setEnabled(model.getSingleSelectedRow() != null && model.getSingleSelectedRow().isNewCode());
535 }
536 }
537
538 @Override
539 public void removeDataBinding() {
540 if (model != null) {
541 model.removePropertyChangeListener("singleSelectedRow", this);
542 }
543 }
544 });
545 registerDataBinding(new DefaultJAXXBinding(this, BINDING_REMOVE_CONTROL_RULE_BUTTON_ENABLED, true, true) {
546
547 @Override
548 public void applyDataBinding() {
549 if (controlRuleTable != null) {
550 controlRuleTable.addPropertyChangeListener("editable", this);
551 }
552 if (model != null) {
553 model.addPropertyChangeListener("selectedRows", this);
554 }
555 }
556
557 @Override
558 public void processDataBinding() {
559 if (controlRuleTable != null && handler != null && model != null && model.getSelectedRows() != null) {
560 removeControlRuleButton.setEnabled(controlRuleTable.isEditable() && !model.getSelectedRows().isEmpty() && handler.canDeleteSelectedRows());
561 }
562 }
563
564 @Override
565 public void removeDataBinding() {
566 if (controlRuleTable != null) {
567 controlRuleTable.removePropertyChangeListener("editable", this);
568 }
569 if (model != null) {
570 model.removePropertyChangeListener("selectedRows", this);
571 }
572 }
573 });
574 }
575
576 }