Statistics
| Revision:

root / trunk / extensions / extTopology / src / org / gvsig / topology / ui / NewTopologyRulePanel.java @ 18258

History | View | Annotate | Download (9.03 KB)

1
/*
2
 * Created on 10-abr-2006
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45
 *
46
 * $Id: 
47
 * $Log: 
48
 */
49
package org.gvsig.topology.ui;
50

    
51
import java.awt.BorderLayout;
52
import java.awt.Dimension;
53
import java.awt.Insets;
54
import java.awt.event.ActionEvent;
55
import java.awt.event.ActionListener;
56
import java.lang.reflect.Method;
57
import java.net.URL;
58
import java.util.HashMap;
59
import java.util.Iterator;
60
import java.util.List;
61
import java.util.Map;
62

    
63
import javax.swing.JComboBox;
64
import javax.swing.JEditorPane;
65
import javax.swing.JLabel;
66
import javax.swing.JPanel;
67
import javax.swing.JScrollPane;
68
import javax.swing.text.html.HTMLEditorKit;
69

    
70
import org.gvsig.gui.beans.AcceptCancelPanel;
71
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
72
import org.gvsig.topology.ITopologyRule;
73
import org.gvsig.topology.ITwoLyrRule;
74
import org.gvsig.topology.TopologyRuleDefinitionException;
75
import org.gvsig.topology.TopologyRuleFactory;
76

    
77
import com.iver.andami.PluginServices;
78
import com.iver.andami.ui.mdiManager.IWindow;
79
import com.iver.andami.ui.mdiManager.WindowInfo;
80
import com.iver.cit.gvsig.fmap.MapContext;
81
import com.iver.cit.gvsig.fmap.layers.FLayers;
82
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
83
import com.iver.utiles.extensionPoints.ExtensionPoint;
84
import com.iver.utiles.extensionPoints.ExtensionPoints;
85
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
86

    
87
/**
88
 * GUI component to create new topology rules instances.
89
 * 
90
 * @author Alvaro Zabala
91
 * 
92
 */
