Revision 40919

View differences:

tags/org.gvsig.desktop-2.0.21/org.gvsig.desktop.library/org.gvsig.fmap.control/src/test/java/org/gvsig/fmap/mapcontrol/tools/AreaListenerTest.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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.
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.
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.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.mapcontrol.tools;
25

  
26
import java.awt.geom.Point2D;
27

  
28
import org.cresques.cts.IProjection;
29
import org.gvsig.fmap.crs.CRSFactory;
30
import org.gvsig.fmap.mapcontext.MapContext;
31
import org.gvsig.fmap.mapcontext.ViewPort;
32
import org.gvsig.fmap.mapcontrol.MapControl;
33
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
34

  
35

  
36
public class AreaListenerTest extends AbstractLibraryAutoInitTestCase {
37
	private IProjection projectionUTM = CRSFactory.getCRS("EPSG:23030");
38
	private IProjection projectionGeo = CRSFactory.getCRS("EPSG:4230");
39
		
40
	@Override
41
	protected void doSetUp() throws Exception {
42
		// Nothing to do	
43
	}
44

  
45
	public void test1() {
46
		AreaListenerImpl areaListenerUTM=new AreaListenerImpl(newMapControlUTM());
47
		AreaListenerImpl areaListenerGeo=new AreaListenerImpl(newMapControlGeo());
48
		Double[] xsUTM=new Double[] {new Double(731292),new Double(731901),new Double(730138)};
49
		Double[] ysUTM=new Double[] {new Double(4351223),new Double(4350768),new Double(4349232)};
50
		double areaUTM=areaListenerUTM.returnCoordsArea(xsUTM,ysUTM,new Point2D.Double(730138,4349232));
51
		Double[] xsGeo=new Double[] {new Double(-0.31888183),new Double(-0.31173131),new Double(-0.33268401)};
52
		Double[] ysGeo=new Double[] {new Double(39.27871741),new Double(39.27464327),new Double(39.26117368)};
53
		double areaGeo=areaListenerGeo.returnGeoCArea(xsGeo,ysGeo,new Point2D.Double(-0.33268401,39.26117368));
54
		assertTrue("Area UTM igual a Geo",areaUTM<(areaGeo+1000)&& areaUTM>(areaGeo-1000));
55
	}
56
	private MapControl newMapControlUTM() {
57
		ViewPort vp = new ViewPort(projectionUTM);
58
		MapControl mc=new MapControl();
59
		mc.setMapContext(new MapContext(vp));
60
		return mc;
61
	}
62
	private MapControl newMapControlGeo() {
63
		ViewPort vp = new ViewPort(projectionGeo);
64
		MapControl mc=new MapControl();
65
		mc.setMapContext(new MapContext(vp));
66
		return mc;
67
	}
68
}
0 69

  
tags/org.gvsig.desktop-2.0.21/org.gvsig.desktop.library/org.gvsig.fmap.control/src/main/java/org/gvsig/app/gui/JComboBoxUnits.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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.
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.
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.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.app.gui;
25

  
26
import org.gvsig.fmap.mapcontext.MapContext;
27
import org.gvsig.i18n.Messages;
28
import org.gvsig.utils.swing.JComboBox;
29

  
30

  
31
/**
32
 * <p>Class representing a JComboBox with the measure units handled by the application.
33
 * It takes values from Attributes.NAMES and Attributes.CHANGE static fields. So, to
34
 * add more measure units, you must edit Attributes class and change will be automatically
35
 * reflected in the combo box.</p>
36
 *
37
 * <p>The internatiolanization of the field is automatically handled by the system</p>
38
 * @author jaume dominguez faus - jaume.dominguez@iver.es
39
 */
