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.smartgwt.client.types.Alignment;
019import com.smartgwt.client.widgets.Label;
020import com.smartgwt.client.widgets.events.ClickEvent;
021import com.smartgwt.client.widgets.events.ClickHandler;
022import com.smartgwt.client.widgets.form.DynamicForm;
023import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
024import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
025import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
026import com.smartgwt.client.widgets.layout.HLayout;
027import com.smartgwt.client.widgets.layout.VLayout;
028import org.modeshape.web.shared.Align;
029import org.modeshape.web.shared.Columns;
030
031/**
032 * Panel for session/workspace actions.
033 * 
034 * Layout:
035 * 
036 * |----------------------------------------------|
037 * |       col1             |    col2             |
038 * |----------------------------------------------|
039 * 
040 * @author kulikov
041 */
042public class WorkspacePanel extends Columns {
043    
044    private final static String STYLE_NAME = "viewport";
045    private final static int LAYOUT_MARGIN = 5;
046    
047    private final static String COL2_WIDTH = "25%";
048            
049    private final Contents contents;
050    
051    private final Column1 col1 = new Column1();
052    private final Column2 col2 = new Column2();
053
054    private RefreshSessionDialog refreshDialog;
055    /**
056     * Create new instance.
057     * 
058     * @param contents 
059     */
060    public WorkspacePanel(final Contents contents) {
061        super(Align.LEFT, Align.CENTER);
062        this.contents = contents;
063        refreshDialog = new RefreshSessionDialog(contents);
064        
065        setStyleName(STYLE_NAME);
066        setLayoutMargin(LAYOUT_MARGIN);
067
068        HLayout strut = new HLayout();
069        strut.setWidth(40);
070        
071        addMember(col1);
072        addMember(strut);
073        addMember(col2);
074    }
075
076    public void setEnabled(boolean enabled) {
077        col1.setEnabled(enabled);
078    }
079    
080    /**
081     * Assigns workspace names to the combo box into column 2.
082     * 
083     * @param values 
084     */
085    public void setWorkspaceNames(String[] values) {
086        col2.combo.setValueMap(values);
087        if (values.length > 0) {
088            col2.combo.setValue(values[0]);
089        }
090    }
091    
092    /**
093     * Gets selected combo box value.
094     * 
095     * @return 
096     */
097    public String getSelectedWorkspace() {
098        return col2.combo.getValueAsString();
099    }
100    
101    /**
102     * |---------------------------------|
103     * | combo_title : combox_box        |
104     * |---------------------------------|
105     */
106    private class Column2 extends Columns {
107
108        private ComboBoxItem combo = new ComboBoxItem();
109
110        public Column2() {
111            super(Align.LEFT, Align.CENTER);
112            setWidth(COL2_WIDTH);
113            
114            final DynamicForm form = new DynamicForm();
115            form.setFields(combo);
116
117            combo.setTitle("Workspace");
118            combo.setWidth(250);
119
120            combo.addChangedHandler(new ChangedHandler() {
121                @Override
122                public void onChanged(ChangedEvent event) {
123                    contents.changeWorkspace((String) event.getValue());
124                }
125            });
126
127            addMember(form);
128        }
129
130    }
131
132    /**
133     * |---------------------------------|
134     * |               row1              |
135     * |---------------------------------|
136     * |               row2              |
137     * |---------------------------------|
138     */
139    private class Column1 extends VLayout {
140        private final Label saveButton = new Label();
141        
142        public Column1() {
143            setWidth100();
144
145            //append rows
146            addMember(Row1());
147            addMember(Row2());
148        }
149        
150        public void setEnabled(boolean enabled) {
151            if (enabled) {
152                saveButton.enable();
153                saveButton.setStyleName("button-label");
154            } else {
155                saveButton.disable();
156                saveButton.setStyleName("button-label-disable");
157            }
158            saveButton.redraw();
159        }
160        
161        private HLayout Row1() {
162            Columns top = new Columns(Align.LEFT, Align.CENTER);
163            top.setAlign(Alignment.LEFT);
164
165            top.setWidth100();
166            top.setHeight(30);
167
168            final Label caption = new Label();
169            caption.setContents("Session ");
170            caption.setAutoWidth();
171
172            saveButton.setIcon("icons/cd.png");
173            saveButton.setWidth(16);
174            saveButton.setTooltip("Save session");
175            saveButton.setStyleName("button-label-disabled");
176            saveButton.setContents("Save");
177            saveButton.addClickHandler(new ClickHandler() {
178                @Override
179                public void onClick(ClickEvent event) {
180                    contents.save();
181                }
182            });
183
184            final Label refreshButton = new Label();
185            refreshButton.setIcon("icons/apply.png");
186            refreshButton.setWidth(16);
187            refreshButton.setTooltip("Refresh session");
188            refreshButton.setStyleName("button-label");
189            refreshButton.setContents("Refresh");
190            refreshButton.addClickHandler(new ClickHandler() {
191                @Override
192                public void onClick(ClickEvent event) {
193                    refreshDialog.showModal();
194                }
195            });
196
197            top.addMember(caption);
198            top.addStrut(5);
199            top.addMember(saveButton);
200            top.addStrut(5);
201            top.addMember(refreshButton);
202
203            return top;
204        }
205
206        private HLayout Row2() {
207            Columns panel = new Columns(Align.LEFT, Align.CENTER);
208            panel.setWidth100();
209            panel.setHeight(50);
210            panel.setStyleName("session-hint");
211            
212            final Label hint = new Label();
213            hint.setWidth100();
214            hint.setHeight100();
215            hint.setContents("Each Session object is associated one-to-one with a "
216                    + "Workspace object. The Workspace object represents a \"view\" "
217                    + "of an actual repository workspace entity as seen through "
218                    + "the authorization settings of its associated Session. ");
219            panel.addMember(hint);
220            return panel;
221        }
222    }
223}