Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.exportto / org.gvsig.exportto.swing / org.gvsig.exportto.swing.impl / src / main / java / org / gvsig / export / swing / impl / panels / SelectFormatPanel.java @ 44395

History | View | Annotate | Download (8.87 KB)

1
package org.gvsig.export.swing.impl.panels;
2

    
3
import java.awt.event.ActionEvent;
4
import java.awt.event.ActionListener;
5
import java.util.List;
6
import java.util.function.Predicate;
7
import javax.swing.DefaultListModel;
8
import javax.swing.JComponent;
9
import javax.swing.JScrollPane;
10
import javax.swing.event.ListSelectionEvent;
11
import javax.swing.event.ListSelectionListener;
12
import org.apache.commons.lang3.StringUtils;
13
import org.gvsig.export.ExportLocator;
14
import org.gvsig.export.ExportParameters;
15
import org.gvsig.export.Filter;
16
import org.gvsig.export.spi.ExportServiceFactory;
17
import org.gvsig.export.swing.JExportProcessPanel;
18
import org.gvsig.export.swing.spi.ExportPanel;
19
import org.gvsig.export.swing.spi.ExportPanelValidationException;
20
import org.gvsig.tools.ToolsLocator;
21
import org.gvsig.tools.bookmarksandhistory.Bookmark;
22
import org.gvsig.tools.bookmarksandhistory.Bookmarks;
23
import org.gvsig.tools.bookmarksandhistory.History;
24
import org.gvsig.tools.i18n.I18nManager;
25
import org.gvsig.tools.swing.api.ListElement;
26
import org.gvsig.tools.swing.api.ToolsSwingLocator;
27
import org.gvsig.tools.swing.api.bookmarkshistory.ActionEventWithCurrentValue;
28
import static org.gvsig.tools.swing.api.bookmarkshistory.ActionEventWithCurrentValue.ID_GETVALUE;
29
import static org.gvsig.tools.swing.api.bookmarkshistory.ActionEventWithCurrentValue.ID_SETVALUE;
30
import org.gvsig.tools.swing.api.bookmarkshistory.BookmarksController;
31
import org.gvsig.tools.swing.api.bookmarkshistory.HistoryController;
32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34

    
35
/**
36
 *
37
 * @author jjdelcerro
38
 */
39
@SuppressWarnings("UseSpecificCatch")
40
public class SelectFormatPanel
41
        extends SelectFormatPanelView