40
public class JComboBoxUnits extends JComboBox {
41
	private static final long serialVersionUID = 8015263853737441433L;
42

  
43
	/**
44
	 * Creates a new instance of JUnitComboBox including "pixel" units and
45
	 * setting them as automatically pre-selected.
46
	 */
47
	public JComboBoxUnits() {
48
		this(true);
49
	}
50

  
51
	/**
52
	 *
53
	 * Creates a new instance of JUnitComboBox. If includePixel is true
54
	 * then pixel units are included in the list and they are automatically
55
	 * pre-selected. Otherwise, meters are preselected.
56
	 *
57
	 */
58
	public JComboBoxUnits(boolean includePixel) {
59
		super();
60
		String[] names=MapContext.getDistanceNames();
61

  
62
		for (int i = 0; i < names.length; i++) {
63
			super.addItem(Messages.getText(names[i]));
64
		}
65
		if (includePixel) {
66
			super.addItem(Messages.getText("_Pixels"));
67
			setSelectedItem(Messages.getText("_Pixels"));
68
		} else {
69
			setSelectedIndex(1);
70
		}
71
		setMaximumRowCount(10);
72
	}
73

  
74

  
75
	/**
76
	 * Returns the conversion factor from the <b>unit selected in the combo box</b>
77
	 * to <b>meters</b> or <b>0</b> if pixels have been selected as the size unit.
78
	 * @return
79
	 */
80
	public double getUnitConversionFactor() {
81
			double unitFactor;
82
			try {
83
				unitFactor = MapContext.getDistanceTrans2Meter()[getSelectedIndex()];
84
			} catch (ArrayIndexOutOfBoundsException aioobEx) { //jijiji
85
				unitFactor = 0; // which represents size in pixel
86
			}
87
			return unitFactor;
88

  
89
	}
90

  
91
	/**
92
	 * the use of this method is not allowed in this combo box.
93
	 * @deprecated
94
	 */
95
	public void addItem(Object anObject) {
96
		throw new Error("Operation not allowed");
97
	}
98

  
99
	/**
100
	 * the use of this method is not allowed for this combo box.
101
	 * @deprecated
102
	 */
103
	public void removeAllItems() {
104
		throw new Error("Operation not allowed");
105
	}
106

  
107
	public int getSelectedUnitIndex() {
108
		int i = getSelectedIndex();
109
		if (i>MapContext.getDistanceNames().length-1)
110
			return -1;
111
		else return i;
112
	}
113

  
114
	public void setSelectedUnitIndex(int unitIndex) {
115
		if (unitIndex == -1) {
116
			setSelectedIndex(getItemCount()-1);
117
		} else {
118
			setSelectedIndex(unitIndex);
119
		}
120
	}
121

  
122

  
123

  
124
}
0 125

  
tags/org.gvsig.desktop-2.0.21/org.gvsig.desktop.library/org.gvsig.fmap.control/src/main/java/org/gvsig/fmap/IconThemeHelper.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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.
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.
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.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap;
25

  
26
import java.awt.Image;
27

  
28
import javax.swing.ImageIcon;
29

  
30
import org.gvsig.tools.swing.api.ToolsSwingLocator;
31
import org.gvsig.tools.swing.icontheme.IconTheme;
32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34

  
35
public class IconThemeHelper {
36

  
37
	private static Logger logger = LoggerFactory.getLogger(IconThemeHelper.class);
38
	
39
	public static void registerIcon(String group, String name, Object obj) {
40
		String resourceName;
41
		ClassLoader loader;
42
		IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getCurrent();
43
		if( group == null || group.trim().length()==0 ) {
44
			resourceName = "images/"+name+".png";
45
		} else {
46
			resourceName = "images/"+group+"/"+name+".png";
47
		}
48
		if( obj instanceof Class ) {
49
			loader = ((Class) obj).getClassLoader();
50
		} else {
51
			loader = obj.getClass().getClassLoader();
52
		}
53
		try {
54
			iconTheme.registerDefault("mapcontrol", group, name, null, loader.getResource(resourceName));
55
		} catch( Throwable e) {
56
			logger.info(e.getMessage());
57
		}
58
	}
59

  
60
	public static ImageIcon getImageIcon(String iconName) {
61
		IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getCurrent();
62
		return iconTheme.get(iconName);
63
	}
64
	
65
	public static Image getImage(String iconName) {
66
		IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getCurrent();
67
		return iconTheme.get(iconName).getImage();
68
	}
69
}
0 70

  
tags/org.gvsig.desktop-2.0.21/org.gvsig.desktop.library/org.gvsig.fmap.control/src/main/java/org/gvsig/fmap/mapcontrol/PrimitivesDrawer.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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.
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.
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.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/* gvSIG. Geographic Information System of the Valencian Government
25
*
26
* Copyright (C) 2007-2008 Infrastructures and Transports Department
27
* of the Valencian Government (CIT)
28
* 
29
* This program is free software; you can redistribute it and/or
30
* modify it under the terms of the GNU General Public License
31
* as published by the Free Software Foundation; either version 2
32
* of the License, or (at your option) any later version.
33
* 
34
* This program is distributed in the hope that it will be useful,
35
* but WITHOUT ANY WARRANTY; without even the implied warranty of
36
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
* GNU General Public License for more details.
38
* 
39
* You should have received a copy of the GNU General Public License
40
* along with this program; if not, write to the Free Software
41
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
42
* MA  02110-1301, USA.
43
* 
44
*/
45

  
46
/*
47
* AUTHORS (In addition to CIT):
48
* 2009 {Iver T.I.}   {Task}
49
*/
50
 
51
package org.gvsig.fmap.mapcontrol;
52

  
53
import java.awt.Color;
54
import java.awt.Graphics;
55

  
56
/**
57
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
58
 */
