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 org.drools.brms.client.common.DirtyableComposite;
020: import org.drools.brms.client.common.DirtyableFlexTable;
021: import org.drools.brms.client.common.DirtyableVerticalPane;
022: import org.drools.brms.client.common.FormStylePopup;
023: import org.drools.brms.client.common.ImageButton;
024: import org.drools.brms.client.modeldriven.HumanReadable;
025: import org.drools.brms.client.modeldriven.SuggestionCompletionEngine;
026: import org.drools.brms.client.modeldriven.brl.CompositeFactPattern;
027: import org.drools.brms.client.modeldriven.brl.FactPattern;
028:
029: import com.google.gwt.user.client.ui.ChangeListener;
030: import com.google.gwt.user.client.ui.ClickListener;
031: import com.google.gwt.user.client.ui.Composite;
032: import com.google.gwt.user.client.ui.Grid;
033: import com.google.gwt.user.client.ui.HorizontalPanel;
034: import com.google.gwt.user.client.ui.Image;
035: import com.google.gwt.user.client.ui.Label;
036: import com.google.gwt.user.client.ui.ListBox;
037: import com.google.gwt.user.client.ui.VerticalPanel;
038: import com.google.gwt.user.client.ui.Widget;
039:
040: /**
041: * This represents a top level CE, like an OR, NOT, EXIST etc...
042: * Contains a list of FactPatterns.
043: *
044: * @author Michael Neale
045: *
046: */
047: public class CompositeFactPatternWidget extends DirtyableComposite {
048:
049: private final SuggestionCompletionEngine completions;
050: private CompositeFactPattern pattern;
051: private DirtyableFlexTable layout;
052: private RuleModeller modeller;
053:
054: public CompositeFactPatternWidget(RuleModeller modeller,
055: CompositeFactPattern pattern,
056: SuggestionCompletionEngine completions) {
057: this .completions = completions;
058: this .pattern = pattern;
059: this .modeller = modeller;
060:
061: this .layout = new DirtyableFlexTable();
062: this .layout.setStyleName("model-builderInner-Background");
063:
064: doLayout();
065: initWidget(layout);
066: }
067:
068: private void doLayout() {
069: this .layout.setWidget(0, 0, getCompositeLabel());
070:
071: if (pattern.patterns != null) {
072: DirtyableVerticalPane vert = new DirtyableVerticalPane();
073: FactPattern[] facts = pattern.patterns;
074: for (int i = 0; i < facts.length; i++) {
075: vert.add(new FactPatternWidget(modeller, facts[i],
076: this .completions, false));
077: }
078: this .layout.setWidget(0, 1, vert);
079: }
080: }
081:
082: private Widget getCompositeLabel() {
083:
084: HorizontalPanel horiz = new HorizontalPanel();
085: Image edit = new ImageButton("images/add_field_to_fact.gif");
086: edit
087: .setTitle("Add a fact to this constraint. If it is an 'or' type, it will need at least 2.");
088: edit.addClickListener(new ClickListener() {
089: public void onClick(Widget w) {
090: showFactTypeSelector(w);
091: }
092: });
093:
094: horiz.add(new Label(HumanReadable
095: .getCEDisplayName(pattern.type)));
096: horiz.add(edit);
097: horiz.setStyleName("modeller-composite-Label");
098: return horiz;
099: }
100:
101: /**
102: * Pops up the fact selector.
103: */
104: protected void showFactTypeSelector(final Widget w) {
105: final ListBox box = new ListBox();
106: String[] facts = completions.getFactTypes();
107:
108: box.addItem("Choose...");
109: for (int i = 0; i < facts.length; i++) {
110: box.addItem(facts[i]);
111: }
112: box.setSelectedIndex(0);
113:
114: final FormStylePopup popup = new FormStylePopup(
115: "images/new_fact.gif", "New fact pattern...");
116: popup.addAttribute("choose fact type", box);
117:
118: box.addChangeListener(new ChangeListener() {
119: public void onChange(Widget w) {
120: pattern.addFactPattern(new FactPattern(box
121: .getItemText(box.getSelectedIndex())));
122: modeller.refreshWidget();
123: popup.hide();
124: }
125: });
126: popup.setStyleName("ks-popups-Popup");
127:
128: popup.setPopupPosition(w.getAbsoluteLeft() - 400, w
129: .getAbsoluteTop());
130: popup.show();
131: }
132:
133: public boolean isDirty() {
134: return layout.hasDirty();
135: }
136:
137: }
|