001// Copyright 2004, 2005 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry.contrib.inspector;
016
017import java.util.HashMap;
018import java.util.Map;
019
020import org.apache.tapestry.IComponent;
021import org.apache.tapestry.IPage;
022import org.apache.tapestry.IRequestCycle;
023import org.apache.tapestry.components.Block;
024import org.apache.tapestry.html.BasePage;
025
026/**
027 *  The Tapestry Inspector page.
028 *
029 *  @author Howard Lewis Ship
030 **/
031
032public abstract class Inspector extends BasePage
033{
034    private Map _blocks = new HashMap();
035
036    protected void finishLoad()
037    {
038        _blocks.put(View.TEMPLATE, getComponent("templateBlock"));
039        _blocks.put(View.SPECIFICATION, getComponent("specificationBlock"));
040        _blocks.put(View.ENGINE, getComponent("engineBlock"));
041        _blocks.put(View.PROPERTIES, getComponent("propertiesBlock"));
042    }
043
044    public abstract String getView();
045
046    public abstract void setView(String value);
047
048    public abstract String getInspectedPageName();
049    
050    public abstract void setInspectedPageName(String value);
051
052    public abstract String getInspectedIdPath();
053
054    public abstract void setInspectedIdPath(String value);
055
056    /** 
057     *  Invoked to change the component being inspected within the current
058     *  page.
059     *
060     *  @since 1.0.6
061     **/
062
063    public void selectComponent(String idPath)
064    {
065        setInspectedIdPath(idPath);
066    }
067
068    /**
069     *  Method invoked by the {@link InspectorButton} component, 
070     *  to begin inspecting a page.
071     *
072     **/
073
074    public void inspect(String pageName, IRequestCycle cycle)
075    {
076        setInspectedPageName(pageName);
077        selectComponent((String) null);
078
079        cycle.activate(this);
080    }
081
082    /**
083     *  Listener for the component selection, which allows a particular component.  
084     *  
085     *  <p>The context is a single string,
086     *  the id path of the component to be selected (or null to inspect
087     *  the page itself).  This invokes
088     *  {@link #selectComponent(String)}.
089     *
090     **/
091
092    public void selectComponent(IRequestCycle cycle)
093    {
094        Object[] parameters = cycle.getListenerParameters();
095
096        String newIdPath;
097
098        // The up button may generate a null context.
099
100        if (parameters == null || parameters.length == 0)
101            newIdPath = null;
102        else
103            newIdPath = (String) parameters[0];
104
105        selectComponent(newIdPath);
106    }
107
108    /**
109     *  Returns the {@link IPage} currently inspected by the Inspector, as determined
110     *  from the inspectedPageName property.
111     *
112     **/
113
114    public IPage getInspectedPage()
115    {
116        return getRequestCycle().getPage(getInspectedPageName());
117    }
118
119    /**
120     *  Returns the {@link IComponent} current inspected; this is determined
121     *  from the inspectedPageName and inspectedIdPath properties.
122     *
123     **/
124
125    public IComponent getInspectedComponent()
126    {
127        return getInspectedPage().getNestedComponent(getInspectedIdPath());
128    }
129
130    public String getInspectorTitle()
131    {
132        return "Tapestry Inspector: " + getEngine().getSpecification().getName();
133    }
134
135    /**
136     *  Returns the {@link Block} for the currently selected view.
137     *
138     **/
139
140    public Block getBlockForView()
141    {
142        return (Block) _blocks.get(getView());
143    }
144}