KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > specifier > components > BindingMediator


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.specifier.components;
31
32 import java.awt.Component JavaDoc;
33 import java.awt.event.ActionEvent JavaDoc;
34 import java.awt.event.KeyEvent JavaDoc;
35
36 import javax.swing.AbstractAction JavaDoc;
37 import javax.swing.JButton JavaDoc;
38 import javax.swing.JComboBox JavaDoc;
39 import javax.swing.JComponent JavaDoc;
40 import javax.swing.JTextArea JavaDoc;
41 import javax.swing.JTextField JavaDoc;
42 import javax.swing.KeyStroke JavaDoc;
43
44 import com.genimen.djeneric.language.Messages;
45 import com.genimen.djeneric.repository.exceptions.DjenericException;
46 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException;
47 import com.genimen.djeneric.tools.specifier.interfaces.ObjectViewer;
48 import com.genimen.djeneric.ui.DjFocusMeLater;
49 import com.genimen.djeneric.util.DjLogger;
50
51 /**
52  * @author Wido
53  *
54  * To change the template for this generated type comment go to
55  * Window>Preferences>Java>Code Generation>Code and Comments
56  */

57 public class BindingMediator
58 {
59   protected String JavaDoc _propertyName = null;
60   protected ObjectViewer _viewer;
61   protected int _cellIndex;
62   private boolean _updateable = false;
63   private boolean _clearStatusOnExit = false;
64   private DjBindable _comp;
65   private boolean wasSetup = false;
66
67   public BindingMediator(DjBindable comp)
68   {
69     _comp = comp;
70   }
71
72   public BindingMediator(DjBindable comp, ObjectViewer viewer, String JavaDoc propertyName) throws DjenericException
73   {
74     _comp = comp;
75     _viewer = viewer;
76     setPropertyName(propertyName);
77   }
78
79   protected void setupBinder()
80   {
81     try
82     {
83       if (!wasSetup && _viewer != null && _propertyName != null)
84       {
85         _cellIndex = _viewer.getCellIndex(_propertyName);
86         _updateable = _viewer.isCellUpdateable(_cellIndex);
87         bindKeyboardActions(this, _viewer, _cellIndex, _comp, true);
88         wasSetup = true;
89       }
90     }
91     catch (ObjectNotDefinedException e)
92     {
93       _viewer.setStatusMessage(e);
94     }
95   }
96
97   public DjBindable getBindable()
98   {
99     return _comp;
100   }
101
102   public void setPropertyName(String JavaDoc propertyName) throws DjenericException
103   {
104     _propertyName = propertyName;
105     setupBinder();
106
107   }
108
109   public void setViewer(ObjectViewer viewer)
110   {
111     _viewer = viewer;
112     setupBinder();
113   }
114
115   public void synchronizeComponent() throws DjenericException
116   {
117     _comp.synchronize();
118   }
119
120   public void setValue(Object JavaDoc value) throws DjenericException
121   {
122     if (_viewer == null) return;
123
124     if (!createIfNeeded(value))
125     {
126       return;
127     }
128
129     try
130     {
131       boolean propertyChanged = false;
132
133       if (shouldUpdate(value))
134       {
135         if (!isComponentWritable())
136         {
137           _viewer.setStatusMessage(Messages.getString("BindingMediator.ValueNotUpdateable"), false);
138           synchronizeComponent();
139         }
140         else
141         {
142           setPropertyValue(value);
143           propertyChanged = true;
144         }
145       }
146
147       // Clear any messages; property set OK
148
if (propertyChanged || _clearStatusOnExit)
149       {
150         _viewer.setStatusMessage(Messages.getString("global.Ok"), true);
151       }
152       _clearStatusOnExit = false;
153     }
154     catch (DjenericException ce)
155     {
156       _viewer.setStatusMessage(DjenericException.getNiceMessage(ce), false);
157       _clearStatusOnExit = true;
158       // Put the caret on the editor in question..
159
new DjFocusMeLater((Component JavaDoc) _comp);
160     }
161   }
162
163   private boolean shouldUpdate(Object JavaDoc value) throws DjenericException
164   {
165     String JavaDoc curr = getPropertyValueString();
166     if (curr == null && value != null) return true;
167     if (curr != null && value == null) return true;
168
169     return !getPropertyValueString().equals(value.toString());
170   }
171
172   public boolean isComponentWritable()
173   {
174     try
175     {
176       return (_viewer.isEditable() && _updateable && !_viewer.isRestricted(_cellIndex)) || _viewer.isNew();
177     }
178     catch (Exception JavaDoc x)
179     {
180       _viewer.setStatusMessage(x);
181     }
182     return false;
183   }
184
185   public String JavaDoc getPropertyValueString() throws DjenericException
186   {
187     return _viewer.getString(_cellIndex);
188   }
189
190   public Object JavaDoc getPropertyValue() throws DjenericException
191   {
192     return _viewer.getValue(_cellIndex);
193   }
194
195   public void setPropertyValue(String JavaDoc strVal) throws DjenericException
196   {
197     _viewer.setString(_cellIndex, strVal);
198   }
199
200   public void setPropertyValue(Object JavaDoc obj) throws DjenericException
201   {
202     _viewer.setValue(_cellIndex, obj);
203   }
204
205   public boolean createIfNeeded(Object JavaDoc initialValue) throws DjenericException
206   {
207     if (_viewer != null && _viewer.getModel().isNoObjectsPresent())
208     {
209       if (initialValue instanceof String JavaDoc && initialValue.toString().trim().length() == 0)
210       {
211         // Empty record, empty editor DO NOTHING
212
return false;
213       }
214       if (!_viewer.getModel().isInsertable())
215       {
216         synchronizeComponent();
217         throw new DjenericException(Messages.getString("BindingMediator.CannotCreate", _viewer.getTitle()));
218       }
219       _viewer.createNew();
220       if (initialValue != null)
221       {
222         _viewer.setValue(_cellIndex, initialValue);
223       }
224       synchronizeComponent();
225     }
226     return true;
227   }
228
229   public static void bindKeyboardActions(BindingMediator mediator, ObjectViewer obj, int propertyIndex,
230                                          DjBindable comp, boolean isEditor)
231   {
232     JComponent JavaDoc target = comp.getFocussableComponent();
233
234     if (comp instanceof JComboBox JavaDoc)
235     {
236       JComboBox JavaDoc jco = (JComboBox JavaDoc) comp;
237
238       if (jco.isEditable())
239       {
240         // the focus should be set on the JTextField component
241
for (int i = 0; i < jco.getComponentCount(); i++)
242         {
243           if (jco.getComponent(i) instanceof JTextField JavaDoc)
244           {
245             target = (JComponent JavaDoc) jco.getComponent(i);
246           }
247         }
248       }
249       else
250       {
251         // the focus should be set on the button...
252
for (int i = 0; i < jco.getComponentCount(); i++)
253         {
254           if (jco.getComponent(i) instanceof JButton JavaDoc)
255           {
256             target = (JComponent JavaDoc) jco.getComponent(i);
257           }
258         }
259       }
260       target.registerKeyboardAction(new ClearPropertyAction(obj, propertyIndex), KeyStroke
261           .getKeyStroke(KeyEvent.VK_DELETE, 0), JComponent.WHEN_FOCUSED);
262
263     }
264
265     if (target == null)
266     {
267       DjLogger.log(Messages.getString("BindingMediator.CannotBindTo", comp.getClass().getName()));
268       return;
269     }
270
271     target.registerKeyboardAction(new DeleteRowAction(obj), KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,
272                                                                                    ActionEvent.CTRL_MASK),
273                                   JComponent.WHEN_FOCUSED);
274     target.registerKeyboardAction(new InsertRowAction(mediator, obj), KeyStroke.getKeyStroke(KeyEvent.VK_INSERT,
275                                                                                              ActionEvent.CTRL_MASK),
276                                   JComponent.WHEN_FOCUSED);
277     target.registerKeyboardAction(new ReloadAction(obj), KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK),
278                                   JComponent.WHEN_FOCUSED);
279
280     if (isEditor)
281     {
282       if (!(target instanceof JTextArea JavaDoc))
283       {
284         target.registerKeyboardAction(new NextRowAction(obj), KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 0),
285                                       JComponent.WHEN_FOCUSED);
286
287         target.registerKeyboardAction(new PrevRowAction(obj), KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0),
288                                       JComponent.WHEN_FOCUSED);
289         target.registerKeyboardAction(new NextRowAction(obj), KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN,
290                                                                                      ActionEvent.SHIFT_MASK),
291                                       JComponent.WHEN_FOCUSED);
292
293         target.registerKeyboardAction(new PrevRowAction(obj), KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP,
294                                                                                      ActionEvent.SHIFT_MASK),
295                                       JComponent.WHEN_FOCUSED);
296       }
297       target.registerKeyboardAction(new FirstRowAction(obj), KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP,
298                                                                                     ActionEvent.CTRL_MASK),
299                                     JComponent.WHEN_FOCUSED);
300
301       target.registerKeyboardAction(new LastRowAction(obj), KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN,
302                                                                                    ActionEvent.CTRL_MASK),
303                                     JComponent.WHEN_FOCUSED);
304     }
305   }
306
307   public String JavaDoc getPropertyName()
308   {
309     return _propertyName;
310   }
311
312   public ObjectViewer getViewer()
313   {
314     return _viewer;
315   }
316
317 }
318
319 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
320

