001: package org.drools.eclipse.rulebuilder.ui;
002:
003: import org.drools.brms.client.modeldriven.SuggestionCompletionEngine;
004: import org.drools.brms.client.modeldriven.brl.ActionInsertFact;
005: import org.drools.brms.client.modeldriven.brl.ActionFieldValue;
006: import org.drools.brms.client.modeldriven.brl.ActionInsertLogicalFact;
007: import org.drools.eclipse.rulebuilder.modeldriven.HumanReadable;
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.Display;
015: import org.eclipse.swt.widgets.MessageBox;
016: import org.eclipse.swt.widgets.Shell;
017: import org.eclipse.swt.widgets.Text;
018: import org.eclipse.ui.forms.events.HyperlinkEvent;
019: import org.eclipse.ui.forms.events.IHyperlinkListener;
020: import org.eclipse.ui.forms.widgets.FormToolkit;
021: import org.eclipse.ui.forms.widgets.ImageHyperlink;
022:
023: /**
024: *
025: *
026: * @author Anton Arhipov
027: * @author Ahti Kitsik
028: *
029: */
030: public class ActionInsertFactWidget extends Widget {
031:
032: private final ActionInsertFact fact;
033:
034: public ActionInsertFactWidget(FormToolkit toolkit,
035: Composite parent, RuleModeller mod, ActionInsertFact fact,
036: int index) {
037: super (parent, toolkit, mod, index);
038:
039: this .fact = fact;
040:
041: GridLayout l = new GridLayout();
042: l.numColumns = 4;
043: l.marginBottom = 0;
044: l.marginHeight = 0;
045: l.marginLeft = 0;
046: l.marginRight = 0;
047: l.marginTop = 0;
048: l.marginWidth = 0;
049: l.verticalSpacing = 0;
050: parent.setLayout(l);
051:
052: create();
053: }
054:
055: private void create() {
056:
057: String assertType = "assert";
058: if (fact instanceof ActionInsertLogicalFact) {
059: assertType = "assertLogical";
060: }
061:
062: toolkit.createLabel(parent, HumanReadable
063: .getActionDisplayName(assertType)
064: + " " + this .fact.factType);
065: addDeleteRHSAction();
066: addMoreOptionsAction();
067: Composite constraintComposite = toolkit.createComposite(parent);
068: GridLayout constraintLayout = new GridLayout();
069: constraintLayout.numColumns = 3;
070: constraintComposite.setLayout(constraintLayout);
071: createConstraintRows(constraintComposite);
072: toolkit.paintBordersFor(constraintComposite);
073: }
074:
075: private void addMoreOptionsAction() {
076: final Shell shell = new Shell(Display.getCurrent());
077: ImageHyperlink link = addImage(parent, "icons/new_item.gif");
078:
079: link.addHyperlinkListener(new IHyperlinkListener() {
080: public void linkActivated(HyperlinkEvent e) {
081: RuleDialog popup = new AddNewInsertedFactFieldDialog(
082: shell, getModeller(), fact);
083: popup.open();
084: }
085:
086: public void linkEntered(HyperlinkEvent e) {
087: }
088:
089: public void linkExited(HyperlinkEvent e) {
090: }
091: });
092: link
093: .setToolTipText("Add another field to this so you can set its value");
094: }
095:
096: private void createConstraintRows(Composite constraintComposite) {
097: for (int row = 0; row < fact.fieldValues.length; row++) {
098: ActionFieldValue val = fact.fieldValues[row];
099: toolkit.createLabel(constraintComposite, val.field);
100: valueEditor(constraintComposite, val);
101: addRemoveFieldAction(constraintComposite, row);
102: }
103: }
104:
105: private void addRemoveFieldAction(Composite constraintComposite,
106: final int row) {
107: ImageHyperlink delLink = addImage(constraintComposite,
108: "icons/delete_item_small.gif");
109: delLink.setToolTipText("Remove this field action");
110:
111: delLink.addHyperlinkListener(new IHyperlinkListener() {
112: public void linkActivated(HyperlinkEvent e) {
113: MessageBox dialog = new MessageBox(Display.getCurrent()
114: .getActiveShell(), SWT.YES | SWT.NO
115: | SWT.ICON_WARNING);
116: dialog.setMessage("Remove this item?");
117: dialog.setText("Remove this item?");
118: if (dialog.open() == SWT.YES) {
119: fact.removeField(row);
120: getModeller().setDirty(true);
121: getModeller().reloadRhs();
122: }
123: }
124:
125: public void linkEntered(HyperlinkEvent e) {
126: }
127:
128: public void linkExited(HyperlinkEvent e) {
129: }
130: });
131: }
132:
133: private void valueEditor(Composite parent,
134: final ActionFieldValue val) {
135: final Text box = toolkit.createText(parent, "");
136:
137: if (val.value != null) {
138: box.setText(val.value);
139: }
140:
141: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
142: gd.grabExcessHorizontalSpace = true;
143: gd.minimumWidth = 100;
144: box.setLayoutData(gd);
145:
146: box.addModifyListener(new ModifyListener() {
147: public void modifyText(ModifyEvent e) {
148: getModeller().setDirty(true);
149: val.value = box.getText();
150: }
151: });
152:
153: if (val.type.equals(SuggestionCompletionEngine.TYPE_NUMERIC)) {
154: new NumericKeyFilter(box);
155: }
156:
157: }
158:
159: public SuggestionCompletionEngine getCompletion() {
160: return getModeller().getSuggestionCompletionEngine();
161: }
162:
163: }
|