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.repo;
017
018import com.smartgwt.client.types.Alignment;
019import com.smartgwt.client.types.VerticalAlignment;
020import com.smartgwt.client.widgets.Img;
021import com.smartgwt.client.widgets.Label;
022import com.smartgwt.client.widgets.events.ClickEvent;
023import com.smartgwt.client.widgets.events.ClickHandler;
024import com.smartgwt.client.widgets.layout.HLayout;
025import com.smartgwt.client.widgets.layout.VLayout;
026import org.modeshape.web.client.Console;
027import org.modeshape.web.client.admin.AdminView;
028import org.modeshape.web.client.nt.NodeTypesModalForm;
029import org.modeshape.web.client.query.QueryModalForm;
030
031/**
032 *
033 * @author kulikov
034 */
035public class RepositoryHeader extends HLayout {
036    
037    private final static int HEIGHT = 55;
038    private final static int LOGO_WIDTH = 45;
039    
040    private Label label = new Label();
041    private String repositoryName;
042    
043    //inner bar with
044    private HLayout mainLayout = new HLayout();
045
046    private final Console console;
047    
048    private final NodeTypesModalForm ntForm;
049    private final QueryModalForm queryForm;
050    private final DescriptorModalForm descriptorForm;
051    private final AdminView adminView;
052    
053    public RepositoryHeader(final Console console) {
054        this.console = console;
055        
056        ntForm = new NodeTypesModalForm(console);
057        queryForm = new QueryModalForm(console);
058        descriptorForm = new DescriptorModalForm(console);
059        adminView = new AdminView(console);
060        //set layout alignment for center        
061        alignAndResize();
062
063        //add main layout separated bounded with struts
064        addMember(new Strut(10));
065        addMember(mainLayout);
066        addMember(new Strut(10));
067
068        //resize main layout
069        mainLayout.setWidth("80%"); //do not modify, otherwise it wont resize it properly (don't know why)
070        mainLayout.setHeight(HEIGHT);
071        
072        //separate main layout on two columns
073        HLayout col1 = new HLayout();
074        col1.setLayoutAlign(VerticalAlignment.CENTER);
075        col1.setDefaultLayoutAlign(VerticalAlignment.CENTER);
076        col1.setWidth100();
077        
078        VLayout col2 = new VLayout();
079        col2.setHeight(30);
080        col2.setLayoutAlign(Alignment.RIGHT);
081        col2.setDefaultLayoutAlign(Alignment.RIGHT);
082        col2.setDefaultLayoutAlign(VerticalAlignment.CENTER);
083        col2.setLayoutAlign(VerticalAlignment.CENTER);
084        col2.setAlign(Alignment.RIGHT);
085        col2.setAutoWidth();
086        
087        mainLayout.addMember(col1);
088        mainLayout.addMember(col2);
089        
090        
091        //put repository name label into left column(col1)
092        label.setStyleName("repository-caption");
093        label.setWidth("100%");
094        label.addClickHandler(new ClickHandler() {
095            @Override
096            public void onClick(ClickEvent event) {
097                console.loadRepositoriesList();
098            }
099        });
100        label.setValign(VerticalAlignment.CENTER);
101
102
103        col1.addMember(new RepositoryIcon(LOGO_WIDTH, HEIGHT));
104        col1.addMember(label);
105
106        //fill column 2
107        col2.addMember(new Toolbar(console));
108    }
109
110    private void alignAndResize() {
111        setStyleName("repository");
112        setLayoutAlign(Alignment.CENTER);
113        setLayoutAlign(VerticalAlignment.CENTER);
114        setDefaultLayoutAlign(Alignment.CENTER);
115        setDefaultLayoutAlign(VerticalAlignment.CENTER);
116
117        setHeight(HEIGHT);
118        setWidth100();
119    }
120
121    /**
122     * Set width of main layout.
123     * 
124     * @param value 
125     */
126    public void setLayoutWidth(String value) {
127        mainLayout.setWidth(value);
128    }
129
130    /**
131     * Gets the name of the repository.
132     * 
133     * @return the name of repository displayed.
134     */
135    public String repository() {
136        return this.repositoryName;
137    }
138
139    /**
140     * Displays given repository name.
141     * 
142     * @param name repository name.
143     */
144    public void show(String name) {
145        this.repositoryName = name;
146        label.setContents("Repository: " + name);
147        show();
148    }
149
150    public void showRepositoryInfo() {
151        descriptorForm.showModal();
152    } 
153    
154    public void showNodeTypes() {
155        ntForm.showModal();
156    }
157    
158    public void showContent() {
159        console.loadNodeSpecifiedByURL();
160    }
161     
162    public void showQuery() {
163        queryForm.showModal();
164    }
165
166    public void showAdmin() {
167        console.display(adminView);
168    }
169    
170    /**
171     * Icon image near repository name
172     */
173    private class RepositoryIcon extends Img {
174
175        private final static String IMG_PATH = "icons/attach.png";
176
177        public RepositoryIcon(int width, int height) {
178            setSrc(IMG_PATH);
179            setWidth(width);
180            setHeight(height);
181            setValign(VerticalAlignment.CENTER);
182        }
183    }
184
185    private class Toolbar extends HLayout {
186        
187        public Toolbar(final Console console) {
188            setHeight(30);
189            setLayoutAlign(Alignment.RIGHT);
190            setDefaultLayoutAlign(Alignment.RIGHT);
191            setAlign(Alignment.RIGHT);
192            setStyleName("viewport");
193            setAutoWidth();
194
195            ToolButton explore = new ToolButton("Explore");
196            explore.addClickHandler(new ClickHandler() {
197                @Override
198                public void onClick(ClickEvent event) {
199                    showContent();
200                }
201            });
202
203            ToolButton nodeTypes = new ToolButton("NodeTypes");
204            nodeTypes.addClickHandler(new ClickHandler() {
205                @Override
206                public void onClick(ClickEvent event) {
207                    showNodeTypes();
208                }
209            });
210
211            ToolButton descriptor = new ToolButton("Descriptor");
212            descriptor.addClickHandler(new ClickHandler() {
213                @Override
214                public void onClick(ClickEvent event) {
215                    showRepositoryInfo();
216                }
217            });
218
219            ToolButton query = new ToolButton("Query");
220            query.addClickHandler(new ClickHandler() {
221                @Override
222                public void onClick(ClickEvent event) {
223                    showQuery();
224                }
225            });
226
227            ToolButton admin = new ToolButton("Admin");
228            admin.addClickHandler(new ClickHandler() {
229                @Override
230                public void onClick(ClickEvent event) {
231                    showAdmin();
232                }
233            });
234
235            addMember(new Spacer(10));
236            addMember(explore);
237            addMember(new Spacer(10));
238            addMember(nodeTypes);
239            addMember(new Spacer(10));
240            addMember(descriptor);
241            addMember(new Spacer(10));
242            addMember(query);
243            addMember(new Spacer(10));
244            addMember(admin);
245            addMember(new Spacer(10));
246            setHeight(30);
247            
248        }
249    }
250
251    private class ToolButton extends Label {
252
253        public ToolButton(String title) {
254            super("<b>" + title + "</b>");
255            setAlign(Alignment.RIGHT);
256            setAutoWidth();
257            setStyleName("tab-label");
258        }
259    }
260
261    private class Spacer extends HLayout {
262
263        protected Spacer(int size) {
264            super();
265            setWidth(size);
266        }
267    }
268
269    private class Strut extends VLayout {
270
271        public Strut(int size) {
272            super();
273            setHeight(size);
274        }
275    }
276}