001: // @@
002: // @@
003: /*
004: * Wi.Ser Framework
005: *
006: * Version: 1.8.1, 20-September-2007
007: * Copyright (C) 2005 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.util.*;
035:
036: import javax.swing.*;
037: import javax.swing.event.*;
038:
039: import de.ug2t.kernel.*;
040: import de.ug2t.unifiedGui.interfaces.*;
041: import de.ug2t.unifiedGui.views.*;
042:
043: public class HoSwingSpinner extends HoSwingComponent implements
044: IUnSpinner, IUnSyncComponent {
045: protected JSpinner pdm_swingObj = null;
046: protected HashMap pdm_values = new LinkedHashMap();
047: protected HashMap pdm_values2 = new LinkedHashMap();
048: protected Component pdm_subCmp = null;
049:
050: class MySpinnerModel extends AbstractSpinnerModel {
051: private int pem_idx = 0;
052:
053: public Object getNextValue() {
054: ArrayList l_list = new ArrayList(
055: HoSwingSpinner.this .pdm_values.keySet());
056: if (l_list.size() == 0)
057: return (null);
058:
059: pem_idx++;
060: if (pem_idx >= l_list.size())
061: pem_idx = 0;
062:
063: Object l_ret = l_list.get(pem_idx);
064: HoSwingSpinner.this .pemf_setSpinner(l_ret, false);
065: this .fireStateChanged();
066:
067: return (l_ret);
068: }
069:
070: public Object getPreviousValue() {
071: ArrayList l_list = new ArrayList(
072: HoSwingSpinner.this .pdm_values.keySet());
073: if (l_list.size() == 0)
074: return (null);
075:
076: pem_idx--;
077: if (pem_idx < 0)
078: pem_idx = l_list.size() - 1;
079:
080: Object l_ret = l_list.get(pem_idx);
081: HoSwingSpinner.this .pemf_setSpinner(l_ret, false);
082: this .fireStateChanged();
083:
084: return (l_ret);
085: }
086:
087: public Object getValue() {
088: ArrayList l_list = new ArrayList(
089: HoSwingSpinner.this .pdm_values.keySet());
090: if (l_list.size() == 0)
091: return (null);
092:
093: return l_list.get(pem_idx);
094: }
095:
096: public void setValue(Object value) {
097: ArrayList l_list = new ArrayList(
098: HoSwingSpinner.this .pdm_values.keySet());
099: int l_idx = l_list.indexOf(value);
100:
101: if (l_idx != pem_idx) {
102: this .pem_idx = l_idx;
103:
104: Object l_ret = l_list.get(pem_idx);
105: HoSwingSpinner.this .pemf_setSpinner(l_ret, false);
106: this .fireStateChanged();
107: }
108: }
109: }
110:
111: public HoSwingSpinner(String xName, IUnApplication xAppl)
112: throws Exception {
113: super (xName, xAppl);
114:
115: this .pdm_swingObj = new JSpinner();
116: this .pdm_swingObj.setForeground(Color.BLACK);
117: this .pdm_swingObj.setBackground(Color.WHITE);
118: this .pdm_swingObj.setEditor(new JLabel(" "));
119: this .pdm_swingObj.setModel(new MySpinnerModel());
120:
121: super .pdm_realSwingCmp = this .pdm_swingObj;
122:
123: this .pdm_swingObj.setMinimumSize(this .pdm_swingObj
124: .getPreferredSize());
125: ((HoSwingClient) xAppl).pcmf_addKeyDispatcher(this );
126:
127: final ChangeListener l_f = new ChangeListener() {
128: public void stateChanged(ChangeEvent ev) {
129: try {
130: if (HoSwingSpinner.this .pcmf_getAppl()
131: .pcmf_getActive() == null)
132: return;
133:
134: String l_objName = HoSwingSpinner.this
135: .pcmf_getObjName();
136:
137: // Lokale Listener aufrufen
138: Object l_selection = HoSwingSpinner.this .pdm_swingObj
139: .getValue();
140:
141: if (l_selection == null) {
142: HoSwingSpinner.this .pdm_swingObj
143: .setValue(HoSwingSpinner.this .pdm_values2
144: .get(HoSwingSpinner.this
145: .pcmf_getValue()));
146: return;
147: }
148: ;
149: Object l_value = HoSwingSpinner.this .pdm_values
150: .get(HoSwingSpinner.this .pdm_swingObj
151: .getValue());
152: if (l_value == null)
153: l_value = HoSwingSpinner.this .pdm_swingObj
154: .getValue();
155:
156: HoSwingSpinner.this .pcmf_setLocalValue(l_value);
157: HoSwingSpinner.this .pcmf_setRefresh();
158: HoSwingSpinner.this .pcmf_dispatchEvent();
159:
160: if (HoSwingSpinner.this .pem_session
161: .pcmf_isDisabled())
162: return;
163:
164: // Werte setzen um diese zum Server zu übertragen
165: ((HoSwingPage) HoSwingSpinner.this .pcmf_getAppl()
166: .pcmf_getActive()).pcmf_setSubmitValue(
167: l_objName, HoSwingSpinner.this
168: .pcmf_getValue().toString());
169: HoSwingSpinner.this .pcmf_addSyncedWidgets(
170: (HoSwingPage) HoSwingSpinner.this
171: .pcmf_getAppl().pcmf_getActive(),
172: HoSwingSpinner.this );
173: if (HoSwingSpinner.this .pcmf_getUnComponent()
174: .pcmf_isSubmit() == true)
175: ((HoSwingPage) HoSwingSpinner.this
176: .pcmf_getAppl().pcmf_getActive())
177: .pcmf_submit();
178: } catch (Exception e) {
179: KeLog.pcmf_logException("ug2t", this , e);
180: }
181: ;
182: };
183: };
184: this .pdm_swingObj.addChangeListener(l_f);
185:
186: return;
187: }
188:
189: public void pcmf_addValueObj(String xValue, KeRegisteredObject xObj) {
190: pdm_values.put(xObj, xValue);
191: pdm_values2.put(xValue, xObj);
192: this .pdm_swingObj.setMinimumSize(this .pdm_swingObj
193: .getPreferredSize());
194: };
195:
196: public void pcmf_addValue(String xValue, String xString) {
197: pdm_values.put(KeTools.pcmf_deRefObj(xString), xValue);
198: pdm_values2.put(xValue, KeTools.pcmf_deRefObj(xString));
199: this .pdm_swingObj.setMinimumSize(this .pdm_swingObj
200: .getPreferredSize());
201: };
202:
203: public void pcmf_removeValue(String xValue) {
204: pdm_values2.remove(pdm_values.remove(xValue));
205: this .pdm_swingObj.setMinimumSize(this .pdm_swingObj
206: .getPreferredSize());
207: };
208:
209: public void pcmf_beginTr() {
210: return;
211: }
212:
213: public void pcmf_commitTr() {
214: return;
215: }
216:
217: public HashMap pcmf_getValues() {
218: return (pdm_values);
219: };
220:
221: public void pcmf_setValue(Object xValue) {
222: Object l_obj = null;
223: if (xValue != null)
224: l_obj = this .pdm_values2.get(xValue);
225:
226: this .pcmf_setLocalValue(xValue);
227: pemf_setSpinner(l_obj, true);
228:
229: return;
230: }
231:
232: private void pemf_setSpinner(Object xObj, boolean xModel) {
233: if (xObj == null) {
234: if (xModel)
235: this .pdm_swingObj.setValue(null);
236: JLabel l_label = new JLabel(" ");
237: l_label.setOpaque(true);
238: pemf_applyLabelSettings(l_label);
239:
240: this .pdm_swingObj.setEditor(l_label);
241: } else {
242: JLabel l_label = new JLabel(xObj.toString());
243: l_label.setOpaque(true);
244:
245: if (xModel)
246: this .pdm_swingObj.setValue(xObj);
247:
248: if (xObj instanceof HoSwingComponent) {
249: this .pdm_swingObj
250: .setEditor((JComponent) ((HoSwingComponent) xObj)
251: .pcmf_getRealSwingObj());
252: return;
253: } else if (xObj instanceof IKeViewable) {
254: IKeView l_view = ((IKeViewable) xObj).pcmf_getView();
255: if (l_view != null && l_view instanceof UnIconValueView) {
256: pemf_applyLabelSettings(l_label);
257: ((UnIconValueView) l_view)
258: .pcmf_applyStdView(l_label);
259: }
260: } else
261: pemf_applyLabelSettings(l_label);
262:
263: this .pdm_swingObj.setEditor(l_label);
264: }
265: }
266:
267: private void pemf_applyLabelSettings(JLabel xLabel) {
268: xLabel.setFont(this .pdm_swingObj.getFont());
269: xLabel.setCursor(this .pdm_swingObj.getCursor());
270: xLabel.setForeground(this .pdm_swingObj.getForeground());
271: xLabel.setBackground(this .pdm_swingObj.getBackground());
272: };
273:
274: public Object pcmf_getSelectedObject() {
275: int l_idx = this .pcmf_getSelectedRow();
276: if (l_idx == -1)
277: return (null);
278:
279: Object l_ret = this .pdm_values2.keySet().toArray()[l_idx];
280: return (l_ret);
281: }
282:
283: public Object pcmf_setSelectedRow(int xRow) {
284: if (xRow >= this .pdm_values.size())
285: return (null);
286:
287: Object l_ret = this .pdm_values2.keySet().toArray()[xRow];
288: this .pcmf_setValue(l_ret);
289:
290: return (l_ret);
291: }
292:
293: public int pcmf_getSelectedRow() {
294: return (new ArrayList(this .pdm_values2.keySet()).indexOf(this
295: .pcmf_getValue()));
296: }
297:
298: public void pcmf_clearSpinner() {
299: this .pdm_values.clear();
300: this .pdm_values2.clear();
301:
302: Iterator l_it = new ArrayList(this .pcmf_getAllSubs())
303: .iterator();
304: Object l_obj = null;
305: while (l_it.hasNext()) {
306: l_obj = l_it.next();
307: if (l_obj instanceof IUnContextMenu
308: || l_obj instanceof IUnEventChannel)
309: continue;
310:
311: this .pcmf_removeNode((KeTreeNode) l_obj);
312: }
313: };
314:
315: public void pcmf_clearAndReleaseSpinner() {
316: this .pdm_values.clear();
317: this .pdm_values2.clear();
318:
319: Iterator l_it = new ArrayList(this .pcmf_getAllSubs())
320: .iterator();
321: Object l_obj = null;
322: while (l_it.hasNext()) {
323: l_obj = l_it.next();
324: if (l_obj instanceof IUnContextMenu
325: || l_obj instanceof IUnEventChannel)
326: continue;
327:
328: this .pcmf_removeNode((KeTreeNode) l_obj);
329: ((KeTreeNode) l_obj).pcmf_releaseSubs();
330: }
331: };
332:
333: private int pem_syncCnt = 0;
334:
335: public Object pcmf_getValueToSync() {
336: Object l_value = this .pdm_values
337: .get(HoSwingSpinner.this .pdm_swingObj.getValue());
338: if (l_value == null)
339: l_value = this .pdm_swingObj.getValue();
340:
341: return (l_value);
342: }
343:
344: public void pcmf_setSynced() {
345: this .pem_syncCnt++;
346: }
347:
348: public void pcmf_unSync() {
349: this .pem_syncCnt--;
350: if (this .pem_syncCnt < 0)
351: this .pem_syncCnt = 0;
352: }
353:
354: public boolean pcmf_isSynced() {
355: if (this .pem_syncCnt == 0)
356: return (false);
357: else
358: return (true);
359: }
360:
361: public void pcmf_clearListWidget() {
362: this .pcmf_clearSpinner();
363: }
364:
365: public void pcmf_clearAndReleaseListWidget() {
366: this .pcmf_clearAndReleaseSpinner();
367: }
368:
369: public boolean pcmf_isSelectable() {
370: return (true);
371: }
372:
373: private int pem_talign = IUnInputField.TEXT_ALIGN_WEST;
374:
375: public void pcmf_setTextAlign(int xAlign) {
376: if (IUnInputField.TEXT_ALIGN_EAST == xAlign)
377: this .pdm_swingObj.setAlignmentX(Component.RIGHT_ALIGNMENT);
378: else if (IUnInputField.TEXT_ALIGN_CENTER == xAlign)
379: this .pdm_swingObj.setAlignmentX(Component.CENTER_ALIGNMENT);
380: else
381: this .pdm_swingObj.setAlignmentX(Component.LEFT_ALIGNMENT);
382:
383: this .pem_talign = xAlign;
384: }
385:
386: public int pcmf_getTextAlign() {
387: return (this .pem_talign);
388: }
389:
390: public KeTreeNode pcmf_addNode(String xName, KeTreeNode xNode) {
391: if (xNode instanceof IUnContextMenu)
392: this .pdm_realSwingCmp
393: .add(((HoSwingComponent) xNode).pdm_realSwingCmp);
394:
395: if (xNode instanceof IUnEventChannel)
396: this .pdmf_addChannelListener((IUnEventChannel) xNode);
397:
398: return (super .pcmf_addNodeLocal(xName, xNode));
399: };
400:
401: public KeTreeElement pcmf_addElement(String xName,
402: KeTreeElement xNode) {
403: if (xNode instanceof IUnContextMenu)
404: this .pdm_realSwingCmp
405: .add(((HoSwingComponent) xNode).pdm_realSwingCmp);
406:
407: if (xNode instanceof IUnEventChannel) {
408: this .pdmf_addChannelListener((IUnEventChannel) xNode);
409: return (this );
410: }
411: ;
412:
413: return (super .pcmf_addElementLocal(xName, xNode));
414: };
415:
416: public void pcmf_spinUp() {
417: Object l_selected = this .pcmf_getValue();
418: ArrayList l_keys = new ArrayList(this .pdm_values.keySet());
419: int l_idx = l_keys.indexOf(l_selected);
420: l_idx++;
421: if (l_idx >= l_keys.size())
422: l_idx = 0;
423:
424: this .pcmf_setValue(l_keys.get(l_idx));
425: }
426:
427: public void pcmf_spinDown() {
428: Object l_selected = this .pcmf_getValue();
429: ArrayList l_keys = new ArrayList(this .pdm_values.keySet());
430: int l_idx = l_keys.indexOf(l_selected);
431: l_idx--;
432: if (l_idx < 0)
433: l_idx = l_keys.size() - 1;
434:
435: this.pcmf_setValue(l_keys.get(l_idx));
436: }
437: }
|