001: package org.drools.brms.client.ruleeditor;
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.HashMap;
020: import java.util.Map;
021:
022: import org.drools.brms.client.common.AssetFormats;
023: import org.drools.brms.client.common.GenericCallback;
024: import org.drools.brms.client.common.LoadingPopup;
025: import org.drools.brms.client.decisiontable.DecisionTableXLSWidget;
026: import org.drools.brms.client.modeldriven.ui.RuleModeller;
027: import org.drools.brms.client.packages.ModelAttachmentFileWidget;
028: import org.drools.brms.client.packages.PackageExplorerWidget;
029: import org.drools.brms.client.packages.SuggestionCompletionCache;
030: import org.drools.brms.client.rpc.RepositoryServiceFactory;
031: import org.drools.brms.client.rpc.RuleAsset;
032:
033: import com.google.gwt.user.client.Command;
034: import com.google.gwt.user.client.Window;
035: import com.google.gwt.user.client.ui.TabPanel;
036: import com.google.gwt.user.client.ui.Widget;
037:
038: /**
039: * This launches the appropriate editor for the asset type.
040: * This uses the format attribute to determine the appropriate editor, and
041: * ALSO to unpackage the content payload from the generic asset RPC object.
042: *
043: * NOTE: when adding new editors for asset types, this will also need to be enhanced to load
044: * it up/unpackage it correctly for the editor.
045: * The editors will make changes to the rpc objects in place, and when checking in the whole RPC
046: * objects will be sent back to the server.
047: *
048: * @author Michael Neale
049: */
050: public class EditorLauncher {
051:
052: public static final Map TYPE_IMAGES = getTypeImages();
053:
054: /**
055: * This will return the appropriate viewer for the asset.
056: */
057: public static Widget getEditorViewer(RuleAsset asset,
058: RuleViewer viewer) {
059: //depending on the format, load the appropriate editor
060: if (asset.metaData.format.equals(AssetFormats.BUSINESS_RULE)) {
061: return new RuleValidatorWrapper(new RuleModeller(asset),
062: asset);
063: } else if (asset.metaData.format
064: .equals(AssetFormats.DSL_TEMPLATE_RULE)) {
065: return new RuleValidatorWrapper(new DSLRuleEditor(asset),
066: asset);
067: } else if (asset.metaData.format.equals(AssetFormats.MODEL)) {
068: return new ModelAttachmentFileWidget(asset, viewer);
069: } else if (asset.metaData.format
070: .equals(AssetFormats.DECISION_SPREADSHEET_XLS)) {
071: return new RuleValidatorWrapper(new DecisionTableXLSWidget(
072: asset, viewer), asset);
073: } else if (asset.metaData.format
074: .equals(AssetFormats.RULE_FLOW_RF)) {
075: return new RuleFlowUploadWidget(asset, viewer);
076: } else if (asset.metaData.format.equals(AssetFormats.DRL)) {
077: return new RuleValidatorWrapper(
078: new DefaultRuleContentWidget(asset), asset);
079: } else if (asset.metaData.format
080: .equals(AssetFormats.ENUMERATION)) {
081: return new RuleValidatorWrapper(
082: new DefaultRuleContentWidget(asset), asset);
083: } else {
084: return new DefaultRuleContentWidget(asset);
085: }
086:
087: }
088:
089: private static Map getTypeImages() {
090: Map result = new HashMap();
091:
092: result.put(AssetFormats.DRL, "technical_rule_assets.gif");
093: result.put(AssetFormats.DSL, "dsl.gif");
094: result.put(AssetFormats.FUNCTION, "function_assets.gif");
095: result.put(AssetFormats.MODEL, "model_asset.gif");
096: result.put(AssetFormats.DECISION_SPREADSHEET_XLS,
097: "spreadsheet_small.gif");
098: result.put(AssetFormats.BUSINESS_RULE, "rule_asset.gif");
099: result.put(AssetFormats.DSL_TEMPLATE_RULE, "rule_asset.gif");
100: result.put(AssetFormats.RULE_FLOW_RF, "ruleflow_small.gif");
101:
102: return result;
103: }
104:
105: /**
106: * Get the icon name (not the path), including the extension, for the appropriate
107: * asset format.
108: */
109: public static String getAssetFormatIcon(String format) {
110: String result = (String) TYPE_IMAGES.get(format);
111: if (result == null) {
112: return "rule_asset.gif";
113: } else {
114: return result;
115: }
116: }
117:
118: /**
119: * This will show the rule viewer. If it was previously opened, it will show that dialog instead
120: * of opening it again.
121: */
122: public static void showLoadEditor(final Map openedViewers,
123: final TabPanel tab, final String uuid,
124: final boolean readonly) {
125:
126: if (openedViewers.containsKey(uuid)) {
127: if (tab.getWidgetIndex((Widget) openedViewers.get(uuid)) == -1) {
128: String featurename = tab.getWidget(0) instanceof PackageExplorerWidget ? "Rule Viewer"
129: : "Package Manager";
130: Window.alert("Asset already opened in " + featurename);
131: } else {
132: tab.selectTab(tab.getWidgetIndex((Widget) openedViewers
133: .get(uuid)));
134: }
135: LoadingPopup.close();
136: return;
137: }
138:
139: RepositoryServiceFactory.getService().loadRuleAsset(uuid,
140: new GenericCallback() {
141:
142: public void onSuccess(Object o) {
143: final RuleAsset asset = (RuleAsset) o;
144:
145: SuggestionCompletionCache cache = SuggestionCompletionCache
146: .getInstance();
147: cache.doAction(asset.metaData.packageName,
148: new Command() {
149: public void execute() {
150: openRuleViewer(openedViewers,
151: tab, uuid, readonly,
152: asset);
153: }
154:
155: });
156: }
157:
158: });
159:
160: }
161:
162: /**
163: * This will actually show the viewer once everything is loaded and ready.
164: * @param openedViewers
165: * @param tab
166: * @param uuid
167: * @param readonly
168: * @param asset
169: */
170: private static void openRuleViewer(final Map openedViewers,
171: final TabPanel tab, final String uuid,
172: final boolean readonly, RuleAsset asset) {
173: final RuleViewer view = new RuleViewer(asset, readonly);
174:
175: String displayName = asset.metaData.name;
176: if (displayName.length() > 10) {
177: displayName = displayName.substring(0, 7) + "...";
178: }
179: String icon = getAssetFormatIcon(asset.metaData.format);
180:
181: tab.add(view, "<img src='images/" + icon + "'>" + displayName,
182: true);
183:
184: openedViewers.put(uuid, view);
185:
186: view.setCloseCommand(new Command() {
187: public void execute() {
188: tab.remove(tab.getWidgetIndex(view));
189: tab.selectTab(0);
190: openedViewers.remove(uuid);
191:
192: }
193: });
194: tab.selectTab(tab.getWidgetIndex(view));
195: }
196:
197: }
|