001: /*
002: #IFNDEF ALT_LICENSE
003: ThinWire(R) RIA Ajax Framework
004: Copyright (C) 2003-2007 Custom Credit Systems
005:
006: This library is free software; you can redistribute it and/or modify it under
007: the terms of the GNU Lesser General Public License as published by the Free
008: Software Foundation; either version 2.1 of the License, or (at your option) any
009: later version.
010:
011: This library is distributed in the hope that it will be useful, but WITHOUT ANY
012: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
013: PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
014:
015: You should have received a copy of the GNU Lesser General Public License along
016: with this library; if not, write to the Free Software Foundation, Inc., 59
017: Temple Place, Suite 330, Boston, MA 02111-1307 USA
018:
019: Users who would rather have a commercial license, warranty or support should
020: contact the following company who invented, built and supports the technology:
021:
022: Custom Credit Systems, Richardson, TX 75081, USA.
023: email: info@thinwire.com ph: +1 (888) 644-6405
024: http://www.thinwire.com
025: #ENDIF
026: [ v1.2_RC2 ]
027: */
028: package thinwire.render.web;
029:
030: import thinwire.ui.AlignTextComponent.AlignX;
031: import thinwire.ui.Component;
032: import thinwire.ui.EditorComponent;
033: import thinwire.ui.event.PropertyChangeEvent;
034:
035: /**
036: * @author Joshua J. Gertzen
037: */
038: abstract class EditorComponentRenderer extends TextComponentRenderer {
039: private static final String SET_SELECTION_RANGE = "setSelectionRange";
040: private static final String VIEW_STATE_SELECTION_RANGE = "selectionRange";
041:
042: void render(WindowRenderer wr, Component c,
043: ComponentRenderer container) {
044: EditorComponent ed = (EditorComponent) c;
045: addClientSideProperty(EditorComponent.PROPERTY_TEXT);
046: addClientSideProperty(
047: EditorComponent.PROPERTY_SELECTION_BEGIN_INDEX,
048: VIEW_STATE_SELECTION_RANGE);
049: addClientSideProperty(
050: EditorComponent.PROPERTY_SELECTION_END_INDEX,
051: VIEW_STATE_SELECTION_RANGE);
052: addClientSideProperty(EditorComponent.PROPERTY_CURSOR_INDEX,
053: VIEW_STATE_SELECTION_RANGE);
054: addInitProperty(EditorComponent.PROPERTY_ALIGN_X, ed
055: .getAlignX().name().toLowerCase());
056: super .render(wr, c, container);
057: }
058:
059: public void propertyChange(PropertyChangeEvent pce) {
060: String name = pce.getPropertyName();
061: if (isPropertyChangeIgnored(name))
062: return;
063:
064: if (name.equals(EditorComponent.PROPERTY_ALIGN_X)) {
065: postClientEvent(SET_ALIGN_X, ((AlignX) pce.getNewValue())
066: .name().toLowerCase());
067: } else if (name
068: .equals(EditorComponent.PROPERTY_SELECTION_BEGIN_INDEX)
069: || name
070: .equals(EditorComponent.PROPERTY_SELECTION_END_INDEX)) {
071: //TODO: This may fire twice if the begin index and end index change at the same time.
072: EditorComponent ed = (EditorComponent) comp;
073: postClientEvent(SET_SELECTION_RANGE, new Integer(ed
074: .getSelectionBeginIndex()), new Integer(ed
075: .getSelectionEndIndex()));
076: } else {
077: super .propertyChange(pce);
078: }
079: }
080:
081: public void componentChange(WebComponentEvent event) {
082: String name = event.getName();
083: String value = (String) event.getValue();
084: EditorComponent ed = (EditorComponent) comp;
085:
086: if (name.equals(EditorComponent.PROPERTY_TEXT)) {
087: setPropertyChangeIgnored(name, true);
088: ed.setText(value);
089: setPropertyChangeIgnored(name, false);
090: } else {
091: if (name.equals(VIEW_STATE_SELECTION_RANGE)) {
092: int index = value.indexOf(',');
093: int beginIndex = Integer.parseInt(value.substring(0,
094: index));
095: int endIndex = Integer.parseInt(value
096: .substring(index + 1));
097: int textLength = ed.getText().length();
098:
099: if (beginIndex >= 0 && beginIndex <= textLength
100: && endIndex >= 0 && endIndex <= textLength) {
101: setPropertyChangeIgnored(
102: EditorComponent.PROPERTY_SELECTION_BEGIN_INDEX,
103: true);
104: setPropertyChangeIgnored(
105: EditorComponent.PROPERTY_SELECTION_END_INDEX,
106: true);
107: ed.setSelectionRange(beginIndex, endIndex);
108: setPropertyChangeIgnored(
109: EditorComponent.PROPERTY_SELECTION_BEGIN_INDEX,
110: false);
111: setPropertyChangeIgnored(
112: EditorComponent.PROPERTY_SELECTION_END_INDEX,
113: false);
114: }
115: } else {
116: super.componentChange(event);
117: }
118: }
119: }
120: }
|