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 @ 47433

History | View | Annotate | Download (9.93 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
import org.gvsig.tools.swing.api.ToolsSwingUtils;
54

    
55
public class DefaultNewLayerWizard extends NewLayerWizard {
56

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

    
68
    @SuppressWarnings("OverridableMethodCallInConstructor")
69
    public DefaultNewLayerWizard(ImageIcon logo, NewLayerService service) {
70
        super(logo);
71
        
72
        this.service = service;
73
        this.getWizardComponents().setFinishAction(
74
                new MyFinishAction(this.getWizardComponents()));
75
        this.getWizardComponents().setCancelAction(
76
                new MyCancelAction(this.getWizardComponents()));
77
        this.createTypeSelector();
78

    
79
        // ================ disable at the start
80
        this.setFinishButtonEnabled(false);
81
        this.setBackButtonEnabled(false);
82
        this.setNextButtonEnabled(false);
83
//        ToolsSwingUtils.ensureRowsCols(this, 25, 100, 35, 140);
84
        
85
        Dimension screensize = ToolsSwingUtils.getDesktopPanelSize();
86
        ToolsSwingUtils.ensureHeightWitdh(
87
                this, 
88
                Math.max(3*(screensize.height/4), ToolsSwingUtils.rows2px(35)),
89
                Math.max(2*(screensize.width/4), ToolsSwingUtils.cols2px(140)), 
90
                7*(screensize.height/8), 
91
                3*(screensize.width/4)
92
        );           
93
    }
94

    
95
    @Override
96
    public NewLayerService getService() {
97
        return this.service;
98
    }
99

    
100
    private void createTypeSelector() {
101

    
102
        TypeSelectorPanel panel = new TypeSelectorPanel(this);
103
        this.addOptionPanel(panel);
104

    
105
    }
106

    
107
    @Override
108
    public NewLayerProviderPanel createFeatureTypePanel() {
109
        FeatureTypePanel panel = new FeatureTypePanel(this, service.getProvider());
110
        return panel;
111
    }
112

    
113
    private void createProviderPanels() {
114
        DefaultJWizardComponents wizardComponents = this.getWizardComponents();
115
        for (int i = wizardComponents.getWizardPanelList().size() - 1; i >= 1; i--) {
116
            wizardComponents.removeWizardPanel(i);
117
        }
118
        List<NewLayerProviderPanel> panels = service.getProvider().createPanels(this);
119
        Iterator<NewLayerProviderPanel> it = panels.iterator();
120
        while (it.hasNext()) {
121
            NewLayerProviderPanel panel = it.next();
122
            this.addOptionPanel(new OptionPanelWrapper(panel));
123
        }
124
        this.createAddNewLayerQuestionPanel();
125
    }
126

    
127
    private AddNewLayerQuestionPanel getAddNewLayerQuestionPanel() {
128
        if (this.addNewLayerQuestionPanel == null) {
129
            this.addNewLayerQuestionPanel = new AddNewLayerQuestionPanel(this, this.addTableToProjectTitle, this.addTableToProjectLabel);
130
        }
131
        return this.addNewLayerQuestionPanel;
132
    }
133

    
134
    private void createAddNewLayerQuestionPanel() {
135
        this.addOptionPanel(getAddNewLayerQuestionPanel());
136
    }
137

    
138
    private boolean getAddLayerToView() {
139
        return getAddNewLayerQuestionPanel().getAddTableToProject();
140
    }
141

    
142
    @Override
143
    public void setType(NewLayerProviderFactory currentType) {
144
        this.service.setProviderFactory(currentType);
145
        createProviderPanels();
146
    }
147

    
148
    private class OptionPanelWrapper implements OptionPanel {
149

    
150
        private final NewLayerProviderPanel panel;
151

    
152
        public OptionPanelWrapper(NewLayerProviderPanel panel) {
153
            this.panel = panel;
154
        }
155

    
156
        @Override
157
        public String getPanelTitle() {
158
            return this.panel.getTitlePanel();
159
        }
160

    
161
        @Override
162
        public void nextPanel() throws NotContinueWizardException {
163

    
164
            try {
165
                if (!this.panel.validatePanel()) {
166
                    throw new NotContinueWizardException("Error validating the form", panel.asJComponent(), false);
167
                }
168
            } catch (NewLayerException e) {
169
                LOG.info("Error validating the form", e);
170
                throw new NotContinueWizardException("Error validating the form", e, panel.asJComponent());
171
            }
172
        }
173

    
174
        @Override
175
        public void lastPanel() {
176
        }
177

    
178
        @Override
179
        public void updatePanel() {
180
            this.panel.enterPanel();
181
        }
182

    
183
        @Override
184
        public JPanel getJPanel() {
185
            return (JPanel) this.panel.asJComponent();
186
        }
187

    
188
    }
189

    
190
    private class MyFinishAction extends FinishAction {
191

    
192
        public MyFinishAction(JWizardComponents arg0) {
193
            super(arg0);
194
        }
195

    
196
        @Override
197
        public void performAction() {
198
            if (getService().getStoreProviderName() == null) {
199
                JOptionPane.showMessageDialog(
200
                        null,
201
                        Messages.getText("new_layer_not_store_name"),
202
                        Messages.getText("new_layer"),
203
                        JOptionPane.WARNING_MESSAGE
204
                );
205
                return;
206
            }
207
            
208
            if (getService().getNewStoreParametersQuietly()== null) {
209
                JOptionPane.showMessageDialog(
210
                        null,
211
                        Messages.getText("new_layer_parameters_missing"),
212
                        Messages.getText("new_layer"),
213
                        JOptionPane.WARNING_MESSAGE
214
                );
215
                return;
216
            }
217
//            getService().addLayerToView(getAddLayerToView());
218
            try {
219
                getService().createTable();
220
            } catch (NewLayerServiceException e) {
221
                LOG.info("Cannot create the new layer", e);
222
                JOptionPane.showMessageDialog(
223
                        null,
224
                        Messages.getText("cant_create_new_layer") + "\n" + e.getMessageStack(),
225
                        Messages.getText("new_layer"),
226
                        JOptionPane.ERROR_MESSAGE
227
                );
228
                return;
229
            }
230

    
231
//            try {
232
//                getService().loadTable();
233
//            } catch (NewLayerServiceException e) {
234
//                JOptionPane.showMessageDialog(
235
//                        null,
236
//                        "The new layer has been succesfully created but can't load the new layer.\n Try load it yourshelf, please.",
237
//                        "Can't create a new layer",
238
//                        JOptionPane.WARNING_MESSAGE
239
//                );
240
//            }
241
            setVisible(false);
242
        }
243

    
244
    }
245

    
246
    private class MyCancelAction extends CancelAction {
247

    
248
        public MyCancelAction(DefaultJWizardComponents wizardComponents) {
249
            super(wizardComponents);
250
        }
251

    
252
        @Override
253
        public void performAction() {
254
            setVisible(false);
255
        }
256

    
257
    }
258

    
259
    private JButton getButton(int button) {
260
        switch (button) {
261
            case BUTTON_BACK:
262
                return this.getWizardComponents().getBackButton();
263
            case BUTTON_NEXT:
264
                return this.getWizardComponents().getNextButton();
265
            case BUTTON_FINISH:
266
                return this.getWizardComponents().getFinishButton();
267
            case BUTTON_CANCEL:
268
                return this.getWizardComponents().getCancelButton();
269
        }
270
        throw new IllegalArgumentException("Illegal button value (" + button + ").");
271
    }
272

    
273
    @Override
274
    public void setButtonEnabled(int button, boolean enabled) {
275
        this.getButton(button).setEnabled(enabled);
276
    }
277

    
278
    @Override
279
    public void setButtonText(int button, String text) {
280
        this.getButton(button).setText(text);
281
    }
282

    
283
    @Override
284
    public boolean isButtonEnabled(int button) {
285
        return this.getButton(button).isEnabled();
286
    }
287

    
288
    @Override
289
    public String getButtonText(int button) {
290
        return this.getButton(button).getText();
291
    }
292

    
293
    @Override
294
    public void setAddTableToProjectTitle(String text) {
295
        this.addTableToProjectTitle = text;
296
    }
297

    
298
    @Override
299
    public void setAddTableToProjectLabel(String text) {
300
        this.addTableToProjectLabel = text;
301
    }
302

    
303
    
304
}