Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / DefaultZoomDialog.java @ 2847

History | View | Annotate | Download (5.61 KB)

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.tools.swing.impl;
25

    
26
import java.awt.BorderLayout;
27
import java.awt.Component;
28
import java.awt.Dimension;
29
import java.awt.Rectangle;
30
import java.awt.Window;
31
import java.awt.event.ActionEvent;
32
import java.awt.event.ActionListener;
33
import java.awt.event.KeyEvent;
34

    
35
import javax.swing.BorderFactory;
36
import javax.swing.JButton;
37
import javax.swing.JComponent;
38
import javax.swing.JDialog;
39
import javax.swing.JPanel;
40
import javax.swing.JScrollPane;
41
import javax.swing.JTextArea;
42
import javax.swing.KeyStroke;
43
import org.gvsig.tools.ToolsLocator;
44
import org.gvsig.tools.i18n.I18nManager;
45
import org.gvsig.tools.swing.api.ToolsSwingUtils;
46
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
47
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
48

    
49
public class DefaultZoomDialog extends JDialog {
50

    
51
    /**
52
     *
53
     */
54
    private static final long serialVersionUID = 7913363575200612492L;
55

    
56
    private String value = null;
57
    private JTextArea text = null;
58
    private int action = WindowManager_v2.BUTTON_CANCEL;
59
    private JButton btnClose;
60

    
61
    public DefaultZoomDialog(Component parent, String title, WindowManager.MODE mode) {
62
        super(getParentWindow(parent), title, mode == WindowManager.MODE.WINDOW ? ModalityType.MODELESS : ModalityType.DOCUMENT_MODAL);
63
    }
64

    
65
    public DefaultZoomDialog(Component parent, String title, String value, WindowManager.MODE mode) {
66
        this(parent, title, mode);
67
        this.value = value;
68
        initComponents();
69

    
70
    }
71

    
72
    private static Window getParentWindow(Component c) {
73
        while( c!=null ) {
74
            if( c instanceof Window ) {
75
                return (Window) c;
76
            } 
77
            c = c.getParent();
78
        }
79
        return null;
80
    }
81
    
82
    public void setEditable(boolean editable) {
83
        this.text.setEditable(editable);
84
        I18nManager i18n = ToolsLocator.getI18nManager();
85
        if( editable ) {
86
            this.btnClose.setText(i18n.getTranslation("_Accept"));
87
        } else {
88
            this.btnClose.setText(i18n.getTranslation("_Close"));
89
        }
90
    }
91

    
92
    private void initComponents() {
93

    
94
        JPanel panel = new JPanel();
95
        panel.setLayout(new BorderLayout());
96

    
97
        text = new JTextArea();
98
        text.setText(value);
99
        text.setLineWrap(true);
100
        text.setEnabled(true);
101
        JScrollPane scroll = new JScrollPane(text);
102
        panel.add(scroll, BorderLayout.CENTER);
103
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
104

    
105
        JPanel p = new JPanel();
106

    
107
        I18nManager i18n = ToolsLocator.getI18nManager();
108
        this.btnClose = new JButton(i18n.getTranslation("_Accept"));
109
        this.btnClose.addActionListener(new ActionListener() {
110
            @Override
111
            public void actionPerformed(ActionEvent arg0) {
112
                if( text.isEditable() ) {
113
                    value = text.getText();
114
                    setAction(WindowManager_v2.BUTTON_OK);
115
                } else {
116
                    setAction(WindowManager_v2.BUTTON_CANCEL);
117
                }
118
                setVisible(false);
119
            }
120
        });
121

    
122
        p.setLayout(new BorderLayout());
123
        p.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
124
        p.add(this.btnClose, BorderLayout.LINE_END);
125

    
126
        panel.add(p, BorderLayout.PAGE_END);
127
        ContextMenuUtils.setDefaultContextMenu(text, false);
128

    
129
        this.setPreferredSize(ToolsSwingUtils.ensureRowsCols(this.getPreferredSize(), 15, 100, 25, 150));
130

    
131
        this.setContentPane(panel);
132

    
133
        KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
134
        panel.registerKeyboardAction(new ActionListener() {
135
                @Override
136
                public void actionPerformed(ActionEvent e) {
137
                    setVisible(false);
138
                    setAction(WindowManager_v2.BUTTON_CANCEL);
139
                }
140
            }, 
141
            stroke, 
142
            JComponent.WHEN_IN_FOCUSED_WINDOW
143
        );
144
  
145
        this.center();
146
        this.pack();
147
    }
148

    
149
    public void center() {
150
        Dimension size = this.getPreferredSize();
151

    
152
//        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
153
//        int x = (int) ((dimension.getWidth() - size.getWidth()) / 2);
154
//        int y = (int) ((dimension.getHeight() - size.getHeight()) / 2);
155

    
156
        Rectangle r = this.getGraphicsConfiguration().getBounds();
157
        int x = (int) ((r.width - size.getWidth()) / 2) + r.x;
158
        int y = (int) ((r.height - size.getHeight()) / 2) + r.y ;
159

    
160
        this.setLocation(x, y);
161
    }
162

    
163
    public String getText() {
164
        return this.value;
165
    }
166
    
167
    protected void setAction(int action) {
168
        this.action = action;
169
    }
170
    
171
    public int getAction() {
172
        return this.action;
173
    }
174
}