59
public interface PrimitivesDrawer {
60
	
61
	/**
62
	 * This method is used to start a drawing process that needs to 
63
	 * be synchronized.
64
	 * @param obj
65
	 * The object that locks the resource.
66
	 * @throws InterruptedException
67
	 */
68
	public void startDrawing(Object obj) throws InterruptedException;
69
	
70
	/**
71
	 * This method is used to finish a drawing process that needs to 
72
	 * be synchronized. 
73
	 * @param obj
74
	 * The object that has locked the resource.
75
	 */
76
	public void stopDrawing(Object obj);
77
	
78
	/**
79
	 * This method sets the <code>Graphics</code> where the 
80
	 * <code>Drawer</code> has to draw all the objects. 
81
	 * @param graphics
82
	 * The component where the new objects has to be drawn. 
83
	 */
84
	public void setGraphics(Graphics graphics);
85
	
86
	/**
87
	 * Sets the color that is used to draw the objects that don't 
88
	 * have a symbol.
89
	 * @param color
90
	 * The color to use on the drawing operations.
91
	 */
92
	public void setColor(Color color);
93
		
94
	/**
95
	 * It draws a rectangle on the map using the color 
96
	 * specified using the {@link #setColor(Color)} method. 
97
	 * @param x
98
	 * The minimum X coordinate.
99
	 * @param y
100
	 * The minimum Y coordinate.
101
	 * @param width
102
	 * The rectangle width.
103
	 * @param height
104
	 * The rectangle height.
105
	 */
106
	public void drawRect(int x, int y, int width, int height);
107
		
108
	/**
109
	 * It fills a rectangle on the map using the color 
110
	 * specified using the {@link #setColor(Color)} method. 
111
	 * @param x
112
	 * The minimum X coordinate.
113
	 * @param y
114
	 * The minimum Y coordinate.
115
	 * @param width
116
	 * The rectangle width.
117
	 * @param height
118
	 * The rectangle height.
119
	 */
120
	public void fillRect(int x, int y, int width, int height);
121
	
122
	public void drawOval(int i, int j, int sizePixels, int sizePixels2);
123

  
124
	public void drawLine(int x1, int y1, int x3, int y12);
125
}
0 126

  
tags/org.gvsig.desktop-2.0.21/org.gvsig.desktop.library/org.gvsig.fmap.control/src/main/java/org/gvsig/fmap/mapcontrol/dal/swing/datastore/DataStoreParametersCreationPanel.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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.
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.
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.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.mapcontrol.dal.swing.datastore;
25

  
26
import javax.swing.JPanel;
27

  
28
import org.gvsig.fmap.dal.DataServerExplorer;
29
import org.gvsig.fmap.dal.NewDataStoreParameters;
30

  
31

  
32
/**
33
 * @author gvSIG Team
34
 * @version $Id$
35
 *
36
 */
37
public abstract class DataStoreParametersCreationPanel extends JPanel {
38
    private static final long serialVersionUID = 5265923639970241325L;
39

  
40
    public abstract NewDataStoreParameters getDataStoreParameters();
41
    
42
    public abstract DataServerExplorer getDataServerExplorer();
43
}
0 44

  
tags/org.gvsig.desktop-2.0.21/org.gvsig.desktop.library/org.gvsig.fmap.control/src/main/java/org/gvsig/fmap/mapcontrol/dal/swing/datastore/DefaultDataStoreParametersCreationPanel.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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.
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.
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.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.mapcontrol.dal.swing.datastore;
25

  
26
import java.awt.Dimension;
27
import java.awt.GridBagConstraints;
28
import java.awt.GridBagLayout;
29
import java.awt.Insets;
30
import java.awt.event.ActionEvent;
31
import java.awt.event.ActionListener;
32
import java.util.Iterator;
33
import java.util.List;
34

  
35
import javax.swing.JButton;
36
import javax.swing.JComboBox;
37

  
38
import org.gvsig.fmap.dal.DALLocator;
39
import org.gvsig.fmap.dal.DataManager;
40
import org.gvsig.fmap.dal.DataServerExplorer;
41
import org.gvsig.fmap.dal.DataServerExplorerParameters;
42
import org.gvsig.fmap.dal.NewDataStoreParameters;
43
import org.gvsig.fmap.dal.exception.InitializeException;
44
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
45
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
46
import org.gvsig.fmap.mapcontrol.swing.dynobject.DynObjectEditor;
47
import org.gvsig.tools.service.ServiceException;
48
import org.slf4j.Logger;
49
import org.slf4j.LoggerFactory;
50

  
51

  
52
/**
53
 * @author gvSIG Team
54
 * @version $Id$
55
 *
56
 */
