com.objogate.wl.swing.driver.JFileChooserDriver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of windowlicker-swing Show documentation
Show all versions of windowlicker-swing Show documentation
This is the Windowlicker Swing library.
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 extends Component> parentOrOwner, Matcher super JFileChooser> 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 extends JTextComponent> 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());
}
}