001: package org.drools.brms.client.modeldriven.ui;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import org.drools.brms.client.common.DirtyableComposite;
024: import org.drools.brms.client.common.DirtyableHorizontalPane;
025: import org.drools.brms.client.modeldriven.brl.DSLSentence;
026:
027: import com.google.gwt.user.client.ui.ChangeListener;
028: import com.google.gwt.user.client.ui.Composite;
029: import com.google.gwt.user.client.ui.HTML;
030: import com.google.gwt.user.client.ui.HorizontalPanel;
031: import com.google.gwt.user.client.ui.Label;
032: import com.google.gwt.user.client.ui.TextBox;
033: import com.google.gwt.user.client.ui.Widget;
034:
035: /**
036: * This displays a widget to edit a DSL sentence.
037: * @author Michael Neale
038: */
039: public class DSLSentenceWidget extends DirtyableComposite {
040:
041: private final DirtyableHorizontalPane horiz;
042: private final List widgets;
043: private final DSLSentence sentence;
044:
045: public DSLSentenceWidget(DSLSentence sentence) {
046: horiz = new DirtyableHorizontalPane();
047: widgets = new ArrayList();
048: this .sentence = sentence;
049: init();
050: }
051:
052: private void init() {
053: makeWidgets(this .sentence.sentence);
054: initWidget(this .horiz);
055: }
056:
057: /**
058: * This will take a DSL line item, and split it into widget thingamies for displaying.
059: * One day, if this is too complex, this will have to be done on the server side.
060: */
061: public void makeWidgets(String dslLine) {
062:
063: char[] chars = dslLine.toCharArray();
064: FieldEditor currentBox = null;
065: Label currentLabel = null;
066: for (int i = 0; i < chars.length; i++) {
067: char c = chars[i];
068: if (c == '{') {
069: currentLabel = null;
070: currentBox = new FieldEditor();
071: addWidget(currentBox);
072:
073: } else if (c == '}') {
074: currentBox.setVisibleLength(currentBox.getText()
075: .length() + 1);
076: currentBox = null;
077: } else {
078: if (currentBox == null && currentLabel == null) {
079: currentLabel = new Label();
080: addWidget(currentLabel);
081: }
082: if (currentLabel != null) {
083: currentLabel.setText(currentLabel.getText() + c);
084: } else if (currentBox != null) {
085: currentBox.setText(currentBox.getText() + c);
086: }
087:
088: }
089: }
090:
091: }
092:
093: private void addWidget(Widget currentBox) {
094: this .horiz.add(currentBox);
095: widgets.add(currentBox);
096: }
097:
098: /**
099: * This will go through the widgets and build up a sentence.
100: */
101: protected void updateSentence() {
102: String newSentence = "";
103: for (Iterator iter = widgets.iterator(); iter.hasNext();) {
104: Widget wid = (Widget) iter.next();
105: if (wid instanceof Label) {
106: newSentence = newSentence + ((Label) wid).getText();
107: } else if (wid instanceof FieldEditor) {
108: newSentence = newSentence + " {"
109: + ((FieldEditor) wid).getText() + "} ";
110: }
111: }
112: this .sentence.sentence = newSentence.trim();
113:
114: }
115:
116: class FieldEditor extends DirtyableComposite {
117:
118: private TextBox box;
119: private HorizontalPanel panel = new HorizontalPanel();
120:
121: public FieldEditor() {
122: box = new TextBox();
123: //box.setStyleName( "dsl-field-TextBox" );
124:
125: panel.add(new HTML(" "));
126: panel.add(box);
127: panel.add(new HTML(" "));
128:
129: box.addChangeListener(new ChangeListener() {
130: public void onChange(Widget w) {
131: updateSentence();
132: makeDirty();
133: }
134: });
135:
136: initWidget(panel);
137: }
138:
139: public void setText(String t) {
140: box.setText(t);
141: }
142:
143: public void setVisibleLength(int l) {
144: box.setVisibleLength(l);
145: }
146:
147: public String getText() {
148: return box.getText();
149: }
150: }
151:
152: public boolean isDirty() {
153: return horiz.hasDirty();
154: }
155:
156: }
|