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.contents;
017
018import com.google.gwt.core.client.GWT;
019import com.smartgwt.client.types.Alignment;
020import com.smartgwt.client.types.ContentsType;
021import com.smartgwt.client.types.Encoding;
022import com.smartgwt.client.types.FormMethod;
023import com.smartgwt.client.types.VerticalAlignment;
024import com.smartgwt.client.widgets.Button;
025import com.smartgwt.client.widgets.HTMLPane;
026import com.smartgwt.client.widgets.Label;
027import com.smartgwt.client.widgets.events.ClickEvent;
028import com.smartgwt.client.widgets.events.ClickHandler;
029import com.smartgwt.client.widgets.form.DynamicForm;
030import com.smartgwt.client.widgets.form.fields.HiddenItem;
031import com.smartgwt.client.widgets.form.fields.UploadItem;
032import com.smartgwt.client.widgets.layout.HLayout;
033import com.smartgwt.client.widgets.layout.VLayout;
034import org.modeshape.web.shared.JcrNode;
035
036/**
037 *
038 * @author kulikov
039 */
040public class BinaryEditor extends HLayout {
041    private HTMLPane htmlPane = new HTMLPane();
042    private DynamicForm form = new DynamicForm();
043    private HiddenItem repositoryField = new HiddenItem("repository");
044    private HiddenItem workspaceField = new HiddenItem("workspace");
045    private HiddenItem pathField = new HiddenItem("path");
046    private HiddenItem pNameField = new HiddenItem("pname");
047    private UploadItem fileItem = new UploadItem("Upload content");
048    
049    public BinaryEditor() {        
050        this.setStyleName("grid-bg");
051        this.setLayoutMargin(1);
052        
053        VLayout background = new VLayout();
054        background.setWidth100();
055        background.setHeight100();
056        background.setStyleName("grid-panel");
057        addMember(background);
058        
059
060        HLayout topPanel = new HLayout();
061        topPanel.setHeight(30);
062        topPanel.setAlign(VerticalAlignment.CENTER);
063        topPanel.setAlign(Alignment.LEFT);
064        
065        topPanel.setLayoutMargin(3);
066        topPanel.setBackgroundColor("#e6f1f6");
067        
068        Label label = new Label("Binary content");
069        label.setWidth100();
070        label.setStyleName("caption");
071        topPanel.addMember(label);
072
073        background.addMember(topPanel);
074        
075
076//        setAutoHeight();
077
078        HLayout bottomPanel = new HLayout();
079        bottomPanel.setHeight(30);
080        bottomPanel.setBackgroundColor("#e6f1f6");
081
082        
083        background.addMember(bottomPanel);        
084        form.setMethod(FormMethod.POST);
085        form.setAction(GWT.getModuleBaseURL() + "binary-upload/content");
086        form.setEncoding(Encoding.MULTIPART);
087        
088        VLayout vstack = new VLayout();
089        vstack.setTop(20);
090                        
091        vstack.setWidth100();
092        vstack.setHeight(550);
093        
094        htmlPane.setWidth100();
095        htmlPane.setHeight(500);
096        htmlPane.setContentsType(ContentsType.PAGE);
097        htmlPane.setBorder("inset #d3d3d3 1px");
098        htmlPane.setShowCustomScrollbars(false);
099        vstack.addMember(htmlPane);
100        
101        HLayout panel = new HLayout();
102        vstack.addMember(panel);
103        
104        Button submitButton = new Button("Submit");
105        submitButton.addClickHandler(new ClickHandler() {
106            @SuppressWarnings( "synthetic-access" )
107            @Override
108            public void onClick(ClickEvent event) {
109                form.submitForm();
110            }
111        });
112        submitButton.setValign(VerticalAlignment.CENTER);
113        fileItem.setStartRow(true);
114        fileItem.setEndRow(false);
115        
116        fileItem.setWidth("100%");
117        submitButton.setTitle("Upload");
118        
119        form.setWidth100();
120        
121        panel.setLayoutAlign(Alignment.LEFT);
122        panel.setLayoutAlign(VerticalAlignment.CENTER);
123        panel.setBackgroundColor(null);
124        panel.addMember(form);
125        panel.addMember(submitButton);
126        
127        vstack.setAutoHeight();
128        background.addMember(vstack);
129        
130        setVisible(false);
131    }
132    
133    public void setValue(JcrNode node, String name, String reference) {
134        repositoryField.setValue(node.getRepository());
135        workspaceField.setValue(node.getWorkspace());
136        pathField.setValue(node.getPath());
137        pNameField.setValue(name);
138        form.setItems(fileItem, repositoryField, workspaceField, pathField, pNameField);
139        htmlPane.setContentsURL(reference);
140        htmlPane.redraw();
141    }
142    
143}