001    // Copyright 2008 Thiago H. de Paula Figueiredo
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    
015    package br.com.arsmachina.tapestrycrud.components;
016    
017    import org.apache.tapestry5.Asset;
018    import org.apache.tapestry5.BindingConstants;
019    import org.apache.tapestry5.annotations.IncludeStylesheet;
020    import org.apache.tapestry5.annotations.Parameter;
021    import org.apache.tapestry5.annotations.Property;
022    import org.apache.tapestry5.corelib.components.Grid;
023    
024    import br.com.arsmachina.tapestrycrud.Constants;
025    
026    /**
027     * Component that renders the action links in a listing page. It is meant to be used in a
028     * {@link Grid} column. <a
029     * href="http://ars-machina.svn.sourceforge.net/viewvc/ars-machina/example/trunk/src/main/webapp/project/ListProject.tml?view=markup"
030     * >Ars Machina Project Example</a>. The default icons used are taken from the <a
031     * href="http://www.famfamfam.com/lab/icons/silk/">Silk</a> icon set (Creative Commons Attribution
032     * 2.5 License).
033     * 
034     * @author Thiago H. de Paula Figueiredo
035     */
036    @IncludeStylesheet(Constants.TAPESTRY_CRUD_CSS_ASSET)
037    public class ActionLinks {
038    
039            private static final String IMAGES_ASSET_ROOT = "asset:classpath:/br/com/arsmachina/tapestrycrud/components/images/";
040    
041            private static final String DEFAULT_EDIT_ICON_ASSET = IMAGES_ASSET_ROOT + "edit.png";
042    
043            private static final String DEFAULT_DELETE_ICON_ASSET = IMAGES_ASSET_ROOT + "delete.png";
044    
045            private static final String DEFAULT_VIEW_ICON_ASSET = IMAGES_ASSET_ROOT + "view.png";
046    
047            /**
048             * Name of the page that is used to edits objects listed in this page.
049             */
050            @Parameter(defaultPrefix = BindingConstants.LITERAL)
051            @SuppressWarnings("unused")
052            private String editPage;
053    
054            /**
055             * Name of the page that is used to edits objects listed in this page.
056             */
057            @Parameter(defaultPrefix = BindingConstants.LITERAL)
058            @SuppressWarnings("unused")
059            private String viewPage;
060    
061            /**
062             * Show the edit link?
063             */
064            @Parameter(value = "true")
065            @Property
066            @SuppressWarnings("unused")
067            private boolean edit;
068    
069            /**
070             * Show the remove link?
071             */
072            @Parameter(value = "true")
073            @Property
074            @SuppressWarnings("unused")
075            private boolean remove;
076    
077            /**
078             * Show the view link?
079             */
080            @Parameter(value = "false")
081            @Property
082            @SuppressWarnings("unused")
083            private boolean view;
084    
085            /**
086             * The object that the links will refer to.
087             */
088            @Parameter(required = true)
089            @Property
090            @SuppressWarnings("unused")
091            private Object object;
092    
093            @Parameter(defaultPrefix = BindingConstants.ASSET, value = DEFAULT_EDIT_ICON_ASSET)
094            @Property
095            @SuppressWarnings("unused")
096            private Asset editIcon;
097    
098            @Parameter(defaultPrefix = BindingConstants.ASSET, value = DEFAULT_VIEW_ICON_ASSET)
099            @Property
100            @SuppressWarnings("unused")
101            private Asset viewIcon;
102    
103            @Parameter(defaultPrefix = BindingConstants.ASSET, value = DEFAULT_DELETE_ICON_ASSET)
104            @Property
105            @SuppressWarnings("unused")
106            private Asset deleteIcon;
107    
108            /**
109             * Returns the value of the <code>editPage</code> property.
110             * 
111             * @return a {@link String}.
112             */
113            public String getEditPage() {
114    
115                    if (edit && (editPage == null || editPage.trim().length() == 0)) {
116                            
117                            throw new IllegalArgumentException(
118                                            "When parameter edit is true, parameter editPage must have a non-empty value");
119                            
120                    }
121    
122                    return editPage;
123            }
124    
125            /**
126             * Returns the value of the <code>viewPage</code> property.
127             * 
128             * @return a {@link String}.
129             */
130            public String getViewPage() {
131    
132                    if (view && viewPage == null || viewPage.trim().length() == 0) {
133                            
134                            throw new IllegalArgumentException(
135                                            "When parameter view is true, parameter viewPage must have a non-empty value");
136                            
137                    }
138                    
139                    return viewPage;
140                    
141            }
142    
143    }