321 class DeleteRowAction extends AbstractAction JavaDoc
322 {
323
324   private static final long serialVersionUID = 1L;
325   ObjectViewer _objectViewer;
326
327   /**
328    * Constructor for the DeleteRowAction object
329    *
330    * @param obj
331    * Description of the Parameter
332    */

333   public DeleteRowAction(ObjectViewer obj)
334   {
335     super("DeleteRowAction");
336     _objectViewer = obj;
337   }
338
339   /**
340    * Description of the Method
341    *
342    * @param e
343    * Description of the Parameter
344    */

345   public void actionPerformed(ActionEvent JavaDoc e)
346   {
347     if (!_objectViewer.getModel().isNoObjectsPresent())
348     {
349       try
350       {
351         if (!_objectViewer.isDeleteable() && !(_objectViewer.isNew() && _objectViewer.isInserteable()))
352         {
353           throw new Exception JavaDoc(Messages.getString("BindingMediator.DeletionOf", _objectViewer.getExtent()
354               .getNameSingular()));
355         }
356
357         _objectViewer.markForDelete();
358         _objectViewer.tellRowPosition();
359       }
360       catch (Exception JavaDoc x)
361       {
362         _objectViewer.setStatusMessage(x);
363       }
364     }
365     else
366     {
367       _objectViewer.setStatusMessage(Messages.getString("BindingMediator.NoMore", _objectViewer.getExtent()
368           .getNamePlural()), true);
369     }
370
371   }
372 }
373
374 class InsertRowAction extends AbstractAction JavaDoc
375 {
376
377   private static final long serialVersionUID = 1L;
378   ObjectViewer _objectViewer;
379   BindingMediator _mediator;
380
381   /**
382    * Constructor for the InsertRowAction object
383    * @param mediator
384    *
385    * @param obj
386    * Description of the Parameter
387    */

388   public InsertRowAction(BindingMediator mediator, ObjectViewer obj)
389   {
390     super("InsertRowAction");
391     _objectViewer = obj;
392     _mediator = mediator;
393
394   }
395
396   /**
397    * Description of the Method
398    *
399    * @param e
400    * Description of the Parameter
401    */

402   public void actionPerformed(ActionEvent JavaDoc e)
403   {
404     try
405     {
406       if (!_objectViewer.isInserteable())
407       {
408         throw new Exception JavaDoc(Messages.getString("BindingMediator.CreationOf", _objectViewer.getExtent().getNamePlural()));
409       }
410
411       // Use createIfNeeded so we won't loose the value typed in the field;
412
// this is the
413
// case when the manager is empty; the user entered a value and then
414
// presses ctrl-ins
415
// otherwise just call createnew.
416
if (_mediator.getBindable() != null)
417       {
418         if (!_mediator.createIfNeeded(_mediator.getBindable().getDisplayedValue()))
419         {
420           _objectViewer.createNew();
421         }
422         else
423         {
424           _objectViewer.createNew();
425         }
426       }
427
428       _objectViewer.tellRowPosition();
429     }
430     catch (Exception JavaDoc x)
431     {
432       _objectViewer.setStatusMessage(x);
433     }
434   }
435 }
436
437 class NextRowAction extends AbstractAction JavaDoc
438 {
439
440   private static final long serialVersionUID = 1L;
441   ObjectViewer _objectViewer;
442
443   /**
444    * Constructor for the NextRowAction object
445    *
446    * @param obj
447    * Description of the Parameter
448    */

449   public NextRowAction(ObjectViewer obj)
450   {
451     super("NextRowAction");
452     _objectViewer = obj;
453   }
454
455   /**
456    * Description of the Method
457    *
458    * @param e
459    * Description of the Parameter
460    */

461   public void actionPerformed(ActionEvent JavaDoc e)
462   {
463     try
464     {
465       _objectViewer.next();
466     }
467     catch (Exception JavaDoc x)
468     {
469       _objectViewer.setStatusMessage(x);
470     }
471   }
472 }
473
474 class PrevRowAction extends AbstractAction JavaDoc
475 {
476
477   private static final long serialVersionUID = 1L;
478   ObjectViewer _objectViewer;
479
480   /**
481    * Constructor for the PrevRowAction object
482    *
483    * @param obj
484    * Description of the Parameter
485    */

486   public PrevRowAction(ObjectViewer obj)
487   {
488     super("PrevRowAction");
489     _objectViewer = obj;
490   }
491
492   /**
493    * Description of the Method
494    *
495    * @param e
496    * Description of the Parameter
497    */

498   public void actionPerformed(ActionEvent JavaDoc e)
499   {
500     try
501     {
502       _objectViewer.prev();
503     }
504     catch (Exception JavaDoc x)
505     {
506       _objectViewer.setStatusMessage(x);
507     }
508   }
509 }
510
511 class FirstRowAction extends AbstractAction JavaDoc
512 {
513
514   private static final long serialVersionUID = 1L;
515   ObjectViewer _objectViewer;
516
517   /**
518    * Constructor for the FirstRowAction object
519    *
520    * @param obj
521    * Description of the Parameter
522    */

523   public FirstRowAction(ObjectViewer obj)
524   {
525     super("FirstRowAction");
526     _objectViewer = obj;
527   }
528
529   /**
530    * Description of the Method
531    *
532    * @param e
533    * Description of the Parameter
534    */

535   public void actionPerformed(ActionEvent JavaDoc e)
536   {
537     try
538     {
539       if (!_objectViewer.getModel().isNoObjectsPresent())
540       {
541         _objectViewer.setSelectedIndex(0);
542       }
543       _objectViewer.tellRowPosition();
544     }
545     catch (Exception JavaDoc x)
546     {
547       _objectViewer.setStatusMessage(x);
548     }
549   }
550 }
551
552 class LastRowAction extends AbstractAction JavaDoc
553 {
554
555   private static final long serialVersionUID = 1L;
556   ObjectViewer _objectViewer;
557
558   /**
559    * Constructor for the LastRowAction object
560    *
561    * @param obj
562    * Description of the Parameter
563    */

564   public LastRowAction(ObjectViewer obj)
565   {
566     super("LastRowAction");
567     _objectViewer = obj;
568   }
569
570   /**
571    * Description of the Method
572    *
573    * @param e
574    * Description of the Parameter
575    */

576   public void actionPerformed(ActionEvent JavaDoc e)
577   {
578     try
579     {
580       if (!_objectViewer.getModel().isNoObjectsPresent())
581       {
582         _objectViewer.setSelectedIndex(_objectViewer.getModel().getRowCount() - 1);
583       }
584       _objectViewer.tellRowPosition();
585     }
586     catch (Exception JavaDoc x)
587     {
588       _objectViewer.setStatusMessage(x);
589     }
590   }
591 }
592
593 class ReloadAction extends AbstractAction JavaDoc
594 {
595
596   private static final long serialVersionUID = 1L;
597   ObjectViewer _objectViewer;
598
599   /**
600    * Constructor for the ReloadAction object
601    *
602    * @param obj
603    * Description of the Parameter
604    */

605   public ReloadAction(ObjectViewer obj)
606   {
607     super("ReloadAction");
608     _objectViewer = obj;
609   }
610
611   /**
612    * Description of the Method
613    *
614    * @param e
615    * Description of the Parameter
616    */

617   public void actionPerformed(ActionEvent JavaDoc e)
618   {
619     try
620     {
621       _objectViewer.reload();
622     }
623     catch (Exception JavaDoc x)
624     {
625       _objectViewer.setStatusMessage(x);
626     }
627   }
628 }
629
630 class ClearPropertyAction extends AbstractAction JavaDoc
631 {
632
633   private static final long serialVersionUID = 1L;
634   ObjectViewer _objectViewer;
635   int _cellIndex;
636
637   /**
638    * Constructor for the ClearPropertyAction object
639    *
640    * @param obj
641    * Description of the Parameter
642    * @param propertyIndex
643    * Description of the Parameter
644    */

645   public ClearPropertyAction(ObjectViewer obj, int propertyIndex)
646   {
647     super("ClearPropertyAction");
648     _objectViewer = obj;
649     _cellIndex = propertyIndex;
650   }
651
652   /**
653    * Description of the Method
654    *
655    * @param e
656    * Description of the Parameter
657    */

658   public void actionPerformed(ActionEvent JavaDoc e)
659   {
660     try
661     {
662       if (_cellIndex != -1)
663       {
664         _objectViewer.setString(_cellIndex, "");
665         _objectViewer.synchronize();
666       }
667     }
668     catch (Exception JavaDoc x)
669     {
670       _objectViewer.setStatusMessage(x);
671     }
672   }
673 }
Popular Tags