001: package org.drools.eclipse.rulebuilder.ui;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005:
006: import org.drools.brms.client.modeldriven.brl.RuleAttribute;
007: import org.eclipse.swt.SWT;
008: import org.eclipse.swt.events.ModifyEvent;
009: import org.eclipse.swt.events.ModifyListener;
010: import org.eclipse.swt.events.SelectionEvent;
011: import org.eclipse.swt.events.SelectionListener;
012: import org.eclipse.swt.layout.GridData;
013: import org.eclipse.swt.layout.GridLayout;
014: import org.eclipse.swt.widgets.Button;
015: import org.eclipse.swt.widgets.Composite;
016: import org.eclipse.swt.widgets.Display;
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: public class RuleAttributeWidget extends Widget {
025:
026: public RuleAttributeWidget(FormToolkit toolkit, Composite parent,
027: RuleModeller modeller) {
028: super (parent, toolkit, modeller, 0);
029:
030: GridLayout l = new GridLayout();
031: l.numColumns = 3;
032: // l.marginBottom = 5;
033: // l.marginHeight = 5;
034: // l.marginLeft = 5;
035: // l.marginRight = 5;
036: // l.marginTop = 10;
037: // l.marginWidth = 10;
038: // l.verticalSpacing = 15;
039: parent.setLayout(l);
040:
041: create();
042: }
043:
044: private void create() {
045:
046: RuleAttribute[] attrs = modeller.getModel().attributes;
047: for (int i = 0; i < attrs.length; i++) {
048: RuleAttribute at = attrs[i];
049: addAttribute(at);
050: }
051: toolkit.paintBordersFor(parent);
052: }
053:
054: private void addAttribute(RuleAttribute at) {
055: toolkit.createLabel(parent, at.attributeName);
056:
057: if (at.attributeName.equals("no-loop")) {
058: toolkit.createLabel(parent, "");
059: } else if (at.attributeName.equals("enabled")
060: || at.attributeName.equals("auto-focus")
061: || at.attributeName.equals("lock-on-active")) {
062: createCheckbox(at);
063: } else {
064: createText(at);
065: }
066:
067: addDeleteLink(at);
068:
069: }
070:
071: private void createText(final RuleAttribute at) {
072: final Text box = toolkit.createText(parent, "");
073:
074: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
075: gd.grabExcessHorizontalSpace = true;
076: gd.minimumWidth = 100;
077: box.setLayoutData(gd);
078:
079: box.setText(at.value);
080:
081: box.addModifyListener(new ModifyListener() {
082: public void modifyText(ModifyEvent e) {
083: at.value = box.getText();
084: modeller.setDirty(true);
085: }
086: });
087:
088: if (at.attributeName.equals("date-effective")
089: || at.attributeName.equals("date-expires")) {
090: if (at.value == null || "".equals(at.value.trim())) {
091: box.setText("dd-MMM-yyyy");
092: }
093: }
094:
095: }
096:
097: private void createCheckbox(final RuleAttribute at) {
098: final Button checkbox = toolkit.createButton(parent, "",
099: SWT.CHECK);
100:
101: if (at.value == null) {
102: checkbox.setSelection(true);
103: at.value = "true";
104: } else {
105: checkbox.setSelection(at.value.equals("true") ? true
106: : false);
107: }
108:
109: checkbox.addSelectionListener(new SelectionListener() {
110:
111: public void widgetDefaultSelected(SelectionEvent e) {
112:
113: }
114:
115: public void widgetSelected(SelectionEvent e) {
116: at.value = (checkbox.getSelection()) ? "true" : "false";
117: modeller.setDirty(true);
118: }
119:
120: });
121:
122: }
123:
124: private void addDeleteLink(final RuleAttribute at) {
125: ImageHyperlink delLink = addImage(parent,
126: "icons/delete_item_small.gif");
127: delLink.setToolTipText("Remove this fieldconstraint");
128: delLink.addHyperlinkListener(new IHyperlinkListener() {
129: public void linkActivated(HyperlinkEvent e) {
130: MessageBox dialog = new MessageBox(Display.getCurrent()
131: .getActiveShell(), SWT.YES | SWT.NO
132: | SWT.ICON_WARNING);
133: dialog.setMessage("Remove this rule option?");
134: dialog.setText("Remove this rule option?");
135: if (dialog.open() == SWT.YES) {
136: RuleAttribute[] attrs = modeller.getModel().attributes;
137: for (int i = 0; i < attrs.length; i++) {
138: if (attrs[i] == at) {
139: modeller.getModel().removeAttribute(i);
140:
141: modeller.setDirty(true);
142: modeller.reloadOptions();
143: }
144: }
145: }
146: }
147:
148: public void linkEntered(HyperlinkEvent e) {
149: }
150:
151: public void linkExited(HyperlinkEvent e) {
152: }
153: });
154:
155: }
156:
157: /**
158: * Return a listbox of choices for rule attributes.
159: *
160: * @return
161: */
162: public static List getAttributeList() {
163: List list = new ArrayList();
164: list.add("...");
165:
166: list.add("salience");
167: list.add("enabled");
168: list.add("date-effective");
169: list.add("date-expires");
170: list.add("no-loop");
171: list.add("agenda-group");
172: list.add("activation-group");
173: list.add("duration");
174: list.add("auto-focus");
175: list.add("lock-on-active");
176: list.add("ruleflow-group");
177: list.add("dialect");
178:
179: return list;
180: }
181:
182: }
|