svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / layout / mapbox / model / CellImpl.java @ 1714
History | View | Annotate | Download (4.28 KB)
1 | 1714 | fdiaz | /*
|
---|---|---|---|
2 | * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||
3 | * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||
4 | */
|
||
5 | package org.gvsig.layout.mapbox.model; |
||
6 | |||
7 | //import java.awt.Rectangle;
|
||
8 | |||
9 | import java.awt.geom.Rectangle2D; |
||
10 | import org.gvsig.tools.ToolsLocator; |
||
11 | import org.gvsig.tools.dynobject.DynStruct; |
||
12 | import org.gvsig.tools.persistence.PersistenceManager; |
||
13 | import org.gvsig.tools.persistence.PersistentState; |
||
14 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
15 | |||
16 | |||
17 | /**
|
||
18 | *
|
||
19 | * @author fdiaz
|
||
20 | */
|
||
21 | public class CellImpl implements Cell { |
||
22 | |||
23 | public static final String PERSISTENCE_DEFINITION_NAME = "Cell"; |
||
24 | |||
25 | Rectangle2D r;
|
||
26 | |||
27 | public CellImpl() {
|
||
28 | } |
||
29 | |||
30 | public CellImpl(double x, double y, double width, double height) { |
||
31 | this.r = new Rectangle2D.Double(x, y, width, height); |
||
32 | } |
||
33 | |||
34 | @Override
|
||
35 | public Cell clone() throws CloneNotSupportedException { |
||
36 | CellImpl other = (CellImpl) super.clone();
|
||
37 | if(r != null) { |
||
38 | other.r = (Rectangle2D) r.clone();
|
||
39 | } |
||
40 | return other;
|
||
41 | } |
||
42 | |||
43 | @Override
|
||
44 | public double getX() { |
||
45 | return r.getX();
|
||
46 | } |
||
47 | |||
48 | @Override
|
||
49 | public void setX(double x) { |
||
50 | ((Rectangle2D.Double)this.r).x = x; |
||
51 | } |
||
52 | |||
53 | @Override
|
||
54 | public double getY() { |
||
55 | return r.getY();
|
||
56 | } |
||
57 | |||
58 | @Override
|
||
59 | public void setY(double y) { |
||
60 | ((Rectangle2D.Double)this.r).y = y; |
||
61 | } |
||
62 | |||
63 | @Override
|
||
64 | public double getWidth() { |
||
65 | return r.getWidth();
|
||
66 | } |
||
67 | |||
68 | @Override
|
||
69 | public void setWidth(double width) { |
||
70 | ((Rectangle2D.Double)this.r).width = width; |
||
71 | } |
||
72 | |||
73 | @Override
|
||
74 | public double getHeight() { |
||
75 | return r.getHeight();
|
||
76 | } |
||
77 | |||
78 | @Override
|
||
79 | public void setHeight(double height) { |
||
80 | ((Rectangle2D.Double)this.r).height = height; |
||
81 | } |
||
82 | |||
83 | @Override
|
||
84 | public String toString() { |
||
85 | // return "y:" + this.r.y + " x:" + this.r.x + " width:" + this.r.width + " height:" + this.r.height;
|
||
86 | return "("+this.r.getX()+", " + this.r.getY() + ", " + this.r.getWidth() + ", " + this.r.getHeight()+")"; |
||
87 | } |
||
88 | |||
89 | @Override
|
||
90 | public boolean contains(double x, double y) { |
||
91 | return r.contains(x, y);
|
||
92 | // return(this.r.x < x && this.r.x+this.r.width > x && this.r.y < y && this.r.y+this.r.height > y);
|
||
93 | } |
||
94 | |||
95 | @Override
|
||
96 | public boolean isContained(Rectangle2D rect) { |
||
97 | return rect.contains(this.r); |
||
98 | } |
||
99 | |||
100 | @Override
|
||
101 | public boolean intersects(Rectangle2D rect) { |
||
102 | return rect.intersects(this.r); |
||
103 | } |
||
104 | |||
105 | @Override
|
||
106 | public void join(Cell c) { |
||
107 | this.r.add(((CellImpl) c).r);
|
||
108 | } |
||
109 | |||
110 | @Override
|
||
111 | public Rectangle2D getRectangle() { |
||
112 | return r;
|
||
113 | } |
||
114 | |||
115 | @Override
|
||
116 | public boolean equals(Object o) { |
||
117 | if(!(o instanceof Cell)){ |
||
118 | return false; |
||
119 | } |
||
120 | Cell other = (Cell)o; |
||
121 | return this.r.equals(other.getRectangle()); |
||
122 | } |
||
123 | |||
124 | @Override
|
||
125 | public int hashCode() { |
||
126 | return this.r.hashCode(); |
||
127 | } |
||
128 | |||
129 | public static void registerPersistent() { |
||
130 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
131 | if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) { |
||
132 | DynStruct definition = |
||
133 | manager.addDefinition(CellImpl.class, |
||
134 | PERSISTENCE_DEFINITION_NAME, |
||
135 | "Cell persistence definition", null, null); |
||
136 | |||
137 | definition.addDynFieldDouble("x").setMandatory(true); |
||
138 | definition.addDynFieldDouble("y").setMandatory(true); |
||
139 | definition.addDynFieldDouble("width").setMandatory(true); |
||
140 | definition.addDynFieldDouble("height").setMandatory(true); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | |||
145 | @Override
|
||
146 | public void saveToState(PersistentState state) throws PersistenceException { |
||
147 | state.set("x", this.r.getX()); |
||
148 | state.set("y", this.r.getY()); |
||
149 | state.set("width", this.r.getWidth()); |
||
150 | state.set("height", this.r.getHeight()); |
||
151 | } |
||
152 | |||
153 | @Override
|
||
154 | public void loadFromState(PersistentState state) throws PersistenceException { |
||
155 | double x = state.getDouble("x"); |
||
156 | double y = state.getDouble("y"); |
||
157 | double width = state.getDouble("width"); |
||
158 | double height = state.getDouble("height"); |
||
159 | this.r = new Rectangle2D.Double(x, y, width, height); |
||
160 | } |
||
161 | |||
162 | |||
163 | } |