< prev index next >
src/org/netbeans/jemmy/operators/JFileChooserOperator.java
Print this page
*** 35,51 ****
--- 35,53 ----
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JList;
+ import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.ListModel;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileSystemView;
import javax.swing.filechooser.FileView;
import javax.swing.plaf.FileChooserUI;
+ import javax.swing.table.TableModel;
import org.netbeans.jemmy.ComponentChooser;
import org.netbeans.jemmy.ComponentSearcher;
import org.netbeans.jemmy.JemmyException;
import org.netbeans.jemmy.JemmyProperties;
*** 57,66 ****
--- 59,69 ----
import org.netbeans.jemmy.Waiter;
/**
*
* Class provides methods to cover main JFileChooser component functionality.
+ * Supports choosers using either JList or JTable as the component showing files.
*
* @author Alexandre Iline (alexandre.iline@oracle.com)
*
*/
public class JFileChooserOperator extends JComponentOperator
*** 328,360 ****
}
}));
}
/**
! * Returns file list.
*
! * @return a list being used to display directory content.
*/
! public JList<?> getFileList() {
! return ((JList) innerSearcher.
findComponent(new ComponentChooser() {
@Override
public boolean checkComponent(Component comp) {
return (comp != null
! && comp instanceof JList);
}
@Override
public String getDescription() {
! return "JList";
}
@Override
public String toString() {
return "JFileChooserOperator.getFileList.ComponentChooser{description = " + getDescription() + '}';
}
! }));
}
/**
* Pushes approve button.
*/
--- 331,363 ----
}
}));
}
/**
! * Returns either a JList or JTable, depending on the implementation.
*
! * @return a component being used to display directory content.
*/
! public Component getFileList() {
! return innerSearcher.
findComponent(new ComponentChooser() {
@Override
public boolean checkComponent(Component comp) {
return (comp != null
! && (comp instanceof JList || comp instanceof JTable));
}
@Override
public String getDescription() {
! return "JList or JTable used to show list of files";
}
@Override
public String toString() {
return "JFileChooserOperator.getFileList.ComponentChooser{description = " + getDescription() + '}';
}
! });
}
/**
* Pushes approve button.
*/
*** 443,460 ****
* @param clickCount click count
*/
public void clickOnFile(int index, int clickCount) {
getQueueTool().waitEmpty();
output.printTrace("Click " + Integer.toString(clickCount)
! + "times on " + Integer.toString(index)
+ "`th file in JFileChooser\n : "
+ toStringSource());
- JListOperator listOper = new JListOperator(getFileList());
waitPainted(index);
listOper.copyEnvironment(this);
listOper.setOutput(output.createErrorOutput());
listOper.clickOnItem(index, clickCount);
}
/**
* Clicks on file in the list.
*
--- 446,472 ----
* @param clickCount click count
*/
public void clickOnFile(int index, int clickCount) {
getQueueTool().waitEmpty();
output.printTrace("Click " + Integer.toString(clickCount)
! + " times on " + Integer.toString(index)
+ "`th file in JFileChooser\n : "
+ toStringSource());
waitPainted(index);
+ Component list = getFileList();
+ System.out.println(list);
+ if(list instanceof JList) {
+ JListOperator listOper = new JListOperator((JList) list);
listOper.copyEnvironment(this);
listOper.setOutput(output.createErrorOutput());
listOper.clickOnItem(index, clickCount);
+ } else {
+ JTableOperator tableOper = new JTableOperator((JTable) list);
+ tableOper.copyEnvironment(this);
+ tableOper.setOutput(output.createErrorOutput());
+ tableOper.clickOnCell(index, 0, clickCount);
+ }
}
/**
* Clicks on file in the list.
*
*** 462,472 ****
* @param comparator a comparator defining string comparision criteria
* @param clickCount click count
*/
public void clickOnFile(String file, StringComparator comparator, int clickCount) {
output.printTrace("Click " + Integer.toString(clickCount)
! + "times on \"" + file
+ "\" file in JFileChooser\n : "
+ toStringSource());
clickOnFile(findFileIndex(file, comparator), clickCount);
}
--- 474,484 ----
* @param comparator a comparator defining string comparision criteria
* @param clickCount click count
*/
public void clickOnFile(String file, StringComparator comparator, int clickCount) {
output.printTrace("Click " + Integer.toString(clickCount)
! + " times on \"" + file
+ "\" file in JFileChooser\n : "
+ toStringSource());
clickOnFile(findFileIndex(file, comparator), clickCount);
}
*** 587,601 ****
* @see #clickOnFile
*/
public void selectFile(String file, StringComparator comparator) {
getQueueTool().waitEmpty();
int index = findFileIndex(file, comparator);
! JListOperator listOper = new JListOperator(getFileList());
! waitPainted(index);
listOper.copyEnvironment(this);
listOper.setOutput(output.createErrorOutput());
listOper.setSelectedIndex(index);
}
/**
* Selects a file curently in the list.
*
--- 599,620 ----
* @see #clickOnFile
*/
public void selectFile(String file, StringComparator comparator) {
getQueueTool().waitEmpty();
int index = findFileIndex(file, comparator);
! Component list = getFileList();
! if(list instanceof JList) {
! JListOperator listOper = new JListOperator((JList) list);
listOper.copyEnvironment(this);
listOper.setOutput(output.createErrorOutput());
listOper.setSelectedIndex(index);
+ } else {
+ JTableOperator tableOper = new JTableOperator((JTable) list);
+ tableOper.copyEnvironment(this);
+ tableOper.setOutput(output.createErrorOutput());
+ tableOper.changeSelection(index, 0, false, false);
+ }
}
/**
* Selects a file curently in the list.
*
*** 747,772 ****
*
* @return a number of items in the file list.
*/
public int getFileCount() {
waitPainted(-1);
! return getFileList().getModel().getSize();
}
/**
* Return files currently displayed.
*
* @return an array of items from the file list.
*/
public File[] getFiles() {
waitPainted(-1);
! ListModel<?> listModel = getFileList().getModel();
File[] result = new File[listModel.getSize()];
for (int i = 0; i < listModel.getSize(); i++) {
result[i] = (File) listModel.getElementAt(i);
}
return result;
}
/**
* Waits for the file list to have required number of items.
*
--- 766,802 ----
*
* @return a number of items in the file list.
*/
public int getFileCount() {
waitPainted(-1);
! Component list = getFileList();
! return (list instanceof JList) ? ((JList)list).getModel().getSize() : ((JTable)list).getModel().getRowCount();
}
/**
* Return files currently displayed.
*
* @return an array of items from the file list.
*/
public File[] getFiles() {
waitPainted(-1);
! Component list = getFileList();
! if(list instanceof JList) {
! ListModel<?> listModel = ((JList)list).getModel();
File[] result = new File[listModel.getSize()];
for (int i = 0; i < listModel.getSize(); i++) {
result[i] = (File) listModel.getElementAt(i);
}
return result;
+ } else {
+ TableModel listModel = ((JTable)list).getModel();
+ File[] result = new File[listModel.getRowCount()];
+ for (int i = 0; i < listModel.getRowCount(); i++) {
+ result[i] = (File) listModel.getValueAt(i, 0);
+ }
+ return result;
+ }
}
/**
* Waits for the file list to have required number of items.
*
*** 1485,1503 ****
////////////////////////////////////////////////////////
private void waitPainted(int index) {
Waiter<Rectangle, Integer> drawingWaiter = new Waiter<>(new Waitable<Rectangle, Integer>() {
@Override
public Rectangle actionProduced(Integer param) {
! JList<?> list = getFileList();
! int last_one = list.getModel().getSize() - 1;
! if (last_one == -1) {
return new Rectangle();
}
int current = (param != null) ? param : 0;
try {
! if (list.getCellBounds(current, current) != null) {
! return list.getCellBounds(last_one, last_one);
} else {
return null;
}
} catch (NullPointerException e) {
//sometimes thrown from list.getCellBounds when item exists but not painted
--- 1515,1541 ----
////////////////////////////////////////////////////////
private void waitPainted(int index) {
Waiter<Rectangle, Integer> drawingWaiter = new Waiter<>(new Waitable<Rectangle, Integer>() {
@Override
public Rectangle actionProduced(Integer param) {
! Component list = getFileList();
! int size = (list instanceof JList) ? ((JList) list).getModel().getSize() :
! ((JTable)list).getModel().getRowCount();
! if (size == 0) {
return new Rectangle();
}
int current = (param != null) ? param : 0;
try {
! if(list instanceof JList)
! if (((JList) list).getCellBounds(current, current) != null) {
! return ((JList) list).getCellBounds(size - 1, size - 1);
! } else {
! return null;
! }
! else
! if (((JTable)list).getCellRect(current, 0, false) != null) {
! return ((JTable)list).getCellRect(size - 1, 0, false);
} else {
return null;
}
} catch (NullPointerException e) {
//sometimes thrown from list.getCellBounds when item exists but not painted
< prev index next >