Statistics
| Revision:

root / tags / v2_0_0_Build_2050 / libraries / org.gvsig.annotation / org.gvsig.annotation.swing / org.gvsig.annotation.swing.impl / src / main / java / org / gvsig / annotation / swing / impl / DefaultJAnnotationCreationServicePanel.java @ 38684

History | View | Annotate | Download (7.41 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.annotation.swing.impl;
23

    
24
import java.awt.BorderLayout;
25
import java.net.URL;
26

    
27
import javax.swing.ImageIcon;
28
import javax.swing.JOptionPane;
29

    
30
import org.slf4j.Logger;
31
import org.slf4j.LoggerFactory;
32

    
33
import org.gvsig.annotation.AnnotationCreationException;
34
import org.gvsig.annotation.AnnotationCreationService;
35
import org.gvsig.annotation.AnnotationCreationServiceException;
36
import org.gvsig.annotation.swing.AnnotationSwingManager;
37
import org.gvsig.annotation.swing.JAnnotationCreationServicePanel;
38
import org.gvsig.annotation.swing.impl.wizard.AnnotationProgressWizard;
39
import org.gvsig.annotation.swing.impl.wizard.MainOptionsWizard;
40
import org.gvsig.annotation.swing.impl.wizard.OptionalOptionsWizard;
41
import org.gvsig.annotation.swing.impl.wizard.SelectOutputFileWizard;
42
import org.gvsig.fmap.dal.exception.DataException;
43
import org.gvsig.gui.beans.wizard.WizardPanel;
44
import org.gvsig.gui.beans.wizard.WizardPanelActionListener;
45
import org.gvsig.gui.beans.wizard.WizardPanelWithLogo;
46
import org.gvsig.i18n.Messages;
47
import org.gvsig.tools.service.ServiceException;
48
import org.gvsig.tools.swing.api.ToolsSwingLocator;
49
import org.gvsig.tools.swing.icontheme.IconThemeManager;
50
import org.gvsig.tools.task.TaskStatus;
51

    
52
/**
53
 * Default implementation for the {@link JAnnotationCreationServicePanel}.
54
 * 
55
 * @author gvSIG Team
56
 * @version $Id$
57
 */
58
public class DefaultJAnnotationCreationServicePanel extends JAnnotationCreationServicePanel implements WizardPanel{
59

    
60
        private static final Logger LOG =
61
                LoggerFactory.getLogger(DefaultJAnnotationCreationServicePanel.class);
62

    
63
        private static final long serialVersionUID = 2965442763236823977L;
64

    
65
        private AnnotationCreationService annotationCreationService;
66
        private AnnotationSwingManager annotationSwingManager;
67

    
68
        private WizardPanelWithLogo wizardPanelWithLogo = null;
69

    
70
        private MainOptionsWizard mainOptionsWizard = null;
71
        private OptionalOptionsWizard optionalOptionsWizard = null;
72
        private SelectOutputFileWizard selectOutputFileWizard = null;
73
        private AnnotationProgressWizard annotationProgressWizard = null;
74
                
75
        private WizardListenerAdapter wizardListenerAdapter = null;
76

    
77
        public DefaultJAnnotationCreationServicePanel(
78
                        DefaultAnnotationSwingManager annotationSwingManager, AnnotationCreationService annotationCreationService) throws ServiceException {
79

    
80
                super();
81

    
82
        IconThemeManager themeManager = ToolsSwingLocator.getIconThemeManager();
83
                wizardPanelWithLogo = new WizardPanelWithLogo(themeManager.getCurrent().get("wizard-annotation"));
84

    
85
                this.annotationCreationService = annotationCreationService;
86
                this.annotationSwingManager = annotationSwingManager;
87

    
88
                try {
89
                        mainOptionsWizard = new MainOptionsWizard(this);
90
                        optionalOptionsWizard = new OptionalOptionsWizard(this);
91
                        selectOutputFileWizard = new SelectOutputFileWizard(this);
92
                        annotationProgressWizard = new AnnotationProgressWizard(this);
93
                } catch (DataException e) {
94
                        new AnnotationCreationServiceException("Exception creating the wizard forms", e);
95
                }
96
                
97

    
98
                addWizards();
99

    
100
                //Adding the listener
101
                wizardPanelWithLogo.setWizardListener(this);                
102

    
103
                setFinishButtonEnabled(false);
104

    
105
                wizardPanelWithLogo.getWizardComponents().getFinishButton().setText(Messages.getText("Comenzar"));
106
                
107
                this.setLayout(new BorderLayout());
108
                this.add(wizardPanelWithLogo, BorderLayout.CENTER);                        
109
        }
110

    
111
        private void addWizards(){
112
                wizardPanelWithLogo.addOptionPanel(mainOptionsWizard);        
113
                wizardPanelWithLogo.addOptionPanel(optionalOptionsWizard);
114
                wizardPanelWithLogo.addOptionPanel(selectOutputFileWizard);        
115
            wizardPanelWithLogo.addOptionPanel(annotationProgressWizard);     
116
        }
117

    
118

    
119
        @Override
120
        public AnnotationCreationService getAnnotationCreationService() {
121
                return annotationCreationService;
122
        }
123

    
124
        public void setFinishButtonEnabled(boolean isEnabled){
125
                wizardPanelWithLogo.setFinishButtonEnabled(isEnabled);
126
        }
127
        
128
        public void setCancellButtonEnabled(boolean isEnabled){
129
                wizardPanelWithLogo.setCancelButtonEnabled(isEnabled);
130
        }        
131
        
132
        public String getTextValueAttribute(){
133
                return mainOptionsWizard.getTextValueAttribute();
134
        }
135

    
136
        public WizardPanelActionListener getWizardPanelActionListener() {
137
                if ((wizardListenerAdapter == null && (getAnnotationServicePanelActionListener() != null))){
138
                        return new WizardListenerAdapter(this);
139
                }
140
                return wizardListenerAdapter;
141
        }
142

    
143
        public void setWizardPanelActionListener(
144
                        WizardPanelActionListener wizardActionListener) {
145
                // TODO Auto-generated method stub                
146
        }
147

    
148
        public String getDestinationShapeFile() {
149
                return selectOutputFileWizard.getSelectedFileName();
150
        }
151
        
152
        public void setNextButtonEnabled(boolean isEnabled){
153
                wizardPanelWithLogo.setNextButtonEnabled(isEnabled);
154
        }        
155
        
156
    /**
157
     * @return the annotationSwingManager
158
     */
159
    public AnnotationSwingManager getAnnotationSwingManager() {
160
        return annotationSwingManager;
161
    }
162

    
163
    /**
164
     * @param translation
165
     */
166
    public void setCancelButtonText(String translation) {
167
        wizardPanelWithLogo.getWizardComponents().getCancelButton().setText(translation);
168
    }
169

    
170
    public void createAnnotation() {
171
        this.lastWizard();
172
        
173
        Annotation annotation = new Annotation(getDestinationShapeFile(), getTextValueAttribute());
174
        annotation.start();
175
    }
176
    
177
    public void lastWizard() {
178
        this.wizardPanelWithLogo.getWizardComponents().getNextButton().getActionListeners()[0].actionPerformed(null);
179
    }
180
    
181
    private class Annotation extends Thread{
182
        private String file;
183
        private String textValueAttributeName;
184

    
185
        public Annotation(String file, String textValueAttributeName) {
186
            super();
187
            this.file = file;
188
            this.textValueAttributeName = textValueAttributeName;
189
        }
190

    
191
        public synchronized void run() {
192
            AnnotationCreationService annotationCreationService = getAnnotationCreationService();
193
            annotationProgressWizard.bind(annotationCreationService.getTaskStatus());           
194
            try {             
195
                annotationCreationService.createAnnotationStore(file, textValueAttributeName);                
196
            } catch (AnnotationCreationException e) {             
197
                LOG.error("Error creating the annotation", e);
198
                showError(e, annotationCreationService.getTaskStatus());              
199
            }
200
        }
201
    }
202
    
203
    private void showError(AnnotationCreationException e, TaskStatus taskStatus){        
204
        String message = e.getMessage();
205
        if (e.getCause() != null){
206
            message = e.getCause().getMessage();
207
        }
208
        JOptionPane.showMessageDialog(annotationProgressWizard, message);
209
    }
210
}