All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.objogate.wl.swing.driver.JFileChooserDriver Maven / Gradle / Ivy

The newest version!
package com.objogate.wl.swing.driver;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.text.JTextComponent;
import java.awt.Component;
import java.awt.HeadlessException;
import java.io.File;
import java.io.IOException;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import com.objogate.exception.Defect;
import com.objogate.wl.Probe;
import com.objogate.wl.Prober;
import com.objogate.wl.swing.ComponentSelector;
import com.objogate.wl.swing.UI;
import com.objogate.wl.swing.gesture.GesturePerformer;

public class JFileChooserDriver extends JComponentDriver {
    @SuppressWarnings("unchecked")
    public JFileChooserDriver(ComponentDriver parentOrOwner, Matcher matcher) {
        super(parentOrOwner, JFileChooser.class, matcher);
    }

    public JFileChooserDriver(GesturePerformer gesturePerformer, ComponentSelector selector, Prober prober) {
        super(gesturePerformer, selector, prober);
    }
    
    private FileChooserUIDriver getRelevantComponentDriver() {
        if (UI.is(UI.METAL)) {
            return new MetalFileChooserUIDriver(this);
        } else if (UI.is(UI.WINDOWS)) {
            return new WindowsFileChooserUIDriver(this);
        } else if (UI.is(UI.GTK)) {
            return new GTKFileChooserUIDriver(this);
        } else if (UI.is(UI.AQUA)) {
            return new AquaFileChooserUIDriver(this);
        }

        throw new UnsupportedOperationException("not known about or supported yet ");
    }

    public void cancel() {
        chooserUI().cancel();
    }

    public void approve() {
        chooserUI().approve();
    }

    public void enterManually(String someText) {
        JTextComponentDriver textComponentDriver = chooserUI().textBox();
        textComponentDriver.moveMouseToCenter();
        textComponentDriver.selectAll();
        textComponentDriver.typeText(someText);
    }

    private FileChooserUIDriver chooserUI() {
        isShowingOnScreen();
        return getRelevantComponentDriver();
    }

    public void desktop() {
        chooserUI().desktop();
    }

    public void documents() {
        chooserUI().documents();
    }

    public void home() {
        chooserUI().home();
    }

    public void upOneFolder() {
        chooserUI().upOneFolder();
    }

    public void intoDir(String name) {
        chooserUI().intoDir(name);
    }

    public void createNewFolder(String folderName) {
        chooserUI().createNewFolder(folderName);
    }

    public void selectFile(String fileName) {
        chooserUI().selectFile(fileName);
    }

    public void currentDirectory(final File expectedDir) {
        currentDirectory(new TypeSafeMatcher() {
            @Override
            public boolean matchesSafely(File file) {
                try {
                    return file.getCanonicalPath().equals(expectedDir.getCanonicalPath());
                } catch (IOException e) {
                    throw new Defect("Cannot get path of directory " + expectedDir);
                }
            }

            public void describeTo(Description description) {
                try {
                    description.appendText(expectedDir.getCanonicalPath());
                } catch (IOException e) {
                    throw new Defect("Cannot get path of directory " + expectedDir);
                }
            }
        });
    }

    public void currentDirectory(final Matcher matcher) {
        check(new Probe() {
            File currentDirectory;

            public void probe() {
                currentDirectory = component().component().getCurrentDirectory();
            }

            public boolean isSatisfied() {
                return matcher.matches(currentDirectory);
            }

            public void describeTo(Description description) {
                description.appendText("current directory matches ").
                        appendDescriptionOf(matcher);
            }
            
            public void describeFailureTo(Description description) {
                description.appendText("current directory was ")
                        .appendValue(currentDirectory);
            }
        });
    }

    public static JFrame rootFrameFor(Component parentComponent)
            throws HeadlessException 
    {
        if (parentComponent == null) throw new IllegalArgumentException("Dialog needs a parent");
        if (parentComponent instanceof JFrame) return (JFrame) parentComponent;
        return rootFrameFor(parentComponent.getParent());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy