Revision 9515 org.gvsig.online/trunk/org.gvsig.online/org.gvsig.online.swing/org.gvsig.online.swing.impl/src/main/java/org/gvsig/online/swing/impl/initworkspace/OnlineJInitWorkingcopyImpl.java

View differences:

OnlineJInitWorkingcopyImpl.java
24 24
package org.gvsig.online.swing.impl.initworkspace;
25 25

  
26 26
import java.awt.event.ActionEvent;
27
import java.awt.event.ActionListener;
27 28
import java.awt.event.FocusEvent;
28 29
import java.awt.event.FocusListener;
29 30
import java.io.File;
......
34 35
import javax.swing.DefaultComboBoxModel;
35 36
import javax.swing.ImageIcon;
36 37
import javax.swing.JComponent;
38
import javax.swing.JOptionPane;
37 39
import javax.swing.SwingUtilities;
38 40
import javax.swing.event.ChangeEvent;
39 41
import javax.swing.event.ChangeListener;
......
46 48
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer;
47 49
import org.gvsig.fmap.dal.swing.DALSwingLocator;
48 50
import org.gvsig.fmap.dal.swing.DataSwingManager;
51
import org.gvsig.fmap.geom.primitive.Envelope;
49 52
import org.gvsig.fmap.mapcontrol.MapControl;
50 53
import org.gvsig.online.lib.api.OnlineLayer;
51 54
import org.gvsig.online.lib.api.OnlineLocator;
52 55
import org.gvsig.online.lib.api.OnlineManager;
56
import static org.gvsig.online.lib.api.OnlineManager.AUTH_MODE_ONLINE;
53 57
import static org.gvsig.online.lib.api.OnlineManager.ERR_NO_ERROR;
54 58
import static org.gvsig.online.lib.api.OnlineManager.ERR_PROJECT_REQUIRED;
55 59
import static org.gvsig.online.lib.api.OnlineManager.ERR_WORKINGAREA_REQUIRED;
56 60
import org.gvsig.online.lib.api.OnlineProject;
57 61
import org.gvsig.online.lib.api.OnlineSite;
62
import org.gvsig.online.lib.api.OnlineUserIdentificationRequester;
63
import org.gvsig.online.lib.api.OnlineUserIdentificationRequester.OnlineUserIdentificationRequesterFactory;
58 64
import org.gvsig.online.lib.api.workingcopy.OnlineWorkingcopy;
59 65
import org.gvsig.online.lib.api.workingcopy.OnlineWorkingcopyDescriptor;
60 66
import org.gvsig.online.lib.api.workingcopy.WorkingArea;
......
79 85
import org.gvsig.tools.swing.api.pickercontroller.PickerController;
80 86
import org.gvsig.tools.swing.api.task.TaskStatusController;
81 87
import org.gvsig.tools.swing.api.task.TaskStatusSwingManager;
88
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager;
82 89
import org.gvsig.tools.swing.api.windowmanager.Dialog;
83 90
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
84 91
import org.gvsig.tools.task.SimpleTaskStatus;
......
107 114
    private TextFieldWithHistoryAndBookmarkController siteURLController;
108 115
    private WorkingAreaPickerControllerImpl workingAreaPicker;
109 116
    private TaskStatusController taskStatusController;
117
    private OnlineUserIdentificationRequester.OnlineUserIdentificationRequesterConfig authenticationRequesterConfig;
