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 org.drools.brms.client.common.DirtyableComposite;
020: import org.drools.brms.client.common.FormStylePopup;
021: import org.drools.brms.client.common.GenericCallback;
022: import org.drools.brms.client.common.ImageButton;
023: import org.drools.brms.client.common.RulePackageSelector;
024: import org.drools.brms.client.common.StatusChangePopup;
025: import org.drools.brms.client.rpc.MetaData;
026: import org.drools.brms.client.rpc.RepositoryServiceFactory;
027: import org.drools.brms.client.rpc.RuleAsset;
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.ClickListener;
033: import com.google.gwt.user.client.ui.Composite;
034: import com.google.gwt.user.client.ui.FlexTable;
035: import com.google.gwt.user.client.ui.HTML;
036: import com.google.gwt.user.client.ui.HasHorizontalAlignment;
037: import com.google.gwt.user.client.ui.HasVerticalAlignment;
038: import com.google.gwt.user.client.ui.HorizontalPanel;
039: import com.google.gwt.user.client.ui.Image;
040: import com.google.gwt.user.client.ui.TextBox;
041: import com.google.gwt.user.client.ui.Widget;
042: import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
043:
044: /**
045: * This contains the widgets used to action a rule asset
046: * (ie checkin, change state, close window)
047: * @author Michael Neale
048: */
049: public class ActionToolbar extends Composite {
050:
051: private FlexTable layout = new FlexTable();
052: private Command closeCommand;
053:
054: private MetaData metaData;
055: private Command checkinAction;
056: private Command archiveAction;
057: private Command deleteAction;
058: private String uuid;
059: private HTML state;
060:
061: public ActionToolbar(final RuleAsset asset,
062:
063: final Command checkin, final Command archiv,
064: final Command minimiseMaximise, final Command delete,
065: boolean readOnly) {
066:
067: this .metaData = asset.metaData;
068: this .checkinAction = checkin;
069: this .uuid = asset.uuid;
070: this .archiveAction = archiv;
071: this .deleteAction = delete;
072: this .state = new HTML();
073: String status = metaData.status;
074:
075: FlexCellFormatter formatter = layout.getFlexCellFormatter();
076: HorizontalPanel saveControls = new HorizontalPanel();
077: setState(status);
078:
079: saveControls.add(state);
080:
081: if (!readOnly) {
082: controls(formatter, saveControls);
083:
084: }
085:
086: windowControls(minimiseMaximise, formatter);
087:
088: initWidget(layout);
089: setWidth("100%");
090: }
091:
092: /**
093: * Sets the visible status display.
094: */
095: private void setState(String status) {
096: state.setHTML("Status: <b>[" + status + "]</b>");
097: }
098:
099: private void controls(FlexCellFormatter formatter,
100: HorizontalPanel saveControls) {
101: Image editState = new ImageButton("images/edit.gif");
102: editState.setTitle("Change status.");
103: editState.addClickListener(new ClickListener() {
104: public void onClick(Widget w) {
105: showStatusChanger(w);
106: }
107:
108: });
109: saveControls.add(editState);
110:
111: layout.setWidget(0, 0, saveControls);
112: formatter.setAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT,
113: HasVerticalAlignment.ALIGN_MIDDLE);
114:
115: //Image save = new Image("images/save_edit.gif");
116: Button save = new Button("Save changes");
117: save.setTitle("Check in changes.");
118: save.addClickListener(new ClickListener() {
119: public void onClick(Widget w) {
120: doCheckinConfirm(w);
121: }
122: });
123:
124: saveControls.add(save);
125:
126: Button copy = new Button("Copy");
127: copy.addClickListener(new ClickListener() {
128: public void onClick(Widget w) {
129: doCopyDialog(w);
130: }
131: });
132:
133: saveControls.add(copy);
134:
135: Button archive = new Button("Archive");
136: archive.addClickListener(new ClickListener() {
137: public void onClick(Widget w) {
138: if (Window
139: .confirm("Are you sure you want to archive this item?")) {
140: metaData.checkinComment = "Archived Item on "
141: + new java.util.Date().toString();
142: archiveAction.execute();
143: }
144: }
145: });
146: saveControls.add(archive);
147:
148: if (this .metaData.versionNumber == 0) {
149: Button delete = new Button("Delete");
150: delete.addClickListener(new ClickListener() {
151:
152: public void onClick(Widget w) {
153: if (Window
154: .confirm("Are you sure you want to permanently delete this (unversioned) item?")) {
155: deleteAction.execute();
156: }
157: }
158: });
159: saveControls.add(delete);
160: }
161:
162: }
163:
164: private void windowControls(final Command minimiseMaximise,
165: FlexCellFormatter formatter) {
166: HorizontalPanel windowControls = new HorizontalPanel();
167:
168: Image maxMinImage = new ImageButton("images/max_min.gif");
169: maxMinImage.addClickListener(new ClickListener() {
170: public void onClick(Widget w) {
171: minimiseMaximise.execute();
172: }
173: });
174:
175: windowControls.add(maxMinImage);
176:
177: Image closeImg = new ImageButton("images/close.gif");
178: closeImg.setTitle("Close.");
179: closeImg.addClickListener(new ClickListener() {
180: public void onClick(Widget w) {
181: closeCommand.execute();
182: }
183: });
184:
185: windowControls.add(closeImg);
186:
187: layout.setWidget(0, 1, windowControls);
188: formatter.setAlignment(0, 1,
189: HasHorizontalAlignment.ALIGN_RIGHT,
190: HasVerticalAlignment.ALIGN_MIDDLE);
191:
192: }
193:
194: protected void doCopyDialog(Widget w) {
195: final FormStylePopup form = new FormStylePopup(
196: "images/rule_asset.gif", "Copy this item");
197: final TextBox newName = new TextBox();
198: final RulePackageSelector newPackage = new RulePackageSelector();
199: form.addAttribute("New name:", newName);
200: form.addAttribute("New package:", newPackage);
201:
202: Button ok = new Button("Create copy");
203: ok.addClickListener(new ClickListener() {
204: public void onClick(Widget w) {
205: RepositoryServiceFactory.getService().copyAsset(uuid,
206: newPackage.getSelectedPackage(),
207: newName.getText(), new GenericCallback() {
208: public void onSuccess(Object data) {
209: completedCopying(newName.getText(),
210: newPackage.getSelectedPackage());
211: form.hide();
212: }
213:
214: });
215: }
216: });
217: form.addAttribute("", ok);
218:
219: form.setPopupPosition((DirtyableComposite.getWidth() - form
220: .getOffsetWidth()) / 2, 100);
221: form.show();
222:
223: }
224:
225: private void completedCopying(String name, String pkg) {
226: Window.alert("Created a new item called [" + name
227: + "] in package: [" + pkg + "] successfully.");
228:
229: }
230:
231: /**
232: * Called when user wants to checkin.
233: */
234: protected void doCheckinConfirm(Widget w) {
235:
236: final CheckinPopup pop = new CheckinPopup(w.getAbsoluteLeft(),
237: w.getAbsoluteTop(), "Check in changes.");
238: pop.setCommand(new Command() {
239:
240: public void execute() {
241: metaData.checkinComment = pop.getCheckinComment();
242: checkinAction.execute();
243: }
244: });
245: pop.show();
246: }
247:
248: /**
249: * Show the stats change popup.
250: */
251: private void showStatusChanger(Widget w) {
252: final StatusChangePopup pop = new StatusChangePopup(uuid, false);
253: pop.setChangeStatusEvent(new Command() {
254: public void execute() {
255: setState(pop.getState());
256: }
257: });
258: pop.setPopupPosition(w.getAbsoluteLeft(), w.getAbsoluteTop());
259: pop.show();
260: }
261:
262: /**
263: * This needs to be set to allow the current viewer to be closed.
264: */
265: public void setCloseCommand(Command c) {
266: this.closeCommand = c;
267: }
268:
269: }
|