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.Date;
020:
021: import org.drools.brms.client.common.FormStyleLayout;
022: import org.drools.brms.client.common.FormStylePopup;
023: import org.drools.brms.client.common.GenericCallback;
024: import org.drools.brms.client.common.ImageButton;
025: import org.drools.brms.client.common.RulePackageSelector;
026: import org.drools.brms.client.rpc.MetaData;
027: import org.drools.brms.client.rpc.RepositoryServiceFactory;
028:
029: import com.google.gwt.user.client.Command;
030: import com.google.gwt.user.client.Window;
031: import com.google.gwt.user.client.ui.Button;
032: import com.google.gwt.user.client.ui.ChangeListener;
033: import com.google.gwt.user.client.ui.ClickListener;
034: import com.google.gwt.user.client.ui.HTML;
035: import com.google.gwt.user.client.ui.HorizontalPanel;
036: import com.google.gwt.user.client.ui.Image;
037: import com.google.gwt.user.client.ui.Label;
038: import com.google.gwt.user.client.ui.TextBox;
039: import com.google.gwt.user.client.ui.Widget;
040:
041: /**
042: * This displays the metadata for a versionable asset.
043: * It also captures edits, but it does not load or save anything itself.
044: * @author Michael Neale
045: */
046: public class MetaDataWidget extends FormStyleLayout {
047:
048: private MetaData data;
049: private boolean readOnly;
050: private String uuid;
051: private Command refreshView;
052: AssetCategoryEditor ed;
053:
054: public MetaDataWidget(MetaData d, boolean readOnly, String uuid,
055: Command refreshView) {
056:
057: setStyleName("metadata-Widget");
058:
059: if (!readOnly) {
060: Image edit = new ImageButton("images/edit.gif",
061: "Rename this asset");
062: edit.addClickListener(new ClickListener() {
063: public void onClick(Widget w) {
064: showRenameAsset(w);
065: }
066: });
067: addHeader("images/meta_data.png", d.name, edit);
068: } else {
069: addHeader("images/asset_version.png", d.name);
070: }
071: this .uuid = uuid;
072: this .data = d;
073: this .readOnly = readOnly;
074: this .refreshView = refreshView;
075: loadData(d);
076: }
077:
078: private void loadData(MetaData d) {
079: this .data = d;
080: addAttribute("Categories:", categories());
081: addRow(new HTML("<hr/>"));
082:
083: addAttribute("Modified on:",
084: readOnlyDate(data.lastModifiedDate));
085: addAttribute("by:", readOnlyText(data.lastContributor));
086: addAttribute("Note:", readOnlyText(data.checkinComment));
087: addAttribute("Version:", getVersionNumberLabel());
088:
089: if (!readOnly) {
090: addAttribute("Created on:", readOnlyDate(data.createdDate));
091: }
092: addAttribute("Created by:", readOnlyText(data.creator));
093: addAttribute("Format:", new HTML("<b>" + data.format + "</b>"));
094:
095: addRow(new HTML("<hr/>"));
096:
097: addAttribute("Package:", packageEditor(data.packageName));
098: addAttribute("Subject:", editableText(new FieldBinding() {
099: public String getValue() {
100: return data.subject;
101: }
102:
103: public void setValue(String val) {
104: data.subject = val;
105: }
106: }, "A short description of the subject matter."));
107:
108: addAttribute("Type:", editableText(new FieldBinding() {
109: public String getValue() {
110: return data.type;
111: }
112:
113: public void setValue(String val) {
114: data.type = val;
115: }
116:
117: }, "This is for classification purposes."));
118:
119: addAttribute("External link:", editableText(new FieldBinding() {
120: public String getValue() {
121: return data.externalRelation;
122: }
123:
124: public void setValue(String val) {
125: data.externalRelation = val;
126: }
127:
128: }, "This is for relating the asset to an external system."));
129:
130: addAttribute(
131: "Source:",
132: editableText(new FieldBinding() {
133: public String getValue() {
134: return data.externalSource;
135: }
136:
137: public void setValue(String val) {
138: data.externalSource = val;
139: }
140:
141: },
142: "A short description or code indicating the source of the rule."));
143:
144: if (!readOnly) {
145: addRow(new VersionBrowser(this .uuid, this .data, refreshView));
146: }
147: }
148:
149: private Widget packageEditor(final String packageName) {
150: if (this .readOnly) {
151: return readOnlyText(packageName);
152: } else {
153: HorizontalPanel horiz = new HorizontalPanel();
154: horiz.setStyleName("current-Asset-Package");
155: horiz.add(readOnlyText(packageName));
156: Image editPackage = new ImageButton("images/edit.gif");
157: editPackage.addClickListener(new ClickListener() {
158: public void onClick(Widget w) {
159: showEditPackage(packageName, w);
160: }
161: });
162: horiz.add(editPackage);
163: return horiz;
164: }
165: }
166:
167: private void showRenameAsset(Widget source) {
168: final FormStylePopup pop = new FormStylePopup(
169: "images/package_large.png", "Rename this item");
170: final TextBox box = new TextBox();
171: pop.addAttribute("New name", box);
172: Button ok = new Button("Rename item");
173: pop.addAttribute("", ok);
174: ok.addClickListener(new ClickListener() {
175: public void onClick(Widget w) {
176: RepositoryServiceFactory.getService().renameAsset(uuid,
177: box.getText(), new GenericCallback() {
178: public void onSuccess(Object data) {
179: refreshView.execute();
180: Window.alert("Item has been renamed");
181: pop.hide();
182: }
183: });
184: }
185: });
186:
187: pop.setPopupPosition(source.getParent().getParent()
188: .getAbsoluteLeft() - 18, source.getParent().getParent()
189: .getAbsoluteTop());
190: pop.show();
191: }
192:
193: private void showEditPackage(final String pkg, Widget source) {
194: final FormStylePopup pop = new FormStylePopup(
195: "images/package_large.png",
196: "Move this item to another package");
197: pop.addAttribute("Current package:", new Label(pkg));
198: final RulePackageSelector sel = new RulePackageSelector();
199: pop.addAttribute("New package:", sel);
200: Button ok = new Button("Change package");
201: pop.addAttribute("", ok);
202: ok.addClickListener(new ClickListener() {
203:
204: public void onClick(Widget w) {
205: if (sel.getSelectedPackage().equals(pkg)) {
206: Window
207: .alert("You need to pick a different package to move this to.");
208: return;
209: }
210: RepositoryServiceFactory.getService()
211: .changeAssetPackage(uuid,
212: sel.getSelectedPackage(),
213: "Moved from : " + pkg,
214: new GenericCallback() {
215: public void onSuccess(Object data) {
216: makeDirty();
217: refreshView.execute();
218: pop.hide();
219: }
220:
221: });
222:
223: }
224:
225: });
226: pop.setPopupPosition(source.getParent().getParent()
227: .getAbsoluteLeft(), source.getParent().getParent()
228: .getAbsoluteTop());
229: pop.show();
230: }
231:
232: private Widget getVersionNumberLabel() {
233: if (data.versionNumber == 0) {
234: return new HTML("<i>Not checked in yet</i>");
235: } else {
236: return readOnlyText(Long.toString(data.versionNumber));
237: }
238:
239: }
240:
241: private Widget readOnlyDate(Date lastModifiedDate) {
242: if (lastModifiedDate == null) {
243: return null;
244: } else {
245: return new Label(lastModifiedDate.toLocaleString());
246: }
247: }
248:
249: private Label readOnlyText(String text) {
250: Label lbl = new Label(text);
251: lbl.setWidth("100%");
252: return lbl;
253: }
254:
255: private Widget categories() {
256: ed = new AssetCategoryEditor(this .data, this .readOnly);
257: return ed;
258: }
259:
260: /** This binds a field, and returns a text editor for it */
261: private Widget editableText(final FieldBinding bind, String toolTip) {
262: if (!readOnly) {
263: final TextBox box = new TextBox();
264: box.setTitle(toolTip);
265: box.setText(bind.getValue());
266: ChangeListener listener = new ChangeListener() {
267: public void onChange(Widget w) {
268: makeDirty();
269: bind.setValue(box.getText());
270: }
271: };
272: box.addChangeListener(listener);
273: return box;
274: } else {
275: return new Label(bind.getValue());
276: }
277: }
278:
279: /** used to bind fields in the meta data DTO to the form */
280: static interface FieldBinding {
281: void setValue(String val);
282:
283: String getValue();
284: }
285:
286: /**
287: * Return the data if it is to be saved.
288: */
289: public MetaData getData() {
290: return data;
291: }
292:
293: public boolean isDirty() {
294: return (ed.isDirty() || dirtyflag);
295: }
296:
297: }
|