57
public class DefaultDataStoreParametersCreationPanel extends DataStoreParametersCreationPanel implements ActionListener{   
58
    private static final long serialVersionUID = -1453149638996485029L;
59

  
60
    private static final Logger LOG =
61
        LoggerFactory.getLogger(DefaultDataStoreParametersCreationPanel.class);
62

  
63
    private static final DataManager DATA_MANAGER = DALLocator.getDataManager();
64

  
65
    private static final int FORM_HEIGTH = 18;
66

  
67
    private DataServerExplorer dataServerExplorer = null;
68
    private DataServerExplorerParameters dataServerExplorerParameters = null;
69

  
70
    private NewDataStoreParameters dataStoreParameters = null;
71

  
72
    private JButton explorersButton;
73
    private JComboBox explorersComboBox;
74
    private JButton providersButton;
75
    private JComboBox providersComboBox;
76

  
77
    public DefaultDataStoreParametersCreationPanel() {
78
        super(); 
79
        initComponents();
80
        populateExplorerCombo();
81
        initListeners();
82
        enableExplorerControls(false);
83
    }     
84

  
85
    private void initListeners() {
86
        this.explorersComboBox.addActionListener(this);
87
        this.explorersButton.addActionListener(this);
88
        this.providersComboBox.addActionListener(this);
89
        this.providersButton.addActionListener(this);        
90
    }
91

  
92
    @SuppressWarnings("rawtypes")
93
    private void populateExplorerCombo(){
94
        explorersComboBox.addItem(null);
95

  
96
        Iterator it = DATA_MANAGER.getExplorerProviders().iterator();
97

  
98
        while(it.hasNext()) {
99
            String explorer = (String)it.next();
100
            explorersComboBox.addItem(explorer);            
101
        }
102
    }   
103

  
104
    private void initComponents() {
105
        GridBagConstraints gridBagConstraints;
106

  
107
        explorersComboBox = new JComboBox();
108
        providersComboBox = new JComboBox();
109
        explorersButton = new JButton();
110
        providersButton = new JButton();
111

  
112
        setLayout(new GridBagLayout());
113

  
114
        explorersComboBox.setPreferredSize(new Dimension(0, FORM_HEIGTH));
115
        gridBagConstraints = new GridBagConstraints();
116
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
117
        gridBagConstraints.weightx = 1.0;
118
        gridBagConstraints.insets = new Insets(0, 2, 0, 2);
119
        add(explorersComboBox, gridBagConstraints);
120

  
121
        explorersButton.setText("...");
122
        explorersButton.setPreferredSize(new Dimension(25, FORM_HEIGTH));
123
        gridBagConstraints = new GridBagConstraints();
124
        gridBagConstraints.gridx = 1;
125
        gridBagConstraints.gridy = 0;
126
        gridBagConstraints.insets = new Insets(0, 2, 0, 2);
127
        add(explorersButton, gridBagConstraints);
128

  
129
        providersComboBox.setPreferredSize(new Dimension(0, FORM_HEIGTH));
130
        gridBagConstraints = new GridBagConstraints();
131
        gridBagConstraints.gridx = 2;
132
        gridBagConstraints.gridy = 0;
133
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
134
        gridBagConstraints.weightx = 1.0;
135
        gridBagConstraints.insets = new Insets(0, 2, 0, 2);
136
        add(providersComboBox, gridBagConstraints);      
137

  
138
        providersButton.setText("...");
139
        providersButton.setPreferredSize(new Dimension(25, FORM_HEIGTH));
140
        gridBagConstraints = new GridBagConstraints();
141
        gridBagConstraints.gridx = 3;
142
        gridBagConstraints.gridy = 0;
143
        gridBagConstraints.insets = new Insets(0, 2, 0, 2);
144
        add(providersButton, gridBagConstraints);
145
    }
146

  
147
    @Override
148
    public NewDataStoreParameters getDataStoreParameters(){       
149
        return dataStoreParameters;
150
    }
151

  
152
    @Override
153
    public DataServerExplorer getDataServerExplorer() {       
154
        return dataServerExplorer;
155
    }
156

  
157
    public void actionPerformed(ActionEvent e) {       
158
        if (e.getSource().equals(explorersButton)) {           
159
            explorersButtonClicked();
160
        }else if (e.getSource().equals(providersButton)) {
161
            providersButtonClicked();
162
        }else if (e.getSource().equals(explorersComboBox)) {           
163
            explorersComboSelectionChanged();
164
        }else if (e.getSource().equals(providersComboBox)) {
165
            providersComboSelectionChanged();
166
        }
167
    }  
168

  
169
    private void explorersComboSelectionChanged(){        
170
        String explorerName = (String)explorersComboBox.getSelectedItem();
171
        if (explorerName == null){
172
            dataServerExplorerParameters = null;
173
            dataServerExplorer = null;
174
            removeProvidersCombo();
175
            enableExplorerControls(false);
176
        }else{
177
            //Only update the explorer if the selection is different
178
            if ((dataServerExplorer == null) || (!explorerName.equals(dataServerExplorer.getProviderName()))){        
179
                dataServerExplorerParameters = null;
180

  
181
                //Remove all the previous providers
182
                removeProvidersCombo(); 
183

  
184
                //Disable all the components and enable the explorer button
185
                this.enableExplorerControls(false);
186
                this.explorersButton.setEnabled(true);       
187
                //Sometimes is possible to create an explorer without parameters (e.g: filesystem explorer"
188
                try {                    
189
                    dataServerExplorerParameters =
190
                        DATA_MANAGER.createServerExplorerParameters(explorerName);
191
                    dataServerExplorer = 
192
                        DATA_MANAGER.openServerExplorer(explorerName, dataServerExplorerParameters);
193
                    populateProvidersCombo();  
194
                    this.providersComboBox.setEnabled(true);     
195
                } catch (InitializeException e) {
196
                    LOG.error("Error creating the explorer parameters", e);                    
197
                } catch (ProviderNotRegisteredException e) {
198
                    LOG.error("The explorer has not been registeger", e);                    
199
                } catch (ValidateDataParametersException e) {
200
                    LOG.error("Error creating the explorer", e);                  
201
                }                
202
            }
203
        }
204
    }
205

  
206
    private void enableExplorerControls(boolean isEnabled){        
207
        this.explorersButton.setEnabled(isEnabled);  
208
        this.providersComboBox.setEnabled(isEnabled);    
209
        this.providersButton.setEnabled(isEnabled);
210
    }
211

  
212
    private void enableProviderControls(boolean isEnabled){       
213
        this.providersButton.setEnabled(isEnabled);  
214
    }
215

  
216
    private void explorersButtonClicked(){
217
        dataServerExplorer = null;
218
        String explorerName = (String)explorersComboBox.getSelectedItem();
219
        if (explorerName != null){
220
            try {
221
                if ((dataServerExplorerParameters == null) || (!dataServerExplorerParameters.getExplorerName().equals(explorerName))){
222
                    dataServerExplorerParameters =
223
                        DATA_MANAGER.createServerExplorerParameters(explorerName);
224
                }
225
                DynObjectEditor dynObjectEditor = new DynObjectEditor(dataServerExplorerParameters);
226
                dynObjectEditor.editObject(true);               
227
                dataServerExplorer = 
228
                    DATA_MANAGER.openServerExplorer(explorerName, dataServerExplorerParameters);
229
                //Remove all the previous providers
230
                removeProvidersCombo(); 
231
                populateProvidersCombo();
232
                this.providersComboBox.setEnabled(true);  
233
            } catch (InitializeException e) {
234
                LOG.error("Error creating the explorer parameters", e);
235
            } catch (ProviderNotRegisteredException e) {
236
                LOG.error("The explorer has not been registered", e);
237
            } catch (ServiceException e) {
238
                LOG.error("Error creating the explorer panel", e);
239
            } catch (ValidateDataParametersException e) {
240
                LOG.error("Error creating the explorer", e);
241
            }    
242
        }
243
    }
244

  
245
    @SuppressWarnings("rawtypes")
246
    private void populateProvidersCombo(){       
247
        providersComboBox.addItem(null);
248
        if (dataServerExplorer != null){
249
            List providerNames = dataServerExplorer.getDataStoreProviderNames();
250
            for (int i=0 ; i<providerNames.size() ; i++){
251
                providersComboBox.addItem(providerNames.get(i));         
252
            }
253
        }
254
    }
255

  
256
    private void removeProvidersCombo(){
257
        providersComboBox.removeAllItems();
258
    }
259

  
260
    private void providersComboSelectionChanged(){
261
        dataStoreParameters = null;
262
        String providerName = (String)providersComboBox.getSelectedItem();   
263
        if ((dataServerExplorer != null) && (providerName != null)){
264
            enableProviderControls(true);
265
        }else{
266
            enableProviderControls(false);
267
        }
268
    }
269

  
270
    private void providersButtonClicked(){
271
        String providerName = (String)providersComboBox.getSelectedItem();       
272
        if ((dataServerExplorer != null) && (providerName != null)){
273
            try {
274
                if ((dataStoreParameters == null) || (!dataStoreParameters.getDataStoreName().equals(providerName))){
275
                    dataStoreParameters = 
276
                        DATA_MANAGER.createNewStoreParameters(dataServerExplorer.getProviderName(), providerName);
277
                }
278
                DynObjectEditor dynObjectEditor = new DynObjectEditor(dataStoreParameters);
279
                dynObjectEditor.editObject(true);             
280
            } catch (InitializeException e) {
281
                LOG.error("Error creating the store parameters", e);
282
            } catch (ProviderNotRegisteredException e) {
283
                LOG.error("The provider has not been registered", e);
284
            } catch (ServiceException e) {
285
                LOG.error("Error creating the store panel", e);
286
            }
287
        }
288
    }  
289
}
0 290

  
tags/org.gvsig.desktop-2.0.21/org.gvsig.desktop.library/org.gvsig.fmap.control/src/main/java/org/gvsig/fmap/mapcontrol/dal/feature/swing/table/GetFeatureAtException.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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.
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.
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.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2008 {DiSiD Technologies}  {{Task}}
27
 */
