package bluej.groupwork.actions;

import java.io.File;

import bluej.Config;
import bluej.groupwork.Repository;
import bluej.groupwork.TeamSettingsController;
import bluej.groupwork.TeamUtils;
import bluej.groupwork.TeamworkCommand;
import bluej.groupwork.TeamworkCommandResult;
import bluej.groupwork.ui.ModuleSelectDialog;
import bluej.pkgmgr.Import;
import bluej.pkgmgr.Package;
import bluej.pkgmgr.PkgMgrFrame;
import bluej.pkgmgr.Project;
import bluej.utility.Debug;
import bluej.utility.DialogManager;
import bluej.utility.FileUtility;
import bluej.utility.FXWorker;
import bluej.utility.javafx.FXPlatformConsumer;

import threadchecker.OnThread;
import threadchecker.Tag;


| An action to perform a checkout of a module in CVS. The module is checked | out into some directory (chosen by the user) and then opened as a BlueJ | project. | | @author Kasper | @OnThread(Tag.FXPlatform) public class CheckoutAction extends TeamAction{ public CheckoutAction() { super("team.checkout", true); } public void actionPerformed(PkgMgrFrame oldFrame) { final TeamSettingsController tsc = new TeamSettingsController(new File(".").getAbsoluteFile()); if (tsc.getTeamSettingsDialog().showAndWait().isPresent()) { Repository repository = tsc.trytoEstablishRepository(true); if (repository == null) { return; } FXPlatformConsumer<File> finishCheckout = projectDir -> { PkgMgrFrame newFrame; if (oldFrame.isEmptyFrame()) { newFrame = oldFrame; } else { newFrame = PkgMgrFrame.createFrame(); newFrame.setVisible(true); } new CheckoutWorker(newFrame, repository, projectDir, tsc).start(); }; if (!tsc.isDVCS()) { ModuleSelectDialog moduleDialog = new ModuleSelectDialog(oldFrame::getFXWindow, repository); moduleDialog.setLocationRelativeTo(oldFrame.getFXWindow()); String moduleName = moduleDialog.showAndWait().orElse(null); if (moduleName == null) return; File parentDir = FileUtility.getSaveProjectFX(oldFrame.getProject(), oldFrame.getFXWindow(), Config.getString("team.checkout.filechooser.title")); if (parentDir == null) return; if (Package.isPackage(parentDir)) { Debug.message("Attempted to checkout a project into an existing project: " + parentDir); DialogManager.showErrorFX(null, "team-cannot-import-into-existing-project"); return; } finishCheckout.accept(new File(parentDir, moduleName)); } else { File projectDir = FileUtility.getSaveProjectFX(oldFrame.getProject(),oldFrame.getFXWindow(), Config.getString("team.checkout.DVCS.filechooser.title")); if (projectDir == null) return; if (Package.isPackage(projectDir)) { Debug.message("Attempted to checkout a project into an existing project: " + projectDir); DialogManager.showErrorFX(null, "team-cannot-import-into-existing-project"); return; } finishCheckout.accept(projectDir); } } }
| A worker to perform the checkout operation. | | @author Davin McCall | private class CheckoutWorker extends FXWorker { private Repository repository; private PkgMgrFrame newFrame; private File projDir; private TeamSettingsController tsc; private TeamworkCommandResult response; private boolean failed = true; public CheckoutWorker(PkgMgrFrame newFrame, Repository repository, File projDir, TeamSettingsController tsc) { this.newFrame = newFrame; this.repository = repository; this.projDir = projDir; this.tsc = tsc; }
| | Get the files from the repository. | @see bluej.utility.FXWorker#construct() | @OnThread(Tag.Worker) public Object construct() { newFrame.setStatus(Config.getString("team.checkingout")); newFrame.startProgress(); TeamworkCommand checkoutCmd = repository.checkout(projDir); response = checkoutCmd.getResult(); failed = response.isError(); newFrame.stopProgress(); if (! failed) { newFrame.setStatus(Config.getString("team.checkedout")); } newFrame.stopProgress(); return response; }
| | Now open the directory as a BlueJ project. | @see bluej.utility.FXWorker#finished() | public void finished() { if (! failed) { if (! Project.isProject(projDir.toString())) { if (! Import.convertNonBlueJ(newFrame::getFXWindow, projDir)) { cleanup(); return; } } Project project = Project.openProject(projDir.toString()); project.setTeamSettingsController(tsc); Package initialPackage = project.getPackage(project.getInitialPackageName()); newFrame.openPackage(initialPackage, newFrame); } else { TeamUtils.handleServerResponseFX(response, newFrame.getFXWindow()); cleanup(); } }
| Clean up after failed checkout. | public void cleanup() { deleteDirectory(projDir); projDir.delete(); newFrame.doClose(true, false); }
| Deletes all files and subdirectories inside a directory | @param dir | private void deleteDirectory(File dir) { if (dir != null && dir.listFiles() != null) { for (File f : dir.listFiles()) { if (f.isDirectory()) { deleteDirectory(f); } f.delete(); } } } public void abort() { } } }
top, use, map, class CheckoutAction

.   CheckoutAction
.   actionPerformed

top, use, map, class CheckoutWorker

.   CheckoutWorker
.   construct
.   finished
.   cleanup
.   deleteDirectory
.   abort




229 neLoCode + 15 LoComm