001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.modeshape.web.shared; 017 018import com.smartgwt.client.widgets.Canvas; 019import com.smartgwt.client.widgets.Window; 020import com.smartgwt.client.widgets.events.CloseClickHandler; 021import com.smartgwt.client.widgets.events.CloseClientEvent; 022import com.smartgwt.client.widgets.form.DynamicForm; 023import com.smartgwt.client.widgets.form.fields.FormItem; 024import com.smartgwt.client.widgets.form.fields.SpacerItem; 025import com.smartgwt.client.widgets.form.fields.SubmitItem; 026import com.smartgwt.client.widgets.form.fields.events.ClickEvent; 027import com.smartgwt.client.widgets.form.fields.events.ClickHandler; 028import com.smartgwt.client.widgets.layout.VStack; 029 030/** 031 * Implements basic features of "OK-Cancel" dialog. 032 * 033 * @author kulikov 034 */ 035public abstract class ModalDialog { 036 //components required to display dialog window 037 private Window window = new Window(); 038 private DynamicForm form = new DynamicForm(); 039 040 //ok and cancel buttons 041 private SubmitItem confirmButton = new SubmitItem("OK"); 042 private SubmitItem cancelButton = new SubmitItem("Cancel"); 043 private VStack vStack = new VStack(); 044 045 /** 046 * Creates new Dialog. 047 * 048 * @param title title of the dialog window. 049 * @param width the width of the dialog. 050 * @param height the height of the dialog. 051 */ 052 public ModalDialog(String title, int width, int height) { 053 form.setNumCols(2); 054 form.setPadding(25); 055 056 vStack.setTop(10); 057 vStack.addMember(form); 058 059 window.addChild(vStack); 060 window.setTitle(title); 061 window.setCanDragReposition(true); 062 window.setCanDragResize(false); 063 window.setShowMinimizeButton(false); 064 window.setShowCloseButton(true); 065 window.setWidth(width); 066 window.setHeight(height); 067 window.setAutoCenter(true); 068 069 window.addCloseClickHandler(new CloseClickHandler() { 070 @Override 071 public void onCloseClick(CloseClientEvent event) { 072 hide(); 073 } 074 }); 075 076 confirmButton.setTitle("OK"); 077 confirmButton.setWidth(100); 078 confirmButton.setStartRow(true); 079 confirmButton.setEndRow(false); 080 confirmButton.addClickHandler(new ClickHandler() { 081 @Override 082 public void onClick(ClickEvent event) { 083 onConfirm(event); 084 hide(); 085 } 086 }); 087 088 cancelButton.setTitle("Cancel"); 089 cancelButton.setWidth(100); 090 cancelButton.setStartRow(false); 091 cancelButton.setEndRow(true); 092 cancelButton.addClickHandler(new ClickHandler() { 093 @Override 094 public void onClick(ClickEvent event) { 095 hide(); 096 } 097 }); 098 099 } 100 101 protected void addMember(Canvas canvas) { 102 vStack.addMember(canvas); 103 } 104 105 /** 106 * Adds controls to this dialog. 107 * 108 * @param items controls 109 */ 110 public void setControls(FormItem... items) { 111 FormItem[] controls = new FormItem[items.length + 3]; 112 113 int i = 0; 114 for (FormItem item : items) { 115 controls[i++] = item; 116 } 117 118 controls[i++] = new SpacerItem(); 119 controls[i++] = confirmButton; 120 controls[i++] = cancelButton; 121 122 form.setItems(controls); 123 } 124 125 /** 126 * Shows this dialog modal. 127 */ 128 public void showModal() { 129 window.show(); 130 } 131 132 /** 133 * Hides this dialog. 134 */ 135 public void hide() { 136 window.hide(); 137 } 138 139 /** 140 * Executes action when 'OK' button clicked. 141 * 142 * @param event 143 */ 144 public abstract void onConfirm(ClickEvent event); 145 146 public void setAction(String action) { 147 form.setAction(action); 148 } 149 150 public void submitForm() { 151 form.submitForm(); 152 } 153 154 protected DynamicForm form() { 155 return form; 156 } 157 158 protected Window window() { 159 return window; 160 } 161}