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;
017
018import org.modeshape.web.client.repo.RepositoryHeader;
019import org.modeshape.web.client.repo.RepositoriesList;
020import com.google.gwt.core.client.EntryPoint;
021import com.google.gwt.core.client.GWT;
022import com.google.gwt.event.logical.shared.ValueChangeEvent;
023import com.google.gwt.event.logical.shared.ValueChangeHandler;
024import com.smartgwt.client.types.Alignment;
025import com.smartgwt.client.widgets.Canvas;
026import com.smartgwt.client.widgets.layout.VLayout;
027import java.util.Collection;
028import org.modeshape.web.client.contents.Contents;
029import org.modeshape.web.shared.BaseCallback;
030import org.modeshape.web.shared.RepositoryName;
031
032/**
033 * Entry point classes define <code>onModuleLoad()</code>.
034 */
035public class Console implements EntryPoint, ValueChangeHandler<String> {
036    private final static String LAYOUT_WIDTH = "85%";
037    
038    /**
039     * Create a remote service proxy to talk to the server-side Greeting service.
040     */
041    private final JcrServiceAsync jcrService = GWT.create(JcrService.class);
042    
043    //Main form
044    private final VLayout mainForm = new VLayout();
045
046    //Browser's URL and history.
047    private final JcrURL jcrURL = new JcrURL();
048    private final HtmlHistory htmlHistory = new HtmlHistory();
049
050    private final VLayout viewPort = new VLayout();        
051    
052    private final Header header = new Header(this);
053    private final Footer footer = new Footer();
054    
055    private final RepositoriesList  repositoriesList = new RepositoriesList(this, jcrService);
056    private final RepositoryHeader  repositoryHeader = new RepositoryHeader(this);    
057    private final Contents contents = new Contents(this);
058    
059    private final LoadingIcon loadingIcon = new LoadingIcon();
060    
061    /**
062     * Provides data access.
063     * 
064     * @return 
065     */
066    public JcrServiceAsync jcrService() {
067        return this.jcrService;
068    }
069    
070    /**
071     * This is the entry point method.
072     */
073    @Override
074    public void onModuleLoad() {
075        //start from the requested URL
076        jcrService.getRequestedURI(new BaseCallback<String>() {
077
078            @SuppressWarnings( "synthetic-access" )
079            @Override
080            public void onSuccess( String result ) {
081                //parse requested url to determine navigation
082                jcrURL.parse(result);
083  
084                //before navigate to the requested URL we need to
085                //check is this user already logged in or not yet.
086                getCredentials();
087            }
088        });
089    }
090
091    /**
092     * Checks user's credentials.
093     */
094    private void getCredentials() {
095        jcrService.getUserName(new BaseCallback<String>() {
096            @Override
097            public void onSuccess(String name) {
098                showMainForm(name);
099            }
100        });
101    }
102    
103    /**
104     * Reconstructs URL and points browser to the requested node path.
105     */
106    public void loadNodeSpecifiedByURL() {
107        repositoriesList.select(jcrURL.getRepository(), jcrURL.getWorkspace(), jcrURL.getPath(), true);
108    }
109
110    /**
111     * Displays name of the logged in user.
112     * 
113     * @param userName 
114     */
115    protected void changeUserName(String userName) {
116        header.setUserName(userName);
117    }
118    
119    /**
120     * Gets selected repository name.
121     * 
122     * @return repository name;
123     */
124    public String repository() {
125        return this.repositoryHeader.repository();
126    }
127    
128    /**
129     * Shows main page for the logged in user.
130     * 
131     * @param userName the name of the user.
132     */
133    public void showMainForm(String userName) {
134        align();
135        changeUserName(userName);
136
137        mainForm.addMember(header); 
138        mainForm.addMember(repositoryHeader);
139        mainForm.addMember(viewPort);
140        mainForm.addMember(strut(30));
141        mainForm.addMember(footer);
142           
143        setLayoutWidth(LAYOUT_WIDTH);     
144        loadData();
145        
146        //init HTML history
147        htmlHistory.addValueChangeHandler(this);
148        mainForm.draw();        
149    }
150
151    /**
152     * Align components
153     */
154    private void align() {
155        mainForm.setLayoutMargin(5);
156        mainForm.setWidth100();
157        mainForm.setHeight100();
158        mainForm.setBackgroundColor("#FFFFFF");
159        mainForm.setAlign(Alignment.CENTER);
160        mainForm.setLayoutAlign(Alignment.CENTER);
161        mainForm.setDefaultLayoutAlign(Alignment.CENTER);
162        
163        viewPort.setWidth(LAYOUT_WIDTH);
164        viewPort.setLayoutAlign(Alignment.CENTER);
165        viewPort.setAlign(Alignment.CENTER);
166        viewPort.setDefaultLayoutAlign(Alignment.CENTER);
167    }
168    
169    /**
170     * Tests is initial URL points to the specific node.
171     * 
172     * @return true if URL points to the node.
173     */
174    private boolean isInitialUrlPointsNode() {
175        return jcrURL.getRepository() != null && jcrURL.getRepository().length() > 0;
176    }
177
178    /**
179     * Loads data.
180     */
181    private void loadData() {
182        if (isInitialUrlPointsNode()) {
183            loadNodeSpecifiedByURL();
184        } else {
185            loadRepositoriesList();
186        }
187    }
188    
189    /**
190     * Load repositories available for the logged in user.
191     */
192    public void loadRepositoriesList() {
193        repositoriesList.load();
194    }
195        
196    /**
197     * Synchronously aligns header and view port.
198     * 
199     * @param value the relative width;
200     */
201    private void setLayoutWidth(String value) {
202        repositoryHeader.setLayoutWidth(value);
203        viewPort.setWidth(value);
204    }
205    
206    /**
207     * 
208     * @return 
209     */
210    public Contents contents() {
211        return contents;
212    }
213    
214    /**
215     * Hides contents and shows "animated" load icon.
216     */
217    public void showLoadingIcon() {
218        loadingIcon.show(mainForm.getWidth() / 2, mainForm.getHeight() / 2);
219    }
220
221    /**
222     * Hides "animated" load icon and shows contents.
223     */
224    public void hideLoadingIcon() {
225        loadingIcon.hide();
226    }
227    
228    /**
229     * Display repository header with given name.
230     * 
231     * @param name the name of the repository.
232     */
233    public void displayRepository(String name) {
234        repositoryHeader.show(name);
235    }
236    
237    /**
238     * Hides repository header.
239     */
240    public void hideRepository() {
241        repositoryHeader.hide();
242    }
243    
244    /**
245     * Changes repository name in URL displayed by browser.
246     * 
247     * @param name the name of the repository.
248     * @param changeHistory if true store URL changes in history.
249     */
250    public void changeRepositoryInURL(String name, boolean changeHistory) {
251        jcrURL.setRepository(name);
252        if (changeHistory) {
253            htmlHistory.newItem(jcrURL.toString(), false);
254        }
255    }
256
257    /**
258     * Changes workspace in the URL displayed by browser.
259     * 
260     * @param name the name of the repository.
261     * @param changeHistory if true store URL changes in browser's history.
262     */
263    public void changeWorkspaceInURL(String name, boolean changeHistory) {
264        jcrURL.setWorkspace(name);
265        if (changeHistory) {
266            htmlHistory.newItem(jcrURL.toString(), false);
267        }
268    }
269    
270    /**
271     * Changes node path in the URL displayed by browser.
272     * 
273     * @param path the path to the node.
274     * @param changeHistory if true store URL changes in browser's history.
275     */
276    public void changePathInURL(String path, boolean changeHistory) {
277        jcrURL.setPath(path);
278        if (changeHistory) {
279            htmlHistory.newItem(jcrURL.toString(), false);
280        }
281    }
282
283    @Override
284    public void onValueChange(ValueChangeEvent<String> event) {
285        jcrURL.parse2(event.getValue());
286        repositoriesList.select(jcrURL.getRepository(), jcrURL.getWorkspace(), jcrURL.getPath(), false);
287    }
288    
289    /**
290     * Displays list of availables repositories.
291     * 
292     * @param names names of repositories.
293     */
294    public void showRepositories(Collection<RepositoryName> names) {
295        repositoriesList.show(names);
296        display(repositoriesList);
297        this.hideRepository();
298    }
299  
300    /**
301     * Displays node for the given repository, workspace and path.
302     * 
303     * @param repository the name of the repository.
304     * @param workspace the name of workspace.
305     * @param path the path to the node.
306     * @param changeHistory store changes in browser history.
307     */
308    public void displayContent(String repository, String workspace, String path,
309            boolean changeHistory) {
310        contents.show(repository, workspace, path, changeHistory);
311        displayRepository(repository);
312        display(contents);
313        changeRepositoryInURL(repository, changeHistory);
314    }
315    
316    /**
317     * Displays root node for given repository and first available workspace.
318     * 
319     * @param repository the name of the repository.
320     * @param changeHistory store changes in browser history or no.
321     */
322    public void displayContent(String repository, boolean changeHistory) {
323        contents.show(repository, changeHistory);
324        displayRepository(repository);
325        display(contents);
326        changeRepositoryInURL(repository, changeHistory);
327    }
328    
329    public void display(Canvas view) {
330        Canvas[] members = viewPort.getMembers();
331        for (Canvas canvas : members) {
332            canvas.setVisible(false);
333            viewPort.removeChild(canvas);
334        }
335                
336        viewPort.addMember(view);
337        view.setVisible(true);
338        view.show();
339    }
340    
341    private VLayout strut( int height ) {
342        VLayout s = new VLayout();
343        s.setHeight(height);
344        return s;
345    }
346        
347}