Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.mkmvnproject.app / org.gvsig.mkmvnproject.app.mainplugin / src / main / java / org / gvsig / mkmvnproject / gui / settings / PlatformPropertiesWindow.java @ 40557

History | View | Annotate | Download (10.3 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.mkmvnproject.gui.settings;
25

    
26
import java.awt.BorderLayout;
27
import java.awt.Font;
28
import java.awt.GridBagConstraints;
29
import java.awt.GridBagLayout;
30
import java.awt.GridLayout;
31
import java.awt.Insets;
32
import java.awt.event.ActionEvent;
33
import java.awt.event.ActionListener;
34
import java.io.File;
35
import java.io.FileOutputStream;
36
import java.io.IOException;
37

    
38
import javax.swing.JButton;
39
import javax.swing.JLabel;
40
import javax.swing.JOptionPane;
41
import javax.swing.JPanel;
42
import javax.swing.JScrollBar;
43
import javax.swing.JScrollPane;
44
import javax.swing.JTextArea;
45
import javax.swing.event.DocumentEvent;
46
import javax.swing.event.DocumentListener;
47

    
48
import org.slf4j.Logger;
49
import org.slf4j.LoggerFactory;
50

    
51
import org.gvsig.andami.PluginServices;
52
import org.gvsig.andami.ui.mdiManager.IWindow;
53
import org.gvsig.andami.ui.mdiManager.WindowInfo;
54
import org.gvsig.i18n.Messages;
55

    
56

    
57
/**
58
 * This dialog lets the user write the file .gvsig.platform.properties
59
 * 
60
 * @author jldominguez
61
 *
62
 */
63
public class PlatformPropertiesWindow extends JPanel
64
implements IWindow, ActionListener, DocumentListener {
65
    
66
    private static final Logger logger =
67
        LoggerFactory.getLogger(PlatformPropertiesWindow.class);
68
    
69
    private WindowInfo winfo = null;
70
    private File targetFile = null;
71
    private boolean isWindows = false;
72
    
73
    private JPanel northPanel = null;
74
    
75
    private JTextArea contentArea = null;
76
    
77
    private JPanel southPanel = null;
78
    private JButton saveButton = null;
79
    private JButton suggButton = null;
80
    private JButton okButton = null;
81
    private JButton cancelButton = null;
82
    
83
    private boolean validSettings = false; 
84
    
85
    public PlatformPropertiesWindow(
86
        File non_existing_file, boolean is_win) {
87
        
88
        targetFile = non_existing_file;
89
        isWindows = is_win;
90
        init();
91
    }
92

    
93
    private void init() {
94
        
95
        this.setLayout(new BorderLayout());
96
        
97
        northPanel = new JPanel(new GridBagLayout());
98
        GridBagConstraints c = new GridBagConstraints();  
99
        c.insets = new Insets(1, 10, 1, 10);
100
        c.weightx = 0.5; // 
101
        c.fill = GridBagConstraints.HORIZONTAL;
102
        c.gridx = 0;
103
        
104
        c.gridy = 0;
105
        northPanel.add(new JLabel(" "), c);
106
        c.gridy = 1;
107
        northPanel.add(new JLabel(Messages.getText(
108
            "_Mandatory_platform_properties_file_not_found")), c);
109
        c.gridy = 2;
110
        northPanel.add(new JLabel(Messages.getText(
111
            "_Use_this_text_editor_to_write_it")), c);
112
        c.gridy = 3;
113
        northPanel.add(new JLabel(" "), c);
114
        // =============================================
115
        // =============================================
116
        southPanel = new JPanel();
117
        southPanel.setLayout(new GridLayout(1, 5, 8, 4));
118
        // ------------
119
        southPanel.add(getSaveButton());
120
        southPanel.add(getSuggButton());
121
        // ------------
122
        southPanel.add(new JLabel(" "));
123
        // ------------
124
        southPanel.add(getOkButton());
125
        southPanel.add(getCancelButton());
126
        // =============================================
127
        
128
        this.add(northPanel, BorderLayout.NORTH);
129
        
130
        JScrollPane scroll = new JScrollPane();
131
        scroll.setViewportView(getContentArea());
132
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
133
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
134
        this.add(scroll, BorderLayout.CENTER);
135
        
136
        JPanel south2 = new JPanel(new GridBagLayout());
137
        GridBagConstraints cs = new GridBagConstraints();
138
        cs.insets = new Insets(15,15,15,15);
139
        south2.add(southPanel, cs);
140
        
141
        this.add(south2, BorderLayout.SOUTH);
142
    }
143

    
144
    private JTextArea getContentArea() {
145
        
146
        if (contentArea == null) {
147
            contentArea = new JTextArea();
148
            loadSuggested(contentArea);
149
            contentArea.getDocument().addDocumentListener(this);
150
        }
151
        return contentArea;
152
    }
153

    
154
    private JButton getCancelButton() {
155
        if (cancelButton == null) {
156
            cancelButton = new JButton(Messages.getText("_Cancel"));
157
            cancelButton.addActionListener(this);
158
        }
159
        return cancelButton;
160
    }
161

    
162
    private JButton getSuggButton() {
163
        if (suggButton == null) {
164
            suggButton = new JButton(Messages.getText("_Suggested"));
165
            suggButton.addActionListener(this);
166
        }
167
        return suggButton;
168
    }
169

    
170
    private JButton getOkButton() {
171
        if (okButton == null) {
172
            okButton = new JButton(Messages.getText("_Accept"));
173
            okButton.addActionListener(this);
174
            okButton.setEnabled(false);
175
        }
176
        return okButton;
177
    }
178

    
179
    private JButton getSaveButton() {
180
        if (saveButton == null) {
181
            saveButton = new JButton(Messages.getText("_Save"));
182
            Font fnt = saveButton.getFont();
183
            fnt = fnt.deriveFont(Font.BOLD);
184
            saveButton.setFont(fnt);
185
            saveButton.addActionListener(this);
186
        }
187
        return saveButton;
188
    }
189

    
190
    // ===================================================
191
    // ===================================================
192

    
193
    public WindowInfo getWindowInfo() {
194
        if (winfo == null) {
195
            winfo = new WindowInfo(WindowInfo.MODALDIALOG);
196
            winfo.setWidth(550);
197
            winfo.setHeight(350);
198
            winfo.setTitle(
199
                Messages.getText("_Checking_platform_properties"));
200
        }
201
        return winfo;
202
    }
203

    
204
    public Object getWindowProfile() {
205
        return WindowInfo.DIALOG_PROFILE;
206
    }
207
    
208
    public static final String SUGGESTED_WINDOWN_CONTENT =
209
        "native_platform=win\n" +
210
        "native_distribution=nt\n" +
211
        "native_compiler=vs8\n" +
212
        "native_arch=i386\n" +
213
        "native_libraryType=dynamic";
214
    
215
    public static final String SUGGESTED_NON_WINDOWS_CONTENT =
216
            "native_platform=linux\n" +
217
            "native_distribution=all\n" +
218
            "native_compiler=gcc4\n" +
219
            "native_arch=i386\n" +
220
            "native_libraryType=dynamic";
221

    
222

    
223
    public void actionPerformed(ActionEvent e) {
224
        
225
        Object src = e.getSource();
226
        if (src == getOkButton()) {
227
            if (getSaveButton().isEnabled()) {
228
                if (!userAcceptsNotSaving()) {
229
                    return;
230
                }
231
            }
232
            validSettings = true;
233
            PluginServices.getMDIManager().closeWindow(this);
234
        }
235
        
236
        if (src == getCancelButton()) {
237
            validSettings = false;
238
            PluginServices.getMDIManager().closeWindow(this);
239
        }
240
        
241
        if (src == getSaveButton()) {
242
            if (saveContent()) {
243
                this.getSaveButton().setEnabled(false);
244
            }
245
            this.refreshOkButton();
246
        }
247
        
248
        if (src == getSuggButton()) {
249
            loadSuggested(getContentArea());
250
            this.getSaveButton().setEnabled(true);
251
        }
252
        
253
    }
254

    
255

    
256
    /**
257
     * @return
258
     */
259
    private boolean userAcceptsNotSaving() {
260
        
261
        String _msg =
262
            Messages.getText("_There_are_unsaved_changes_Continue_question");
263
        String _tit =
264
            Messages.getText("_Not_saved");
265
        int usr_opt = JOptionPane.showConfirmDialog(
266
            this, _msg, _tit, JOptionPane.YES_NO_OPTION,
267
            JOptionPane.WARNING_MESSAGE);
268
        return (usr_opt == JOptionPane.YES_OPTION);
269
    }
270

    
271
    private boolean saveContent() {
272
        
273
        try {
274
            if (targetFile.exists()) {
275
                targetFile.delete();
276
            }
277
            targetFile.createNewFile();
278
            
279
            FileOutputStream fos = new FileOutputStream(targetFile);
280
            String contnt = getContentArea().getText();
281
            fos.write(contnt.getBytes());
282
            fos.flush();
283
            fos.close();
284
            return true;
285
        } catch (IOException ioe) {
286
            logger.info("While writing file: " + ioe.getMessage(), ioe);
287
            String _msg = Messages.getText("_Unable_to_store_file")
288
                + ": " + targetFile.getAbsolutePath();
289
            String _tit = Messages.getText("_Error");
290
            JOptionPane.showMessageDialog(
291
                this, _msg, _tit, JOptionPane.ERROR_MESSAGE);
292
            return false;
293
        }
294
        
295
    }
296

    
297
    private void loadSuggested(JTextArea ta) {
298

    
299
        if (isWindows) {
300
            ta.setText(SUGGESTED_WINDOWN_CONTENT);
301
        } else {
302
            ta.setText(SUGGESTED_NON_WINDOWS_CONTENT);
303
        }
304
    }
305

    
306
    public void insertUpdate(DocumentEvent e) {
307
        this.getSaveButton().setEnabled(true);
308
    }
309

    
310
    public void removeUpdate(DocumentEvent e) {
311
        this.getSaveButton().setEnabled(true);
312
    }
313

    
314
    public void changedUpdate(DocumentEvent e) {
315
        // should not be called because this text area is simple
316
    }
317
    
318
    public boolean validSettings() {
319
        return validSettings;
320
    }
321
    
322
    private void refreshOkButton() {
323
        this.getOkButton().setEnabled(targetFile.exists());
324
    }
325

    
326
}