110 118

  
111 119

  
112 120
    public OnlineJInitWorkingcopyImpl() {
......
120 128
        ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
121 129
        TaskStatusSwingManager taskStatusManager = ToolsSwingLocator.getTaskStatusSwingManager();
122 130
        OnlineSwingServices onlineservices = OnlineSwingLocator.getOnlineSwingManager().getDefaultServices();
131
        OnlineManager onlineManager = OnlineLocator.getOnlineManager();
123 132

  
124 133
        this.taskStatusController = taskStatusManager.createTaskStatusController(
125 134
                null,
......
187 196
        this.siteURLController.addActionListener((ActionEvent e) -> {
188 197
            doChangeSiteURL();
189 198
        });
199
        
200
        OnlineUserIdentificationRequesterFactory defaultAuthenticationFactory = null;
201
        DefaultComboBoxModel<LabeledValue<OnlineUserIdentificationRequesterFactory>> userIdentificationRequestersModel = new DefaultComboBoxModel<>();
202
        for (OnlineUserIdentificationRequesterFactory factory : onlineManager.getUserIdentificationRequesters()) {
203
            userIdentificationRequestersModel.addElement(
204
                    new LabeledValueImpl<OnlineUserIdentificationRequesterFactory>(
205
                            factory.getName(), factory
206
                    )
207
            );
208
            if( defaultAuthenticationFactory == null || StringUtils.equalsIgnoreCase(factory.getName(), AUTH_MODE_ONLINE)) {
209
                defaultAuthenticationFactory = factory;
210
            }
211
        }
212
        this.authenticationRequesterConfig = null;
213
        this.cboAuthentication.setModel(userIdentificationRequestersModel);
214
        this.cboAuthentication.setSelectedIndex(-1);
215
        
216
        this.cboAuthentication.addActionListener((ActionEvent e) -> {
217
            doChangeAuthentication();
218
        });
219
        this.btnAuthentication.addActionListener((ActionEvent e) -> {
220
            doAuthenticationRequesterConfig();            
221
        });
222
        this.btnAuthenticationTest.addActionListener((ActionEvent e) -> {
223
                doAuthenticationTest();
224
        });
225
        
190 226
        this.cboProjects.addActionListener((ActionEvent e) -> {
191 227
            SwingUtilities.invokeLater(() -> {doChangeProject();});
192 228
        });
......
232 268
        boolean enableAll = true;
233 269
        
234 270
        boolean isThereURL = false;
271
        boolean isThereAuth = false;
272
        boolean isThereSite = false;
235 273
        boolean isThereProject = false;
236 274
        boolean isThereDbfile = false;
237 275
        boolean isThereWsName = false;
......
255 293
            enableAll = enableAll && isThereURL;
256 294
        }
257 295
            
296
        // Check auth
297
        if( enableAll ) {
298
            isThereAuth = (this.authenticationRequesterConfig != null);
299
            if( !isThereAuth ) {
300
                message2(enableAll, "_Select_a_Online_authentication_mode");
301
            }
302
            enableAll = enableAll && isThereAuth;
303
        }
304

  
305
        // Check site
306
        if( enableAll ) {
307
            isThereSite = (this.getSite() != null);
308
            if( !isThereSite ) {
309
                message2(enableAll, "_Select_a_valid_Online_url_and_authentication_mode");
310
            }
311
            enableAll = enableAll && isThereSite;
312
        }
313

  
258 314
        // 2. Check online project
259 315
        if( enableAll ) {
260 316
            OnlineProject project = this.getProject();
......
320 376
        
321 377
        
322 378
        this.siteURLController.setEnabled(!this.isProcessing());
379
        this.cboAuthentication.setEnabled(isThereURL);
380
        this.btnAuthentication.setEnabled(isThereAuth && this.authenticationRequesterConfig.hasIdentificationConfig());
381
        this.btnAuthenticationTest.setEnabled(isThereAuth);
323 382
        this.cboProjects.setEnabled(isThereURL && this.cboProjects.getModel().getSize()>0 );
324 383
        this.workspaceFilePicker.setEnabled(isThereProject);
325 384
        this.chkCreateConnectionToDatabase.setEnabled(isThereProject && isThereDbfile);
......
467 526
        return err;
468 527
    }
469 528

  
529
    private OnlineSite createSite() {
530
        try {
531
            OnlineManager manager = OnlineLocator.getOnlineManager();
532
            URL url = getSiteURL();
533
            if (url != null){
534
                return manager.connectSite(this.authenticationRequesterConfig, url);
535
            }
536
        } catch (IOException ex) {
537
            LOGGER.warn("Can't connect to site '"+this.txtSiteURL.getText()+"'", ex);
538
        }
539
        return null;
540
    }
541

  
470 542
    private OnlineSite getSite() {
471 543
        if (site == null){
472
            try {
473
                OnlineManager manager = OnlineLocator.getOnlineManager();
474
                URL url = getSiteURL();
475
                if (url != null){
476
                    site = manager.connectSite(url);
477
                }
478
            } catch (IOException ex) {
479
                LOGGER.warn("Can't connect to site '"+this.txtSiteURL.getText()+"'", ex);
480
            }
544
            this.site = createSite();
481 545
        }
482 546
        return site;
483 547
    }
......
534 598
    }
