001: /*
002: * Copyright 2006 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013:
014: package org.wingx;
015:
016: import java.io.IOException;
017: import org.wings.SLabel;
018: import org.wings.event.SDocumentListener;
019: import org.wings.io.StringBuilderDevice;
020: import org.wings.plaf.css.Utils;
021: import org.wings.text.DefaultDocument;
022: import org.wings.text.SDocument;
023:
024: /**
025: * A in place editor is a simple label that becomes editable when activated
026: * by a mouse click.
027: * @author Christian Schyma
028: */
029: public class XInplaceEditor extends SLabel implements
030: XInplaceEditorInterface {
031:
032: /**
033: * number of editor columns
034: */
035: private int cols = 40;
036:
037: /**
038: * numer of editor rows
039: */
040: private int rows = 1;
041:
042: /**
043: * DWR request timeout in ms
044: */
045: private int timeout = 5000;
046:
047: /**
048: * intermediate text of a DWR (Ajax) request
049: */
050: private SDocument ajaxDocument;
051:
052: /**
053: * what to display, when the intermediateDocument gets ""?
054: */
055: private String emptyString = "click here to name";
056:
057: /**
058: * text to be displayed while hovering with the mouse over the editable text
059: */
060: private String clickNotificationText = "click here to edit";
061:
062: /**
063: * Creates a new <code>XInplaceEditor</code> instance with the specified text.
064: *
065: * @param text The text to be displayed by the label.
066: */
067: public XInplaceEditor(String text) {
068: super (text);
069: init();
070: }
071:
072: /**
073: * Creates a new <code>XInplaceEditor</code> instance with the specified text
074: * and number of editor columns.
075: *
076: * @param text The text to be displayed by the label.
077: * @param cols number of editor columns
078: */
079: public XInplaceEditor(String text, int cols) {
080: super (text);
081: init();
082: this .cols = cols;
083: }
084:
085: /**
086: * Creates a new <code>XInplaceEditor</code> instance with the specified text,
087: * numer of editor columns and rows.
088: *
089: * @param text The text to be displayed by the label.
090: * @param cols number of editor columns
091: * @param rows number of editor rows
092: */
093: public XInplaceEditor(String text, int cols, int rows) {
094: super (text);
095: init();
096: this .cols = cols;
097: this .rows = rows;
098: }
099:
100: private void init() {
101: ajaxDocument = new DefaultDocument(text);
102: setWordWrap(true);
103: }
104:
105: /**
106: * @return formatted version of the intermediate text. Used by the
107: * client-side JavaScript.
108: */
109: private String getAjaxFormattedText() {
110: StringBuilderDevice device = new StringBuilderDevice(256);
111: try {
112: Utils.quote(device, ajaxDocument.getText(), true,
113: !isWordWrap(), false);
114: } catch (IOException ex) {
115: ex.printStackTrace();
116: }
117:
118: return device.toString();
119: }
120:
121: public String getAjaxText() {
122: return ajaxDocument.getText();
123: }
124:
125: public String setAjaxText(String text) {
126: this .ajaxDocument.setText(text);
127: this .text = text;
128:
129: // to avoid a invisible label
130: if (text.compareTo("") == 0) {
131: return this .emptyString;
132: } else {
133: return this .getAjaxFormattedText();
134: }
135: }
136:
137: public void setText(String t) {
138: super .setText(t);
139: if (this .ajaxDocument != null) {
140: setAjaxText(t);
141: }
142: }
143:
144: /**
145: * set number of columns of the editor
146: */
147: public void setCols(int cols) {
148: int oldValue = this .cols;
149: this .cols = cols;
150: reloadIfChange(cols, oldValue);
151: }
152:
153: /**
154: * @see #setCols
155: */
156: public int getCols() {
157: return cols;
158: }
159:
160: /**
161: * set number of rows of the editor
162: */
163: public void setRows(int rows) {
164: int oldValue = this .rows;
165: this .rows = rows;
166: reloadIfChange(rows, oldValue);
167: }
168:
169: /**
170: * @see #setRows
171: */
172: public int getRows() {
173: return rows;
174: }
175:
176: /**
177: * Set Ajax request timeout.
178: * @param timeout in ms
179: */
180: public void setTimeout(int timeout) {
181: int oldValue = this .timeout;
182: this .timeout = timeout;
183: reloadIfChange(timeout, oldValue);
184: }
185:
186: /**
187: * @see #setTimeout(int timeout)
188: */
189: public int getTimeout() {
190: return timeout;
191: }
192:
193: public SDocumentListener[] getAjaxDocumentListeners() {
194: return ajaxDocument.getDocumentListeners();
195: }
196:
197: public void addAjaxDocumentListener(SDocumentListener listener) {
198: ajaxDocument.addDocumentListener(listener);
199: }
200:
201: public void removeAjaxDocumentListener(SDocumentListener listener) {
202: ajaxDocument.removeDocumentListener(listener);
203: }
204:
205: /**
206: * @param text to be displayed while hovering with the mouse over
207: * the editable text
208: */
209: public void setClickNotificationText(String text) {
210: String oldValue = this .clickNotificationText;
211: this .clickNotificationText = text;
212: reloadIfChange(text, oldValue);
213: }
214:
215: /**
216: * @return returns the text to be displayed while hovering with the
217: * mouse over the editable text
218: */
219: public String getClickNotificationText() {
220: return this.clickNotificationText;
221: }
222:
223: }
|