gvsig-projects-pool / org.gvsig.topology / trunk / org.gvsig.topology / org.gvsig.topology.swing / org.gvsig.topology.swing.impl / src / main / java / org / gvsig / topology / swing / impl / CreateRuleDialog.java @ 728
History | View | Annotate | Download (11 KB)
1 | 688 | jjdelcerro | /*
|
---|---|---|---|
2 | * To change this license header, choose License Headers in Project Properties.
|
||
3 | * To change this template file, choose Tools | Templates
|
||
4 | * and open the template in the editor.
|
||
5 | */
|
||
6 | package org.gvsig.topology.swing.impl; |
||
7 | |||
8 | 727 | jjdelcerro | import java.awt.Component; |
9 | 688 | jjdelcerro | import java.awt.Dimension; |
10 | import java.awt.event.ItemEvent; |
||
11 | import java.awt.event.ItemListener; |
||
12 | 727 | jjdelcerro | import java.util.Objects; |
13 | 688 | jjdelcerro | import javax.swing.DefaultComboBoxModel; |
14 | 727 | jjdelcerro | import javax.swing.DefaultListCellRenderer; |
15 | 728 | jjdelcerro | import javax.swing.Icon; |
16 | 727 | jjdelcerro | import javax.swing.ImageIcon; |
17 | import javax.swing.JLabel; |
||
18 | import javax.swing.JList; |
||
19 | import javax.swing.ListCellRenderer; |
||
20 | 688 | jjdelcerro | import javax.swing.event.DocumentEvent; |
21 | import javax.swing.event.DocumentListener; |
||
22 | 727 | jjdelcerro | import org.gvsig.fmap.dal.DataStore; |
23 | import org.gvsig.fmap.dal.exception.DataException; |
||
24 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
25 | import org.gvsig.fmap.geom.Geometry; |
||
26 | import org.gvsig.fmap.geom.GeometryUtils; |
||
27 | 688 | jjdelcerro | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
28 | import org.gvsig.tools.swing.api.ToolsSwingManager; |
||
29 | import org.gvsig.tools.swing.api.windowmanager.Dialog; |
||
30 | import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
||
31 | 727 | jjdelcerro | import org.gvsig.tools.swing.icontheme.IconTheme; |
32 | 688 | jjdelcerro | import org.gvsig.topology.lib.api.TopologyDataSet; |
33 | import org.gvsig.topology.lib.api.TopologyLocator; |
||
34 | import org.gvsig.topology.lib.api.TopologyManager; |
||
35 | import org.gvsig.topology.lib.api.TopologyPlan; |
||
36 | import org.gvsig.topology.lib.api.TopologyRule; |
||
37 | import org.gvsig.topology.lib.api.TopologyRuleFactory; |
||
38 | |||
39 | /**
|
||
40 | *
|
||
41 | * @author jjdelcerro
|
||
42 | */
|
||
43 | @SuppressWarnings("UseSpecificCatch") |
||
44 | public class CreateRuleDialog extends CreateRuleDialogView { |
||
45 | |||
46 | 727 | jjdelcerro | private class DataSetListCellRenderer extends DefaultListCellRenderer { |
47 | |||
48 | 728 | jjdelcerro | private Icon pointIcon; |
49 | private Icon lineIcon; |
||
50 | private Icon polygonIcon; |
||
51 | private Icon multipolygonIcon; |
||
52 | private Icon multilineIcon; |
||
53 | private Icon multipointIcon; |
||
54 | 727 | jjdelcerro | |
55 | public DataSetListCellRenderer() {
|
||
56 | IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
||
57 | if( iconTheme.exists("layer-type-point") ) { |
||
58 | this.pointIcon = iconTheme.get("layer-type-point"); |
||
59 | } |
||
60 | if( iconTheme.exists("layer-type-line") ) { |
||
61 | this.lineIcon = iconTheme.get("layer-type-line"); |
||
62 | } |
||
63 | if( iconTheme.exists("layer-type-polygon") ) { |
||
64 | this.polygonIcon = iconTheme.get("layer-type-polygon"); |
||
65 | } |
||
66 | 728 | jjdelcerro | if( iconTheme.exists("layer-type-multipoint") ) { |
67 | this.multipointIcon = iconTheme.get("layer-type-multipoint"); |
||
68 | } |
||
69 | if( iconTheme.exists("layer-type-multiline") ) { |
||
70 | this.multilineIcon = iconTheme.get("layer-type-multiline"); |
||
71 | } |
||
72 | if( iconTheme.exists("layer-type-multipolygon") ) { |
||
73 | this.multipolygonIcon = iconTheme.get("layer-type-multipolygon"); |
||
74 | } |
||
75 | 727 | jjdelcerro | } |
76 | |||
77 | @Override
|
||
78 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
||
79 | JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
||
80 | this.setText(Objects.toString(value, "")); |
||
81 | this.setIcon(null); |
||
82 | if( value instanceof TopologyDataSet ) { |
||
83 | DataStore store = ((TopologyDataSet) value).getStore(); |
||
84 | if( store instanceof FeatureStore ) { |
||
85 | try {
|
||
86 | int geomtype = ((FeatureStore) store).getDefaultFeatureType().getDefaultGeometryAttribute().getGeomType().getType();
|
||
87 | if( GeometryUtils.isSubtype(Geometry.TYPES.POINT, geomtype) ) {
|
||
88 | this.setIcon(this.pointIcon); |
||
89 | 728 | jjdelcerro | } else if( GeometryUtils.isSubtype(Geometry.TYPES.CURVE, geomtype) ) { |
90 | this.setIcon(this.lineIcon); |
||
91 | 727 | jjdelcerro | } else if( GeometryUtils.isSubtype(Geometry.TYPES.SURFACE, geomtype) ) { |
92 | this.setIcon(this.polygonIcon); |
||
93 | 728 | jjdelcerro | } else if( GeometryUtils.isSubtype(Geometry.TYPES.MULTIPOINT, geomtype) ) { |
94 | this.setIcon(this.multipointIcon); |
||
95 | } else if( GeometryUtils.isSubtype(Geometry.TYPES.MULTICURVE, geomtype) ) { |
||
96 | this.setIcon(this.multilineIcon); |
||
97 | } else if( GeometryUtils.isSubtype(Geometry.TYPES.MULTISURFACE, geomtype) ) { |
||
98 | this.setIcon(this.multipolygonIcon); |
||
99 | 727 | jjdelcerro | } |
100 | } catch (DataException ex) {
|
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | return label;
|
||
105 | } |
||
106 | |||
107 | } |
||
108 | 688 | jjdelcerro | private final TopologyPlan plan; |
109 | private Dialog dialog; |
||
110 | |||
111 | CreateRuleDialog(TopologyPlan plan) { |
||
112 | this.plan = plan;
|
||
113 | this.dialog = null; |
||
114 | this.initComponents();
|
||
115 | } |
||
116 | |||
117 | private void initComponents() { |
||
118 | ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
||
119 | |||
120 | toolsSwingManager.setDefaultPopupMenu(this.txtDescription);
|
||
121 | this.txtDescription.setPreferredSize(new Dimension(200,200)); |
||
122 | |||
123 | DefaultComboBoxModel<TopologyDataSet> model1 = new DefaultComboBoxModel<>(); |
||
124 | for (TopologyDataSet dataSet : this.plan.getDataSets()) { |
||
125 | model1.addElement(dataSet); |
||
126 | } |
||
127 | this.cboDataSet1.setModel(model1);
|
||
128 | this.cboDataSet1.setSelectedIndex(-1); |
||
129 | this.cboDataSet1.addItemListener(new ItemListener() { |
||
130 | @Override
|
||
131 | public void itemStateChanged(ItemEvent e) { |
||
132 | performDataSet1Selected(); |
||
133 | } |
||
134 | }); |
||
135 | 728 | jjdelcerro | this.cboDataSet1.setRenderer(new DataSetListCellRenderer()); |
136 | this.cboDataSet2.setRenderer(new DataSetListCellRenderer()); |
||
137 | 688 | jjdelcerro | this.cboRule.addItemListener(new ItemListener() { |
138 | @Override
|
||
139 | public void itemStateChanged(ItemEvent e) { |
||
140 | performRuleSelected(); |
||
141 | } |
||
142 | }); |
||
143 | this.txtTolerance.setText(String.valueOf(this.plan.getTolerance())); |
||
144 | this.txtTolerance.getDocument().addDocumentListener(new DocumentListener() { |
||
145 | @Override
|
||
146 | public void insertUpdate(DocumentEvent e) { |
||
147 | validateData(); |
||
148 | } |
||
149 | |||
150 | @Override
|
||
151 | public void removeUpdate(DocumentEvent e) { |
||
152 | validateData(); |
||
153 | } |
||
154 | |||
155 | @Override
|
||
156 | public void changedUpdate(DocumentEvent e) { |
||
157 | validateData(); |
||
158 | } |
||
159 | }); |
||
160 | |||
161 | this.setOkButtonEnabled(false); |
||
162 | this.cboRule.setSelectedIndex(-1); |
||
163 | this.cboRule.setEnabled(false); |
||
164 | this.cboDataSet2.setEnabled(false); |
||
165 | this.cboDataSet2.setSelectedItem(-1); |
||
166 | |||
167 | this.setPreferredSize(new Dimension(650, 200)); |
||
168 | |||
169 | this.translate();
|
||
170 | } |
||
171 | |||
172 | public void setDialog(Dialog dialog) { |
||
173 | this.dialog = dialog;
|
||
174 | } |
||
175 | |||
176 | private void setOkButtonEnabled(boolean enabled) { |
||
177 | if (this.dialog == null) { |
||
178 | return;
|
||
179 | } |
||
180 | this.dialog.setButtonEnabled(WindowManager_v2.BUTTONS_OK, enabled);
|
||
181 | } |
||
182 | |||
183 | private void translate() { |
||
184 | ToolsSwingManager tsm = ToolsSwingLocator.getToolsSwingManager(); |
||
185 | tsm.translate(this.lblDataSet1);
|
||
186 | tsm.translate(this.lblDataSet2);
|
||
187 | // tsm.translate(this.lblLabelDescription);
|
||
188 | tsm.translate(this.lblRule);
|
||
189 | tsm.translate(this.lblTolerance);
|
||
190 | } |
||
191 | |||
192 | private void validateData() { |
||
193 | try {
|
||
194 | double x = Double.parseDouble(this.txtTolerance.getText()); |
||
195 | } catch (Exception ex) { |
||
196 | this.setOkButtonEnabled(false); |
||
197 | return;
|
||
198 | } |
||
199 | TopologyDataSet dataSet = (TopologyDataSet) this.cboDataSet1.getSelectedItem();
|
||
200 | if (dataSet == null) { |
||
201 | this.setOkButtonEnabled(false); |
||
202 | return;
|
||
203 | } |
||
204 | TopologyRuleFactory ruleFactory = (TopologyRuleFactory) this.cboRule.getSelectedItem();
|
||
205 | if (ruleFactory == null) { |
||
206 | this.setOkButtonEnabled(false); |
||
207 | return;
|
||
208 | } |
||
209 | this.setOkButtonEnabled(true); |
||
210 | } |
||
211 | |||
212 | private void performDataSet1Selected() { |
||
213 | TopologyDataSet dataSet = (TopologyDataSet) this.cboDataSet1.getSelectedItem();
|
||
214 | if (dataSet == null) { |
||
215 | this.setOkButtonEnabled(false); |
||
216 | this.cboRule.setSelectedIndex(-1); |
||
217 | this.cboRule.setEnabled(false); |
||
218 | this.cboDataSet2.setEnabled(false); |
||
219 | this.cboDataSet2.setSelectedItem(-1); |
||
220 | return;
|
||
221 | } |
||
222 | TopologyManager manager = TopologyLocator.getTopologyManager(); |
||
223 | DefaultComboBoxModel<TopologyRuleFactory> model1 = new DefaultComboBoxModel<>(); |
||
224 | for (TopologyRuleFactory ruleFactory : manager.getRuleFactories(dataSet)) {
|
||
225 | model1.addElement(ruleFactory); |
||
226 | } |
||
227 | this.cboRule.setModel(model1);
|
||
228 | this.cboRule.setSelectedIndex(-1); |
||
229 | this.cboRule.setEnabled(true); |
||
230 | } |
||
231 | |||
232 | private void performRuleSelected() { |
||
233 | TopologyRuleFactory ruleFactory = (TopologyRuleFactory) this.cboRule.getSelectedItem();
|
||
234 | if (ruleFactory == null) { |
||
235 | this.setOkButtonEnabled(false); |
||
236 | this.cboDataSet2.setEnabled(false); |
||
237 | this.cboDataSet2.setSelectedItem(-1); |
||
238 | return;
|
||
239 | } |
||
240 | DefaultComboBoxModel<TopologyDataSet> model2 = new DefaultComboBoxModel<>(); |
||
241 | if (ruleFactory.hasSecondaryDataSet()) {
|
||
242 | for (TopologyDataSet dataSet : this.plan.getSecondaryDataSets(ruleFactory)) { |
||
243 | model2.addElement(dataSet); |
||
244 | } |
||
245 | } |
||
246 | if (model2.getSize() == 0) { |
||
247 | this.cboDataSet2.setEnabled(false); |
||
248 | } else {
|
||
249 | this.cboDataSet2.setEnabled(true); |
||
250 | } |
||
251 | this.cboDataSet2.setModel(model2);
|
||
252 | this.cboDataSet2.setSelectedItem(-1); |
||
253 | |||
254 | StringBuilder html = new StringBuilder(); |
||
255 | html.append("<html>\n");
|
||
256 | html.append(ruleFactory.getDescription().replace("\n", "<br>")).append("<br>\n"); |
||
257 | html.append("</html>\n");
|
||
258 | |||
259 | this.txtDescription.setContentType("text/html"); |
||
260 | this.txtDescription.setText(html.toString());
|
||
261 | this.txtDescription.setCaretPosition(0); |
||
262 | |||
263 | this.validateData();
|
||
264 | } |
||
265 | |||
266 | public TopologyRule getRule() {
|
||
267 | TopologyDataSet dataSet1 = (TopologyDataSet) this.cboDataSet1.getSelectedItem();
|
||
268 | TopologyRuleFactory ruleFactory = (TopologyRuleFactory) this.cboRule.getSelectedItem();
|
||
269 | if (dataSet1 == null || ruleFactory == null) { |
||
270 | return null; |
||
271 | } |
||
272 | TopologyDataSet dataSet2 = (TopologyDataSet) this.cboDataSet2.getSelectedItem();
|
||
273 | double tolerance = this.plan.getTolerance(); |
||
274 | try {
|
||
275 | tolerance = Double.parseDouble(this.txtTolerance.getText()); |
||
276 | } catch (Exception ex) { |
||
277 | |||
278 | } |
||
279 | 712 | jjdelcerro | TopologyRule rule; |
280 | if( dataSet2==null ) { |
||
281 | rule = ruleFactory.createRule(plan, dataSet1.getName(), null, tolerance);
|
||
282 | } else {
|
||
283 | rule = ruleFactory.createRule(plan, dataSet1.getName(), dataSet2.getName(), tolerance); |
||
284 | } |
||
285 | 688 | jjdelcerro | return rule;
|
286 | } |
||
287 | |||
288 | } |