28
package org.gvsig.fmap.mapcontrol.dal.feature.swing.table;
29

  
30
import java.util.Collections;
31
import java.util.Map;
32

  
33
import org.gvsig.tools.exception.BaseRuntimeException;
34

  
35
/**
36
 * Exception thrown when there is an error while getting a Feature in a
37
 * position.
38
 * 
39
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
40
 */
41
public class GetFeatureAtException extends BaseRuntimeException {
42

  
43
    private static final long serialVersionUID = 3981212631490713671L;
44

  
45
    private static final String KEY = "get_feature_at_exception";
46

  
47
    private static final String MESSAGE = "Error getting the feature "
48
            + "at row =  %(rowIndex)";
49

  
50
    private int rowIndex;
51

  
52
    /**
53
     * Creates a new Exception when getting a Feature.
54
     * 
55
     * @param rowIndex
56
     *            the position of the Feature
57
     */
58
    public GetFeatureAtException(int rowIndex) {
59
        super(MESSAGE, KEY, serialVersionUID);
60
        this.rowIndex = rowIndex;
61
    }
62

  
63
    /**
64
     * Creates a new Exception when getting a Feature.
65
     * 
66
     * @param rowIndex
67
     *            the position of the Feature
68
     * @param cause
69
     *            the original Throwable
70
     */
71
    public GetFeatureAtException(int rowIndex, Throwable cause) {
72
        super(MESSAGE, cause, KEY, serialVersionUID);
73
        this.rowIndex = rowIndex;
74
    }
75

  
76
    /**
77
     * @return the rowIndex
78
     */
79
    public int getRowIndex() {
80
        return rowIndex;
81
    }
82

  
83
    @Override
84
    protected Map<String, ?> values() {
85
        return Collections.singletonMap("rowIndex", getRowIndex());
86
    }
87

  
88
}
0 89

  
tags/org.gvsig.desktop-2.0.21/org.gvsig.desktop.library/org.gvsig.fmap.control/src/main/java/org/gvsig/fmap/mapcontrol/dal/feature/swing/table/GetRowCountException.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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.
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.
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.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/* gvSIG. Geographic Information System of the Valencian Government
25
*
26
* Copyright (C) 2007-2008 Infrastructures and Transports Department
27
* of the Valencian Government (CIT)
28
* 
29
* This program is free software; you can redistribute it and/or
30
* modify it under the terms of the GNU General Public License
31
* as published by the Free Software Foundation; either version 2
32
* of the License, or (at your option) any later version.
33
* 
34
* This program is distributed in the hope that it will be useful,
35
* but WITHOUT ANY WARRANTY; without even the implied warranty of
36
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
* GNU General Public License for more details.
38
* 
39
* You should have received a copy of the GNU General Public License
40
* along with this program; if not, write to the Free Software
41
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
42
* MA  02110-1301, USA.
43
* 
44
*/
45

  
46
/*
47
* AUTHORS (In addition to CIT):
48
* 2008 {DiSiD Technologies}  {{Task}}
49
*/
50
package org.gvsig.fmap.mapcontrol.dal.feature.swing.table;
51

  
52
import java.util.Collections;
53
import java.util.Map;
54

  
55
import org.gvsig.tools.exception.BaseRuntimeException;
56

  
57
/**
58
 * Exception thrown when getting the total row number, or the total number of
59
 * Features, throws an error.
60
 * 
61
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
62
 */
