KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > tagutil > WidgetIdBean


1 /**
2  * May 9, 2006
3  *
4  * Copyright 2004 uitags
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package net.sf.uitags.tagutil;
19
20 import javax.servlet.jsp.PageContext JavaDoc;
21
22 public class WidgetIdBean {
23   private static final String JavaDoc INSTANCE_KEY = WidgetIdBean.class.getName();
24
25   private String JavaDoc id;
26   private String JavaDoc name;
27   private PageContext JavaDoc pageContext;
28
29   public void setPageContext(PageContext JavaDoc pageContext) {
30     this.pageContext = pageContext;
31   }
32
33   public void setId(String JavaDoc id) {
34     this.id = id;
35   }
36
37   public void setName(String JavaDoc name) {
38     this.name = name;
39   }
40
41   /**
42    * Returns widget's ID. Auto-generates unique ID if necessary.
43    */

44   public String JavaDoc getId() {
45     if (this.id == null && this.name == null) {
46       long instanceId = ScopedIdGenerator.nextId(
47           PageContext.REQUEST_SCOPE, INSTANCE_KEY, this.pageContext) - 1;
48       this.id = INSTANCE_KEY + instanceId;
49       this.pageContext.getRequest().setAttribute("id", this.id);
50     }
51
52     // If one of ID or name is not null, return the ID without auto-generation.
53
return this.id;
54   }
55
56   /**
57    * Returns widget's ID. Auto-generates unique ID if necessary.
58    */

59   public String JavaDoc getName() {
60     return this.name;
61   }
62 }
63
Popular Tags