KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtriver > open > vectorvisuals > LODManager


1 /*
2  * LODManager.java
3  *
4  * Created on Mar 19, 2006 at 2:12:26 PM
5  */

6 package com.thoughtriver.open.vectorvisuals;
7
8 import java.util.*;
9
10 /**
11  * This class keeps track of the levels of detail that have been specified for a
12  * particular <CODE>VisualObject</CODE>. It provides a facility for the
13  * appropriate level of detail to be returned for a given scale value.
14  * <P>
15  * An instance of this class is kept by every <CODE>VisualObject</CODE> that
16  * has any levels of detail specified.
17  *
18  * @author Brandon Franklin
19  * @version $Date: 2006/11/25 09:18:36 $
20  */

21 public class LODManager {
22
23     /** The <CODE>SortedSet</CODE> of */
24     final private SortedSet<Level> levels;
25
26     /**
27      * Constructs a new instance of <CODE>LODManager</CODE>.
28      */

29     public LODManager() {
30         levels = new TreeSet<Level>();
31         addLevel(Double.MAX_VALUE, Integer.MAX_VALUE);
32     }
33
34     /**
35      * Adds a new level of detail to this manager.
36      *
37      * @param scale the scale at and below which the specified level should come
38      * into effect
39      * @param detail the level of detail that should be active below the
40      * specified scale
41      */

42     public void addLevel(final double scale, final int detail) {
43         Level level = new Level(scale, detail);
44         levels.add(level);
45     }
46
47     /**
48      * Given a scale value, returns the level of detail that should be active.
49      *
50      * @param scale the scale value being asked about
51      * @return the level of detail that should be active
52      */

53     public int getLevel(final double scale) {
54         for (Level level : levels) {
55             if (scale <= level.getScale()) {
56                 return level.getDetail();
57             }
58         }
59
60         // This should never happen
61
return 0;
62     }
63
64     /**
65      * An instance of this class is used to represent a switch to a particular
66      * detail level at a particular scale value and below.
67      *
68      * @author Brandon Franklin
69      */

70     private class Level implements Comparable JavaDoc<Level> {
71
72         /** The scale below which the <CODE>Level</CODE> applies */
73         final private double scale;
74
75         /** The detail level to apply when this <CODE>Level</CODE> is in effect */
76         final private int detail;
77
78         /**
79          * Constructs a new instance of <CODE>Level</CODE> to represent a
80          * switch to the specified detail level at the specified scale value and
81          * below.
82          *
83          * @param scale the scale below which the <CODE>Level</CODE> applies
84          * @param detail the detail level to apply when this <CODE>Level</CODE>
85          * is in effect
86          */

87         Level(final double scale, final int detail) {
88             this.scale = scale;
89             this.detail = detail;
90         }
91
92         /**
93          * {@inheritDoc}
94          */

95         public int compareTo(final Level o) {
96             double diff = getScale() - o.getScale();
97             return (diff < 0 ? -1 : 1);
98         }
99
100         /**
101          * Returns the value of detail.
102          *
103          * @return the value of detail
104          */

105         int getDetail() {
106             return detail;
107         }
108
109         /**
110          * Returns the value of scale.
111          *
112          * @return the value of scale
113          */

114         double getScale() {
115             return scale;
116         }
117
118     }
119 }
120
Popular Tags