63
public class GetRowCountException extends BaseRuntimeException {
64

  
65
    private static final String KEY = "get_row_count_exception";
66

  
67
    private static final String MESSAGE = "Error getting the total table row count";
68

  
69
    private static final long serialVersionUID = 7803270649446415844L;
70

  
71
    /**
72
     * Creates a new GetRowCountException.
73
     */
74
    public GetRowCountException() {
75
        super(MESSAGE, KEY, serialVersionUID);
76
    }
77

  
78
    /**
79
     * Creates a new GetRowCountException.
80
     * 
81
     * @param cause
82
     *            the cause of the exception
83
     */
84
    public GetRowCountException(Throwable cause) {
85
        super(MESSAGE, cause, KEY, serialVersionUID);
86
    }
87

  
88
    @Override
89
    protected Map<String, ?> values() {
90
        return Collections.emptyMap();
91
    }
92

  
93
}
0 94

  
tags/org.gvsig.desktop-2.0.21/org.gvsig.desktop.library/org.gvsig.fmap.control/src/main/java/org/gvsig/fmap/mapcontrol/dal/feature/swing/table/ConfigurationTableModel.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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.
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.
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.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2008 {DiSiD Technologies}  {Create a JTable TableModel for a FeatureQuery}
27
 */
28
package org.gvsig.fmap.mapcontrol.dal.feature.swing.table;
29

  
30
import javax.swing.table.AbstractTableModel;
31

  
32
import org.gvsig.i18n.Messages;
33

  
34
/**
35
 * TableModel to configure a ConfigurableFeatureTableModel.
36
 * 
37
 * Allows to set Feature attributes as visible or not, and to set aliases for
38
 * the Feature attribute names.
39
 * 
40
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
41
 */
42
public class ConfigurationTableModel extends AbstractTableModel {
43

  
44
    private static final long serialVersionUID = -825156698327593853L;
45

  
46
    private static final int VISIBILITY_COLUMN = 0;
47
    private static final int NAME_COLUMN = 1;
48
    private static final int ALIAS_COLUMN = 2;
49

  
50
    private ConfigurableFeatureTableModel configurable;
51

  
52
    public ConfigurationTableModel(ConfigurableFeatureTableModel configurable) {
53
        super();
54
        this.configurable = configurable;
55
    }
56

  
57
    public int getColumnCount() {
58
        // 1: visibility, 2: field name, 3: alias
59
        return 3;
60
    }
61

  
62
    public int getRowCount() {
63
        return configurable.getOriginalColumnCount();
64
    }
65

  
66
    public Object getValueAt(int rowIndex, int columnIndex) {
67
        String name = configurable.getOriginalColumnName(rowIndex);
68
        switch (columnIndex) {
69
        case VISIBILITY_COLUMN:
70
            return configurable.isVisible(name);
71
        case NAME_COLUMN:
72
            return name;
73
        case ALIAS_COLUMN:
74
            return configurable.getAliasForColumn(name);
75
        default:
76
            return null;
77
        }
78
    }
79

  
80
    @Override
81
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
82
        String name = configurable.getOriginalColumnName(rowIndex);
83
        switch (columnIndex) {
84
        case VISIBILITY_COLUMN:
85
            configurable.setVisible(name, Boolean.TRUE.equals(value));
86
            break;
87
        case ALIAS_COLUMN:
88
            configurable.setAlias(name, (String) value);
89
            break;
90
        }
91
    }
92
    
93
    @Override
94
    public boolean isCellEditable(int rowIndex, int columnIndex) {
95
        switch (columnIndex) {
96
        case VISIBILITY_COLUMN:
97
        case ALIAS_COLUMN:
98
            return true;
99
        default:
100
            return false;
101
        }
102
    }
103

  
104
    @Override
105
    public Class<?> getColumnClass(int columnIndex) {
106
        switch (columnIndex) {
107
        case VISIBILITY_COLUMN:
108
            return Boolean.class;
109
        case NAME_COLUMN:
110
        case ALIAS_COLUMN:
111
            return String.class;
112
        default:
113
            return Object.class;
114
        }
115

  
116
    }
117

  
118
    @Override
