Statistics
| Revision:

gvsig-educa / org.gvsig.educa.thematicmap / trunk / org.gvsig.educa.thematicmap / org.gvsig.educa.thematicmap.lib / org.gvsig.educa.thematicmap.lib.impl / src / main / java / org / gvsig / educa / thematicmap / impl / editor / DefaultThematicMapCompilationEditor.java @ 224

History | View | Annotate | Download (9.05 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.educa.thematicmap.impl.editor;
23

    
24
import java.awt.BorderLayout;
25
import java.net.URL;
26
import java.util.ArrayList;
27
import java.util.List;
28

    
29
import javax.swing.BorderFactory;
30
import javax.swing.ImageIcon;
31
import javax.swing.JComponent;
32
import javax.swing.JPanel;
33

    
34
import org.gvsig.educa.thematicmap.ThematicMapLocator;
35
import org.gvsig.educa.thematicmap.ThematicMapManager;
36
import org.gvsig.educa.thematicmap.ThematicMapWindowManager.MESSAGE_DIALOG_TYPE;
37
import org.gvsig.educa.thematicmap.compilation.CompilationValidationMessage;
38
import org.gvsig.educa.thematicmap.compilation.ThematicMapCompilation;
39
import org.gvsig.educa.thematicmap.editor.ThematicMapCompilationEditor;
40
import org.gvsig.educa.thematicmap.editor.ThematicMapCompilationEditorListener;
41
import org.gvsig.educa.thematicmap.impl.IconThemeHelper;
42
import org.gvsig.gui.beans.wizard.WizardPanel;
43
import org.gvsig.gui.beans.wizard.WizardPanelActionListener;
44
import org.gvsig.gui.beans.wizard.WizardPanelWithLogo;
45

    
46
/**
47
 * <p>
48
 * Default implementation of {@link ThematicMapCompilationEditor}
49
 * </p>
50
 * <p>
51
 * This uses <i>org.gvsig.ui</i> Wizard implementation.
52
 * </p>
53
 * <p>
54
 * There are three steps:
55
 * <ol>
56
 * <li>{@link StepIdentification}: Base values for <i>Thematic Map</i>
57
 * identification</li>
58
 * <li>{@link StepLayers}: Layers definition which will compound the <i>Thematic
59
 * Map</i></li>
60
 * <li>{@link StepValidation}: validation of all data</li>
61
 * </ol>
62
 * </p>
63
 * 
64
 * @author gvSIG Team
65
 * @version $Id$
66
 * 
67
 */
68
public class DefaultThematicMapCompilationEditor extends JPanel implements
69
    ThematicMapCompilationEditor, WizardPanel, WizardPanelActionListener {
70

    
71
    /**
72
     *
73
     */
74
    private static final long serialVersionUID = -9153359086582532100L;
75

    
76
    private final ThematicMapManager manager;
77
    private final ThematicMapCompilation compilation;
78

    
79
    /**
80
     * Wizard component
81
     */
82
    private WizardPanelWithLogo wizard;
83

    
84
    /**
85
     * Wizard step for input Thematic Map information values
86
     */
87
    private StepIdentification stepIdentification;
88

    
89
    /**
90
     * Wizard step to configure layer composition
91
     */
92
    private StepLayers stepLayers;
93

    
94
    /**
95
     * Wizard step to validate all data
96
     */
97
    private StepValidation stepValidation;
98

    
99
    /**
100
     * UI initialized flag
101
     */
102
    private boolean initialized = false;
103

    
104
    private ThematicMapCompilationEditorListener listener;
105

    
106
    private final boolean editLayers;
107

    
108
    private boolean allowChangeId = true;
109

    
110
    public DefaultThematicMapCompilationEditor(
111
        ThematicMapCompilation compilation) {
112
        this(compilation, true);
113
    }
114

    
115
    /**
116
     * @param thematicMapCompilation
117
     * @param editLayers
118
     */
119
    public DefaultThematicMapCompilationEditor(
120
        ThematicMapCompilation compilation, boolean editLayers) {
121
        manager = ThematicMapLocator.getManager();
122

    
123
        this.compilation = compilation;
124
        this.editLayers = editLayers;
125
    }
126

    
127
    /**
128
     * Initialize all UI component of the panel
129
     */
130
    private void initializeUI() {
131
        if (initialized) {
132
            return;
133
        }
134
        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
135

    
136
//        URL iconURL =
137
//            getClass().getClassLoader().getResource(
138
//                "images/thematicmap-editor.png");
139
        
140
        ImageIcon icon = IconThemeHelper.getImageIcon("thematicmap-editor-wizard");
141

    
142
        wizard = new WizardPanelWithLogo(icon);
143

    
144
        stepIdentification = new StepIdentification(this, allowChangeId);
145
        stepValidation = new StepValidation(this);
146
        
147
        
148
        // TODO: if thematicmap is a game, add the list of optionPanels to the wizard.
149

    
150
        wizard.addOptionPanel(stepIdentification);
151
        if (editLayers) {
152
            stepLayers = new StepLayers(this);
153
            wizard.addOptionPanel(stepLayers);
154
        }
155
        wizard.addOptionPanel(stepValidation);
156

    
157
        wizard.setWizardListener(this);
158
        setFinishButtonEnabled(false);
159

    
160
        this.setLayout(new BorderLayout());
161
        this.add(wizard, BorderLayout.CENTER);
162

    
163
        wizard.show();
164

    
165
        initialized = true;
166
    }
167

    
168
    /** {@inheridDoc} */
169
    public JComponent getSwingComponent() {
170
        initializeUI();
171
        return this;
172
    }
173

    
174
    /** {@inheridDoc} */
175
    public ThematicMapCompilation getCompilation() {
176
        return compilation;
177
    }
178

    
179
    /** {@inheridDoc} */
180
    public boolean goToFirstStep() {
181
        wizard.doAction(WizardPanelWithLogo.ACTION_FINISH);
182
        return true;
183
    }
184

    
185
    /** {@inheridDoc} */
186
    public boolean goNextStep() {
187
        wizard.doAction(WizardPanelWithLogo.ACTION_NEXT);
188
        return true;
189
    }
190

    
191
    /** {@inheridDoc} */
192
    public boolean goPreviousStep() {
193
        wizard.doAction(WizardPanelWithLogo.ACTION_PREVIOUS);
194
        return true;
195
    }
196

    
197
    /** {@inheridDoc} */
198
    public boolean goToLastStep() {
199
        wizard.doAction(WizardPanelWithLogo.ACTION_FINISH);
200
        return true;
201
    }
202

    
203
    /** {@inheridDoc} */
204
    public boolean isLastStep() {
205
        return wizard.getWizardComponents().getCurrentIndex() == wizard
206
            .getWizardComponents().getWizardPanelList().size() - 1;
207
    }
208

    
209
    /** {@inheridDoc} */
210
    public boolean isFirstStep() {
211
        return wizard.getWizardComponents().getCurrentIndex() == 0;
212
    }
213

    
214
    /** {@inheridDoc} */
215
    public boolean isCompilationValid() {
216
        List<CompilationValidationMessage> messages =
217
            new ArrayList<CompilationValidationMessage>();
218
        return compilation.isValid(messages);
219
    }
220

    
221
    public void setNextButtonEnabled(boolean isEnabled) {
222
        wizard.setNextButtonEnabled(isEnabled);
223
    }
224

    
225
    public void setCancelButtonEnabled(boolean isEnabled) {
226
        wizard.setCancelButtonEnabled(isEnabled);
227
    }
228

    
229
    public void setFinishButtonEnabled(boolean isEnabled) {
230
        wizard.setFinishButtonEnabled(isEnabled);
231
    }
232

    
233
    public void setBackButtonEnabled(boolean isEnabled) {
234
        wizard.setBackButtonEnabled(isEnabled);
235
    }
236

    
237
    /**
238
     * Finalizes wizard
239
     */
240
    public void finish() {
241
        // Checks all steps
242
        if (!updateFinishButton()) {
243
            return;
244
        }
245

    
246
        // Checks that compilation is valid
247
        if (!isCompilationValid()) {
248
            manager.getWindowManager().showMessageDialog(
249
                this.getSwingComponent(),
250
                manager.getTranslation("thematic_map_compilation_editor"),
251
                manager.getTranslation("current_values_are_not_valid"),
252
                MESSAGE_DIALOG_TYPE.ERROR);
253
            return;
254
        }
255
        close();
256
        this.listener.finished(this);
257
    }
258

    
259
    /**
260
     * Cancels wizard
261
     */
262
    public void cancel() {
263
        close();
264
        this.listener.canceled(this);
265
    }
266

    
267
    /**
268
     * Close this panel
269
     */
270
    private void close() {
271
        this.setVisible(false);
272
    }
273

    
274
    /** {@inheridDoc} */
275
    public void setListener(ThematicMapCompilationEditorListener listener) {
276
        this.listener = listener;
277
    }
278

    
279
    /**
280
     * Checks all steps states
281
     * 
282
     * @return true if all steps are ok
283
     */
284
    public boolean updateFinishButton() {
285
        if (!stepIdentification.isPanelDataOk()) {
286
            setFinishButtonEnabled(false);
287
            return false;
288
        }
289
        if (editLayers && !stepLayers.isPanelDataOk()) {
290
            setFinishButtonEnabled(false);
291
            return false;
292
        }
293
        if (!stepValidation.isPanelDataOk()) {
294
            setFinishButtonEnabled(false);
295
            return false;
296
        }
297
        setFinishButtonEnabled(true);
298
        return true;
299
    }
300

    
301
    /** {@inheridDoc} */
302
    public void setWizardPanelActionListener(
303
        WizardPanelActionListener wizardActionListener) {
304
        // Nothing to do
305

    
306
    }
307

    
308
    /** {@inheridDoc} */
309
    public WizardPanelActionListener getWizardPanelActionListener() {
310
        return this;
311
    }
312

    
313
    /** {@inheridDoc} */
314
    public void finish(WizardPanel wizardPanel) {
315
        finish();
316

    
317
    }
318

    
319
    /** {@inheridDoc} */
320
    public void cancel(WizardPanel wizardPanel) {
321
        cancel();
322

    
323
    }
324

    
325
    public void allowChangeId(boolean allow) {
326
        allowChangeId = allow;
327
        if (stepIdentification != null) {
328
            stepIdentification.allowChangeId(allow);
329
        }
330

    
331
    }
332

    
333
    public boolean isAllowedChangeId() {
334
        return allowChangeId;
335
    }
336
}