535 599

  
536 600
    private void doChangeSiteURL() {
537
        String url_s = null;
601
        URL url = null;
538 602
        try {
539
            OnlineManager onlineManager = OnlineLocator.getOnlineManager();
540
            
541
            url_s = this.siteURLController.getText();
542
            if( StringUtils.isBlank(url_s) ) {
603
            OnlineManager onlineManager = OnlineLocator.getOnlineManager();            
604
            url = this.getSiteURL();
605
            if( url == null || this.authenticationRequesterConfig==null ) {
543 606
                DisposeUtils.dispose(this.site);
544 607
                this.site = null;
545 608
                return;
546 609
            }
547
            URL url = new URL(url_s);
548
            this.site = onlineManager.connectSite(url);
549
            this.siteURLController.addHistory(url_s);
610
            this.site = onlineManager.connectSite(this.authenticationRequesterConfig,url);
611
            this.siteURLController.addHistory(url.toString());
550 612
            doFillProjects();
551 613
            doUpdateComponents();
552 614
        } catch (Exception ex) {
553
            LOGGER.warn("Can't chage site url ("+url_s+").",ex);
615
            LOGGER.warn("Can't chage site url ("+url+").",ex);
554 616
        }        
555 617
    }
556 618
    
......
636 698
    private void clearMessage() {
637 699
        message(" ");
638 700
    }
701
    
702
    private void doChangeAuthentication() {
703
        LabeledValue<OnlineUserIdentificationRequesterFactory> item = (LabeledValue<OnlineUserIdentificationRequesterFactory>) this.cboAuthentication.getSelectedItem();
704
        if (item == null) {
705
            return;
706
        }
707
        OnlineUserIdentificationRequesterFactory factory = item.getValue();
708
        if (this.authenticationRequesterConfig!=null && StringUtils.equalsIgnoreCase(factory.getName(), this.authenticationRequesterConfig.getName())) {
709
            return;
710
        }
711
        URL url = null;
712
        try {
713
            url = this.getSiteURL();
714
            if( url == null ) {
715
                return;
716
            }
717
            this.authenticationRequesterConfig = factory.create(url.toString());
718
            OnlineManager onlineManager = OnlineLocator.getOnlineManager();
719
            if (url == null || this.authenticationRequesterConfig == null) {
720
                DisposeUtils.dispose(this.site);
721
                this.site = null;
722
                return;
723
            }
724
            this.site = onlineManager.connectSite(this.authenticationRequesterConfig, url);
725
            doFillProjects();
726
            doUpdateComponents();
727
        } catch (Exception ex) {
728
            LOGGER.warn("Can't chage autentication mode.", ex);
729
        }
730
    }
731

  
732
    private void doAuthenticationRequesterConfig() {
733
        if( this.getSiteURL()==null || this.authenticationRequesterConfig==null ) {
734
            return;
735
        }
736
        if( !this.authenticationRequesterConfig.hasIdentificationConfig() ) {
737
            return;
738
        }
739
        if( this.authenticationRequesterConfig.requestIdentificationConfig() ) {
740
            // reconstruimos el site con la nueva configuaracion.
741
            this.site = this.createSite();
742
        }
743
    }
744
    
745
    private void doAuthenticationTest() {
746
        if( this.getSiteURL()==null || this.authenticationRequesterConfig==null ) {
747
            LOGGER.warn("Can't run authentication test. Configure authentication.");
748
            return;
749
        }
750
        ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager();
751
        I18nManager i18n = ToolsLocator.getI18nManager();
752
        OnlineUserIdentificationRequester requester = this.authenticationRequesterConfig.createUserIdentificationRequester();
753
        if( requester==null ) {
754
//            LOGGER.warn("Can't run authentication test. Configure authentication.");
755
            dialogs.messageDialog(
756
                    i18n.getTranslation("_Cant_run_authentication_test") 
757
                            + "\n"
758
                            + i18n.getTranslation("_Review_the_authentication_settings"), 
759
                    i18n.getTranslation(i18n.getTranslation("_Authentication_testing")), 
760
                    JOptionPane.WARNING_MESSAGE
761
            );
762
            return;
763
        }
764
        if( !requester.requestIdentification() ) {
765
//            LOGGER.warn("Test authentication failed");
766
            dialogs.messageDialog(
767
                    i18n.getTranslation("_Test_authentication_failed"), 
768
                    i18n.getTranslation(i18n.getTranslation("_Authentication_testing")), 
769
                    JOptionPane.WARNING_MESSAGE
770
            );
771
            return;
772
        }
773
//        LOGGER.warn("Test authentication ok");
774
        dialogs.messageDialog(
775
                i18n.getTranslation("_Passed_the_authentication_test"), 
776
                i18n.getTranslation(i18n.getTranslation("_Authentication_testing")), 
777
                JOptionPane.WARNING_MESSAGE
778
        );
779
    }
639 780
}

Also available in: Unified diff