Statistics
| Revision:

gvsig-projects-pool / org.gvsig.vcsgis / trunk / org.gvsig.vcsgis / org.gvsig.vcsgis.app / org.gvsig.vcsgis.app.mainplugin / src / main / java / org / gvsig / vcsgis / app / VCSGisDialogsHelper.java @ 3461

History | View | Annotate | Download (6.98 KB)

1
/*
2
 * gvSIG. Desktop Geographic Information System.
3
 * 
4
 * Copyright (C) 2007-2020 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, see <https://www.gnu.org/licenses/>. 
18
 * 
19
 * For any additional information, do not hesitate to contact us
20
 * at info AT gvsig.com, or visit our website www.gvsig.com.
21
 */
22

    
23
package org.gvsig.vcsgis.app;
24

    
25
import java.awt.event.ActionEvent;
26
import java.awt.event.ComponentAdapter;
27
import java.awt.event.ComponentEvent;
28
import java.util.HashMap;
29
import java.util.Map;
30
import org.gvsig.tools.ToolsLocator;
31
import org.gvsig.tools.i18n.I18nManager;
32
import org.gvsig.tools.swing.api.Component;
33
import org.gvsig.tools.swing.api.ToolsSwingLocator;
34
import org.gvsig.tools.swing.api.windowmanager.Dialog;
35
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
36
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
37
import org.gvsig.vcsgis.swing.VCSGisPanel;
38
import org.slf4j.Logger;
39
import org.slf4j.LoggerFactory;
40

    
41
/**
42
 *
43
 * @author gvSIG Team
44
 */
45
public class VCSGisDialogsHelper {
46
    private static final Logger LOGGER = LoggerFactory.getLogger(VCSGisDialogsHelper.class);
47

    
48
    public static class VCSGisDialogInfo {
49

    
50
        private VCSGisPanel panel;
51
        private final String name;
52
        private final String title;
53
        private String header;
54
        private String okLabel;
55
        private boolean closed;
56
        private Runnable action;
57
        private Dialog dialog;
58
        private boolean autoclose;
59

    
60
        public VCSGisDialogInfo(String name, VCSGisPanel thePanel, String title) {
61
            this.name = name;
62
            this.panel = thePanel;
63
            this.title = title;
64
            this.closed = true;
65
            this.autoclose = false;
66

    
67
            this.panel.asJComponent().addComponentListener(new ComponentAdapter() {
68
                @Override
69
                public void componentHidden(ComponentEvent e) {
70
                    closed = true;
71
                    if (!panel.isProcessing()) {
72
                        panel = null;
73
                        dialog = null;
74
                    }
75
                }
76
            });
77
        }
78
        
79
        public VCSGisDialogInfo(String name, VCSGisPanel thePanel, String title, String header, String okLabel) {
80
            this(name, thePanel, title);
81
            this.header = header;
82
            this.okLabel = okLabel;
83
        }
84

    
85
        private void performDialogAction() {
86
            action.run();
87
        }
88

    
89
        public void setAction(Runnable action) {
90
            this.action = action;
91
        }
92

    
93
        public Component getPanel() {
94
            return this.panel;
95
        }
96

    
97
        public boolean isClosed() {
98
            return this.closed;
99
        }
100

    
101
        public String getName() {
102
            return this.name;
103
        }
104

    
105
        public void setAutoclose(boolean autoclose) {
106
            if( this.dialog!=null ) {
107
                this.dialog.setAutoclose(autoclose);
108
            }
109
            this.autoclose = autoclose;
110
        }
111
        
112
        private Dialog createDialog() {
113
            I18nManager i18n = ToolsLocator.getI18nManager();
114
            WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
115

    
116
            Dialog theDialog = winManager.createDialog(
117
                    this.panel.asJComponent(),
118
                    i18n.getTranslation(title),
119
                    i18n.getTranslation(header),
120
                    WindowManager_v2.BUTTONS_OK_CANCEL
121
            );
122
            theDialog.setAutoclose(this.autoclose);
123

    
124
            this.panel.setDialog(dialog);
125
            theDialog.setButtonLabel(
126
                    WindowManager_v2.BUTTON_OK,
127
                    i18n.getTranslation(okLabel)
128
            );
129
            theDialog.setButtonLabel(
130
                    WindowManager_v2.BUTTON_CANCEL,
131
                    i18n.getTranslation("_Close")
132
            );
133
            if (this.panel.isProcessing()) {
134
                theDialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, false);
135
            }
136
            theDialog.addActionListener((ActionEvent e) -> {
137
                switch (theDialog.getAction()) {
138
                    case WindowManager_v2.BUTTON_OK:
139
                        Thread task = new Thread(() -> {
140
                            performDialogAction();
141
                        }, "VCSGis" + name);
142
                        task.start();
143
                        break;
144
                    case WindowManager_v2.BUTTON_CANCEL:
145
                        theDialog.asJComponent().setVisible(false);
146
                        break;
147
                }
148
            });
149
            return theDialog;
150
        }
151

    
152
        public void show() {
153
            if (!this.closed) {
154
                return;
155
            }
156
            if( this.action==null ) {
157
                I18nManager i18n = ToolsLocator.getI18nManager();
158
                WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
159
                winManager.showWindow(
160
                        this.panel.asJComponent(),
161
                        i18n.getTranslation(this.title),
162
                        WindowManager.MODE.WINDOW
163
                );
164
            } else {
165
                this.dialog = this.createDialog();
166
                this.dialog.show(WindowManager.MODE.WINDOW);
167
            }
168
        }
169
    }
170

    
171
    private Map<String, VCSGisDialogInfo> dialogsInfo;
172

    
173
    public VCSGisDialogInfo getDialog(String name) {
174
        if (this.dialogsInfo == null) {
175
            return null;
176
        }
177
        VCSGisDialogInfo info = this.dialogsInfo.get(name);
178
        return info;
179
    }
180

    
181
    public VCSGisDialogInfo getOrCreateDialog(String name, VCSGisPanel panel, String title, String header, String okLabel, Runnable action) {
182
        if (this.dialogsInfo == null) {
183
            this.dialogsInfo = new HashMap<>();
184
        }
185
        VCSGisDialogInfo info = this.dialogsInfo.get(name);
186
        if (info == null || info.isClosed() ) {
187
            info = new VCSGisDialogInfo(name, panel, title, header, okLabel);
188
            info.setAction(action);
189
            this.dialogsInfo.put(name, info);
190
        }
191
        return info;
192
    }
193

    
194
    public VCSGisDialogInfo getOrCreateDialog(String name, VCSGisPanel panel, String title) {
195
        if (this.dialogsInfo == null) {
196
            this.dialogsInfo = new HashMap<>();
197
        }
198
        VCSGisDialogInfo info = this.dialogsInfo.get(name);
199
        if (info == null || info.isClosed()) {
200
            info = new VCSGisDialogInfo(name, panel, title);
201
            this.dialogsInfo.put(name, info);
202
        }
203
        return info;
204
    }
205

    
206
}