001    package org.apache.myfaces.tobago.example.reference;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    import org.apache.myfaces.tobago.taglib.component.ButtonTag;
023    import org.apache.myfaces.tobago.taglib.component.LinkTag;
024    import org.apache.myfaces.tobago.taglib.extension.InExtensionTag;
025    
026    import javax.servlet.jsp.tagext.TagSupport;
027    import javax.faces.event.ValueChangeEvent;
028    import javax.faces.context.FacesContext;
029    import javax.faces.model.SelectItem;
030    import java.util.ArrayList;
031    import java.util.List;
032    
033    public class Controller {
034    
035      private static final Log LOG = LogFactory.getLog(Controller.class);
036    
037      private List<TagData> tags;
038    
039      private List<AttributeData> attributes;
040    
041      private String text;
042    
043      private boolean bool;
044    
045      private SelectItem[] vehicleOptionItems;
046    
047      private SelectItem[] carOptionItems;
048    
049      private SelectItem[] motorbikeOptionItems;
050    
051      private int vehicle;
052    
053      private int manufacturer;
054    
055      public Controller() {
056        tags = new ArrayList<TagData>();
057        TagData in = new TagData(InExtensionTag.class);
058        in.setName("In");
059        in.setTip("Ein In");
060        tags.add(in);
061        TagData button = new TagData(ButtonTag.class);
062        button.setName("Button");
063        button.setTip("Ein Knopf");
064        tags.add(button);
065        TagData link = new TagData(LinkTag.class);
066        link.setName("Link");
067        link.setTip("Ein Link");
068        tags.add(link);
069        attributes = new ArrayList<AttributeData>();
070        vehicleOptionItems = new SelectItem[]{
071            new SelectItem(new Integer(0), "car"),
072            new SelectItem(new Integer(1), "motorbike")};
073        carOptionItems = new SelectItem[]{
074            new SelectItem(new Integer(0), "Audi"),
075            new SelectItem(new Integer(1), "BMW"),
076            new SelectItem(new Integer(2), "Mercedes")};
077        motorbikeOptionItems = new SelectItem[]{
078            new SelectItem(new Integer(3), "Moto Guzzi"),
079            new SelectItem(new Integer(4), "BMW"),
080            new SelectItem(new Integer(5), "KTM")};
081      }
082    
083    
084      public int getManufacturer() {
085        return manufacturer;
086      }
087    
088      public void setManufacturer(int manufacturer) {
089        this.manufacturer = manufacturer;
090      }
091    
092      public SelectItem[] getSelectItems() {
093        return vehicleOptionItems;
094      }
095    
096      public int getVehicle() {
097        return vehicle;
098      }
099    
100      public String action() {
101        LOG.error("action invoke");
102        return null;
103      }
104    
105      public void setVehicle(int vehicle) {
106        this.vehicle = vehicle;
107      }
108    
109      public SelectItem[] getManufacturerSelectItems() {
110        if (vehicle == 0) {
111          return carOptionItems;
112        } else {
113          return motorbikeOptionItems;
114        }
115      }
116    
117    
118      public void valueChanged(ValueChangeEvent event) {
119        LOG.error("value Changed " + event.getComponent().getClientId(FacesContext.getCurrentInstance()));
120        LOG.error("value Changed " + event.getOldValue() + " " + event.getNewValue());
121      }
122    
123      public TagSupport createTag() {
124        try {
125          Class clazz = tags.get(0).getTagClass();
126          InExtensionTag tag = (InExtensionTag) clazz.newInstance();
127          tag.setValue("Hallo Tester");
128          tag.setLabel("Label");
129          return tag;
130        } catch (Exception e) {
131          LOG.error("", e); // fixme
132          throw new RuntimeException(e);
133        }
134      }
135    
136      public List<TagData> getTags() {
137        return tags;
138      }
139    
140      public List<AttributeData> getAttributes() {
141        return attributes;
142      }
143    
144      public String getText() {
145        return text;
146      }
147    
148      public void setText(String text) {
149        this.text = text;
150      }
151    
152    
153      public boolean isBool() {
154        return bool;
155      }
156    
157      public void setBool(boolean bool) {
158        this.bool = bool;
159      }
160    }
161