Revision 41426 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.fmap.control/src/main/java/org/gvsig/fmap/mapcontrol/swing/dynobject/impl/DefaultLayersDynObjectSetComponent.java

View differences:

DefaultLayersDynObjectSetComponent.java
3 3
 *
4 4
 * Copyright (C) 2007-2013 gvSIG Association.
5 5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
6
 * This program is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10 10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15 15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 19
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
23 22
 */
24 23
package org.gvsig.fmap.mapcontrol.swing.dynobject.impl;
25 24

  
......
66 65
/**
67 66
 * @author gvSIG Team
68 67
 * @version $Id$
69
 * 
68
 *
70 69
 */
71 70
public class DefaultLayersDynObjectSetComponent extends JPanel implements
72
    LayersDynObjectSetComponent, TreeSelectionListener, JDynFormSetListener {
71
        LayersDynObjectSetComponent, TreeSelectionListener, JDynFormSetListener {
73 72

  
74 73
    private static final long serialVersionUID = 5864674721657215264L;
75 74

  
76 75
    private static final Logger LOG = LoggerFactory
77
        .getLogger(DefaultLayersDynObjectSetComponent.class);
76
            .getLogger(DefaultLayersDynObjectSetComponent.class);
78 77

  
79 78
    private final LayersDynObjectSetComponentModel model;
80 79

  
......
85 84

  
86 85
    private final boolean writable;
87 86

  
88
	private DefaultMutableTreeNode topNode;
89
	private MutableTreeNode previousSelection = null;
90
	
91
	private MapContext mapContext = null;
87
    private DefaultMutableTreeNode topNode;
88
    private MutableTreeNode previousSelection = null;
92 89

  
90
    private MapContext mapContext = null;
91

  
93 92
    /**
94 93
     * Creates a new {@link DefaultLayersDynObjectSetComponent} with the given
95 94
     * information for a list of layers.
96 95
     */
97 96
    public DefaultLayersDynObjectSetComponent(
98
        Map<String, DynObjectSet> layerName2InfoByPoint) {
97
            Map<String, DynObjectSet> layerName2InfoByPoint) {
99 98
        this(layerName2InfoByPoint, true);
100 99
    }
101 100

  
......
103 102
     * @param isDoubleBuffered
104 103
     */
105 104
    public DefaultLayersDynObjectSetComponent(
106
        Map<String, DynObjectSet> layerName2InfoByPoint,
107
 boolean writable) {
105
            Map<String, DynObjectSet> layerName2InfoByPoint,
106
            boolean writable) {
108 107
        super(new BorderLayout());
109 108
        this.writable = writable;
110 109
        model = new LayersDynObjectSetComponentModel(layerName2InfoByPoint);
......
115 114
        addLayerTree();
116 115
    }
117 116

  
118
    private void addLayerTree(){
119
    	topNode = new DefaultMutableTreeNode("");
120
    	layersTree = new JTree(topNode);
121
    	layersTree.getSelectionModel().setSelectionMode
122
    			(TreeSelectionModel.SINGLE_TREE_SELECTION);
117
    private void addLayerTree() {
118
        topNode = new DefaultMutableTreeNode("");
119
        layersTree = new JTree(topNode);
120
        layersTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
123 121

  
124
    	//layersTree.setShowsRootHandles(true);
125
    	layersTree.addTreeSelectionListener(this);
126
    	createTreeModel();
127
    	
128
    	JScrollPane scroll = new JScrollPane(layersTree);
129
    	scroll.setPreferredSize(new Dimension(200,200));
130
    	
131
    	contentPanel = new JPanel();
132
    	contentPanel.setLayout(new BorderLayout());
133
    	
134
    	JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
122
        //layersTree.setShowsRootHandles(true);
123
        layersTree.addTreeSelectionListener(this);
124
        createTreeModel();
125

  
126
        JScrollPane scroll = new JScrollPane(layersTree);
127
        scroll.setPreferredSize(new Dimension(200, 200));
128

  
129
        contentPanel = new JPanel();
130
        contentPanel.setLayout(new BorderLayout());
131

  
132
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
135 133
                scroll, contentPanel);
136
		splitPane.setOneTouchExpandable(true);
137
		splitPane.setDividerLocation(170);
138
		
139
		//Provide minimum sizes for the two components in the split pane
140
		Dimension minimumSize = new Dimension(100,150);
141
		scroll.setMinimumSize(minimumSize);
142
    	
143
    	add(splitPane, BorderLayout.CENTER);
144
    	
145
    	for (int i = 0; i < layersTree.getRowCount(); i++) {
146
    		layersTree.expandRow(i);
147
    	}
148
    	layersTree.setRootVisible(false);
149
    	
150
    	if(topNode.getChildCount()>0 && topNode.getFirstChild().getChildCount()>0)
151
    		layersTree.setSelectionPath(new TreePath(((DefaultTreeModel)layersTree.getModel()).getPathToRoot(topNode.getFirstChild().getChildAt(0))));
134
        splitPane.setOneTouchExpandable(true);
135
        splitPane.setDividerLocation(170);
136

  
137
        //Provide minimum sizes for the two components in the split pane
138
        Dimension minimumSize = new Dimension(100, 150);
139
        scroll.setMinimumSize(minimumSize);
140

  
141
        add(splitPane, BorderLayout.CENTER);
142

  
143
        for (int i = 0; i < layersTree.getRowCount(); i++) {
144
            layersTree.expandRow(i);
145
        }
146
        layersTree.setRootVisible(false);
147

  
148
        if (topNode.getChildCount() > 0 && topNode.getFirstChild().getChildCount() > 0) {
149
            layersTree.setSelectionPath(new TreePath(((DefaultTreeModel) layersTree.getModel()).getPathToRoot(topNode.getFirstChild().getChildAt(0))));
150
        }
152 151
    }
153
    
152

  
154 153
    private void createTreeModel() {
155
    	for(int i=0;i<model.getSize();i++){
156
    		MutableTreeNode newLayer = new DefaultMutableTreeNode(
157
    				model.getElementAt(i), 
158
    				true);
159
    		
160
    		DynObjectSet dynObjects = model.getLayerInfoByPoint((String) model.getElementAt(i));
161
    		Iterator it;
162
			try {
163
				it = dynObjects.iterator();
164
				int j=0;
165
				while(it.hasNext()){
166
					DynObject dynObject = (DynObject) it.next();
167
	    			MutableTreeNode newDynObject = new DefaultMutableTreeNode(
168
	    					concatString(dynObject.toString(),20), 
169
	        				false);
170
	    			newLayer.insert(newDynObject, j);
171
	    			j++;
172
	    		}
173
			} catch (BaseException e) {
174
				 LOG.error("Error getting the DynObjects of "
175
			                + "the DynObjectSet: " + dynObjects, e);
176
			}
177
    		topNode.add(newLayer);
178
    		
179
    	}
154
        for (int i = 0; i < model.getSize(); i++) {
155
            MutableTreeNode newLayer = new DefaultMutableTreeNode(
156
                    model.getElementAt(i),
157
                    true);
158

  
159
            DynObjectSet dynObjects = model.getLayerInfoByPoint((String) model.getElementAt(i));
160
            Iterator it;
161
            try {
162
                it = dynObjects.iterator();
163
                int j = 0;
164
                while (it.hasNext()) {
165
                    DynObject dynObject = (DynObject) it.next();
166
                    MutableTreeNode newDynObject = new DefaultMutableTreeNode(
167
                            concatString(dynObject.toString(), 20),
168
                            false);
169
                    newLayer.insert(newDynObject, j);
170
                    j++;
171
                }
172
            } catch (BaseException e) {
173
                LOG.error("Error getting the DynObjects of "
174
                        + "the DynObjectSet: " + dynObjects, e);
175
            }
176
            topNode.add(newLayer);
177

  
178
        }
180 179
    }
181
    
180

  
182 181
    private String concatString(String cadena, int i) {
183
		if(cadena.length()>i && i>3){
184
			String newCadena = cadena.substring(0, i-3);
185
			cadena = newCadena+"...";
186
		}
187
		return cadena;
188
	}
182
        if (cadena.length() > i && i > 3) {
183
            String newCadena = cadena.substring(0, i - 3);
184
            cadena = newCadena + "...";
185
        }
186
        return cadena;
187
    }
189 188

  
190 189
    private void setCurrentLayerInfoByPoint(String layerName) {
191 190
        JDynFormSet newComponent = null;
192 191

  
193 192
        DynObjectSet dynObjectSet = model.getLayerInfoByPoint(layerName);
194 193
        try {
195
            newComponent =
196
            	DynFormLocator.getDynFormManager()
197
            		.createJDynFormSet(dynObjectSet);
194
            newComponent
195
                    = DynFormLocator.getDynFormManager()
196
                    .createJDynFormSet(dynObjectSet);
198 197
            newComponent.setReadOnly(!this.writable);
199 198
        } catch (BaseException e) {
200 199
            LOG.error("Error creating the JDynFormSet for "
201
                + "the DynObjectSet: " + dynObjectSet, e);
200
                    + "the DynObjectSet: " + dynObjectSet, e);
202 201
        }
203 202

  
204 203
        if (newComponent != null) {
205
        	newComponent.setAllowDelete(false);
206
        	newComponent.setAllowNew(false);
207
        	newComponent.setAllowSearch(false);
208
        	newComponent.setAllowUpdate(false);
209
        	if( this.mapContext!=null ) {
210
        		DataTypesManager dataManager = ToolsLocator.getDataTypesManager();
211
        		newComponent.addActionToPopupMenu( dataManager.get(DataTypes.GEOMETRY), "Center in current view", new CenterGeometryInMapContext(this.mapContext));
212
        	}
204
            newComponent.setAllowDelete(false);
205
            newComponent.setAllowNew(false);
206
            newComponent.setAllowSearch(false);
207
            newComponent.setAllowUpdate(false);
208
            if (this.mapContext != null) {
209
                DataTypesManager dataManager = ToolsLocator.getDataTypesManager();
210
                newComponent.addActionToPopupMenu(dataManager.get(DataTypes.GEOMETRY), "Center in current view", new CenterGeometryInMapContext(this.mapContext));
211
            }
213 212
            removeCurrentDynObjectSetComponent();
214 213
            component = newComponent;
215 214
            component.addListener(this);
......
232 231
    private void removeCurrentDynObjectSetComponent() {
233 232
        if (component != null) {
234 233
            remove(component.asJComponent());
235
          //  component.dispose();
234
            //  component.dispose();
236 235
            contentPanel.removeAll();
237 236
            contentPanel.repaint();
238 237
        }
239 238
    }
240 239

  
241
	public void valueChanged(TreeSelectionEvent e) {
242
			TreePath newPath = e.getNewLeadSelectionPath();
243
			TreePath oldPath = e.getOldLeadSelectionPath();
244
			
245
			if(!newPath.equals(oldPath)){
246
				DefaultMutableTreeNode aux = 
247
					       (DefaultMutableTreeNode)layersTree.getLastSelectedPathComponent(); 
248
				
249
				if(layersTree.getSelectionPath() != null){
250
					if(aux.isLeaf()){ // A Feature node is selected
251
						String layerName = "";
252
						if(previousSelection == null || 
253
								!aux.getParent().equals(previousSelection.getParent())){
254
							layerName = aux.getParent().toString();
255
			            	setCurrentLayerInfoByPoint(layerName);
256
						}
257
						int childIndex = aux.getParent().getIndex(aux);
258
		            	if(childIndex != -1){
259
		            		component.setCurrentIndex(childIndex);
260
		            	}
261
						previousSelection = aux;
262
					}
263
				}
264
			}
265
	}
240
    public void valueChanged(TreeSelectionEvent e) {
241
        TreePath newPath = e.getNewLeadSelectionPath();
242
        TreePath oldPath = e.getOldLeadSelectionPath();
266 243

  
267
	public void message(String arg0) {
268
		// Do nothing
269
	}
244
        if (!newPath.equals(oldPath)) {
245
            DefaultMutableTreeNode aux
246
                    = (DefaultMutableTreeNode) layersTree.getLastSelectedPathComponent();
270 247

  
271
	public void formAfterDelete() {
272
		// Do nothing
273
	}
248
            if (layersTree.getSelectionPath() != null) {
249
                if (aux.isLeaf()) { // A Feature node is selected
250
                    String layerName = "";
251
                    if (previousSelection == null
252
                            || !aux.getParent().equals(previousSelection.getParent())) {
253
                        layerName = aux.getParent().toString();
254
                        setCurrentLayerInfoByPoint(layerName);
255
                    }
256
                    int childIndex = aux.getParent().getIndex(aux);
257
                    if (childIndex != -1) {
258
                        component.setCurrentIndex(childIndex);
259
                    }
260
                    previousSelection = aux;
261
                }
262
            }
263
        }
264
    }
274 265

  
275
	public void formAfterNew() {
276
		// Do nothing
277
	}
266
    public void message(String arg0) {
267
        // Do nothing
268
    }
278 269

  
279
	public void formAfterSave() {
280
		// Do nothing
281
	}
270
    public void formClose() {
271
        this.setVisible(false);
272
    }
282 273

  
283
	public void formAfterSearch() {
284
		// Do nothing
285
	}
274
    public void formMessage(String arg0) {
275
        // Do nothing
276
    }
286 277

  
287
	public void formBeforeDelete() throws AbortActionException {
288
		// Do nothing
289
	}
278
    public void formMovedTo(int arg0) {
279
        DefaultMutableTreeNode aux
280
                = (DefaultMutableTreeNode) layersTree.getLastSelectedPathComponent();
281
        TreeNode parent = aux.getParent();
282
        TreeNode selectNode = parent.getChildAt(arg0);
290 283

  
291
	public void formBeforeNew() throws AbortActionException {
292
		// Do nothing
293
	}
284
        TreePath path = new TreePath(((DefaultTreeModel) layersTree.getModel()).getPathToRoot(selectNode));
285
        layersTree.setSelectionPath(path);
286
    }
294 287

  
295
	public void formBeforeSave() throws AbortActionException {
296
		// Do nothing
297
	}
288
    public void setMapContext(MapContext mapContext) {
289
        this.mapContext = mapContext;
290
    }
298 291

  
299
	public void formBeforeSearch() throws AbortActionException {
300
		// Do nothing
301
	}
292
    public void formBeforeSave(JDynFormSet jdfs) throws AbortActionException {
293
    }
302 294

  
303
	public void formClose() {
304
		this.setVisible(false);
305
	}
295
    public void formBeforeNew(JDynFormSet jdfs) throws AbortActionException {
296
    }
306 297

  
307
	public void formMessage(String arg0) {
308
		// Do nothing
309
	}
298
    public void formBeforeDelete(JDynFormSet jdfs) throws AbortActionException {
299
    }
310 300

  
311
	public void formMovedTo(int arg0) {
312
		DefaultMutableTreeNode aux = 
313
			       (DefaultMutableTreeNode)layersTree.getLastSelectedPathComponent(); 
314
		TreeNode parent = aux.getParent();
315
		TreeNode selectNode = parent.getChildAt(arg0);
316
		
317
		TreePath path = new TreePath(((DefaultTreeModel)layersTree.getModel()).getPathToRoot(selectNode));
318
		layersTree.setSelectionPath(path);
319
	}
301
    public void formBeforeSearch(JDynFormSet jdfs) throws AbortActionException {
302
    }
320 303

  
321
	public void setMapContext(MapContext mapContext) {
322
		this.mapContext = mapContext;
323
	}
324
	
304
    public void formAfterSave(JDynFormSet jdfs) {
305
    }
306

  
307
    public void formAfterNew(JDynFormSet jdfs) {
308
    }
309

  
310
    public void formAfterDelete(JDynFormSet jdfs) {
311
    }
312

  
313
    public void formAfterSearch(JDynFormSet jdfs) {
314
    }
315

  
325 316
    public class CenterGeometryInMapContext extends AbstractAction implements Action {
326 317

  
327
    	private MapContext mapContext;
318
        private MapContext mapContext;
328 319

  
329
		public CenterGeometryInMapContext(MapContext mapContext) {
330
			super("Center in the current view");
331
			this.mapContext = mapContext;
332
			this.putValue(Action.SHORT_DESCRIPTION, "Center geometry in the current view");
333
			this.putValue(Action.LONG_DESCRIPTION, "Center geometry in the current view");
334
		}
335
    	
336
		public void actionPerformed(ActionEvent arg0) {
337
			if( mapContext ==  null ) {
338
				return ;
339
			}
340
			Object source = null;
341
			try {
342
				source = arg0.getSource();
343
				JDynFormField field = (JDynFormField) source;
344
				Geometry geom = (Geometry) field.getValue();
345
				if( geom == null ) {
346
					JOptionPane.showMessageDialog(component.asJComponent(), "The geometri is not valid. Can't center the view in it.", "Warning", JOptionPane.INFORMATION_MESSAGE);
347
					return;
348
				}
349
				mapContext.getViewPort().setEnvelope(geom.getEnvelope());
350
				mapContext.invalidate();
351
			} catch(Exception ex) {
352
				LOG.warn("Can't center view in the geometry (source="+source.toString()+").",ex);
353
			}
354
		}
355
    	
320
        public CenterGeometryInMapContext(MapContext mapContext) {
321
            super("Center in the current view");
322
            this.mapContext = mapContext;
323
            this.putValue(Action.SHORT_DESCRIPTION, "Center geometry in the current view");
324
            this.putValue(Action.LONG_DESCRIPTION, "Center geometry in the current view");
325
        }
326

  
327
        public void actionPerformed(ActionEvent arg0) {
328
            if (mapContext == null) {
329
                return;
330
            }
331
            Object source = null;
332
            try {
333
                source = arg0.getSource();
334
                JDynFormField field = (JDynFormField) source;
335
                Geometry geom = (Geometry) field.getValue();
336
                if (geom == null) {
337
                    JOptionPane.showMessageDialog(component.asJComponent(), "The geometri is not valid. Can't center the view in it.", "Warning", JOptionPane.INFORMATION_MESSAGE);
338
                    return;
339
                }
340
                mapContext.getViewPort().setEnvelope(geom.getEnvelope());
341
                mapContext.invalidate();
342
            } catch (Exception ex) {
343
                LOG.warn("Can't center view in the geometry (source=" + source.toString() + ").", ex);
344
            }
345
        }
346

  
356 347
    }
357 348

  
358 349
}

Also available in: Unified diff