gt:           return Pair.create(resource, readChildren(resource));
context: /*
  GanttProject is an opensource project management tool. License: GPL3
  Copyright (C) 2003-2012 GanttProject Team
  
  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 3
  of the License, or (at your option) any later version.
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
  
  package net.sourceforge.ganttproject.document.webdav;
  
  import io.milton.http.exceptions.NotAuthorizedException;
  
  import java.awt.BorderLayout;
  import java.awt.Color;
  import java.awt.Component;
  import java.awt.Dimension;
  import java.awt.GridLayout;
  import java.awt.event.ActionEvent;
  import java.awt.event.KeyAdapter;
  import java.awt.event.KeyEvent;
  import java.awt.event.MouseAdapter;
  import java.awt.event.MouseEvent;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.util.List;
  import java.util.concurrent.ExecutionException;
  
  import javax.swing.Action;
  import javax.swing.BorderFactory;
  import javax.swing.Box;
  import javax.swing.JButton;
  import javax.swing.JComponent;
  import javax.swing.JLabel;
  import javax.swing.JOptionPane;
  import javax.swing.JPanel;
  import javax.swing.JScrollPane;
  import javax.swing.SpringLayout;
  import javax.swing.SwingUtilities;
  import javax.swing.SwingWorker;
  import javax.swing.UIManager;
  import javax.swing.event.ListSelectionEvent;
  import javax.swing.event.ListSelectionListener;
  
  import net.sourceforge.ganttproject.GPLogger;
  import net.sourceforge.ganttproject.IGanttProject;
  import net.sourceforge.ganttproject.action.CancelAction;
  import net.sourceforge.ganttproject.action.GPAction;
  import net.sourceforge.ganttproject.action.OkAction;
  import net.sourceforge.ganttproject.document.webdav.WebDavResource.WebDavException;
  import net.sourceforge.ganttproject.gui.UIFacade;
  import net.sourceforge.ganttproject.gui.UIUtil;
  import net.sourceforge.ganttproject.gui.options.OptionsPageBuilder;
  import net.sourceforge.ganttproject.gui.options.SpringUtilities;
  import net.sourceforge.ganttproject.language.GanttLanguage;
  import net.sourceforge.ganttproject.util.collect.Pair;
  
  import org.divxdede.swing.busy.JBusyComponent;
  import org.jdesktop.swingx.JXHyperlink;
  import org.jdesktop.swingx.JXList;
  
  import biz.ganttproject.core.option.BooleanOption;
  import biz.ganttproject.core.option.ChangeValueEvent;
  import biz.ganttproject.core.option.ChangeValueListener;
  import biz.ganttproject.core.option.DefaultStringOption;
  import biz.ganttproject.core.option.EnumerationOption;
  import biz.ganttproject.core.option.IntegerOption;
  import biz.ganttproject.core.option.ListOption;
  import biz.ganttproject.core.option.StringOption;
  
  import com.google.common.base.Objects;
  import com.google.common.base.Predicate;
  import com.google.common.base.Strings;
  import com.google.common.collect.Lists;
  
  /**
   * UI component for WebDAV operarions.
   *
   * @author dbarashev (Dmitry Barashev)
   */
  class GanttURLChooser {
    private static final GanttLanguage language = GanttLanguage.getInstance();
  
    private final StringOption myUsername;
  
    private final StringOption myPassword;
  
    private final StringOption myPath;
  
    private final IntegerOption myTimeout;
  
    private SelectionListener mySelectionListener;
  
    private final ListOption<WebDavServerDescriptor> myServers;
  
    private final MiltonResourceFactory myWebDavFactory;
  
    private final GPAction myReloadAction = new GPAction("fileChooser.reload") {
      @Override
      public void actionPerformed(ActionEvent event) {
        if (!isEnabled()) {
          return;
        }
        reloadFilesTable();
      }
    };
  
    private final GPAction myUpAction = new GPAction("fileChooser.up") {
      @Override
      public void actionPerformed(ActionEvent event) {
        try {
          WebDavResource collection = tableModel.getCollection();
          WebDavResource parent = collection.getParent();
          if (parent != null && parent.exists() && parent.isCollection()) {
            new ReloadWorker(parent).execute();
            myPath.setValue(new URL(parent.getUrl()).getPath());
          }
        } catch (WebDavException e) {
          showError(e);
        } catch (MalformedURLException e) {
          showError(e);
        }
      }
    };
  
    private final GPAction myLockAction = new GPAction("fileChooser.lock") {
      @Override
      public void actionPerformed(ActionEvent evt) {
        WebDavResource resource = getSelectedResource();
        if (resource == null) {
          return;
        }
        try {
          if (!resource.isLocked()) {
            resource.lock(myTimeout.getValue());
            reloadFilesTable();
          }
        } catch (WebDavException e) {
          showError(e);
        }
      }
    };
  
    private final GPAction myUnlockAction = new GPAction("fileChooser.unlock") {
      @Override
      public void actionPerformed(ActionEvent evt) {
        WebDavResource resource = getSelectedResource();
        if (resource == null) {
          return;
        }
        try {
          if (resource.isLocked()) {
            resource.unlock();
            reloadFilesTable();
          }
        } catch (WebDavException e) {
          showError(e);
        }
      }
    };
  
    private final GPAction myDeleteAction = new GPAction("fileChooser.delete") {
      @Override
      public void actionPerformed(ActionEvent evt) {
        final WebDavResource resource = getSelectedResource();
        if (resource == null) {
          return;
        }
        myUiFacade.showOptionDialog(JOptionPane.QUESTION_MESSAGE, GanttLanguage.getInstance().getText("fileChooser.delete.question"), new Action[] {
          new OkAction() {
            @Override
            public void actionPerformed(ActionEvent evt) {
              try {
                resource.delete();
              } catch (WebDavException e) {
                showError(e);
              }
            }
          }, CancelAction.EMPTY
        });
      }
    };
  
    private final FilesTableModel tableModel = new FilesTableModel();
  
    private JXList table;
  
    private JButton myLockButton;
  
    private final UIFacade myUiFacade;
  
    private final IGanttProject myProject;
  
    private JScrollPane myFilesComponent;
  
    private WebDavUri myInitialUri;
  
    private JLabel myPasswordLabel;
  
    private OkAction myOkAction;
  
    private final List<Runnable> myListenerRemovers = Lists.newArrayList();
  
    private JBusyComponent<JComponent> myBusyComponent;
  
    static interface SelectionListener {
      public void setSelection(WebDavResource resource);
    }
  
    GanttURLChooser(IGanttProject project, UIFacade uiFacade, ListOption<WebDavServerDescriptor> servers, WebDavUri currentUri, StringOption username,
        StringOption password, IntegerOption lockTimeoutOption, BooleanOption releaseLockOption,
        MiltonResourceFactory webDavFactory) {
      myProject = project;
      myUiFacade = uiFacade;
      myWebDavFactory = webDavFactory;
      myPath = new DefaultStringOption("path");
      myServers = servers;
      myUsername = username;
      myPassword = password;
      myTimeout = lockTimeoutOption;
      myInitialUri = currentUri;
  
      myListenerRemovers.add(myServers.addChangeValueListener(new ChangeValueListener() {
        @Override
        public void changeValue(ChangeValueEvent event) {
          myPath.setValue("");
          updateUsernameAndPassword();
          myReloadAction.actionPerformed(null);
        }
      }));
  
      myListenerRemovers.add(myUsername.addChangeValueListener(new ChangeValueListener() {
        @Override
        public void changeValue(ChangeValueEvent event) {
          if (myServers.getValue() != null) {
            myServers.getValue().username = myUsername.getValue();
          }
        }
      }));
  
      myListenerRemovers.add(myPassword.addChangeValueListener(new ChangeValueListener() {
        @Override
        public void changeValue(ChangeValueEvent event) {
          if (myServers.getValue() != null) {
            myServers.getValue().password = myPassword.getValue();
          }
        }
      }));
    }
  
    class ReloadWorker extends SwingWorker<Pair<WebDavResource, List<WebDavResource>>, Object> {
      private WebDavResource myResource;
  
      public ReloadWorker(WebDavResource resource) {
        myResource = resource;
        setProgressBar(true);
      }
      @Override
      protected Pair<WebDavResource, List<WebDavResource>> doInBackground() throws Exception {
        try {
          WebDavResource resource = myResource;
          if (resource.exists() && resource.isCollection()) {
  