Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.newlayer / org.gvsig.newlayer.lib / org.gvsig.newlayer.lib.impl / src / main / java / org / gvsig / newlayer / impl / DefaultNewLayerWizard.java @ 45564

History | View | Annotate | Download (9.04 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.newlayer.impl;
25

    
26
import java.awt.Dimension;
27
import java.util.Iterator;
28
import java.util.List;
29

    
30
import javax.swing.ImageIcon;
31
import javax.swing.JButton;
32
import javax.swing.JOptionPane;
33
import javax.swing.JPanel;
34

    
35
import jwizardcomponent.CancelAction;
36
import jwizardcomponent.DefaultJWizardComponents;
37
import jwizardcomponent.FinishAction;
38
import jwizardcomponent.JWizardComponents;
39

    
40
import org.slf4j.Logger;
41
import org.slf4j.LoggerFactory;
42

    
43
import org.gvsig.gui.beans.wizard.panel.NotContinueWizardException;
44
import org.gvsig.gui.beans.wizard.panel.OptionPanel;
45
import org.gvsig.i18n.Messages;
46
import org.gvsig.newlayer.NewLayerException;
47
import org.gvsig.newlayer.NewLayerProviderFactory;
48
import org.gvsig.newlayer.NewLayerProviderPanel;
49
import org.gvsig.newlayer.NewLayerService;
50
import org.gvsig.newlayer.NewLayerServiceException;
51
import org.gvsig.newlayer.NewLayerWizard;
52
import org.gvsig.newlayer.impl.panels.FeatureTypePanel;
53

    
54
public class DefaultNewLayerWizard extends NewLayerWizard {
55

    
56
    /**
57
     *
58
     */
59
    private static final long serialVersionUID = -5827106636136663823L;
60
    private final NewLayerService service;
61
    private AddNewLayerQuestionPanel addNewLayerQuestionPanel;
62
    private static final Logger LOG
63
            = LoggerFactory.getLogger(DefaultNewLayerWizard.class);
64

    
65
    public DefaultNewLayerWizard(ImageIcon logo, NewLayerService service) {
66
        super(logo);
67
        setPreferredSize(new Dimension(800, 400));
68
        this.service = service;
69
        this.getWizardComponents().setFinishAction(
70
                new MyFinishAction(this.getWizardComponents()));
71
        this.getWizardComponents().setCancelAction(
72
                new MyCancelAction(this.getWizardComponents()));
73
        this.createTypeSelector();
74

    
75
        // ================ disable at the start
76
        this.setFinishButtonEnabled(false);
77
        this.setBackButtonEnabled(false);
78
        this.setNextButtonEnabled(false);
79
        this.setPreferredSize(new Dimension(850, 550));
80
    }
81

    
82
    @Override
83
    public NewLayerService getService() {
84
        return this.service;
85
    }
86

    
87
    private void createTypeSelector() {
88

    
89
        TypeSelectorPanel panel = new TypeSelectorPanel(this);
90
        this.addOptionPanel(panel);
91

    
92
    }
93

    
94
    @Override
95
    public NewLayerProviderPanel createFeatureTypePanel() {
96
        FeatureTypePanel panel = new FeatureTypePanel(this, service.getProvider());
97
        return panel;
98
    }
99

    
100
    private void createProviderPanels() {
101
        DefaultJWizardComponents wizardComponents = this.getWizardComponents();
102
        for (int i = wizardComponents.getWizardPanelList().size() - 1; i >= 1; i--) {
103
            wizardComponents.removeWizardPanel(i);
104
        }
105
        List<NewLayerProviderPanel> panels = service.getProvider().createPanels(this);
106
        Iterator<NewLayerProviderPanel> it = panels.iterator();
107
        while (it.hasNext()) {
108
            NewLayerProviderPanel panel = it.next();
109
            this.addOptionPanel(new OptionPanelWrapper(panel));
110
        }
111
        this.createAddNewLayerQuestionPanel();
112
    }
113

    
114
    private AddNewLayerQuestionPanel getAddNewLayerQuestionPanel() {
115
        if (this.addNewLayerQuestionPanel == null) {
116
            this.addNewLayerQuestionPanel = new AddNewLayerQuestionPanel(this);
117
        }
118
        return this.addNewLayerQuestionPanel;
119
    }
120

    
121
    private void createAddNewLayerQuestionPanel() {
122
        this.addOptionPanel(getAddNewLayerQuestionPanel());
123
    }
124

    
125
    private boolean getAddLayerToView() {
126
        return getAddNewLayerQuestionPanel().getAddLayerToView();
127
    }
128

    
129
    @Override
130
    public void setType(NewLayerProviderFactory currentType) {
131
        this.service.setProviderFactory(currentType);
132
        createProviderPanels();
133
    }
134

    
135
    private class OptionPanelWrapper implements OptionPanel {
136

    
137
        private final NewLayerProviderPanel panel;
138

    
139
        public OptionPanelWrapper(NewLayerProviderPanel panel) {
140
            this.panel = panel;
141
        }
142

    
143
        @Override
144
        public String getPanelTitle() {
145
            return this.panel.getTitlePanel();
146
        }
147

    
148
        @Override
149
        public void nextPanel() throws NotContinueWizardException {
150

    
151
            try {
152
                if (!this.panel.validatePanel()) {
153
                    throw new NotContinueWizardException("Error validating the form", panel.asJComponent(), false);
154
                }
155
            } catch (NewLayerException e) {
156
                LOG.info("Error validating the form", e);
157
                throw new NotContinueWizardException("Error validating the form", e, panel.asJComponent());
158
            }
159
        }
160

    
161
        @Override
162
        public void lastPanel() {
163
        }
164

    
165
        @Override
166
        public void updatePanel() {
167
            this.panel.enterPanel();
168
        }
169

    
170
        @Override
171
        public JPanel getJPanel() {
172
            return (JPanel) this.panel.asJComponent();
173
        }
174

    
175
    }
176

    
177
    private class MyFinishAction extends FinishAction {
178

    
179
        public MyFinishAction(JWizardComponents arg0) {
180
            super(arg0);
181
        }
182

    
183
        @Override
184
        public void performAction() {
185
            if (getService().getStoreName() == null) {
186
                JOptionPane.showMessageDialog(
187
                        null,
188
                        Messages.getText("new_layer_not_store_name"),
189
                        Messages.getText("new_layer"),
190
                        JOptionPane.WARNING_MESSAGE
191
                );
192
                return;
193
            }
194
            
195
            if (getService().getNewStoreParametersQuietly()== null) {
196
                JOptionPane.showMessageDialog(
197
                        null,
198
                        Messages.getText("new_layer_parameters_missing"),
199
                        Messages.getText("new_layer"),
200
                        JOptionPane.WARNING_MESSAGE
201
                );
202
                return;
203
            }
204
            getService().addLayerToView(getAddLayerToView());
205
            try {
206
                getService().createTable();
207
            } catch (NewLayerServiceException e) {
208
                LOG.info("Cannot create the new layer", e);
209
                JOptionPane.showMessageDialog(
210
                        null,
211
                        Messages.getText("cant_create_new_layer") + "\n" + e.getMessageStack(),
212
                        Messages.getText("new_layer"),
213
                        JOptionPane.ERROR_MESSAGE
214
                );
215
                return;
216
            }
217

    
218
            try {
219
                getService().loadTable();
220
            } catch (NewLayerServiceException e) {
221
                JOptionPane.showMessageDialog(
222
                        null,
223
                        "The new layer has been succesfully created but can't load the new layer.\n Try load it yourshelf, please.",
224
                        "Can't create a new layer",
225
                        JOptionPane.WARNING_MESSAGE
226
                );
227
            }
228
            setVisible(false);
229
        }
230

    
231
    }
232

    
233
    private class MyCancelAction extends CancelAction {
234

    
235
        public MyCancelAction(DefaultJWizardComponents wizardComponents) {
236
            super(wizardComponents);
237
        }
238

    
239
        @Override
240
        public void performAction() {
241
            setVisible(false);
242
        }
243

    
244
    }
245

    
246
    private JButton getButton(int button) {
247
        switch (button) {
248
            case BUTTON_BACK:
249
                return this.getWizardComponents().getBackButton();
250
            case BUTTON_NEXT:
251
                return this.getWizardComponents().getNextButton();
252
            case BUTTON_FINISH:
253
                return this.getWizardComponents().getFinishButton();
254
            case BUTTON_CANCEL:
255
                return this.getWizardComponents().getCancelButton();
256
        }
257
        throw new IllegalArgumentException("Illegal button value (" + button + ").");
258
    }
259

    
260
    @Override
261
    public void setButtonEnabled(int button, boolean enabled) {
262
        this.getButton(button).setEnabled(enabled);
263
    }
264

    
265
    @Override
266
    public void setButtonText(int button, String text) {
267
        this.getButton(button).setText(text);
268
    }
269

    
270
    @Override
271
    public boolean isButtonEnabled(int button) {
272
        return this.getButton(button).isEnabled();
273
    }
274

    
275
    @Override
276
    public String getButtonText(int button) {
277
        return this.getButton(button).getText();
278
    }
279
    
280
}