Revision 1575 org.gvsig.vectorediting/trunk/org.gvsig.vectorediting/org.gvsig.vectorediting.swing/org.gvsig.vectorediting.swing.impl/src/main/java/org/gvsig/vectorediting/swing/impl/contextmenu/DynamicEditingPointPanelView.java

View differences:

DynamicEditingPointPanelView.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2014 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 2
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 1
package org.gvsig.vectorediting.swing.impl.contextmenu;
26 2

  
27
import java.awt.GridBagConstraints;
28
import java.awt.GridBagLayout;
29
import java.awt.Insets;
30

  
3
import com.jeta.open.i18n.I18NUtils;
4
import com.jgoodies.forms.layout.CellConstraints;
5
import com.jgoodies.forms.layout.FormLayout;
6
import java.awt.BorderLayout;
7
import java.awt.ComponentOrientation;
8
import java.awt.Container;
9
import java.awt.Dimension;
10
import javax.swing.Box;
11
import javax.swing.ButtonGroup;
12
import javax.swing.ImageIcon;
13
import javax.swing.JComboBox;
14
import javax.swing.JFrame;
31 15
import javax.swing.JLabel;
32 16
import javax.swing.JPanel;
17
import javax.swing.JRadioButton;
33 18
import javax.swing.JTextField;
34 19

  
35
import org.gvsig.fmap.geom.Geometry;
36
import org.gvsig.fmap.geom.primitive.Point;
37
import org.gvsig.fmap.geom.type.GeometryType;
38 20

  
39
/**
40
 * @author fdiaz
41
 *
42
 */
