Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGraph / src / com / iver / cit / gvsig / graph / gui / wizard / servicearea / ServiceAreaPage1.java @ 17626

History | View | Annotate | Download (15.4 KB)

1
package com.iver.cit.gvsig.graph.gui.wizard.servicearea;
2

    
3
import java.awt.BorderLayout;
4
import java.awt.Color;
5
import java.awt.Component;
6
import java.awt.Dimension;
7
import java.awt.event.ActionEvent;
8
import java.awt.event.ActionListener;
9
import java.awt.event.ComponentEvent;
10
import java.awt.event.ComponentListener;
11
import java.awt.event.KeyEvent;
12
import java.awt.event.KeyListener;
13
import java.awt.event.MouseEvent;
14
import java.awt.event.MouseListener;
15

    
16
import javax.swing.ImageIcon;
17
import javax.swing.JCheckBox;
18
import javax.swing.JLabel;
19
import javax.swing.JOptionPane;
20
import javax.swing.JPanel;
21
import javax.swing.JScrollPane;
22
import javax.swing.JTable;
23
import javax.swing.JTextArea;
24
import javax.swing.border.TitledBorder;
25
import javax.swing.table.TableCellRenderer;
26

    
27
import jwizardcomponent.JWizardPanel;
28

    
29
import com.iver.andami.PluginServices;
30
import com.iver.cit.gvsig.graph.NetworkUtils;
31
import com.iver.cit.gvsig.graph.gui.wizard.servicearea.AbstractPointsModel.InvalidCostException;
32

    
33
public class ServiceAreaPage1 extends JWizardPanel implements KeyListener, MouseListener, ComponentListener, ActionListener{
34
        
35
        /**
36
         * 
37
         */
38
        private static final long serialVersionUID = 7051194740312102283L;
39

    
40
        private ServiceAreaWizard owner;
41
        
42
        private int selectedRowIndex;
43
                
44
        private JPanel contentpane;
45
        private JPanel panelNorth;
46
        private JLabel labelTitle;
47
        private JLabel labelAreaMainCost;
48
        private JLabel labelErrorMainCosts;
49
        private ImageIcon iconErrorCost;
50
        private JScrollPane scrollAreaMainCosts;
51
        private JTextArea textAreaMainCosts;
52
        private JLabel labelAreaSecondaryCosts;
53
        private JLabel labelErrorSecondaryCosts;
54
        private JScrollPane scrollAreaSecondaryCosts;
55
        private JTextArea textAreaSecondaryCosts;
56
        private JCheckBox checkSameCosts;
57
        private TitledBorder borderPanelPoints;
58
        private JPanel panelTable;
59
        private JScrollPane scrollPoints;
60
        //private JTable tablePoints;
61
        private ColoredTable tablePoints;
62
        
63
        private static final int BORDER_HGAP = 7;
64
        private static final int BORDER_VGAP = 7;
65
        private static final int COMPONENT_HGAP = 4;
66
        private static final int COMPONENT_VGAP = 4;
67
        private static final int PREFERRED_HEIGHT = 23;
68
        private static final int PREFERRED_WIDTH  = 120;
69

    
70
        public ServiceAreaPage1(ServiceAreaWizard wizard){
71
                super(wizard.getWizardComponents());
72
                this.owner=wizard;
73
                this.selectedRowIndex=-1;
74
                this.initialize();
75
        }
76
        
77
        private void initialize(){
78
                this.addComponentListener(this);
79
                
80
                this.contentpane=new JPanel();
81
                this.panelNorth=new JPanel();
82
                this.labelTitle=new JLabel("<html><b>Edici?n de costes de cada ?rea</b></html>");
83
                this.labelAreaMainCost=new JLabel("<html>Costes <b>primarios</b> de ?rea:</html>");
84
                this.labelErrorMainCosts=new JLabel();
85
                this.labelErrorMainCosts.setOpaque(true);
86
                this.iconErrorCost=PluginServices.getIconTheme().get("service_area_wrong_costs");
87
                this.scrollAreaMainCosts=new JScrollPane();
88
                this.scrollAreaMainCosts.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
89
                this.scrollAreaMainCosts.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
90
                this.textAreaMainCosts=new JTextArea();
91
                this.textAreaMainCosts.setRows(4);
92
                this.textAreaMainCosts.setLineWrap(true);
93
                this.textAreaMainCosts.addKeyListener(this);
94
                this.scrollAreaMainCosts.getViewport().add(this.textAreaMainCosts);
95
                this.labelAreaSecondaryCosts=new JLabel("<html>Costes <b>secundarios</b> de ?rea:</html>");
96
                this.labelErrorSecondaryCosts=new JLabel();
97
                this.labelErrorSecondaryCosts.setOpaque(true);
98
                this.scrollAreaSecondaryCosts=new JScrollPane();
99
                this.scrollAreaSecondaryCosts.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
100
                this.scrollAreaSecondaryCosts.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
101
                this.textAreaSecondaryCosts=new JTextArea();
102
                this.textAreaSecondaryCosts.setRows(4);
103
                this.textAreaSecondaryCosts.setLineWrap(true);
104
                this.textAreaSecondaryCosts.addKeyListener(this);
105
                this.scrollAreaSecondaryCosts.getViewport().add(this.textAreaSecondaryCosts);
106
                this.checkSameCosts=new JCheckBox("<html><p align='left'>Utilizar los costes del</p><p align='left'>punto seleccionado</p><p align='left'>para el resto</p><p align='left'>de puntos</p></html>");
107
                this.checkSameCosts.addActionListener(this);
108
                this.panelTable=new JPanel();
109
                this.borderPanelPoints=new TitledBorder("Gestor de paradas");
110
                this.panelTable.setBorder(this.borderPanelPoints);
111
                this.scrollPoints=new JScrollPane();
112
                //this.tablePoints=new JTable();
113
                this.tablePoints=new ColoredTable();
114
                this.tablePoints.enablePijamaEffect(Color.white, new Color(229, 251, 252));
115
                //this.tablePoints.setModel(this.owner.getModel());
116
                this.tablePoints.addMouseListener(this);
117
                this.scrollPoints.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
118
                this.scrollPoints.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
119
                this.scrollPoints.getViewport().add(this.tablePoints);
120
                
121
                this.panelNorth.setLayout(null);
122
                this.labelTitle.setBounds(BORDER_HGAP, BORDER_VGAP, this.labelTitle.getFontMetrics(this.labelTitle.getFont()).stringWidth(this.labelTitle.getText()), PREFERRED_HEIGHT);
123
                this.labelAreaMainCost.setBounds(BORDER_HGAP, this.labelTitle.getY()+this.labelTitle.getHeight()+COMPONENT_VGAP, this.labelAreaMainCost.getFontMetrics(this.labelAreaMainCost.getFont()).stringWidth(this.labelAreaMainCost.getText()), 23);
124
                this.labelErrorMainCosts.setBounds(this.labelAreaMainCost.getWidth()+this.labelAreaMainCost.getX()+COMPONENT_HGAP, this.labelAreaMainCost.getY(), 23, 23);
125
                this.scrollAreaMainCosts.setBounds(BORDER_HGAP, this.labelAreaMainCost.getY()+this.labelAreaMainCost.getHeight()+COMPONENT_VGAP, 200, 70);
126
                this.checkSameCosts.setBounds(this.scrollAreaMainCosts.getX()+this.scrollAreaMainCosts.getWidth()+BORDER_HGAP, this.scrollAreaMainCosts.getY(), this.checkSameCosts.getFontMetrics(this.checkSameCosts.getFont()).stringWidth(this.checkSameCosts.getText()), this.scrollAreaMainCosts.getHeight());
127
                this.labelAreaSecondaryCosts.setBounds(BORDER_HGAP, this.scrollAreaMainCosts.getY()+this.scrollAreaMainCosts.getHeight()+COMPONENT_VGAP, this.labelAreaSecondaryCosts.getFontMetrics(this.labelAreaSecondaryCosts.getFont()).stringWidth(this.labelAreaSecondaryCosts.getText()), PREFERRED_HEIGHT);
128
                this.labelErrorSecondaryCosts.setBounds(this.labelAreaSecondaryCosts.getWidth()+this.labelAreaSecondaryCosts.getX()+COMPONENT_HGAP, this.labelAreaSecondaryCosts.getY(), 23, 23);
129
                this.scrollAreaSecondaryCosts.setBounds(BORDER_HGAP, this.labelAreaSecondaryCosts.getY()+this.labelAreaSecondaryCosts.getHeight()+BORDER_VGAP, 200, 70);
130
                                
131
                this.panelNorth.setPreferredSize(new Dimension(0, 
132
                                this.labelTitle.getHeight() + 
133
                                this.labelAreaMainCost.getHeight() + 
134
                                this.scrollAreaMainCosts.getHeight() +
135
                                this.labelAreaSecondaryCosts.getHeight() +
136
                                this.scrollAreaSecondaryCosts.getHeight() + 40));
137
                
138
                this.panelNorth.add(this.labelTitle);
139
                this.panelNorth.add(this.labelAreaMainCost);
140
                this.panelNorth.add(this.labelErrorMainCosts);
141
                this.panelNorth.add(this.scrollAreaMainCosts);
142
                this.panelNorth.add(this.checkSameCosts);
143
                this.panelNorth.add(this.labelAreaSecondaryCosts);
144
                this.panelNorth.add(this.labelErrorSecondaryCosts);
145
                this.panelNorth.add(this.scrollAreaSecondaryCosts);
146
                
147
                this.panelTable.setLayout(new BorderLayout(7, 7));
148
                this.panelTable.add(this.scrollPoints, BorderLayout.CENTER);
149
                
150
                this.contentpane.setLayout(new BorderLayout(7, 7));
151
                this.contentpane.add(this.panelNorth, BorderLayout.NORTH);
152
                this.contentpane.add(this.panelTable, BorderLayout.CENTER);
153
                
154
                this.setLayout(new BorderLayout(7, 7));
155
                this.add(this.contentpane, BorderLayout.CENTER);
156
        }
157
        
158
        public void next(){
159
                AbstractPointsModel model=this.owner.getController().getModel();
160
                
161
                if(model.getEnabledRowsCount()>0){
162
                        if(!model.isUniqueMainCostEnabled() || !model.isUniqueSecondaryCostEnabled()){
163
                                boolean flag=true;
164
                                int rowCount=model.getRowCount();
165
                                int i=0;
166

    
167
                                for (i = 0; i < rowCount && flag; i++) {
168
                                        if(model.isRowEnabled(i)){
169
                                                if(String.valueOf(model.getValueAt(i, 3)).trim().equals("") || String.valueOf(model.getValueAt(i, 4)).trim().equals("")){
170
                                                        flag=false;
171
                                                }
172
                                        }
173
                                }
174

    
175
                                if(flag){
176
                                        super.next();
177
                                }
178
                                else{
179
                                        JOptionPane.showMessageDialog(this, "La fila con ID "+Integer.valueOf(String.valueOf(model.getValueAt(i, 1)))+" no tiene todos los costes asignados", "Costes no asignados", JOptionPane.WARNING_MESSAGE);
180
                                }
181
                        }
182
                        else{
183
                                super.next();
184
                        }
185
                }
186
                else{
187
                        JOptionPane.showMessageDialog(this, "No hay ninguna fila habilitada", "Advertencia", JOptionPane.WARNING_MESSAGE);
188
                }
189
        }
190

    
191
        public void keyPressed(KeyEvent e) {
192

    
193
        }
194

    
195
        public void keyReleased(KeyEvent e) {
196

    
197
        }
198

    
199
        public void keyTyped(KeyEvent e) {
200
                if(e.getSource()==this.textAreaMainCosts){
201
                        try{
202
                                AbstractPointsModel model=this.owner.getController().getModel();
203
                                double[] costs=NetworkUtils.string2doubleArray(this.textAreaMainCosts.getText()+e.getKeyChar(), model.COSTS_SEPARATOR);
204
                                this.labelErrorMainCosts.setIcon(null);
205
                                this.labelErrorMainCosts.setToolTipText("");
206
                                this.textAreaMainCosts.setBackground(Color.white);
207
                                if(this.selectedRowIndex>=0 && this.selectedRowIndex<this.owner.getController().getModel().getRowCount()){
208
                                        String s="";
209
                                        for (int i = 0; i < costs.length; i++) {
210
                                                s+=costs[i]+model.COSTS_SEPARATOR;
211
                                        }
212
                                        s=s.trim();
213
                                        if(s.endsWith(model.COSTS_SEPARATOR)) s=s.substring(0, s.length()-1);
214
                                        this.owner.getController().getModel().setValueAt(s, this.selectedRowIndex, 3);
215
                                        this.tablePoints.updateUI();
216
                                }
217
                        }
218
                        catch(NumberFormatException except){
219
                                this.labelErrorMainCosts.setIcon(this.iconErrorCost);
220
                                this.labelErrorMainCosts.setToolTipText("Hay un error en los costes primarios");
221
                                this.textAreaMainCosts.setBackground(new Color(255, 210, 210));
222
                        }
223
                }
224
                else if(e.getSource()==this.textAreaSecondaryCosts){
225
                        try{
226
                                AbstractPointsModel model=this.owner.getController().getModel();
227
                                double[] costs=NetworkUtils.string2doubleArray(this.textAreaSecondaryCosts.getText()+e.getKeyChar(), model.COSTS_SEPARATOR);
228
                                this.labelErrorSecondaryCosts.setIcon(null);
229
                                this.labelErrorSecondaryCosts.setToolTipText("");
230
                                this.textAreaSecondaryCosts.setBackground(Color.white);
231
                                if(this.selectedRowIndex>=0 && this.selectedRowIndex<this.owner.getController().getModel().getRowCount()){
232
                                        String s="";
233
                                        for (int i = 0; i < costs.length; i++) {
234
                                                s+=costs[i]+model.COSTS_SEPARATOR;
235
                                        }
236
                                        s=s.trim();
237
                                        if(s.endsWith(model.COSTS_SEPARATOR)) s=s.substring(0, s.length()-1);
238
                                        this.owner.getController().getModel().setValueAt(s, this.selectedRowIndex, 4);
239
                                        this.tablePoints.updateUI();
240
                                }
241
                        }
242
                        catch(NumberFormatException except){
243
                                this.labelErrorSecondaryCosts.setIcon(this.iconErrorCost);
244
                                this.labelErrorSecondaryCosts.setToolTipText("Hay un error en los costes secundarios");
245
                                this.textAreaSecondaryCosts.setBackground(new Color(255, 210, 210));
246
                        }
247
                }
248
        }
249

    
250
        public void mouseClicked(MouseEvent e) {
251
                if(e.getSource()==this.tablePoints){
252
                        this.selectedRowIndex=this.tablePoints.getSelectedRow();
253
                        if(this.selectedRowIndex>=0 && this.selectedRowIndex<this.owner.getController().getModel().getRowCount()){
254
                                String s=String.valueOf(this.owner.getController().getModel().getValueAt(this.selectedRowIndex, 3));
255
                                this.textAreaMainCosts.setText(s.trim());
256
                                s=String.valueOf(this.owner.getController().getModel().getValueAt(this.selectedRowIndex, 4));
257
                                this.textAreaSecondaryCosts.setText(s.trim());
258
                        }
259
                        
260
                        this.textAreaMainCosts.setBackground(Color.white);
261
                        this.textAreaSecondaryCosts.setBackground(Color.white);
262
                }
263
        }
264

    
265
        public void mouseEntered(MouseEvent e) {
266
        }
267

    
268
        public void mouseExited(MouseEvent e) {
269
        }
270

    
271
        public void mousePressed(MouseEvent e) {
272
        }
273

    
274
        public void mouseReleased(MouseEvent e) {
275
        }
276

    
277
        public void componentHidden(ComponentEvent e) {
278
        }
279

    
280
        public void componentMoved(ComponentEvent e) {
281
        }
282

    
283
        public void componentResized(ComponentEvent e) {
284
        }
285

    
286
        public void componentShown(ComponentEvent e) {
287
                this.tablePoints.setModel(this.owner.getController().getModel());
288
                //this.textAreaMainCosts.setText("");
289
                //this.textAreaSecondaryCosts.setText("");
290
        }
291

    
292
        public void actionPerformed(ActionEvent e) {
293
                if(e.getSource()==this.checkSameCosts){
294
                        AbstractPointsModel model=this.owner.getController().getModel();
295

    
296
                        if(this.checkSameCosts.isSelected()){
297
                                if(this.tablePoints.getSelectedRowCount()>0){
298
                                        String mainCosts=String.valueOf(model.getValueAt(this.tablePoints.getSelectedRow(), 3)),
299
                                        secondaryCosts=String.valueOf(model.getValueAt(this.tablePoints.getSelectedRow(),4));
300
                                        if(!mainCosts.trim().equals("") && !secondaryCosts.trim().equals("")){
301
                                                try {
302
                                                        model.enableUniqueMainCost(String.valueOf(model.getValueAt(this.tablePoints.getSelectedRow(), 3)));
303
                                                        model.enableUniqueSecondaryCost(String.valueOf(model.getValueAt(this.tablePoints.getSelectedRow(), 4)));
304
                                                        this.tablePoints.setRowBackground(this.tablePoints.getSelectedRow(), new Color(237, 240, 184));
305
                                                        this.tablePoints.updateUI();
306
                                                } catch (InvalidCostException except) {
307
                                                        JOptionPane.showMessageDialog(this, except.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
308
                                                }
309
                                        }
310
                                        else if(mainCosts.trim().equals("")){
311
                                                JOptionPane.showMessageDialog(this, "El coste primario de la fila seleccionada no es correcto", "Coste incorrecto", JOptionPane.ERROR_MESSAGE);
312
                                                this.checkSameCosts.setSelected(false);
313
                                        }
314
                                        else if(secondaryCosts.trim().equals("")){
315
                                                JOptionPane.showMessageDialog(this, "El coste secundario de la fila seleccionada no es correcto", "Coste incorrecto", JOptionPane.ERROR_MESSAGE);
316
                                                this.checkSameCosts.setSelected(false);
317
                                        }
318
                                }
319
                                else{
320
                                        JOptionPane.showMessageDialog(this, "No se puede asginar un coste ?nico porque no hay ninguna fila seleccionada");
321
                                        this.checkSameCosts.setSelected(false);
322
                                }
323
                        }
324
                        else{
325
                                model.disableUniqueMainCost();
326
                                model.disableUniqueSecondaryCost();
327
                                this.tablePoints.clearColoredRow();
328
                                this.tablePoints.updateUI();
329
                        }
330
                }
331
        }
332
        
333
        class ColoredTable extends JTable{
334
                
335
                /**
336
                 * 
337
                 */
338
                private static final long serialVersionUID = -8283898126306207443L;
339
                private Color rowColor;
340
                private int coloredRowIndex = -1;
341
                
342
                private boolean pijamaEffectEnabled=false;
343
                private Color pijamaColor1;
344
                private Color pijamaColor2;
345
                
346
                public void setRowBackground(int rowIndex, Color color) throws IndexOutOfBoundsException{
347
                        if(this.getModel().getRowCount()>rowIndex){
348
                                this.rowColor=color;
349
                                this.coloredRowIndex=rowIndex;
350
                        }
351
                        else{
352
                                throw new IndexOutOfBoundsException("La fila "+rowIndex+" no existe");
353
                        }
354
                }
355
                
356
                public void clearColoredRow(){
357
                        this.coloredRowIndex=-1;
358
                }
359
                
360
                public void enablePijamaEffect(Color color1, Color color2){
361
                        this.pijamaColor1=color1;
362
                        this.pijamaColor2=color2;
363
                        this.pijamaEffectEnabled=true;
364
                }
365
                
366
                public void disablePijamaEffect(){
367
                        this.pijamaEffectEnabled=false;
368
                }
369
                
370
                public boolean isPijamaEffectEnabled(){
371
                        return this.pijamaEffectEnabled;
372
                }
373

    
374
                public Component prepareRenderer(TableCellRenderer renderer, int row, int column){
375
                        Component returnComp = super.prepareRenderer(renderer, row, column);
376
                        
377
                        if (!returnComp.getBackground().equals(getSelectionBackground()) && this.coloredRowIndex==row){
378
                                returnComp .setBackground(rowColor);
379
                        }
380
                        
381
                        if(this.pijamaEffectEnabled && !returnComp.getBackground().equals(getSelectionBackground()) && row!=coloredRowIndex){
382
                                if(row%2==0){
383
                                        returnComp.setBackground(this.pijamaColor1);
384
                                }
385
                                else{
386
                                        returnComp.setBackground(this.pijamaColor2);
387
                                }
388
                        }
389

    
390
                        return returnComp;
391
                }
392
        }
393
}