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.widgets.Label;
019import com.smartgwt.client.widgets.events.ClickEvent;
020import com.smartgwt.client.widgets.events.ClickHandler;
021import com.smartgwt.client.widgets.layout.HLayout;
022
023/**
024 *
025 * @author kulikov
026 */
027public class PathControl extends HLayout {
028    private final static String PATH_SEPARATOR = "/";
029    
030    private final Label[] segments = new Label[50];
031    private final Label[] separators = new Label[50];
032
033    private final Label addButton = new Label();
034    private final Label importButton = new Label();
035    private final Label exportButton = new Label();
036    
037    public PathControl(final Contents contents) {
038        super();
039        setHeight(30);
040
041        Label path = new Label();
042        path.setAutoWidth();
043        path.setContents("<b>Node:</b>");
044        path.setStyleName("text");
045
046        addMember(path);
047
048
049        for (int i = 0; i < segments.length; i++) {
050            segments[i] = new Label();
051            segments[i].setAutoWidth();
052            segments[i].setStyleName("segment");
053            segments[i].addClickHandler(new ClickHandler() {
054                @Override
055                public void onClick(ClickEvent event) {
056                    Label label = (Label) event.getSource();
057                    contents.getAndDisplayNode(label.getDataPath(), true);
058                }
059            });
060
061            separators[i] = new Label();
062            separators[i].setContents(PATH_SEPARATOR);
063            separators[i].setVisible(false);
064            separators[i].setAutoWidth();
065            separators[i].setStyleName("segment-separator");
066            separators[i].setMargin(3);
067
068            addMember(segments[i]);
069            addMember(separators[i]);
070            
071        }
072        
073        
074        addButton.setIcon("icons/add.png");
075        addButton.setWidth(16);
076        addButton.setTooltip("Add new node");
077        addButton.setStyleName("button-label");
078        addButton.addClickHandler(new ClickHandler() {
079            @Override
080            public void onClick(ClickEvent event) {
081                contents.showAddNodeDialog();
082            }
083        });
084        
085        importButton.setIcon("icons/folder_add.png");
086        importButton.setWidth(16);
087        importButton.setTooltip("Import nodes under this node");
088        importButton.setStyleName("button-label");
089        importButton.addClickHandler(new ClickHandler() {
090            @Override
091            public void onClick(ClickEvent event) {
092                contents.showImportDialog();
093            }
094        });
095        
096        exportButton.setIcon("icons/folder_go.png");
097        exportButton.setWidth(16);
098        exportButton.setTooltip("Export this node");
099        exportButton.setStyleName("button-label");
100        exportButton.addClickHandler(new ClickHandler() {
101
102            @Override
103            public void onClick(ClickEvent event) {
104                contents.showExportDialog();
105            }
106        });
107        
108        addMember(addButton);
109        addMember(importButton);
110        addMember(exportButton);
111    }
112
113    
114    public void display(String url) {
115        for (int i = 0; i < segments.length; i++) {
116            segments[i].setContents("");
117            separators[i].setVisible(false);
118        }
119
120        String[] tokens = url.split(PATH_SEPARATOR);
121        if (tokens.length == 0) {
122            tokens = new String[]{PATH_SEPARATOR};
123        }
124
125        for (int i = 0; i < tokens.length; i++) {
126
127            segments[i].setContents(tokens[i]);
128
129            String path = "";
130            for (int j = 0; j <= i; j++) {
131                path += (PATH_SEPARATOR + segments[j].getContents());
132            }
133
134            segments[i].setTooltip(path);
135            segments[i].setDataPath(path);
136            segments[i].draw();
137
138            if (i < tokens.length - 1) {
139                separators[i].setVisible(true);
140            }
141        }
142    }
143}