93
public class NewTopologyRulePanel extends JPanel implements IWindow{
94

    
95
        private static final long serialVersionUID = 2072228070829536392L;
96
        
97
        private WindowInfo viewInfo;
98
        private GridBagLayoutPanel leftPanel;
99
        private JScrollPane rightPanel;
100
        private AcceptCancelPanel acceptCancelPanel;
101
        private LayerJComboBox lyrCombo;
102
        private LayerJComboBox lyrCombo2;
103
        private JComboBox topologyRulesCombo;
104
        private JEditorPane htmlViewer;
105
        
106
        /**
107
         * Relates the user friendly rule name (showed in the rules' combobox) with the
108
         * class that implements this rule
109
         */
110
        private Map<String, Class<?>> ruleName_ruleClass;
111

    
112
        /**
113
         * Layers that have been selected by the user to form part of a topology
114
         */
115
        private List<FLyrVect> layers;
116
        /**
117
         * MapContext of the active view that opens this dialog
118
         */
119
        private MapContext mapContext;
120
        
121
        /**
122
         * New topology rule created by this panel
123
         */
124
        private ITopologyRule newTopologyRule;
125

    
126
        
127
        /**
128
         * Constructor.
129
         * 
130
         * @param layers
131
         */
132
        public NewTopologyRulePanel(List<FLyrVect> lyrsOfTopology, MapContext mapContext) {
133
                super();
134
                this.mapContext = mapContext;
135
                setLayout(new BorderLayout());
136
                this.layers = lyrsOfTopology;
137
                ruleName_ruleClass = new HashMap<String, Class<?>>();
138
                initialize();
139
        }
140
        
141
        /**
142
         * Returns an instance of the topology rule specified by the user.
143
         * @return
144
         */
145
        public ITopologyRule getNewTopologyRule(){
146
                return this.newTopologyRule;
147
        }
148
        
149

    
150
        /**
151
         * Checks if the rule selected in rules combo applies to two layers.
152
         * @return true if the selected rule applies to two layers.
153
         */
154
        private boolean checkTwoLyrRule(){
155
                 Class clazz = ruleName_ruleClass.get(topologyRulesCombo.getSelectedItem());
156
             if(! clazz.isAssignableFrom(ITwoLyrRule.class)){
157
                    return false;
158
             }else
159
                    return true;
160
        }
161
        
162
        /**
163
         * Enables or disables second layer combo box, in function of the selected rule
164
         * applies to one or to two layers.l
165
         */
166
        private void updateSecondLayerComboStatus(){
167
                if(!checkTwoLyrRule()){
168
                        lyrCombo2.setEnabled(false);
169
                        lyrCombo2.setToolTipText(PluginServices.getText(null, "disable_for_onelyr_rules"));
170
                }else{
171
                        lyrCombo2.setEnabled(true);
172
                }
173
        }
174
        
175
        private void updateRuleDescription(){
176
                 Class clazz = ruleName_ruleClass.get(topologyRulesCombo.getSelectedItem());
177
                 ITopologyRule rule;
178
                try {
179
                        rule = (ITopologyRule) clazz.newInstance();
180
                        URL description = rule.getDescription();
181
                        htmlViewer.setPage(description);         
182
                } catch (Exception e) {
183
                        htmlViewer.setText("<p>"+PluginServices.getText(this, "UNAVAILABLE_DESCRIPTION")+"</p>");
184
                } 
185
        }
186
        /**
187
         * Initializes the component
188
         */
189
        private void initialize() {
190
                leftPanel = new GridBagLayoutPanel();
191
                Insets insets = new Insets(2, 0, 2, 0);
192
                JLabel firstLyrText = new JLabel(PluginServices.getText(this,
193
                                "features_of_first_layer"));
194
                
195
                FLayers flayers = new FLayers(mapContext, mapContext.getLayers());
196
                for(int i = 0; i < layers.size(); i++){
197
                        FLyrVect lyr = layers.get(i);
198
                        flayers.addLayer(lyr);
199
                }
200
                lyrCombo = new LayerJComboBox(flayers);
201
                
202
                JLabel ruleText = new JLabel(PluginServices.getText(this,
203
                                "Topology_rule"));
204
                
205
                topologyRulesCombo = createRulesCombo();
206
                topologyRulesCombo.addActionListener(new ActionListener(){
207
                        public void actionPerformed(ActionEvent e) {
208
                                updateRuleDescription();
209
                                updateSecondLayerComboStatus();
210
                        }});
211
                
212
                
213
                JLabel destinationLyrText = new JLabel(PluginServices.getText(this, 
214
                                "features_of_destination_lyr"));
215
                lyrCombo2 = new LayerJComboBox(flayers);
216
                
217
                
218
                
219
                leftPanel.addComponent(firstLyrText, insets);
220
                leftPanel.addComponent(lyrCombo, insets);
221
                leftPanel.addComponent(ruleText, insets);
222
                leftPanel.addComponent(topologyRulesCombo, insets);
223
                leftPanel.addComponent(destinationLyrText, insets);
224
                leftPanel.addComponent(lyrCombo2, insets);
225
                
226
                rightPanel = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
227
                htmlViewer = new JEditorPane(){
228
                        public URL getPage() {
229
                                return null;
230
                        }
231
                };
232
                htmlViewer.setEditable(false);
233
                htmlViewer.setEditorKit(new HTMLEditorKit());
234
                rightPanel.setViewportView(htmlViewer);
235
                rightPanel.setPreferredSize(new Dimension(250, 140));
236
                updateRuleDescription();
237
                
238
                
239
                acceptCancelPanel = new AcceptCancelPanel(
240
                        new ActionListener(){//ok
241
                        public void actionPerformed(ActionEvent e) {
242
                                String ruleDesc = (String) topologyRulesCombo.getSelectedItem();
243
                                Class ruleClass = ruleName_ruleClass.get(ruleDesc);
244
                                if(ruleClass != null){
245
                                        Map<String, Object> params = new HashMap<String, Object>();
246
                                        params.put("originLyr", lyrCombo.getSelectedLayer());
247
                                        try {
248
                                                newTopologyRule = TopologyRuleFactory.createRule(ruleClass, params, null);
249
                                        } catch (TopologyRuleDefinitionException e1) {
250
                                                e1.printStackTrace();
251
                                        }
252
                                        PluginServices.getMDIManager().closeWindow(
253
                                                        NewTopologyRulePanel.this);
254
                                }
255
                                
256
                        }}, 
257
                        new ActionListener(){//cancel
258
                        public void actionPerformed(ActionEvent arg0) {
259
                                PluginServices.getMDIManager().closeWindow(
260
                                                NewTopologyRulePanel.this);
261
                        }});
262
                
263
                add(leftPanel, BorderLayout.WEST);
264
                add(rightPanel, BorderLayout.EAST);
265
                add(acceptCancelPanel, BorderLayout.SOUTH);
266
                
267
                updateSecondLayerComboStatus();
268
        }
269

    
270
        private JComboBox createRulesCombo() {
271
                JComboBox solution = new JComboBox();
272
                ExtensionPoints extensionPoints = ExtensionPointsSingleton
273
                                .getInstance();
274
                ExtensionPoint topologyRules = (ExtensionPoint) extensionPoints
275
                                .get("TopologyRules");
276
                if (topologyRules == null) {
277
                        solution.addItem(PluginServices.getText(null,
278
                                        "Rules_not_registered"));
279
                }else{
280
                        Iterator i = topologyRules.keySet().iterator();
281
                        while (i.hasNext()) {
282
                                String nombre = (String) i.next();
283
                                Class ruleClass = (Class) topologyRules.get(nombre);
284
                                String localizedText = PluginServices.getText(this, nombre);
285
                                ruleName_ruleClass.put(localizedText, ruleClass);
286
                                solution.addItem(localizedText);
287
                        }//while
288
                }
289
                return solution;
290
        }
291

    
292

    
293
        
294
        public WindowInfo getWindowInfo() {
295
                if (viewInfo == null) {
296
                        viewInfo = new WindowInfo(WindowInfo.MODALDIALOG
297
                                         | WindowInfo.PALETTE);
298
                        viewInfo.setTitle(PluginServices.getText(this,
299
                                        "Topology_rules"));
300
                        viewInfo.setWidth(450);
301
                        viewInfo.setHeight(145);
302
                }
303
                return viewInfo;
304
        }
305

    
306
}