Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.annotation / org.gvsig.annotation.swing / org.gvsig.annotation.swing.impl / src / main / java / org / gvsig / annotation / swing / impl / DefaultJAnnotationCreationServicePanel.java @ 42182

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

    
26
import java.awt.BorderLayout;
27
import java.awt.event.ComponentEvent;
28
import java.awt.event.ComponentListener;
29
import java.awt.event.ContainerEvent;
30
import java.awt.event.ContainerListener;
31

    
32
import javax.swing.JOptionPane;
33
import javax.swing.event.AncestorEvent;
34
import javax.swing.event.AncestorListener;
35

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

    
57
/**
58
 * Default implementation for the {@link JAnnotationCreationServicePanel}.
59
 * 
60
 * @author gvSIG Team
61
 * @version $Id$
62
 */
63
public class DefaultJAnnotationCreationServicePanel extends JAnnotationCreationServicePanel implements WizardPanel{
64

    
65
        private static final Logger LOG =
66
                LoggerFactory.getLogger(DefaultJAnnotationCreationServicePanel.class);
67

    
68
        private static final long serialVersionUID = 2965442763236823977L;
69

    
70
        private AnnotationCreationService annotationCreationService;
71
        private AnnotationSwingManager annotationSwingManager;
72

    
73
        private WizardPanelWithLogo wizardPanelWithLogo = null;
74

    
75
        private MainOptionsWizard mainOptionsWizard = null;
76
        private OptionalOptionsWizard optionalOptionsWizard = null;
77
        private SelectOutputFileWizard selectOutputFileWizard = null;
78
        private AnnotationProgressWizard annotationProgressWizard = null;
79
                
80
        private WizardListenerAdapter wizardListenerAdapter = null;
81

    
82
        public DefaultJAnnotationCreationServicePanel(
83
                        DefaultAnnotationSwingManager annotationSwingManager, AnnotationCreationService annotationCreationService) throws ServiceException {
84

    
85
                super();
86

    
87
        IconThemeManager themeManager = ToolsSwingLocator.getIconThemeManager();
88
                wizardPanelWithLogo = new WizardPanelWithLogo(themeManager.getCurrent().get("wizard-annotation"));
89

    
90
                this.annotationCreationService = annotationCreationService;
91
                this.annotationSwingManager = annotationSwingManager;
92

    
93
                try {
94
                        mainOptionsWizard = new MainOptionsWizard(this);
95
                        optionalOptionsWizard = new OptionalOptionsWizard(this);
96
                        selectOutputFileWizard = new SelectOutputFileWizard(this);
97
                        annotationProgressWizard = new AnnotationProgressWizard(this);
98
                } catch (DataException e) {
99
                        new AnnotationCreationServiceException("Exception creating the wizard forms", e);
100
                }
101
                
102

    
103
                addWizards();
104

    
105
                //Adding the listener
106
                wizardPanelWithLogo.setWizardListener(this);                
107

    
108
                setFinishButtonEnabled(false);
109

    
110
                wizardPanelWithLogo.getWizardComponents().getFinishButton().setText(Messages.getText("Comenzar"));
111
                
112
                this.setLayout(new BorderLayout());
113
                this.add(wizardPanelWithLogo, BorderLayout.CENTER);                
114
                
115
                this.addAncestorListener(new AncestorListener() {
116
                    public void ancestorAdded(AncestorEvent ancestorEvent) {
117
                    }
118

    
119
                    public void ancestorMoved(AncestorEvent ancestorEvent) {
120
                    }
121

    
122
                    public void ancestorRemoved(AncestorEvent ancestorEvent) {
123
                            TaskStatus taskStatus = DefaultJAnnotationCreationServicePanel.this.getAnnotationCreationService().getTaskStatus();
124
                            taskStatus.getManager().remove(taskStatus);
125
                    }
126
                });
127
        }
128

    
129
        private void addWizards(){
130
                wizardPanelWithLogo.addOptionPanel(mainOptionsWizard);        
131
                wizardPanelWithLogo.addOptionPanel(optionalOptionsWizard);
132
                wizardPanelWithLogo.addOptionPanel(selectOutputFileWizard);        
133
            wizardPanelWithLogo.addOptionPanel(annotationProgressWizard);     
134
        }
135

    
136

    
137
        @Override
138
        public AnnotationCreationService getAnnotationCreationService() {
139
                return annotationCreationService;
140
        }
141

    
142
        public void setFinishButtonEnabled(boolean isEnabled){
143
                wizardPanelWithLogo.setFinishButtonEnabled(isEnabled);
144
        }
145
        
146
        public void setCancellButtonEnabled(boolean isEnabled){
147
                wizardPanelWithLogo.setCancelButtonEnabled(isEnabled);
148
        }        
149
        
150
        public String getTextValueAttribute(){
151
                return mainOptionsWizard.getTextValueAttribute();
152
        }
153
        
154
        public String getTextRotationAttribute() {
155
                return optionalOptionsWizard.getRotationAttribute();
156
        }
157
        
158
        public String getTextColorAttribute() {
159
                return optionalOptionsWizard.getColorAttribute();
160
        }
161
        
162
        public String getTextHeightAttribute() {
163
                return optionalOptionsWizard.getHeightAttribute();
164
        }
165
        
166
        public String getTextTypeAttribute(){
167
                return optionalOptionsWizard.getFontAttribute();
168
        }
169

    
170
        public WizardPanelActionListener getWizardPanelActionListener() {
171
                if ((wizardListenerAdapter == null && (getAnnotationServicePanelActionListener() != null))){
172
                        return new WizardListenerAdapter(this);
173
                }
174
                return wizardListenerAdapter;
175
        }
176

    
177
        public void setWizardPanelActionListener(
178
                        WizardPanelActionListener wizardActionListener) {
179
                // TODO Auto-generated method stub                
180
        }
181

    
182
        public String getDestinationShapeFile() {
183
                return selectOutputFileWizard.getSelectedFileName();
184
        }
185
        
186
        public void setNextButtonEnabled(boolean isEnabled){
187
                wizardPanelWithLogo.setNextButtonEnabled(isEnabled);
188
        }        
189
        
190
    /**
191
     * @return the annotationSwingManager
192
     */
193
    public AnnotationSwingManager getAnnotationSwingManager() {
194
        return annotationSwingManager;
195
    }
196

    
197
    /**
198
     * @param translation
199
     */
200
    public void setCancelButtonText(String translation) {
201
        wizardPanelWithLogo.getWizardComponents().getCancelButton().setText(translation);
202
    }
203

    
204
    public void createAnnotation() {
205
        this.lastWizard();
206
        
207
        Annotation annotation = new Annotation(
208
                        getDestinationShapeFile(), 
209
                        getTextValueAttribute(),
210
                        getTextRotationAttribute(),
211
                        getTextColorAttribute(),
212
                        getTextHeightAttribute(),
213
                        getTextTypeAttribute());
214
        annotation.start();
215
    }
216
    
217
    public void lastWizard() {
218
        this.wizardPanelWithLogo.getWizardComponents().getNextButton().getActionListeners()[0].actionPerformed(null);
219
    }
220
    
221
    private class Annotation extends Thread{
222
        private String file;
223
        private String textValueAttributeName;
224
        private String attrRotation;
225
        private String attrColor;
226
        private String attrHeight;
227
        private String attrType;
228

    
229
        /*public Annotation(String file, String textValueAttributeName) {
230
            super();
231
            this.file = file;
232
            this.textValueAttributeName = textValueAttributeName;
233
        }*/
234
        
235
        public Annotation(String file, 
236
                        String textValueAttributeName,
237
                        String attrRotation,
238
                        String attrColor,
239
                        String attrHeight,
240
                        String attrType) {
241
            super();
242
            this.file = file;
243
            this.textValueAttributeName = textValueAttributeName;
244
            this.attrColor = attrColor;
245
            this.attrHeight = attrHeight;
246
            this.attrRotation = attrRotation;
247
            this.attrType = attrType;
248
        }
249

    
250
        public synchronized void run() {
251
            AnnotationCreationService annotationCreationService = getAnnotationCreationService();
252
            annotationProgressWizard.bind(annotationCreationService.getTaskStatus());           
253
            try {             
254
                if(attrRotation != null)
255
                        annotationCreationService.setFontRotationAttribute(attrRotation);
256
                if(attrColor != null)
257
                        annotationCreationService.setFontColorAttribute(attrColor);
258
                if(attrHeight != null)
259
                        annotationCreationService.setFontHeigthAttribute(attrHeight);
260
                if(attrType != null)
261
                        annotationCreationService.setFontTypeAttribute(attrType);
262
                annotationCreationService.createAnnotationStore(file, textValueAttributeName);
263
            } catch (AnnotationCreationException e) {             
264
                LOG.error("Error creating the annotation", e);
265
                showError(e, annotationCreationService.getTaskStatus());              
266
            } catch (DataException e) {
267
                     LOG.error("Error assigning properties to the annotation layer", e);
268
                        }
269
        }
270
    }
271
    
272
    private void showError(AnnotationCreationException e, TaskStatus taskStatus){        
273
        String message = e.getMessage();
274
        if (e.getCause() != null){
275
            message = e.getCause().getMessage();
276
        }
277
        JOptionPane.showMessageDialog(annotationProgressWizard, message);
278
    }
279
}