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.client.peditor;
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.HLayout;
029import com.smartgwt.client.widgets.layout.VStack;
030import org.modeshape.web.client.contents.Contents;
031
032/**
033 *
034 * @author kulikov
035 */
036public abstract class BaseEditor {
037
038    private final Window window = new Window();
039    protected final DynamicForm form = new DynamicForm();
040    //ok and cancel buttons
041    private final SubmitItem confirmButton = new SubmitItem("OK");
042    private final SubmitItem cancelButton = new SubmitItem("Cancel");
043    protected final VStack vStack = new VStack();
044    private HLayout panel = new HLayout();
045    
046    public BaseEditor(String title, int width, int height) {
047        form.setNumCols(2);
048        form.setPadding(25);
049
050        panel.setVisible(false);
051        
052        vStack.setTop(10);
053        vStack.addMember(panel);
054        vStack.addMember(form);
055
056        window.addChild(vStack);
057        window.setTitle(title);
058        window.setCanDragReposition(true);
059        window.setCanDragResize(false);
060        window.setShowMinimizeButton(false);
061        window.setShowCloseButton(true);
062        window.setWidth(width);
063        window.setHeight(height);
064        window.setAutoCenter(true);
065
066        window.addCloseClickHandler(new CloseClickHandler() {
067            @Override
068            public void onCloseClick(CloseClientEvent event) {
069                hide();
070            }
071        });
072
073        confirmButton.setTitle("OK");
074        confirmButton.setWidth(100);
075        confirmButton.setStartRow(true);
076        confirmButton.setEndRow(false);
077        confirmButton.addClickHandler(new ClickHandler() {
078            @Override
079            public void onClick(ClickEvent event) {
080                onConfirm(event);
081                hide();
082            }
083        });
084
085        cancelButton.setTitle("Cancel");
086        cancelButton.setWidth(100);
087        cancelButton.setStartRow(false);
088        cancelButton.setEndRow(true);
089        cancelButton.addClickHandler(new ClickHandler() {
090            @Override
091            public void onClick(ClickEvent event) {
092                hide();
093            }
094        });
095    }
096
097    
098    public BaseEditor(String title) {        
099        panel.setWidth100();
100        panel.setHeight100();
101        
102        vStack.setTop(10);
103
104        window.addChild(vStack);
105        window.setTitle(title);
106        window.setCanDragReposition(true);
107        window.setCanDragResize(false);
108        window.setShowMinimizeButton(false);
109        window.setShowCloseButton(true);
110        window.setAutoCenter(true);
111
112        window.addCloseClickHandler(new CloseClickHandler() {
113            @Override
114            public void onCloseClick(CloseClientEvent event) {
115                hide();
116            }
117        });
118
119        vStack.addMember(panel);
120    }
121    
122    public void setWidth(int width) {
123        window.setWidth(width);
124    }
125    
126    public void setHeight(int height) {
127        window.setHeight(height);
128    }
129    
130    public void setTop(int top) {
131        vStack.setTop(top);
132    }
133    
134    public void addMember(Canvas canvas) {
135        vStack.addMember(canvas);
136    }
137    
138    
139    public static ValueEditor<String> getValueEditor(String pname, String type, Contents contents ) {
140        if (pname.equals("jcr:mixinTypes")) {
141            return new MixinValueEditor(contents);
142        }
143        
144        if (type.equals("Boolean")) {
145            return new BooleanValueEditor(contents);
146        }
147        if (type.equals("Binary")) {
148            return new BinaryValueEditor();
149        }
150        if (type.equals("Date")) {
151            return new DateValueEditor(contents);
152        }
153        
154        return new DefaultValueEditor(contents);
155    }
156
157    /**
158     * Hides this dialog.
159     */
160    public void hide() {
161        window.hide();
162    }
163
164    /**
165     * Executes action when 'OK' button clicked.
166     *
167     * @param event
168     */
169    public abstract void onConfirm(ClickEvent event);
170
171    /**
172     * Adds controls to this dialog.
173     *
174     * @param items controls
175     */
176    public void setControls(FormItem... items) {
177        FormItem[] controls = new FormItem[items.length + 3];
178
179        int i = 0;
180        for (FormItem item : items) {
181            controls[i++] = item;
182        }
183
184        controls[i++] = new SpacerItem();
185        controls[i++] = confirmButton;
186        controls[i++] = cancelButton;
187
188        form.setItems(controls);
189    }
190
191    protected void showModal() {
192        window.show();
193    }
194}