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.google.gwt.user.client.rpc.AsyncCallback;
019import com.smartgwt.client.util.SC;
020import com.smartgwt.client.widgets.Canvas;
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.VLayout;
025import java.util.Collection;
026import org.modeshape.web.client.Console;
027import org.modeshape.web.client.JcrServiceAsync;
028import org.modeshape.web.shared.RepositoryName;
029
030/**
031 *
032 * @author kulikov
033 */
034public class RepositoriesList extends VLayout {
035    private RepositoryItem[] items = new RepositoryItem[100];
036    private JcrServiceAsync jcrService;
037    private Console console;
038    private String selected;
039    
040    public RepositoriesList(Console console, JcrServiceAsync jcrService) {
041        this.console = console;
042        this.jcrService = jcrService;
043                
044        for (int i = 0; i < items.length; i++) {
045            items[i] = new RepositoryItem();
046            addMember(items[i]);
047        }
048    }
049    
050    public String getSelected() {
051        return selected;
052    }
053    
054    public void show(Collection<RepositoryName> repos) {
055        for (int i = 0; i < items.length; i++) {
056            items[i].setVisible(false);
057        }
058        
059        int i = 0;
060        for (RepositoryName repo : repos) {
061            items[i++].show(repo.getName(), repo.getDescriptor());
062        }
063    }
064    
065    public void load() {
066        jcrService.getRepositories(new AsyncCallback<Collection<RepositoryName>>() {
067
068            @Override
069            public void onFailure(Throwable caught) {
070                SC.say(caught.getMessage());
071            }
072
073            @SuppressWarnings( "synthetic-access" )
074            @Override
075            public void onSuccess(Collection<RepositoryName> result) {
076                try {
077                    console.hideRepository();
078                    show(result);
079                    console.display(RepositoriesList.this);
080                } catch (Exception e) {
081                    SC.say(e.getMessage());
082                }
083            }
084        });
085    }
086    
087    public void select(String repository, String workspace, String path, boolean changeHistory) {
088        selected = repository;
089        console.displayContent(repository, workspace, path, changeHistory);
090    }
091    
092    private class RepositoryItem extends VLayout {
093        private Label name = new Label();
094        private Canvas descriptor = new Label();
095        
096        public RepositoryItem() {
097            super();
098            setVisible(false);
099            setStyleName("repository");
100            
101            name.setHeight(30);
102            name.setStyleName("repository-name");
103            name.setIcon("icons/logo-1.png");
104            name.addClickHandler(new ClickHandler() {
105                @SuppressWarnings( "synthetic-access" )
106                @Override
107                public void onClick(ClickEvent event) {
108                    Label repo = (Label)event.getSource(); 
109                    selected = repo.getContents();
110                    console.displayContent(selected, true);
111                }
112            });
113            addMember(name);
114            addMember(descriptor);
115        }
116        
117        public void show(String name, String descriptor) {
118            this.name.setContents(name);
119            this.descriptor.setContents(descriptor);
120            this.show();
121        }
122    }
123}