KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcckit > graphic > Anchor


1 /*
2  * Copyright 2003-2004, Franz-Josef Elmer, All rights reserved
3  *
4  * This library is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details
13  * (http://www.gnu.org/copyleft/lesser.html).
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jcckit.graphic;
20
21 import jcckit.util.ConfigParameters;
22 import jcckit.util.FactoryException;
23
24 /**
25  * Anchor of a graphical element. There exist only the three
26  * instances {@link #LEFT_BOTTOM}, {@link #CENTER}, and
27  * {@link #RIGHT_TOP}.
28  * <p>
29  * The anchor factor can be used in a position formular. Its value
30  * for the three instances reads:
31  * <p>
32  * <center>
33  * <table border=1 cellpadding=5>
34  * <tr><th>Instance</th><th>Factor</th></tr>
35  * <tr><td><tt>LEFT_BOTTOM</tt></td><td>0</td></tr>
36  * <tr><td><tt>CENTER</tt></td><td>1</td></tr>
37  * <tr><td><tt>RIGHT_TOP</tt></td><td>2</td></tr>
38  * </table>
39  * </center>
40  *
41  * @author Franz-Josef Elmer
42  */

43 public class Anchor {
44   /** Anchor constant. */
45   public static final Anchor LEFT_BOTTOM = new Anchor(0),
46                              CENTER = new Anchor(1),
47                              RIGHT_TOP = new Anchor(2);
48   private static final String JavaDoc LEFT_VALUE = "left",
49                               RIGHT_VALUE = "right",
50                               CENTER_VALUE = "center",
51                               TOP_VALUE = "top",
52                               BOTTOM_VALUE = "bottom";
53
54   /**
55    * Returns form the specified configuration parameters the
56    * horizontal anchor defined by the specified key or the
57    * specified default value.
58    * @param config Configuration parameters.
59    * @param key The key of the anchor. <tt>null</tt> is not allowed.
60    * @param defaultValue The default value.
61    * @return one of the three instances of <tt>Anchor</tt>.
62    * @throws FactoryException if the value of <tt>key</tt> is
63    * neither <tt>left</tt>, <tt>center</tt>,
64    * nor <tt>right</tt>.
65    * Note, that {@link FactoryException#getClassName()}
66    * returns the invalid value.
67    */

68   public static Anchor getHorizontalAnchor(ConfigParameters config, String JavaDoc key,
69                                            Anchor defaultValue) {
70     Anchor result = defaultValue;
71     String JavaDoc anchor = config.get(key, null);
72     if (anchor != null) {
73       if (anchor.equals(LEFT_VALUE)) {
74         result = Anchor.LEFT_BOTTOM;
75       } else if (anchor.equals(CENTER_VALUE)) {
76         result = Anchor.CENTER;
77       } else if (anchor.equals(RIGHT_VALUE)) {
78         result = Anchor.RIGHT_TOP;
79       } else {
80         throw new FactoryException(config, key, "Invalid horizontal anchor.");
81       }
82     }
83     return result;
84   }
85
86   /**
87    * Returns form the specified configuration parameters the
88    * vertical anchor defined by the specified key or the
89    * specified default value.
90    * @param config Configuration parameters.
91    * @param key The key of the anchor. <tt>null</tt> is not allowed.
92    * @param defaultValue The default value.
93    * @return one of the three instances of <tt>Anchor</tt>.
94    * @throws FactoryException if the value of <tt>key</tt> is
95    * neither <tt>top</tt>, <tt>center</tt>,
96    * nor <tt>bottom</tt>.
97    * Note, that {@link FactoryException#getClassName()}
98    * returns the invalid value.
99    */

100   public static Anchor getVerticalAnchor(ConfigParameters config, String JavaDoc key,
101                                   Anchor defaultValue) {
102     Anchor result = defaultValue;
103     String JavaDoc anchor = config.get(key, null);
104     if (anchor != null) {
105       if (anchor.equals(BOTTOM_VALUE)) {
106         result = Anchor.LEFT_BOTTOM;
107       } else if (anchor.equals(CENTER_VALUE)) {
108         result = Anchor.CENTER;
109       } else if (anchor.equals(TOP_VALUE)) {
110         result = Anchor.RIGHT_TOP;
111       } else {
112         throw new FactoryException(config, key, "Invalid vertcal anchor.");
113       }
114     }
115     return result;
116   }
117
118   private final int _factor;
119
120   private Anchor(int factor) {
121     _factor = factor;
122   }
123
124   /** Returns the factor. */
125   public int getFactor() {
126     return _factor;
127   }
128 }
129
130
Popular Tags