43
public class DynamicEditingPointPanelView extends JPanel {
21
public class DynamicEditingPointPanelView extends JPanel
22
{
23
   JRadioButton rdbUseAPointPreviouslyCaptured = new JRadioButton();
24
   ButtonGroup buttongroup1 = new ButtonGroup();
25
   JComboBox cboCapturedPoints = new JComboBox();
26
   JRadioButton rdbEnterTheCoordinatesManually = new JRadioButton();
27
   JLabel lblX = new JLabel();
28
   JLabel lblY = new JLabel();
29
   JTextField txtX = new JTextField();
30
   JTextField txtY = new JTextField();
31
   JLabel lblZ = new JLabel();
32
   JLabel lblM = new JLabel();
33
   JTextField txtZ = new JTextField();
34
   JTextField txtM = new JTextField();
44 35

  
45
    /**
46
     *
47
     */
48
    private static final long serialVersionUID = -9039752199011536211L;
36
   /**
37
    * Default constructor
38
    */
39
   public DynamicEditingPointPanelView()
40
   {
41
      initializePanel();
42
   }
49 43

  
50
    private Point defaultPoint = null;
51
    private int dimension = 2;
44
   /**
45
    * Adds fill components to empty cells in the first row and first column of the grid.
46
    * This ensures that the grid spacing will be the same as shown in the designer.
47
    * @param cols an array of column indices in the first row where fill components should be added.
48
    * @param rows an array of row indices in the first column where fill components should be added.
49
    */
50
   void addFillComponents( Container panel, int[] cols, int[] rows )
51
   {
52
      Dimension filler = new Dimension(10,10);
52 53

  
53
    private JLabel labels[];
54
      boolean filled_cell_11 = false;
55
      CellConstraints cc = new CellConstraints();
56
      if ( cols.length > 0 && rows.length > 0 )
57
      {
58
         if ( cols[0] == 1 && rows[0] == 1 )
59
         {
60
            /** add a rigid area  */
61
            panel.add( Box.createRigidArea( filler ), cc.xy(1,1) );
62
            filled_cell_11 = true;
63
         }
64
      }
54 65

  
55
    private JTextField fields[];
66
      for( int index = 0; index < cols.length; index++ )
67
      {
68
         if ( cols[index] == 1 && filled_cell_11 )
69
         {
70
            continue;
71
         }
72
         panel.add( Box.createRigidArea( filler ), cc.xy(cols[index],1) );
73
      }
56 74

  
57
    /**
58
     * @param dimension
59
     */
60
    public DynamicEditingPointPanelView(Point point) {
61
        super();
75
      for( int index = 0; index < rows.length; index++ )
76
      {
77
         if ( rows[index] == 1 && filled_cell_11 )
78
         {
79
            continue;
80
         }
81
         panel.add( Box.createRigidArea( filler ), cc.xy(1,rows[index]) );
82
      }
62 83

  
63
        this.defaultPoint = point;
64
        if(defaultPoint!=null){
65
            dimension = defaultPoint.getDimension();
66
        }
67
        labels = new JLabel[dimension];
68
        fields = new JTextField[dimension];
84
   }
69 85

  
70
        initialize();
86
   /**
87
    * Helper method to load an image file from the CLASSPATH
88
    * @param imageName the package and name of the file to load relative to the CLASSPATH
89
    * @return an ImageIcon instance with the specified image file
90
    * @throws IllegalArgumentException if the image resource cannot be loaded.
91
    */
92
   public ImageIcon loadImage( String imageName )
93
   {
94
      try
95
      {
96
         ClassLoader classloader = getClass().getClassLoader();
97
         java.net.URL url = classloader.getResource( imageName );
98
         if ( url != null )
99
         {
100
            ImageIcon icon = new ImageIcon( url );
101
            return icon;
102
         }
103
      }
104
      catch( Exception e )
105
      {
106
         e.printStackTrace();
107
      }
108
      throw new IllegalArgumentException( "Unable to load image: " + imageName );
109
   }
71 110

  
72
        addHierarchyChanged();
73
    }
111
   /**
112
    * Method for recalculating the component orientation for 
113
    * right-to-left Locales.
114
    * @param orientation the component orientation to be applied
115
    */
116
   public void applyComponentOrientation( ComponentOrientation orientation )
117
   {
118
      // Not yet implemented...
119
      // I18NUtils.applyComponentOrientation(this, orientation);
120
      super.applyComponentOrientation(orientation);
121
   }
74 122

  
75
    /**
76
     * Adds an hierarchy listener to value text field. The purpose of this is
77
     * request focus when texfield ancestor gains focus.
78
     */
79
    private void addHierarchyChanged() {
80
        getCoordinateTextField(0).addHierarchyListener(new DefaultHierarchyListener());
81
    }
123
   public JPanel createPanel()
124
   {
125
      JPanel jpanel1 = new JPanel();
126
      FormLayout formlayout1 = new FormLayout("FILL:4DLU:NONE,FILL:8DLU:NONE,FILL:DEFAULT:NONE,FILL:4DLU:NONE,FILL:DEFAULT:GROW(1.0),FILL:4DLU:NONE","CENTER:2DLU:NONE,CENTER:DEFAULT:NONE,CENTER:2DLU:NONE,CENTER:DEFAULT:NONE,CENTER:4DLU:NONE,CENTER:DEFAULT:NONE,CENTER:2DLU:NONE,CENTER:DEFAULT:NONE,CENTER:2DLU:NONE,CENTER:DEFAULT:NONE,CENTER:2DLU:NONE,CENTER:DEFAULT:NONE,CENTER:2DLU:NONE,CENTER:DEFAULT:NONE,CENTER:2DLU:NONE");
127
      CellConstraints cc = new CellConstraints();
128
      jpanel1.setLayout(formlayout1);
82 129

  
83
    private void initialize() {
84
        setLayout(new GridBagLayout());
130
      rdbUseAPointPreviouslyCaptured.setActionCommand("_Use_a_point_previously_captured");
131
      rdbUseAPointPreviouslyCaptured.setName("rdbUseAPointPreviouslyCaptured");
132
      rdbUseAPointPreviouslyCaptured.setText("_Use_a_point_previously_captured");
133
      buttongroup1.add(rdbUseAPointPreviouslyCaptured);
134
      jpanel1.add(rdbUseAPointPreviouslyCaptured,cc.xywh(2,2,4,1));
85 135

  
86
        for (int i = 0; i < dimension; i++) {
136
      cboCapturedPoints.setName("cboCapturedPoints");
137
      jpanel1.add(cboCapturedPoints,cc.xywh(3,4,3,1));
87 138

  
88
            GridBagConstraints constrains = new GridBagConstraints();
89
            constrains.gridx = GridBagConstraints.RELATIVE;
90
            constrains.gridy = i+1;
91
            constrains.weightx = 0;
92
            constrains.weighty = 0;
93
            constrains.insets = new Insets(5, 5, 5, 5);
94
            constrains.anchor = GridBagConstraints.WEST;
139
      rdbEnterTheCoordinatesManually.setActionCommand("_Enter_the_coordinates_manually");
140
      rdbEnterTheCoordinatesManually.setName("rdbEnterTheCoordinatesManually");
141
      rdbEnterTheCoordinatesManually.setText("_Enter_the_coordinates_manually");
142
      buttongroup1.add(rdbEnterTheCoordinatesManually);
143
      jpanel1.add(rdbEnterTheCoordinatesManually,cc.xywh(2,6,4,1));
95 144

  
96
            add(getCoordinateLabel(i), constrains);
145
      lblX.setName("lblX");
146
      lblX.setText("X");
147
      jpanel1.add(lblX,cc.xy(3,8));
97 148

  
98
            constrains = new GridBagConstraints();
99
            constrains.gridx = GridBagConstraints.RELATIVE;
100
            constrains.gridy = i+1;
101
            constrains.weightx = 1;
102
            constrains.weighty = 0;
103
            constrains.fill = GridBagConstraints.HORIZONTAL;
104
            constrains.insets = new Insets(5, 5, 5, 5);
105
            constrains.anchor = GridBagConstraints.EAST;
149
      lblY.setName("lblY");
150
      lblY.setText("Y");
151
      jpanel1.add(lblY,cc.xy(3,10));
106 152

  
107
            add(getCoordinateTextField(i), constrains);
108
            if (defaultPoint != null) {
109
                getCoordinateTextField(i).setText(String.valueOf(defaultPoint.getCoordinateAt(i)));
110
            }
111
        }
112
    }
153
      txtX.setName("txtX");
154
      jpanel1.add(txtX,cc.xy(5,8));
113 155

  
114
    protected JTextField getCoordinateTextField(int i) {
115
        if (fields[i] == null) {
116
            fields[i] = new JTextField();
117
        }
118
        return fields[i];
119
    }
156
      txtY.setName("txtY");
157
      jpanel1.add(txtY,cc.xy(5,10));
120 158

  
121
    private JLabel getCoordinateLabel(int i) {
122
        if (labels[i] == null) {
123
            labels[i] = new JLabel(createLabel(i));
124
        }
125
        return labels[i];
126
    }
159
      lblZ.setName("lblZ");
160
      lblZ.setText("Z");
161
      jpanel1.add(lblZ,cc.xy(3,12));
127 162

  
128
    private String createLabel(int i){
129
        if(i==0){
130
            return "X";
131
        }
132
        if(i==1){
133
            return "Y";
134
        }
135
        if (defaultPoint != null) {
136
            GeometryType geomType = defaultPoint.getGeometryType();
137
            if ((geomType.isSubTypeOf(Geometry.SUBTYPES.GEOM2DM) || geomType.isSubTypeOf(Geometry.SUBTYPES.GEOM3DM))
138
                && i == defaultPoint.getDimension() - 1) {
139
                return "M";
140
            }
141
            if (i == 2) {
142
                return "Z";
143
            }
144
            if (i > 2) {
145
                return "C".concat(String.valueOf(i));
146
            }
147
        } else {
148
            if (i > 1) {
149
                return "C".concat(String.valueOf(i));
150
            }
151
        }
152
        return "";
153
    }
163
      lblM.setName("lblM");
164
      lblM.setText("M");
165
      jpanel1.add(lblM,cc.xy(3,14));
166

  
167
      txtZ.setName("txtZ");
168
      jpanel1.add(txtZ,cc.xy(5,12));
169

  
170
      txtM.setName("txtM");
171
      jpanel1.add(txtM,cc.xy(5,14));
172

  
173
      addFillComponents(jpanel1,new int[]{ 1,2,3,4,5,6 },new int[]{ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 });
174
      return jpanel1;
175
   }
176

  
177
   /**
178
    * Initializer
179
    */
180
   protected void initializePanel()
181
   {
182
      setLayout(new BorderLayout());
183
      add(createPanel(), BorderLayout.CENTER);
184
   }
185

  
186

  
154 187
}

Also available in: Unified diff