119
    public String getColumnName(int columnIndex) {
120
        switch (columnIndex) {
121
        case VISIBILITY_COLUMN:
122
            return Messages.getText("Visible");
123
        case NAME_COLUMN:
124
            return Messages.getText("Nombre");
125
        case ALIAS_COLUMN:
126
            return Messages.getText("Alias");
127
        default:
128
            return "";
129
        }
130
    }
131

  
132
	public static int getVisibilityColumn() {
133
		return VISIBILITY_COLUMN;
134
	}
135
	
136
    /**
137
     * Make current changes in configuration (visible columns and aliases)
138
     * as definitive.
139
     */
140
	public void acceptChanges() {
141
		configurable.acceptChanges();
142
	}
143
	
144
    /**
145
     * Cancel current changes in configuration (visible columns and aliases)
146
     * and return to previous status.
147
     */
148
	public void cancelChanges() {
149
		configurable.cancelChanges();
150
	}
151
}
0 152

  
tags/org.gvsig.desktop-2.0.21/org.gvsig.desktop.library/org.gvsig.fmap.control/src/main/java/org/gvsig/fmap/mapcontrol/dal/feature/swing/table/GeometryToWKTException.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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.
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.
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.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2008 {DiSiD Technologies}  {{Task}}
27
 */
28
package org.gvsig.fmap.mapcontrol.dal.feature.swing.table;
29

  
30
import org.gvsig.tools.exception.BaseRuntimeException;
31

  
32
/**
33
 * Exception thrown when there is an error while converting a Geometry to WKT
34
 * into a table cell.
35
 * 
36
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
37
 */
38
public class GeometryToWKTException extends BaseRuntimeException {
39

  
40
    private static final long serialVersionUID = 1414646041766565149L;
41

  
42
    private static final String KEY = "geometry_to_wkt_exception";
43

  
44
    private static final String MESSAGE = "Error getting the geometry value "
45
            + "of the cell as WKT";
46

  
47
    /**
48
     * Creates a new Exception when getting a Geometry cell as WKT.
49
     * 
50
     */
51
    public GeometryToWKTException() {
52
        super(MESSAGE, KEY, serialVersionUID);
53
    }
54

  
55
    /**
56
     * Creates a new Exception when getting a Geometry cell as WKT.
57
     * 
58
     * @param cause
59
     *            the original Throwable
60
     */
61
    public GeometryToWKTException(Throwable cause) {
62
        super(MESSAGE, cause, KEY, serialVersionUID);
63
    }
64
}
0 65

  
tags/org.gvsig.desktop-2.0.21/org.gvsig.desktop.library/org.gvsig.fmap.control/src/main/java/org/gvsig/fmap/mapcontrol/dal/feature/swing/table/WKTToGeometryException.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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.
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.
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.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2008 {DiSiD Technologies}  {{Task}}
27
 */
28
package org.gvsig.fmap.mapcontrol.dal.feature.swing.table;
29

  
30
import java.util.Collections;
31
import java.util.Map;
32

  
33
import org.gvsig.tools.exception.BaseRuntimeException;
34

  
35
/**
36
 * Exception thrown when there is an error while converting a WKT text to a
37
 * Geometry into a table cell.
38
 * 
39
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
40
 */
41
public class WKTToGeometryException extends BaseRuntimeException {
42

  
43
    private static final long serialVersionUID = 7450289270030694675L;
44

  
45
    private static final String KEY = "wkt_to_geometry_exception";
46

  
47
    private static final String MESSAGE = "Error getting the geometry value of "
48
            + "the WKT value %(wkt)";
49

  
50
    private String wkt;
51

  
52
    /**
53
     * Creates a new Exception when getting a Geometry cell as WKT.
54
     * 
55
     * @param wkt
56
     *            the geometry in WKT format
57
     */
58
    public WKTToGeometryException(String wkt) {
59
        super(MESSAGE, KEY, serialVersionUID);
60
        this.wkt = wkt;
61
    }
62

  
63
    /**
64
     * Creates a new Exception when getting a Geometry cell as WKT.
65
     * 
66
     * @param wkt
67
     *            the geometry in WKT format
68
     * @param cause
69
     *            the original Throwable
70
     */
71
    public WKTToGeometryException(String wkt, Throwable cause) {
72
        super(MESSAGE, cause, KEY, serialVersionUID);
73
        this.wkt = wkt;
74
    }
75

  
76
    public Object getWKT() {
77
        return wkt;
78
    }
79

  
80
    @Override
81
    protected Map<String, ?> values() {
82
        return Collections.singletonMap("wkt", getWKT());
83
    }
84

  
85
}
0 86

  
tags/org.gvsig.desktop-2.0.21/org.gvsig.desktop.library/org.gvsig.fmap.control/src/main/java/org/gvsig/fmap/mapcontrol/dal/feature/swing/table/TextAreaCellRenderer.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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.
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.
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.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2008 {DiSiD Technologies}  {{Task}}
27
 */
28
package org.gvsig.fmap.mapcontrol.dal.feature.swing.table;
29

  
30
import java.awt.Component;
31
import java.util.HashMap;
32
import java.util.Iterator;
33
import java.util.Map;
34

  
35
import javax.swing.JTable;
36
import javax.swing.JTextArea;
37
import javax.swing.table.DefaultTableCellRenderer;
38
import javax.swing.table.TableCellRenderer;
39
import javax.swing.table.TableColumnModel;
40

  
41
/**
42
 * Renders a text value into a text area.
43
 * <p>
44
 * Taken and adapted from the Article <strong>"Multi-line cells in JTable in JDK
45
 * 1.4+"</strong> by Dr. Heinz M. Kabutz:
46
 * </p>
47
 * <p>
48
 * http://www.javaspecialists.eu/archive/Issue106.html
49
 * </p>
50
 * 
51
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
52
 */
