Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.image.extension / src / main / java / org / gvsig / arcims / image / gui / dialogs / ArcImsPropsDialog.java @ 32321

History | View | Annotate | Download (13.2 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

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2010 Prodevelop S.L. main development
26
 * http://www.prodevelop.es
27
 */
28

    
29
package org.gvsig.arcims.image.gui.dialogs;
30

    
31
import java.awt.Dimension;
32
import java.awt.Toolkit;
33
import java.awt.event.ActionEvent;
34
import java.awt.event.ActionListener;
35
import java.awt.event.KeyEvent;
36
import java.awt.event.KeyListener;
37
import java.net.URL;
38
import java.util.HashMap;
39

    
40
import javax.swing.DefaultListModel;
41
import javax.swing.JButton;
42
import javax.swing.JDialog;
43
import javax.swing.JOptionPane;
44
import javax.swing.JPanel;
45
import javax.swing.JTextField;
46
import javax.swing.JTree;
47
import javax.swing.event.ListDataEvent;
48
import javax.swing.event.ListDataListener;
49
import javax.swing.tree.DefaultMutableTreeNode;
50

    
51
import org.gvsig.andami.PluginServices;
52
import org.gvsig.andami.ui.mdiManager.IWindow;
53
import org.gvsig.andami.ui.mdiManager.WindowInfo;
54
import org.gvsig.arcims.image.fmap.layers.FRasterLyrArcIMS;
55
import org.gvsig.arcims.image.gui.panels.FeatureServicePanel;
56
import org.gvsig.arcims.image.gui.panels.ImageServicePanel;
57
import org.gvsig.arcims.image.gui.panels.utils.LayersListElement;
58
import org.gvsig.arcims.image.gui.wizards.ArcImsImageWizard;
59
import org.gvsig.fmap.mapcontext.layers.FLayer;
60
import org.slf4j.Logger;
61
import org.slf4j.LoggerFactory;
62

    
63
/**
64
 * The TOC ArcIMS properties container panel. It allows users to change a
65
 * previous request (not the server or service name).
66
 * 
67
 * @author jldominguez
68
 * 
69
 */
70
public class ArcImsPropsDialog extends JPanel implements IWindow,
71
                ActionListener, KeyListener, ListDataListener {
72
        private static Logger logger = LoggerFactory
73
                        .getLogger(ArcImsPropsDialog.class.getName());
74
        private static final long serialVersionUID = 0;
75
        private JPanel buttonsPanel = null;
76
        private FLayer fLayer = null;
77
        private String layerName;
78
        private URL hostURL;
79
        private String svcName;
80
        private String svcType;
81
        private String imageFormat;
82
        private JButton btnApply = null;
83
        private JButton btnOk = null;
84
        private JButton btnCancel = null;
85
        private ArcImsImageWizard arcImsWizard;
86
        private WindowInfo theViewInfo;
87
        private JTextField pointerToLayerNameTextField;
88
        private DefaultListModel pointerToSelectedLayersListModel;
89

    
90
        public ArcImsPropsDialog(FLayer layer) {
91
                super();
92
                initialize(layer);
93
        }
94

    
95
        private void initialize(FLayer layer) {
96
                setFLayer(layer);
97

    
98
                JPanel theServicePanel = null;
99

    
100
                if (layer instanceof FRasterLyrArcIMS) {
101
                        arcImsWizard = getArcImsWizard(((FRasterLyrArcIMS) layer)
102
                                        .getProperties(), true);
103
                        theServicePanel = arcImsWizard.getImgServicePanel();
104
                } else {
105
                        logger
106
                                        .debug("Unknow type of layer. ArcImsPropsDialog was not initialized. ");
107

    
108
                        return;
109
                }
110

    
111
                arcImsWizard.setBounds(0, 0, 510, 510);
112
                buttonsPanel = getButtonsPanel();
113
                buttonsPanel.setBounds(0, 510, 510, 45);
114

    
115
                this.setLayout(null);
116
                this.add(arcImsWizard); // , BorderLayout.CENTER);
117
                this.add(buttonsPanel); // , BorderLayout.SOUTH);
118
                this.setSize(new java.awt.Dimension(512, 475));
119
                this.setPreferredSize(new java.awt.Dimension(512, 475));
120

    
121
                loadControlPointers(theServicePanel);
122
                loadAndDisableControls(theServicePanel, layer);
123
        }
124

    
125
        private void loadControlPointers(JPanel svcPanel) {
126
                if (svcPanel instanceof ImageServicePanel) {
127
                        pointerToLayerNameTextField = ((ImageServicePanel) svcPanel)
128
                                        .getNewLayerNameTextField();
129
                        pointerToSelectedLayersListModel = ((ImageServicePanel) svcPanel)
130
                                        .getOrderedLayersListModel();
131
                }
132

    
133
                if ((svcPanel instanceof FeatureServicePanel)
134
                                && (!(svcPanel instanceof ImageServicePanel))) {
135
                        pointerToLayerNameTextField = ((FeatureServicePanel) svcPanel)
136
                                        .getNewLayerNameTextField();
137
                        pointerToSelectedLayersListModel = ((FeatureServicePanel) svcPanel)
138
                                        .getOrderedLayersListModel();
139
                }
140

    
141
                pointerToLayerNameTextField.addKeyListener(this);
142
                pointerToSelectedLayersListModel.addListDataListener(this);
143
        }
144

    
145
        private void getNewLayerConfig() {
146
                if (fLayer instanceof FRasterLyrArcIMS) {
147
                        ((FRasterLyrArcIMS) fLayer).setLayerQuery(arcImsWizard
148
                                        .getLayerQuery());
149
                        ((FRasterLyrArcIMS) fLayer).setName(arcImsWizard.getNewLayerName());
150
                        ((FRasterLyrArcIMS) fLayer)
151
                                        .setFormat(arcImsWizard.getImageFormat());
152

    
153
                        ((FRasterLyrArcIMS) fLayer).setNameQueryChange(true);
154
                }                
155
        }
156

    
157
        private void loadAndDisableControls(JPanel svcPanel, FLayer lyr) {
158
                arcImsWizard.getSvsNamesPanel().setServerComboText(hostURL.toString());
159

    
160
                JButton connB = arcImsWizard.getSvsNamesPanel().getConnectButton();
161
                ActionEvent ae = new ActionEvent(connB, ActionEvent.ACTION_PERFORMED,
162
                                "");
163
                arcImsWizard.getSvsNamesPanel().actionPerformed(ae);
164

    
165
                // JRadioButton userRadio =
166
                // arcImsWizard.getSvsNamesPanel().getServiceNameSelectionModeRadioButton();
167
                // userRadio.setSelected(true);
168
                // ae = new ActionEvent(userRadio, ActionEvent.ACTION_PERFORMED, "");
169
                arcImsWizard.getSvsNamesPanel().setUserDecisionTrue();
170

    
171
                arcImsWizard.getSvsNamesPanel().setServiceType(svcType);
172
                arcImsWizard.getSvsNamesPanel().setUserServiceName(svcName);
173

    
174
                JButton nextB = arcImsWizard.getSvsNamesPanel().getNextButton();
175
                ae = new ActionEvent(nextB, ActionEvent.ACTION_PERFORMED, "");
176

    
177
                // arcImsWizard.getSvsNamesPanel().actionPerformed(ae);
178
                arcImsWizard.getSvsNamesPanel().pseudoNextFired(lyr);
179

    
180
                if (svcPanel instanceof ImageServicePanel) { // ((ImageServicePanel)
181
                                                                                                                // svcPanel)
182

    
183
                        JTree avTree = ((ImageServicePanel) svcPanel)
184
                                        .getAvailableLayersTree();
185
                        Object root = avTree.getModel().getRoot();
186
                        int nofchildren = avTree.getModel().getChildCount(root);
187

    
188
                        String[] lyrIds = new String[1];
189
                        lyrIds = ((FRasterLyrArcIMS) fLayer).getLayerQuery().split(",");
190

    
191
                        int i;
192
                        int j;
193
                        LayersListElement lle;
194
                        DefaultMutableTreeNode dmtn;
195

    
196
                        for (i = 0; i < lyrIds.length; i++) {
197
                                String id = lyrIds[i];
198

    
199
                                for (j = (nofchildren - 1); j >= 0; j--) {
200
                                        dmtn = (DefaultMutableTreeNode) avTree.getModel().getChild(
201
                                                        root, j);
202
                                        lle = (LayersListElement) dmtn.getUserObject();
203

    
204
                                        if (lle.getID().compareTo(id) == 0) {
205
                                                // arcImsWizard.getImgServicePanel().addLayerToSelectedList(lle);
206
                                                ((ImageServicePanel) svcPanel)
207
                                                                .addLayerToSelectedListNoConfirm(lle);
208
                                        }
209
                                }
210
                        }
211

    
212
                        ((ImageServicePanel) svcPanel).refreshSelectedLayersList();
213
                        ((ImageServicePanel) svcPanel).getNewLayerNameTextField().setText(
214
                                        layerName);
215
                        ((ImageServicePanel) svcPanel).getChangeServerButton().setEnabled(
216
                                        false);
217
                        ((ImageServicePanel) svcPanel).setInImageFormatCombo(imageFormat);
218
                }
219

    
220
                if ((svcPanel instanceof FeatureServicePanel)
221
                                && (!(svcPanel instanceof ImageServicePanel))) {
222
                        // ((FeatureServicePanel)
223
                        // svcPanel).getImgServiceTab_2().setVisible(false);
224
                        // ((FeatureServicePanel)
225
                        // svcPanel).getServiceInfoNextButton().setEnabled(false);
226
                        ((FeatureServicePanel) svcPanel).getChangeServerPanel().setVisible(
227
                                        false);
228

    
229
                        // ((FeatureServicePanel) svcPanel)
230
                        // JTree avTree = ((FeatureServicePanel)
231
                        // svcPanel).getAvailableLayersTree();
232
                        // Object root = avTree.getModel().getRoot();
233
                        // int nofchildren = avTree.getModel().getChildCount(root);
234
                        //
235
                        // String[] lyrIds = new String[1];
236
                        // lyrIds = ((FFeatureLyrArcIMS) fLayer).getLayerQuery().split(",");
237
                        //
238
                        // int i, j;
239
                        // LayersListElement lle;
240
                        // DefaultMutableTreeNode dmtn;
241
                        // for (i = 0; i < lyrIds.length; i++) {
242
                        // String id = lyrIds[i];
243
                        // for (j = (nofchildren - 1); j >= 0; j--) {
244
                        // dmtn = (DefaultMutableTreeNode) avTree.getModel().getChild(root,
245
                        // j);
246
                        // lle = (LayersListElement) dmtn.getUserObject();
247
                        // if (lle.getID().compareTo(id) == 0) {
248
                        // // arcImsWizard.getImgServicePanel().addLayerToSelectedList(lle);
249
                        // ((FeatureServicePanel)
250
                        // svcPanel).addLayerToSelectedListNoConfirm(lle);
251
                        // }
252
                        // }
253
                        // }
254
                        // ((FeatureServicePanel) svcPanel).refreshSelectedLayersList();
255
                        ((FeatureServicePanel) svcPanel).getNewLayerNameTextField()
256
                                        .setText(layerName);
257

    
258
                        // ((FeatureServicePanel)
259
                        // svcPanel).getChangeServerButton().setEnabled(false);
260
                        btnApply.setEnabled(false);
261
                }
262
        }
263

    
264
        public void setFLayer(FLayer f) {
265
                fLayer = f;
266
        }
267

    
268
        /**
269
         * Gets a new ArcImsWizard to allow the user to update the ArcIMS layer's
270
         * seetings. Needs the original layer info.
271
         * 
272
         * @param info
273
         *            the layer info
274
         * @return a new ArcImsWizard to allow the user to update the ArcIMS
275
         *         settings
276
         */
277
        public ArcImsImageWizard getArcImsWizard(HashMap info,
278
                        boolean editionallowed) {
279
                if (info != null) {
280
                        try {
281
                                layerName = (String) info.get("layerName");
282
                                hostURL = (URL) info.get("serverUrl");
283
                                svcName = (String) info.get("serviceName");
284
                                svcType = (String) info.get("serviceType");
285
                                imageFormat = (String) info.get("format");
286

    
287
                                // this.imasvcType = (String) info.get("serviceType");
288
                                ArcImsImageWizard wiz = new ArcImsImageWizard(editionallowed,
289
                                                true);
290
                                wiz.initWizard();
291

    
292
                                // wiz.getDataSource().setHostService(hostURL, svcName,
293
                                // svcType);
294
                                return wiz;
295
                        } catch (Exception e) {
296
                                logger.error("While starting wizard ", e);
297
                                JOptionPane.showMessageDialog(null, e.getMessage(), "Error",
298
                                                JOptionPane.ERROR_MESSAGE);
299
                                e.printStackTrace();
300
                        }
301
                }
302

    
303
                return null;
304
        }
305

    
306
        public JPanel getButtonsPanel() {
307
                if (buttonsPanel == null) {
308
                        buttonsPanel = new JPanel();
309
                        buttonsPanel.setLayout(null);
310
                        buttonsPanel.setName("buttonPanel");
311

    
312
                        buttonsPanel.add(getBtnOk(), null);
313
                        buttonsPanel.add(getBtnApply(), null);
314
                        buttonsPanel.add(getBtnCancel(), null);
315
                }
316

    
317
                return buttonsPanel;
318
        }
319

    
320
        public JButton getBtnOk() {
321
                if (btnOk == null) {
322
                        btnOk = new JButton("ok");
323
                        btnOk.setText(PluginServices.getText(this, "ok"));
324
                        btnOk.setActionCommand("OK");
325
                        btnOk.addActionListener(this);
326
                        btnOk.setBounds(338, 10, 90, 25);
327
                }
328

    
329
                return btnOk;
330
        }
331

    
332
        public JButton getBtnApply() {
333
                if (btnApply == null) {
334
                        btnApply = new JButton("apply");
335
                        btnApply.setText(PluginServices.getText(this, "apply"));
336
                        btnApply.setEnabled(true);
337
                        btnApply.setActionCommand("APPLY");
338
                        btnApply.addActionListener(this);
339
                        btnApply.setBounds(238, 10, 90, 25);
340
                }
341

    
342
                return btnApply;
343
        }
344

    
345
        public JButton getBtnCancel() {
346
                if (btnCancel == null) {
347
                        btnCancel = new JButton("cancel");
348
                        btnCancel.setText(PluginServices.getText(this, "cancel"));
349
                        btnCancel.setActionCommand("CANCEL");
350
                        btnCancel.addActionListener(this);
351
                        btnCancel.setBounds(80, 10, 90, 25);
352
                }
353

    
354
                return btnCancel;
355
        }
356

    
357
        public void close() {
358
                PluginServices.getMDIManager().closeWindow(this);
359
        }
360

    
361
        public WindowInfo getWindowInfo() {
362
                if (theViewInfo == null) {
363
                        theViewInfo = new WindowInfo(WindowInfo.MODALDIALOG);
364
                        theViewInfo.setTitle(PluginServices.getText(this,
365
                                        "fit_ArcIms_layer"));
366
                        theViewInfo.setWidth(this.getWidth() + 10);
367
                        theViewInfo.setHeight(this.getHeight() + 40);
368
                }
369

    
370
                return theViewInfo;
371
        }
372

    
373
        public void actionPerformed(ActionEvent e) {
374
                if (e.getSource() == this.btnApply) {
375
                        getNewLayerConfig();
376
                        fLayer.getMapContext().invalidate();
377
                }
378

    
379
                if (e.getSource() == this.btnOk) {
380
                        if (fLayer instanceof FRasterLyrArcIMS) {
381
                                getNewLayerConfig();
382
                                fLayer.getMapContext().invalidate();
383
                        } 
384

    
385
                        close();
386
                }
387

    
388
                if (e.getSource() == this.btnCancel) {
389
                        close();
390
                }
391
        }
392

    
393
        /**
394
         * Utility method to center the given JDialog on the screen.
395
         * 
396
         * @param jd
397
         */
398
        public void centerThis(JDialog jd) {
399
                int dw = jd.getBounds().width;
400
                int dh = jd.getBounds().height;
401
                Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
402
                jd.setBounds((screen.width - dw) / 2, (screen.height - dh) / 2, dw, dh);
403
        }
404

    
405
        public void keyPressed(KeyEvent e) {
406
        }
407

    
408
        public void keyTyped(KeyEvent e) {
409
        }
410

    
411
        public void keyReleased(KeyEvent e) {
412
                if (e.getSource() == pointerToLayerNameTextField) {
413
                        boolean correct = isCorrectlyConfigured();
414
                        btnOk.setEnabled(correct);
415
                        btnApply.setEnabled(correct);
416
                }
417
        }
418

    
419
        public void contentsChanged(ListDataEvent e) {
420
        }
421

    
422
        private void contentChanged() {
423
                boolean correct = isCorrectlyConfigured();
424
                btnOk.setEnabled(correct);
425
                btnApply.setEnabled(correct);
426
        }
427

    
428
        public void intervalAdded(ListDataEvent e) {
429
                contentChanged();
430
        }
431

    
432
        public void intervalRemoved(ListDataEvent e) {
433
                contentChanged();
434
        }
435

    
436
        private boolean isCorrectlyConfigured() {
437
                if (pointerToSelectedLayersListModel.size() == 0) {
438
                        return false;
439
                }
440

    
441
                if (pointerToLayerNameTextField.getText().length() == 0) {
442
                        return false;
443
                }
444

    
445
                return true;
446
        }
447

    
448
        public Object getWindowProfile() {
449
                return WindowInfo.DIALOG_PROFILE;
450
        }
451

    
452
} // @jve:decl-index=0:visual-constraint="10,10"