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 40560 jjdelcerro
/**
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 40435 jjdelcerro
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 45564 jjdelcerro
import javax.swing.JButton;
32 40435 jjdelcerro
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 45564 jjdelcerro
import org.gvsig.newlayer.impl.panels.FeatureTypePanel;
53 46873 jjdelcerro
import org.gvsig.tools.swing.api.ToolsSwingUtils;
54 40435 jjdelcerro
55
public class DefaultNewLayerWizard extends NewLayerWizard {
56
57 45564 jjdelcerro
    /**
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 46873 jjdelcerro
    private String addTableToProjectTitle;
66
    private String addTableToProjectLabel;
67 40435 jjdelcerro
68 46873 jjdelcerro
    @SuppressWarnings("OverridableMethodCallInConstructor")
69 45564 jjdelcerro
    public DefaultNewLayerWizard(ImageIcon logo, NewLayerService service) {
70
        super(logo);
71 46873 jjdelcerro
72 45564 jjdelcerro
        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 40435 jjdelcerro
        this.setFinishButtonEnabled(false);
81
        this.setBackButtonEnabled(false);
82
        this.setNextButtonEnabled(false);
83 47433 jjdelcerro
//        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 45564 jjdelcerro
    }
94 40435 jjdelcerro
95 45564 jjdelcerro
    @Override
96
    public NewLayerService getService() {
97
        return this.service;
98
    }
99 40435 jjdelcerro
100 45564 jjdelcerro
    private void createTypeSelector() {
101 40435 jjdelcerro
102 45564 jjdelcerro
        TypeSelectorPanel panel = new TypeSelectorPanel(this);
103
        this.addOptionPanel(panel);
104 40435 jjdelcerro
105 45564 jjdelcerro
    }
106 40435 jjdelcerro
107 45564 jjdelcerro
    @Override
108
    public NewLayerProviderPanel createFeatureTypePanel() {
109
        FeatureTypePanel panel = new FeatureTypePanel(this, service.getProvider());
110
        return panel;
111
    }
112 40435 jjdelcerro
113 45564 jjdelcerro
    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 40435 jjdelcerro
127 45564 jjdelcerro
    private AddNewLayerQuestionPanel getAddNewLayerQuestionPanel() {
128
        if (this.addNewLayerQuestionPanel == null) {
129 46873 jjdelcerro
            this.addNewLayerQuestionPanel = new AddNewLayerQuestionPanel(this, this.addTableToProjectTitle, this.addTableToProjectLabel);
130 45564 jjdelcerro
        }
131
        return this.addNewLayerQuestionPanel;
132
    }
133 40435 jjdelcerro
134 45564 jjdelcerro
    private void createAddNewLayerQuestionPanel() {
135
        this.addOptionPanel(getAddNewLayerQuestionPanel());
136
    }
137 40435 jjdelcerro
138 45564 jjdelcerro
    private boolean getAddLayerToView() {
139 46873 jjdelcerro
        return getAddNewLayerQuestionPanel().getAddTableToProject();
140 45564 jjdelcerro
    }
141 40435 jjdelcerro
142 45564 jjdelcerro
    @Override
143 40435 jjdelcerro
    public void setType(NewLayerProviderFactory currentType) {
144
        this.service.setProviderFactory(currentType);
145 45564 jjdelcerro
        createProviderPanels();
146
    }
147 40435 jjdelcerro
148 45564 jjdelcerro
    private class OptionPanelWrapper implements OptionPanel {
149 40435 jjdelcerro
150 45564 jjdelcerro
        private final NewLayerProviderPanel panel;
151 40435 jjdelcerro
152 45564 jjdelcerro
        public OptionPanelWrapper(NewLayerProviderPanel panel) {
153
            this.panel = panel;
154
        }
155 40435 jjdelcerro
156 45564 jjdelcerro
        @Override
157
        public String getPanelTitle() {
158
            return this.panel.getTitlePanel();
159
        }
160 40435 jjdelcerro
161 45564 jjdelcerro
        @Override
162
        public void nextPanel() throws NotContinueWizardException {
163 40435 jjdelcerro
164 45564 jjdelcerro
            try {
165
                if (!this.panel.validatePanel()) {
166
                    throw new NotContinueWizardException("Error validating the form", panel.asJComponent(), false);
167
                }
168
            } catch (NewLayerException e) {
169 40435 jjdelcerro
                LOG.info("Error validating the form", e);
170 45564 jjdelcerro
                throw new NotContinueWizardException("Error validating the form", e, panel.asJComponent());
171 40435 jjdelcerro
            }
172 45564 jjdelcerro
        }
173 40435 jjdelcerro
174 45564 jjdelcerro
        @Override
175
        public void lastPanel() {
176
        }
177 40435 jjdelcerro
178 45564 jjdelcerro
        @Override
179
        public void updatePanel() {
180
            this.panel.enterPanel();
181
        }
182 40435 jjdelcerro
183 45564 jjdelcerro
        @Override
184
        public JPanel getJPanel() {
185
            return (JPanel) this.panel.asJComponent();
186
        }
187 40435 jjdelcerro
188 45564 jjdelcerro
    }
189 40435 jjdelcerro
190 45564 jjdelcerro
    private class MyFinishAction extends FinishAction {
191 40435 jjdelcerro
192 45564 jjdelcerro
        public MyFinishAction(JWizardComponents arg0) {
193
            super(arg0);
194
        }
195 40435 jjdelcerro
196 45564 jjdelcerro
        @Override
197
        public void performAction() {
198 46873 jjdelcerro
            if (getService().getStoreProviderName() == null) {
199 45564 jjdelcerro
                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 46873 jjdelcerro
//            getService().addLayerToView(getAddLayerToView());
218 45564 jjdelcerro
            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 40435 jjdelcerro
231 46873 jjdelcerro
//            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 45564 jjdelcerro
            setVisible(false);
242
        }
243 40435 jjdelcerro
244 45564 jjdelcerro
    }
245 40435 jjdelcerro
246 45564 jjdelcerro
    private class MyCancelAction extends CancelAction {
247 40435 jjdelcerro
248 45564 jjdelcerro
        public MyCancelAction(DefaultJWizardComponents wizardComponents) {
249
            super(wizardComponents);
250
        }
251 40435 jjdelcerro
252 45564 jjdelcerro
        @Override
253
        public void performAction() {
254
            setVisible(false);
255
        }
256 40435 jjdelcerro
257 45564 jjdelcerro
    }
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 46873 jjdelcerro
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 45564 jjdelcerro
304 40435 jjdelcerro
}