42
        implements ExportPanel {
43

    
44
    private static final Logger LOG = LoggerFactory.getLogger(SelectFormatPanel.class);
45

    
46
    private final JExportProcessPanel processPanel;
47
    private final Filter<ExportServiceFactory> serviceFilter;
48
    private final ActionListener onExitPanel;
49

    
50
    private ExportParameters parameters;
51
    private BookmarksController<ExportParameters> bookmarksController;
52
    private HistoryController<ExportParameters> historyController;
53

    
54
    public SelectFormatPanel(
55
            JExportProcessPanel processPanel,
56
            ExportParameters parameters,
57
            Filter<ExportServiceFactory> serviceFilter,
58
            ActionListener onExitPanel
59
    ) {
60
        this.processPanel = processPanel;
61
        this.parameters = parameters;
62
        this.serviceFilter = serviceFilter;
63
        this.onExitPanel = onExitPanel;
64
        initComponents();
65
    }
66

    
67
    private void initComponents() {
68
        this.txtDescription.setBackground(this.lblEmpty.getBackground());
69
        this.txtDescription.setForeground(this.lblEmpty.getForeground());
70
        if (this.txtDescription.getParent().getParent() instanceof JScrollPane) {
71
            JScrollPane scrollPanel = (JScrollPane) this.txtDescription.getParent().getParent();
72
            scrollPanel.setBorder(null);
73
        }
74

    
75
        List<ExportServiceFactory> factories = ExportLocator.getServiceManager()
76
                .getServiceFactories(serviceFilter);
77
        DefaultListModel model = new DefaultListModel();
78
        for (ExportServiceFactory factory : factories) {
79
            model.addElement(new ListElement(factory.getLabel(), factory));
80
        }
81
        this.lstServices.setModel(model);
82
        this.lstServices.addListSelectionListener(new ListSelectionListener() {
83
            @Override
84
            public void valueChanged(ListSelectionEvent e) {
85
                if (e.getValueIsAdjusting()) {
86
                    return;
87
                }
88
                doUpdateDescription();
89
            }
90
        });
91

    
92
        Predicate<ExportParameters> filter = new Predicate<ExportParameters>() {
93
            @Override
94
            public boolean test(ExportParameters params) {
95
                if (params == null) {
96
                    return false;
97
                }
98
                ExportServiceFactory selectedService = getSelectedService();
99
                if (selectedService == null) {
100
                    return false;
101
                }
102
                String selectedServiceName = selectedService.getName();
103
                String paramsServiceName = params.getServiceName();
104
                return StringUtils.equals(paramsServiceName, selectedServiceName);
105
            }
106
        };
107
        ActionListener historyAndBookmarkListener = new ActionListener() {
108
            @Override
109
            public void actionPerformed(ActionEvent e) {
110
                ActionEventWithCurrentValue<ExportParameters> b = (ActionEventWithCurrentValue<ExportParameters>) e;
111
                switch (b.getID()) {
112
                    case ID_GETVALUE:
113
                        // Esta deshabilitado esta opcion
114
                        break;
115
                    case ID_SETVALUE:
116
                        if (b.getCurrentValue() == null) {
117
                            return;
118
                        }
119
                        ExportParameters exportParams;
120
                        try {
121
                            exportParams = b.getCurrentValue().clone();
122
                        } catch (Exception ex) {
123
                            LOG.warn("Not been able to clone export parameters", ex);
124
                            return;
125
                        }
126
                        setSelectedService(exportParams.getFactory());
127

    
128
                        processPanel.getProcess().setParameters(exportParams);
129
                        parameters = exportParams;
130
                        break;
131
                }
132

    
133
            }
134
        };
135
        Bookmarks<ExportParameters> bookmarks = ExportLocator.getManager().getBookmarks();
136
        this.bookmarksController = ToolsSwingLocator.getToolsSwingManager().createBookmarksController(bookmarks, this.btnBookmark);
137
        this.bookmarksController.setAllowAddBookmarks(false);
138
        this.bookmarksController.setFilter(new Predicate<Bookmark<ExportParameters>>() {
139
            @Override
140
            public boolean test(Bookmark<ExportParameters> bookmark) {
141
                ExportParameters params = bookmark.getValue();
142
                if (params == null) {
143
                    return false;
144
                }
145
                ExportServiceFactory selectedService = getSelectedService();
146
                if (selectedService == null) {
147
                    return true;
148
                }
149
                String selectedServiceName = selectedService.getName();
150
                String paramsServiceName = params.getServiceName();
151
                return StringUtils.equals(paramsServiceName, selectedServiceName);
152
            }
153
        });       
154
        this.bookmarksController.addActionListener(historyAndBookmarkListener);
155
        History<ExportParameters> history = ExportLocator.getManager().getHistory();
156
        this.historyController = ToolsSwingLocator.getToolsSwingManager().createHistoryController(history, this.btnHistory);
157
        this.historyController.setFilter(filter);
158
        this.historyController.addActionListener(historyAndBookmarkListener);
159
    }
160

    
161
    private void doUpdateDescription() {
162
        ExportServiceFactory factory = this.getSelectedService();
163
        if (factory == null) {
164
            return;
165
        }
166
        this.txtDescription.setText(factory.getDescription());
167
        this.processPanel.setButtonEnabled(JExportProcessPanel.BUTTON_NEXT, true);
168
    }
169

    
170
    @Override
171
    public String getIdPanel() {
172
        return "SelectFormatPanel";
173
    }
174

    
175
    @Override
176
    public String getTitlePanel() {
177
        I18nManager i18n = ToolsLocator.getI18nManager();
178
        return i18n.getTranslation("select_format_to_export");
179
    }
180

    
181
    @Override
182
    public boolean validatePanel() throws ExportPanelValidationException {
183
        I18nManager i18n = ToolsLocator.getI18nManager();
184
        ExportServiceFactory factory = this.getSelectedService();
185
        if (factory == null) {
186
            throw new ExportPanelValidationException(
187
                    i18n.getTranslation("_You_must_select_an_item_from_the_list_of-formats")
188
            );
189
        }
190
        return true;
191
    }
192

    
193
    @Override
194
    public void enterPanel() {
195

    
196
    }
197

    
198
    @Override
199
    public void previousPanel() {
200

    
201
    }
202

    
203
    @Override
204
    public void nextPanel() {
205
        this.onExitPanel.actionPerformed(new ActionEvent(this, 0, "exitPanel"));
206
//        ExportEntry currentEntry = this.exportationPanelParameters.getCurrentEntry();
207
//        if (currentEntry != null) {
208
//            ExportParameters exportParams = currentEntry.getParameters();
209
//            if (exportParams != null) {
210
//                this.processPanel.getProcess().setParameters(exportParams);
211
//            }
212
//        }
213
    }
214

    
215
    @Override
216
    public JComponent asJComponent() {
217
        return this;
218
    }
219

    
220
    public ExportServiceFactory getSelectedService() {
221
        ExportServiceFactory factory = (ExportServiceFactory) ListElement.getSelected(lstServices);
222
        return factory;
223
    }
224

    
225
    public void setSelectedService(ExportServiceFactory factory) {
226
        ListElement.setSelected(lstServices, factory);
227
    }
228

    
229
}