Statistics
| Revision:

gvsig-raster / org.gvsig.raster.multifile / trunk / org.gvsig.raster.multifile / org.gvsig.raster.multifile.app.multifileclient / src / main / java / org / gvsig / raster / multifile / app / panel / LayerNameDialog.java @ 5988

History | View | Annotate | Download (5.09 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.raster.multifile.app.panel;
20

    
21
import java.awt.BorderLayout;
22
import java.awt.GridBagConstraints;
23
import java.awt.GridBagLayout;
24
import java.awt.Insets;
25
import java.awt.geom.Point2D;
26

    
27
import javax.swing.JOptionPane;
28
import javax.swing.JPanel;
29
import javax.swing.JTextField;
30

    
31
import org.gvsig.andami.PluginServices;
32
import org.gvsig.andami.ui.mdiManager.IWindow;
33
import org.gvsig.andami.ui.mdiManager.WindowInfo;
34
import org.gvsig.i18n.Messages;
35
import org.gvsig.raster.swing.basepanel.AbstractButtonsPanel;
36
import org.gvsig.raster.swing.basepanel.ButtonsPanelEvent;
37
import org.gvsig.raster.swing.basepanel.ButtonsPanelListener;
38
import org.gvsig.raster.swing.basepanel.IButtonsPanel;
39

    
40
/**
41
 * <code>LayerNameDialog</code>.
42
 * Window to introduce the name of the new layer
43
 * @author Nacho Brodin (nachobrodin@gmail.com)
44
 */
45
public class LayerNameDialog extends AbstractButtonsPanel implements IWindow, ButtonsPanelListener {
46
        private static final long     serialVersionUID = 7362459094802955247L;
47
        private JPanel                panel            = null;
48
        private String                layerName        = null;
49
        private JTextField            name             = null;
50
        private ButtonsPanelListener  listener         = null;
51
        private Point2D               position         = null;
52

    
53
        /**
54
         * Builds a new window.
55
         * @param position
56
         * @param width
57
         * @param height
58
         * @param listener
59
         */
60
        public LayerNameDialog(Point2D position, int width, int height, ButtonsPanelListener listener) {
61
                super(IButtonsPanel.BUTTONS_ACCEPTCANCEL);
62
                this.position = position;
63
                layerName = null;
64
                this.listener = listener;
65
                this.setSize(width, height);
66
                this.setLayout(new BorderLayout(5, 5));
67
                this.add(getMainPanel(), java.awt.BorderLayout.CENTER);
68
                this.addButtonPressedListener(this);
69
        }
70

    
71
        /**
72
         * Builds a new window.
73
         * @param width
74
         * @param height
75
         * @param listener
76
         */
77
        public LayerNameDialog(int width, int height, ButtonsPanelListener listener) {
78
                this(new Point2D.Double(), width, height, listener);
79
        }
80

    
81
        /**
82
         * Gets the main panel
83
         * @return JPanel
84
         */
85
        public JPanel getMainPanel() {
86
                if (panel == null) {
87
                        panel = new JPanel();
88
                        panel.setLayout(new GridBagLayout());
89
                        GridBagConstraints gbc = new GridBagConstraints();
90
                        gbc.insets = new Insets(0, 0, 0, 0);
91
                        gbc.fill = GridBagConstraints.HORIZONTAL;
92
                        gbc.anchor = GridBagConstraints.CENTER;
93
                        gbc.weightx = 1.0;
94
                        panel.add(getNameField(), gbc);
95
                }
96
                return panel;
97
        }
98

    
99
        private JTextField getNameField() {
100
                if(name == null) {
101
                        name = new JTextField();
102
                }
103
                return name;
104
        }
105

    
106
        public WindowInfo getWindowInfo() {
107
                WindowInfo m_viewinfo=new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE);
108
                m_viewinfo.setTitle(Messages.getText("layer_name"));
109
                m_viewinfo.setHeight(this.getHeight());
110
                m_viewinfo.setWidth(this.getWidth());
111
                m_viewinfo.setX((int)position.getX());
112
                m_viewinfo.setY((int)position.getY());
113
                return m_viewinfo;
114
        }
115

    
116
        /**
117
         * Cancel actions
118
         */
119
        private void close() {
120
                try {
121
                        PluginServices.getMDIManager().closeWindow(this);
122
                } catch (ArrayIndexOutOfBoundsException e) {
123
                        //Si la ventana no se puede eliminar no hacemos nada
124
                }
125
        }
126

    
127
        /**
128
         * Accept actions
129
         */
130
        private void accept() {
131
                String txt = getNameField().getText();
132
                if(txt == null || txt.compareTo("") == 0) {
133
                        JOptionPane.showMessageDialog(null, Messages.getText("character_not_valid"), "", JOptionPane.ERROR_MESSAGE);
134
                        return;
135
                }
136

    
137
                byte[] byteList = txt.getBytes();
138
                for (int i = 0; i < byteList.length; i++) {
139
                        if(byteList[i] < 45 ||
140
                           (byteList[i] > 45 && byteList[i] < 48) ||
141
                           (byteList[i] > 57 && byteList[i] < 65) ||
142
                           (byteList[i] > 90 && byteList[i] < 95) ||
143
                           (byteList[i] > 95 && byteList[i] < 97) ||
144
                           (byteList[i] > 122 && byteList[i] < 126) ||
145
                           (byteList[i] > 126)) {
146
                                JOptionPane.showMessageDialog(null, Messages.getText("character_not_valid"), "", JOptionPane.ERROR_MESSAGE);
147
                                return;
148
                        }
149
                }
150

    
151
                layerName = txt;
152
                close();
153
                listener.actionButtonPressed(new ButtonsPanelEvent(layerName, IButtonsPanel.BUTTON_ACCEPT));
154
        }
155

    
156
        public void actionButtonPressed(ButtonsPanelEvent e) {
157
                if (e.getButton() == IButtonsPanel.BUTTON_CANCEL) {
158
                        close();
159
                }
160

    
161
                if (e.getButton() == IButtonsPanel.BUTTON_ACCEPT) {
162
                        accept();
163
                }
164
        }
165

    
166
        public Object getWindowProfile() {
167
                return WindowInfo.PROPERTIES_PROFILE;
168
        }
169
}