53
public class TextAreaCellRenderer extends JTextArea implements TableCellRenderer {
54

  
55
    private static final long serialVersionUID = 3269365116036190589L;
56

  
57
    private final DefaultTableCellRenderer adaptee = new DefaultTableCellRenderer();
58
    /** map from table to map of rows to map of column heights */
59
    private final Map<JTable, Map<Integer, Map<Integer, Integer>>> cellSizes = new HashMap<JTable, Map<Integer, Map<Integer, Integer>>>();
60
    
61
    private int maxTxtLength = Integer.MAX_VALUE;
62
    
63
    private int maxRowHeight = Integer.MAX_VALUE;
64

  
65
    /**
66
     * Creates a new TextAreaRenderer.
67
     */
68
    public TextAreaCellRenderer() {
69
        this(Integer.MAX_VALUE, Integer.MAX_VALUE);
70
    }
71

  
72
    /**
73
     * Creates a new TextAreaRenderer.
74
     * 
75
     * @param maxTXTLength
76
     *            the maximum text length to be rendered.
77
     * @param maxRowHeight
78
     *            the maximum row height for the rows with cells rendered with
79
     *            this component
80
     */
81
    public TextAreaCellRenderer(int maxTxtLength, int maxRowHeight) {
82
        setLineWrap(true);
83
        setWrapStyleWord(true);
84
        this.maxTxtLength = maxTxtLength;
85
        this.maxRowHeight = maxRowHeight;
86
    }
87

  
88
    public Component getTableCellRendererComponent(
89
            JTable table, Object obj, boolean isSelected, boolean hasFocus,
90
            int row, int column) {
91
        // set the colours, etc. using the standard for that platform
92
        adaptee.getTableCellRendererComponent(table, obj, isSelected, hasFocus,
93
                row, column);
94
        setForeground(adaptee.getForeground());
95
        setBackground(adaptee.getBackground());
96
        setBorder(adaptee.getBorder());
97
        setFont(adaptee.getFont());
98

  
99
        String cellTxt = getCellText(obj, row, column);
100
        if (cellTxt.length() > maxTxtLength) {
101
            cellTxt = cellTxt.substring(0, maxTxtLength - 4).concat(" ...");
102
        }
103

  
104
        setText(cellTxt);
105

  
106
        // This line was very important to get it working with JDK1.4
107
        TableColumnModel columnModel = table.getColumnModel();
108
        setSize(columnModel.getColumn(column).getWidth(), 100000);
109
        int height_wanted = (int) getPreferredSize().getHeight();
110
        
111
        height_wanted = height_wanted > maxRowHeight ? maxRowHeight
112
                : height_wanted;
113
        
114
        addSize(table, row, column, height_wanted);
115
        height_wanted = findTotalMaximumRowSize(table, row);
116
        if (height_wanted != table.getRowHeight(row)) {
117
            table.setRowHeight(row, height_wanted);
118
        }
119
        return this;
120
    }
121

  
122
    /**
123
     * Returns the text for the cell.
124
     * 
125
     * @param value
126
     *            the cell object value
127
     * @return the text to render on the cell
128
     */
129
    protected String getCellText(Object value, int row, int column) {
130
        return adaptee.getText();
131
    }
132

  
133
    private void addSize(JTable table, int row, int column, int height) {
134
        Map<Integer, Map<Integer, Integer>> rows = cellSizes.get(table);
135
        if (rows == null) {
136
            cellSizes.put(table,
137
                    rows = new HashMap<Integer, Map<Integer, Integer>>());
138
        }
139
        Map<Integer, Integer> rowheights = rows.get(new Integer(row));
140
        if (rowheights == null) {
141
            rows.put(new Integer(row),
142
                    rowheights = new HashMap<Integer, Integer>());
143
        }
144
        rowheights.put(new Integer(column), new Integer(height));
145
    }
146

  
147
    /**
148
     * Look through all columns and get the renderer. If it is also a
149
     * TextAreaRenderer, we look at the maximum height in its hash table for
150
     * this row.
151
     */
152
    private int findTotalMaximumRowSize(JTable table, int row) {
153
        int maximum_height = 0;
154
        for (int column = 0; column < table.getColumnCount(); column++) {
155
            TableCellRenderer cellRenderer = table.getCellRenderer(row, column);
156
            if (cellRenderer instanceof TextAreaCellRenderer) {
157
                TextAreaCellRenderer tar = (TextAreaCellRenderer) cellRenderer;
158
                maximum_height = Math.max(maximum_height, tar
159
                        .findMaximumRowSize(table, row));
160
            }
161
        }
162
        return maximum_height;
163
    }
164

  
165
    private int findMaximumRowSize(JTable table, int row) {
166
        Map<Integer, Map<Integer, Integer>> rows = cellSizes.get(table);
167
        if (rows == null)
168
            return 0;
169
        Map<Integer, Integer> rowheights = rows.get(new Integer(row));
170
        if (rowheights == null)
171
            return 0;
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff