001: // @@
002: // @@
003: /*
004: * Wi.Ser Framework
005: *
006: * LGPL Version: 1.8.1, 20-September-2007
007: * Copyright (C) 2005-2006 Dirk von der Weiden <dvdw@imail.de>
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library located in LGPL.txt in the
021: * license directory; if not, write to the
022: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
023: * Boston, MA 02111-1307, USA.
024: *
025: * If this agreement does not cover your requirements, please contact us
026: * via email to get detailed information about the commercial license
027: * or our service offerings!
028: *
029: */
030: // @@
031: package de.ug2t.channel.ho.client.swing;
032:
033: import java.awt.*;
034: import java.awt.event.*;
035: import java.util.*;
036:
037: import javax.swing.*;
038: import javax.swing.event.*;
039: import javax.swing.text.*;
040:
041: import de.ug2t.channel.ho.*;
042: import de.ug2t.kernel.*;
043: import de.ug2t.unifiedGui.interfaces.*;
044: import de.ug2t.xmlScript.*;
045:
046: public final class HoSwingListBox extends HoSwingComponent implements
047: IUnListBox, IUnSyncComponent {
048: protected MyJList pdm_swingObj = null;
049:
050: private JScrollPane pem_pane = null;
051: private int pem_size = 0;
052: private boolean pem_inTr = false;
053:
054: protected HashMap pdm_values = new LinkedHashMap();
055: protected HashMap pdm_values2 = new LinkedHashMap();
056: protected Component pdm_subCmp = null;
057:
058: class MyJList extends JList {
059: public int getNextMatch(String prefix, int startIndex,
060: Position.Bias bias) {
061: if (HoSwingListBox.this .pdm_subCmp == null)
062: return (super .getNextMatch(prefix, startIndex, bias));
063: else
064: return (this .getSelectedIndex());
065: }
066: }
067:
068: final class MyMouseDisp implements MouseListener {
069: public void pemf_handle(MouseEvent e, char xType) {
070: HoSwingListBox.this .pdm_subCmp = null;
071:
072: Object l_sel = HoSwingListBox.this .pdm_swingObj
073: .getSelectedValue();
074: int l_selidx = HoSwingListBox.this .pdm_swingObj
075: .getSelectedIndex();
076: if (l_selidx == -1)
077: return;
078:
079: Rectangle l_rect = HoSwingListBox.this .pdm_swingObj
080: .getCellBounds(l_selidx, l_selidx);
081:
082: if (l_sel instanceof HoSwingComponent) {
083: ((HoSwingComponent) l_sel).pcmf_getSwingWidget()
084: .setBounds(l_rect.x, l_rect.y, l_rect.width,
085: l_rect.height);
086:
087: ((HoSwingComponent) l_sel).pcmf_getSwingWidget()
088: .validate();
089:
090: Container l_comp = (Container) ((Container) ((HoSwingComponent) l_sel)
091: .pcmf_getSwingWidget()).getComponentAt((int) e
092: .getX(), (int) (e.getY() - l_rect.getY()));
093:
094: if (l_comp != null) {
095: Object l_source = e.getSource();
096: e.setSource(l_comp);
097:
098: int l_x = l_comp.getBounds().x;
099: int l_y = l_comp.getBounds().y;
100:
101: if (l_comp != ((HoSwingComponent) l_sel)
102: .pcmf_getSwingWidget()) {
103: l_x = l_comp.getBounds().x;
104: l_y = l_comp.getBounds().y + l_rect.y;
105: }
106:
107: e.translatePoint(-l_x, -l_y);
108:
109: HoSwingListBox.this .pdm_subCmp = HoSwingMouseEventDispatcher
110: .pcmf_dispatch(e, xType, l_comp);
111:
112: if (HoSwingListBox.this .pdm_subCmp.hasFocus())
113: HoSwingListBox.this .pdm_swingObj.requestFocus();
114:
115: e.translatePoint(+l_x, +l_y);
116: e.setSource(l_source);
117: }
118: }
119:
120: HoSwingListBox.this .pdm_swingObj.repaint();
121: }
122:
123: public void mouseClicked(MouseEvent e) {
124: this .pemf_handle(e,
125: HoSwingMouseEventDispatcher.MOUSE_CLICKED);
126: }
127:
128: public void mousePressed(MouseEvent e) {
129: this .pemf_handle(e,
130: HoSwingMouseEventDispatcher.MOUSE_PRESSED);
131: }
132:
133: public void mouseReleased(MouseEvent e) {
134: this .pemf_handle(e,
135: HoSwingMouseEventDispatcher.MOUSE_RELEASED);
136: }
137:
138: public void mouseEntered(MouseEvent e) {
139: this .pemf_handle(e,
140: HoSwingMouseEventDispatcher.MOUSE_ENTERED);
141: }
142:
143: public void mouseExited(MouseEvent e) {
144: this .pemf_handle(e,
145: HoSwingMouseEventDispatcher.MOUSE_EXITED);
146: }
147: };
148:
149: final class MyKeyDisp implements KeyListener {
150: public void pcmf_handle(KeyEvent arg0) {
151: if (HoSwingListBox.this .pdm_subCmp != null) {
152: arg0.setSource(HoSwingListBox.this .pdm_subCmp);
153: HoSwingListBox.this .pdm_subCmp.dispatchEvent(arg0);
154: HoSwingListBox.this .pdm_subCmp.repaint();
155: }
156: HoSwingListBox.this .pdm_swingObj.repaint();
157: }
158:
159: public void keyPressed(KeyEvent arg0) {
160: this .pcmf_handle(arg0);
161: }
162:
163: public void keyReleased(KeyEvent arg0) {
164: this .pcmf_handle(arg0);
165: }
166:
167: public void keyTyped(KeyEvent arg0) {
168: this .pcmf_handle(arg0);
169: }
170: };
171:
172: public HoSwingListBox(String xName, String xValue, String xString,
173: int xSize, IUnApplication xAppl) throws Exception {
174: super (xName, xAppl);
175:
176: this .pdm_swingObj = new MyJList();
177: this .pdm_swingObj.setSelectionMode(0);
178: this .pdm_swingObj.addMouseListener(new MyMouseDisp());
179: this .pdm_swingObj.addKeyListener(new MyKeyDisp());
180: this .pem_pane = new JScrollPane(this .pdm_swingObj);
181: super .pdm_realSwingCmp = this .pem_pane;
182: this .pdm_swingObj
183: .setCellRenderer(new HoSwingFlexListCellRenderer());
184:
185: this .pcmf_setLocalValue(xValue);
186: this .pem_size = xSize;
187:
188: if (xString.equals("") == false) {
189: pdm_values.put(xString, xValue);
190: pdm_values2.put(xValue, xString);
191: }
192: ;
193:
194: this .pemf_addScrollListeners();
195:
196: this .pdm_swingObj.setListData(new Vector(this .pdm_values
197: .keySet()));
198: this .pdm_swingObj.setVisibleRowCount(this .pem_size);
199: this .pdm_swingObj.setMinimumSize(this .pdm_swingObj
200: .getPreferredSize());
201: ((HoSwingClient) xAppl).pcmf_addKeyDispatcher(this );
202:
203: final ListSelectionListener l_f = new ListSelectionListener() {
204: public void valueChanged(ListSelectionEvent e) {
205: try {
206: if (e.getValueIsAdjusting())
207: return;
208:
209: if (HoSwingListBox.this .pcmf_getAppl()
210: .pcmf_getActive() == null)
211: return;
212:
213: String l_objName = HoSwingListBox.this
214: .pcmf_getObjName();
215:
216: // Lokale Listener aufrufen
217: Object l_selection = HoSwingListBox.this .pdm_swingObj
218: .getSelectedValue();
219: if (l_selection == null) {
220: HoSwingListBox.this .pdm_swingObj
221: .setSelectedValue(
222: HoSwingListBox.this .pdm_values2
223: .get(HoSwingListBox.this
224: .pcmf_getValue()),
225: true);
226: return;
227: }
228: ;
229:
230: HoSwingListBox.this
231: .pcmf_setLocalValue(HoSwingListBox.this .pdm_values
232: .get(l_selection));
233: HoSwingListBox.this .pcmf_setRefresh();
234: HoSwingListBox.this .pcmf_dispatchEvent();
235:
236: if (HoSwingListBox.this .pem_session
237: .pcmf_isDisabled())
238: return;
239:
240: // Werte setzen um diese zum Server zu übertragen
241: ((HoSwingPage) HoSwingListBox.this .pcmf_getAppl()
242: .pcmf_getActive()).pcmf_setSubmitValue(
243: l_objName, HoSwingListBox.this
244: .pcmf_getValue().toString());
245: HoSwingListBox.this .pcmf_addSyncedWidgets(
246: (HoSwingPage) HoSwingListBox.this
247: .pcmf_getAppl().pcmf_getActive(),
248: HoSwingListBox.this );
249: if (HoSwingListBox.this .pcmf_getUnComponent()
250: .pcmf_isSubmit() == true)
251: ((HoSwingPage) HoSwingListBox.this
252: .pcmf_getAppl().pcmf_getActive())
253: .pcmf_submit();
254: } catch (Exception ex) {
255: KeLog.pcmf_logException("ug2t", this , ex);
256: }
257: ;
258: };
259: };
260: this .pdm_swingObj.addListSelectionListener(l_f);
261:
262: return;
263: }
264:
265: private void pemf_addScrollListeners() {
266: AdjustmentListener l_listen = new AdjustmentListener() {
267: private int pem_x = 0;
268: private int pem_y = 0;
269: private HoDelayedCall pem_call = null;
270:
271: public void adjustmentValueChanged(AdjustmentEvent e) {
272: if (this .pem_x == HoSwingListBox.this .pem_pane
273: .getHorizontalScrollBar().getValue()
274: && this .pem_y == HoSwingListBox.this .pem_pane
275: .getVerticalScrollBar().getValue())
276: return;
277:
278: if (pem_session != null) {
279: KeStringTemplateWrapper l_remCall = new KeStringTemplateWrapper(
280: "");
281:
282: if (HoSwingListBox.this .pem_session
283: .pcmf_isInTransaction()) {
284: ScXmlScript.pcmf_createPBody(l_remCall);
285: } else {
286: ScXmlScript.pcmf_createxScriptString(l_remCall,
287: null);
288: ScXmlScript.pcmf_addProc(l_remCall, null);
289: }
290: ;
291:
292: this .pem_x = HoSwingListBox.this .pem_pane
293: .getHorizontalScrollBar().getValue();
294: this .pem_y = HoSwingListBox.this .pem_pane
295: .getVerticalScrollBar().getValue();
296:
297: ScXmlScript.pcmf_addCall(l_remCall, null,
298: HoSwingListBox.this .pcmf_getObjName(),
299: "pcmf_setScrollInfo");
300: ScXmlScript.pcmf_addCallPar(l_remCall, Integer
301: .toString(this .pem_x), "false", "int");
302: ScXmlScript.pcmf_addCallPar(l_remCall, Integer
303: .toString(this .pem_y), "false", "int");
304: ScXmlScript.pcmf_endAll(l_remCall);
305:
306: if (this .pem_call == null
307: || this .pem_call.pcmf_isReady())
308: this .pem_call = new HoDelayedCall(l_remCall
309: .toString(),
310: HoSwingListBox.this .pem_session,
311: IUnComponent.EVENT_LATENCY_SCROLL,
312: HoSwingListBox.this , false);
313: else
314: this .pem_call.pcmf_refreshCall(l_remCall
315: .toString());
316: }
317: ;
318: }
319: };
320: this .pem_pane.getHorizontalScrollBar().addAdjustmentListener(
321: l_listen);
322: this .pem_pane.getVerticalScrollBar().addAdjustmentListener(
323: l_listen);
324: }
325:
326: public void pcmf_addValueObj(String xValue, KeRegisteredObject xObj) {
327: pdm_values.put(xObj, xValue);
328: pdm_values2.put(xValue, xObj);
329:
330: if (this .pem_inTr == false) {
331: this .pdm_swingObj.setListData(new Vector(this .pdm_values
332: .keySet()));
333: ((HoSwingFlexListCellRenderer) this .pdm_swingObj
334: .getCellRenderer()).pcmf_resetAllColors();
335: this .pdm_swingObj
336: .setCellRenderer(new HoSwingFlexListCellRenderer());
337: this .pdm_swingObj.setMinimumSize(this .pdm_swingObj
338: .getPreferredSize());
339: }
340: }
341:
342: public void pcmf_addValue(String xValue, String xString) {
343: pdm_values.put(KeTools.pcmf_deRefObj(xString), xValue);
344: pdm_values2.put(xValue, KeTools.pcmf_deRefObj(xString));
345:
346: if (this .pem_inTr == false) {
347: this .pdm_swingObj.setListData(new Vector(this .pdm_values
348: .keySet()));
349: ((HoSwingFlexListCellRenderer) this .pdm_swingObj
350: .getCellRenderer()).pcmf_resetAllColors();
351: this .pdm_swingObj
352: .setCellRenderer(new HoSwingFlexListCellRenderer());
353: this .pdm_swingObj.setMinimumSize(this .pdm_swingObj
354: .getPreferredSize());
355: }
356: };
357:
358: public void pcmf_removeValue(String xValue) {
359: pdm_values2.remove(pdm_values.remove(xValue));
360:
361: if (this .pem_inTr == false) {
362: this .pdm_swingObj.setListData(new Vector(this .pdm_values
363: .keySet()));
364: ((HoSwingFlexListCellRenderer) this .pdm_swingObj
365: .getCellRenderer()).pcmf_resetAllColors();
366: this .pdm_swingObj
367: .setCellRenderer(new HoSwingFlexListCellRenderer());
368: this .pdm_swingObj.setMinimumSize(this .pdm_swingObj
369: .getPreferredSize());
370: }
371: };
372:
373: public void pcmf_beginTr() {
374: this .pem_inTr = true;
375: }
376:
377: public void pcmf_commitTr() {
378: this .pem_inTr = false;
379:
380: this .pdm_swingObj.setListData(new Vector(this .pdm_values
381: .keySet()));
382: ((HoSwingFlexListCellRenderer) this .pdm_swingObj
383: .getCellRenderer()).pcmf_resetAllColors();
384: this .pdm_swingObj
385: .setCellRenderer(new HoSwingFlexListCellRenderer());
386: this .pdm_swingObj.setMinimumSize(this .pdm_swingObj
387: .getPreferredSize());
388: }
389:
390: public void pcmf_setValue(Object xValue) {
391: this .pcmf_setLocalValue(xValue);
392: if (xValue == null)
393: this .pdm_swingObj.clearSelection();
394: else
395: this .pdm_swingObj.setSelectedValue(this .pdm_values2
396: .get(xValue), true);
397:
398: return;
399: };
400:
401: public Object pcmf_getSelectedObject() {
402: int l_idx = this .pcmf_getSelectedRow();
403: if (l_idx == -1)
404: return (null);
405:
406: Object l_ret = this .pdm_values2.keySet().toArray()[l_idx];
407: return (l_ret);
408: }
409:
410: public Object pcmf_setSelectedRow(int xRow) {
411: if (xRow >= this .pdm_values.size())
412: return (null);
413:
414: Object l_ret = this .pdm_values2.keySet().toArray()[xRow];
415: this .pcmf_setValue(l_ret);
416:
417: return (l_ret);
418: }
419:
420: public int pcmf_getSelectedRow() {
421: return (new ArrayList(this .pdm_values2.keySet()).indexOf(this
422: .pcmf_getValue()));
423: }
424:
425: public int pcmf_getVisibleRows() {
426: return (this .pem_size);
427: };
428:
429: public void pcmf_disable() {
430: this .pdm_swingObj.setEnabled(false);
431: super .pcmf_disable();
432: return;
433: };
434:
435: public void pcmf_enable() {
436: this .pdm_swingObj.setEnabled(true);
437: super .pcmf_enable();
438: return;
439: };
440:
441: public void pcmf_setFgColor(String xCol) {
442: try {
443: this .pdm_swingObj.setForeground(HoSwingComponent
444: .pcmf_createColor(xCol));
445:
446: super .pcmf_setFgColor(xCol);
447: } catch (Exception e) {
448: KeLog.pcmf_logException("ug2t", this , e);
449: }
450: ;
451:
452: return;
453: };
454:
455: public void pcmf_setBgColor(String xCol) {
456: try {
457: this .pdm_swingObj.setBackground(HoSwingComponent
458: .pcmf_createColor(xCol));
459:
460: super .pcmf_setBgColor(xCol);
461: } catch (Exception e) {
462: KeLog.pcmf_logException("ug2t", this , e);
463: }
464: ;
465:
466: return;
467: };
468:
469: public java.awt.Container pcmf_getSwingWidget() {
470: return (this .pdm_swingObj);
471: };
472:
473: public void pcmf_clearListBox() {
474: this .pdm_values.clear();
475: this .pdm_values2.clear();
476:
477: Iterator l_it = new ArrayList(this .pcmf_getAllSubs())
478: .iterator();
479: Object l_obj = null;
480: while (l_it.hasNext()) {
481: l_obj = l_it.next();
482: if (l_obj instanceof IUnContextMenu
483: || l_obj instanceof IUnEventChannel)
484: continue;
485:
486: this .pcmf_removeNode((KeTreeNode) l_obj);
487: }
488:
489: this .pdm_swingObj.setListData(new Vector());
490: ((HoSwingFlexListCellRenderer) this .pdm_swingObj
491: .getCellRenderer()).pcmf_resetAllColors();
492: this .pdm_swingObj
493: .setCellRenderer(new HoSwingFlexListCellRenderer());
494: };
495:
496: public void pcmf_clearAndReleaseListBox() {
497: this .pdm_values.clear();
498: this .pdm_values2.clear();
499:
500: Iterator l_it = new ArrayList(this .pcmf_getAllSubs())
501: .iterator();
502: Object l_obj = null;
503: while (l_it.hasNext()) {
504: l_obj = l_it.next();
505: if (l_obj instanceof IUnContextMenu
506: || l_obj instanceof IUnEventChannel)
507: continue;
508:
509: this .pcmf_removeNode((KeTreeNode) l_obj);
510: ((KeTreeNode) l_obj).pcmf_releaseSubs();
511: }
512:
513: this .pdm_swingObj.setListData(new Vector());
514: ((HoSwingFlexListCellRenderer) this .pdm_swingObj
515: .getCellRenderer()).pcmf_resetAllColors();
516: this .pdm_swingObj
517: .setCellRenderer(new HoSwingFlexListCellRenderer());
518: };
519:
520: public void pcmf_scrollXRel(int xPx) {
521: int l_x = this .pem_pane.getHorizontalScrollBar().getValue();
522: l_x += xPx;
523: this .pem_pane.getHorizontalScrollBar().setValue(l_x);
524: }
525:
526: public void pcmf_scrollXAbs(int xPx) {
527: this .pem_pane.getHorizontalScrollBar().setValue(xPx);
528: }
529:
530: public void pcmf_scrollYRel(int xPy) {
531: int l_y = this .pem_pane.getVerticalScrollBar().getValue();
532: l_y += xPy;
533: this .pem_pane.getVerticalScrollBar().setValue(l_y);
534: }
535:
536: public void pcmf_scrollYAbs(int xPy) {
537: this .pem_pane.getVerticalScrollBar().setValue(xPy);
538: }
539:
540: public int pcmf_getXScroll() {
541: return (this .pem_pane.getHorizontalScrollBar().getValue());
542: }
543:
544: public int pcmf_getYScroll() {
545: return (this .pem_pane.getVerticalScrollBar().getValue());
546: }
547:
548: public void pcmf_setReadOnly(boolean xReadOnly) {
549: this .pdm_swingObj.setEnabled(!xReadOnly);
550: }
551:
552: public boolean pcmf_isReadOnly() {
553: return (!this .pdm_swingObj.isEnabled());
554: }
555:
556: private int pem_syncCnt = 0;
557:
558: public Object pcmf_getValueToSync() {
559: Object l_selection = this .pdm_swingObj.getSelectedValue();
560: if (l_selection == null)
561: return (this .pdm_values2.get(this .pcmf_getValue()));
562:
563: return (HoSwingListBox.this .pdm_values.get(l_selection));
564: }
565:
566: public void pcmf_setSynced() {
567: this .pem_syncCnt++;
568: }
569:
570: public void pcmf_unSync() {
571: this .pem_syncCnt--;
572: if (this .pem_syncCnt < 0)
573: this .pem_syncCnt = 0;
574: }
575:
576: public boolean pcmf_isSynced() {
577: if (this .pem_syncCnt == 0)
578: return (false);
579: else
580: return (true);
581: }
582:
583: public HashMap pcmf_getValues() {
584: return (pdm_values);
585: }
586:
587: public void pcmf_clearListWidget() {
588: this .pcmf_clearListBox();
589: }
590:
591: public void pcmf_clearAndReleaseListWidget() {
592: this .pcmf_clearAndReleaseListBox();
593: }
594:
595: public void pcmf_setVisibleRows(int xSize) {
596: this .pem_size = xSize;
597: this .pdm_swingObj.setVisibleRowCount(this .pem_size);
598: }
599:
600: public boolean pcmf_isSelectable() {
601: return (true);
602: }
603:
604: public KeTreeNode pcmf_addNode(String xName, KeTreeNode xNode) {
605: if (xNode instanceof IUnContextMenu)
606: this .pdm_realSwingCmp
607: .add(((HoSwingComponent) xNode).pdm_realSwingCmp);
608:
609: if (xNode instanceof IUnEventChannel)
610: this .pdmf_addChannelListener((IUnEventChannel) xNode);
611:
612: return (super .pcmf_addNodeLocal(xName, xNode));
613: };
614:
615: public KeTreeElement pcmf_addElement(String xName,
616: KeTreeElement xNode) {
617: if (xNode instanceof IUnContextMenu)
618: this .pdm_realSwingCmp
619: .add(((HoSwingComponent) xNode).pdm_realSwingCmp);
620:
621: if (xNode instanceof IUnEventChannel) {
622: this .pdmf_addChannelListener((IUnEventChannel) xNode);
623: return (this);
624: }
625: ;
626:
627: return (super.pcmf_addElementLocal(xName, xNode));
628: };
629: }
|