001: package org.drools.eclipse.rulebuilder.ui;
002:
003: import java.util.ArrayList;
004: import java.util.Iterator;
005: import java.util.List;
006:
007: import org.drools.brms.client.modeldriven.brl.DSLSentence;
008: import org.eclipse.swt.SWT;
009: import org.eclipse.swt.events.ModifyEvent;
010: import org.eclipse.swt.events.ModifyListener;
011: import org.eclipse.swt.layout.GridData;
012: import org.eclipse.swt.layout.GridLayout;
013: import org.eclipse.swt.widgets.Composite;
014: import org.eclipse.swt.widgets.Control;
015: import org.eclipse.swt.widgets.Display;
016: import org.eclipse.swt.widgets.Label;
017: import org.eclipse.swt.widgets.MessageBox;
018: import org.eclipse.swt.widgets.Text;
019: import org.eclipse.ui.forms.events.HyperlinkEvent;
020: import org.eclipse.ui.forms.events.IHyperlinkListener;
021: import org.eclipse.ui.forms.widgets.FormToolkit;
022: import org.eclipse.ui.forms.widgets.ImageHyperlink;
023:
024: /**
025: * This displays a widget to edit a DSL sentence.
026: *
027: * @author Ahti Kitsik
028: * @author Anton Arhipov
029: */
030: public abstract class DSLSentenceWidget extends Widget {
031:
032: private final DSLSentence sentence;
033:
034: private List widgets = new ArrayList();
035:
036: public DSLSentenceWidget(FormToolkit toolkit, Composite parent,
037: DSLSentence sentence, RuleModeller modeller, int index) {
038: super (parent, toolkit, modeller, index);
039:
040: this .sentence = sentence;
041:
042: makeWidget();
043: addDeleteAction();
044: }
045:
046: protected abstract void updateModel();
047:
048: private void addDeleteAction() {
049:
050: ImageHyperlink delLink = addImage(parent,
051: "icons/delete_item_small.gif");
052: delLink.addHyperlinkListener(new IHyperlinkListener() {
053:
054: public void linkActivated(HyperlinkEvent e) {
055: MessageBox dialog = new MessageBox(Display.getCurrent()
056: .getActiveShell(), SWT.YES | SWT.NO
057: | SWT.ICON_WARNING);
058: dialog.setMessage("Remove this DSL sentense?");
059: dialog.setText("Remove this DSL sentense?");
060: if (dialog.open() == SWT.YES) {
061: updateModel();
062: }
063: }
064:
065: public void linkEntered(HyperlinkEvent e) {
066: }
067:
068: public void linkExited(HyperlinkEvent e) {
069: }
070: });
071: delLink.setToolTipText("Remove this condition.");
072: }
073:
074: private void makeWidget() {
075:
076: int elems = 0;
077:
078: char[] chars = this .sentence.sentence.toCharArray();
079: Text currentBox = null;
080: Label currentLabel = null;
081: for (int i = 0; i < chars.length; i++) {
082: char c = chars[i];
083: if (c == '{') {
084: currentLabel = null;
085:
086: currentBox = toolkit.createText(parent, "");
087:
088: //final Text thisBox = currentBox;
089: elems++;
090:
091: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
092: gd.grabExcessHorizontalSpace = true;
093: gd.minimumWidth = 100;
094: currentBox.setLayoutData(gd);
095:
096: widgets.add(currentBox);
097:
098: } else if (c == '}') {
099: currentBox = null;
100: } else {
101: if (currentBox == null && currentLabel == null) {
102: currentLabel = toolkit.createLabel(parent, "");
103: elems++;
104:
105: widgets.add(currentLabel);
106: }
107: if (currentLabel != null) {
108: currentLabel.setText(currentLabel.getText() + c);
109: } else if (currentBox != null) {
110: currentBox.setText(currentBox.getText() + c);
111: }
112: }
113: }
114:
115: GridLayout l = new GridLayout();
116: int cols = elems + 1;
117: l.numColumns = cols;
118: l.verticalSpacing = 0;
119: l.marginTop = 0;
120: l.marginHeight = 2;
121: l.marginBottom = 0;
122: parent.setLayout(l);
123:
124: // Attach listeners
125: Iterator widgetiter = widgets.iterator();
126: while (widgetiter.hasNext()) {
127: Object o = (Object) widgetiter.next();
128: if (o instanceof Text) {
129: ((Text) o).addModifyListener(new ModifyListener() {
130: public void modifyText(ModifyEvent e) {
131: updateSentence();
132: /* Point p = thisBox.getSize();
133:
134: GC gc = new GC(thisBox);
135: gc.setFont(thisBox.getFont());
136: FontMetrics fontMetrics = gc.getFontMetrics();
137: int w = fontMetrics.getAverageCharWidth()*thisBox.getText().length();
138: gc.dispose();
139:
140:
141: thisBox.setSize( w, p.y );
142: thisBox.redraw();
143: parent.redraw();
144: */
145: getModeller().setDirty(true);
146: }
147: });
148:
149: }
150: }
151: toolkit.paintBordersFor(parent);
152: }
153:
154: protected void updateSentence() {
155: String newSentence = "";
156: for (Iterator iter = widgets.iterator(); iter.hasNext();) {
157: Control wid = (Control) iter.next();
158: if (wid instanceof Label) {
159: newSentence = newSentence + ((Label) wid).getText();
160: } else if (wid instanceof Text) {
161: newSentence = newSentence + "{"
162: + ((Text) wid).getText() + "}";
163: }
164: }
165: this.sentence.sentence = newSentence;
166: }
167:
168: }
|