View Javadoc
1   /*
2    * $Source$
3    * $Revision$
4    *
5    * Copyright (C) 2000 William Chesters
6    *
7    * Part of Melati (http://melati.org), a framework for the rapid
8    * development of clean, maintainable web applications.
9    *
10   * Melati is free software; Permission is granted to copy, distribute
11   * and/or modify this software under the terms either:
12   *
13   * a) the GNU General Public License as published by the Free Software
14   *    Foundation; either version 2 of the License, or (at your option)
15   *    any later version,
16   *
17   *    or
18   *
19   * b) any version of the Melati Software License, as published
20   *    at http://melati.org
21   *
22   * You should have received a copy of the GNU General Public License and
23   * the Melati Software License along with this program;
24   * if not, write to the Free Software Foundation, Inc.,
25   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
26   * GNU General Public License and visit http://melati.org to obtain the
27   * Melati Software License.
28   *
29   * Feel free to contact the Developers of Melati (http://melati.org),
30   * if you would like to work out a different arrangement than the options
31   * outlined here.  It is our intention to allow Melati to be used by as
32   * wide an audience as possible.
33   *
34   * This program is distributed in the hope that it will be useful,
35   * but WITHOUT ANY WARRANTY; without even the implied warranty of
36   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37   * GNU General Public License for more details.
38   *
39   * Contact details for copyright holder:
40   *
41   *     William Chesters <williamc At paneris.org>
42   *     http://paneris.org/~williamc
43   *     Obrechtstraat 114, 2517VX Den Haag, The Netherlands
44   */
45  
46  package org.melati.poem;
47  
48  import java.util.Hashtable;
49  
50  /**
51   * An attribute of a {@link Column} which indicates what level of 
52   * reporting of a {@link Table} it should be included in.
53   *
54   */
55  public final class DisplayLevel {
56  
57    /** The numeric Id of the Level. */
58    public final Integer index;
59    /** The name of the level. */
60    private final String name;
61  
62    /** Constructor. */
63    private DisplayLevel(int index, String name) {
64      this.index = new Integer(index);
65      this.name = name;
66    }
67    /**
68     * @return the name
69     */
70    public String getName() {
71      return name;
72    }
73  
74    /**
75     * @return the index.
76     */
77    public Integer getIndex() {
78      return index;
79    }
80  
81  
82    /**
83     * Display level of a {@link Column} used as the name of the 
84     * whole record.
85     *
86     * @see Table#displayColumn()
87     */
88    public static final DisplayLevel primary;
89  
90    /**
91     * Display level of {@link Column}s to be included in a summary of 
92     * records in a set.
93     * <p>
94     * This is the default display level for a {@link Column}.
95     *
96     * @see Table#getSummaryDisplayColumns()
97     * @see Column#defaultDisplayLevel()
98     * @see Persistent#getSummaryDisplayFields()
99     */
100   public static final DisplayLevel summary;
101 
102   /**
103    * Display level of {@link Column}s included in display focusing on a
104    * single record, but without detail.
105    *
106    * @see Table#getRecordDisplayColumns()
107    * @see Persistent#getRecordDisplayFields()
108    */
109   public static final DisplayLevel record;
110 
111   /**
112    * Display level of {@link Column}s included in a detailed display
113    * of a single record.
114    *
115    * @see Table#getRecordDisplayColumns()
116    * @see Persistent#getDetailDisplayFields()
117    */
118   public static final DisplayLevel detail;
119 
120   /**
121    * Display level of {@link Column}s hidden from users.
122    */
123   public static final DisplayLevel never;
124 
125   private static int n = 0;
126 
127   private static final DisplayLevel[] displayLevels =
128     { primary = new DisplayLevel(n++, "primary"),
129       summary = new DisplayLevel(n++, "summary"),
130       record = new DisplayLevel(n++, "record"),
131       detail = new DisplayLevel(n++, "detail"),
132       never = new DisplayLevel(n++, "never") };
133 
134   private static final Hashtable<String, DisplayLevel> levelOfName = new Hashtable<String, DisplayLevel>();
135 
136   static {
137     for (int i = 0; i < displayLevels.length; ++i)
138       levelOfName.put(displayLevels[i].name, displayLevels[i]);
139   }
140 
141   /**
142    * Get by numeric id. 
143    * 
144    * @param index the numeric Id of the level
145    * @return the level corresponding to the index
146    */
147   public static DisplayLevel forIndex(int index) {
148     return displayLevels[index];
149   }
150 
151   /**
152    * @return the number of levels. 
153    */
154   public static int count() {
155     return displayLevels.length;
156   }
157 
158  /**
159   * Thrown when a <code>DisplayLevel</code> which doesn't exist is referenced, 
160   * by misspelling for example.
161   */
162   public static class NameUnrecognisedException extends PoemException {
163     private static final long serialVersionUID = 1L;
164 
165     /** The name we did not recognise. */
166     public String name;
167 
168     /** Constructor. */
169     public NameUnrecognisedException(String name) {
170       this.name = name;
171     }
172 
173     /** @return The detail message. */
174     public String getMessage() {
175       return "No display level found which goes by the name `" + name + "'";
176     }
177   }
178 
179   /**
180    * Get by name. 
181    * @param name The name of the required level
182    * @return the named level
183    */
184   public static DisplayLevel named(String name) {
185     DisplayLevel it = (DisplayLevel)levelOfName.get(name);
186     if (it == null)
187       throw new NameUnrecognisedException(name);
188     return it;
189   }
190   
191   /** 
192    * @return the name and index.
193    * {@inheritDoc}
194    * @see java.lang.Object#toString()
195    */
196   public String toString() {
197     return name + "/" + index;
198   }
199 }