4
package org.sikuli.script;
6
import java.awt.BorderLayout;
7
import java.awt.Component;
10
import java.awt.event.ActionEvent;
11
import java.awt.event.ActionListener;
12
import java.awt.event.WindowAdapter;
13
import java.awt.event.WindowEvent;
15
import java.io.IOException;
17
import javax.imageio.ImageIO;
18
import javax.swing.ImageIcon;
19
import javax.swing.JButton;
20
import javax.swing.JDialog;
21
import javax.swing.JLabel;
22
import javax.swing.JPanel;
27
//- (done) close --> abort
28
//- (done) keep the dialog box always on top
29
//- (done) display target image
30
//- display oversized target images at a proper scale
31
//- beautify the layout
32
//- (done) disable resizing
33
//- ensure the dialog disappears before find is reattempted so that it won't find the
34
//target image in the dialog box.
36
class FindFailedDialog extends JDialog implements ActionListener {
46
FindFailedResponse _response;
48
public <PSC> FindFailedDialog(PSC target){
50
//super(new Frame(),true);
52
JPanel panel = new JPanel();
53
panel.setLayout(new BorderLayout());
55
Component targetComp = createTargetComponent(target);
57
panel.add(targetComp,BorderLayout.NORTH);
59
JPanel buttons = new JPanel();
61
retryButton = new JButton("Retry");
62
retryButton.addActionListener(this);
64
skipButton = new JButton("Skip");
65
skipButton.addActionListener(this);
67
abortButton = new JButton("Abort");
68
abortButton.addActionListener(this);
70
buttons.add(retryButton);
71
buttons.add(skipButton);
72
buttons.add(abortButton);
74
panel.add(buttons,BorderLayout.SOUTH);
77
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
80
addWindowListener(new WindowAdapter(){
81
public void windowClosing(WindowEvent e){
82
_response = FindFailedResponse.ABORT;
87
//pack(); // don't pack, doing so messes up AlwaysOnTop
88
//setLocationRelativeTo(null);
93
public void actionPerformed(ActionEvent e) {
94
if (retryButton == e.getSource()){
95
_response = FindFailedResponse.RETRY;
96
}else if (abortButton == e.getSource()){
97
_response = FindFailedResponse.ABORT;
98
}else if (skipButton == e.getSource()){
99
_response = FindFailedResponse.SKIP;
104
public FindFailedResponse getResponse(){
109
<PSC> Component createTargetComponent(PSC target){
111
if( target instanceof Pattern ){
112
Pattern p = (Pattern) target;
113
JLabel c = new JLabel("Sikuli can not find pattern :" + p);
116
else if( target instanceof String){
118
String s = (String) target;
120
String filename = (new ImageLocator()).locate((String)target);
124
image = ImageIO.read(new File(filename));
125
} catch (IOException e) {
128
JPanel p = new JPanel();
129
p.setLayout(new BorderLayout());
130
JLabel iconLabel = new JLabel();
131
iconLabel.setIcon(new ImageIcon(image));
133
JLabel c = new JLabel("Sikuli is unable to find the target image.");
135
p.add(c,BorderLayout.PAGE_START);
136
p.add(new JLabel((String)target));
137
p.add(iconLabel,BorderLayout.PAGE_END);
141
catch(IOException e){
142
JLabel c = new JLabel("Sikuli can not find text :" + s);
152
public void setVisible(boolean flag){
155
// These can not be called in the constructor.
156
// Doing so somehow made it impossible to keep
157
// the dialog always on top.
159
setAlwaysOnTop(true);
162
setLocationRelativeTo(this);
165
super.setVisible(flag);
168
public static void main(String[] args) {
170
// JFrame f = new JFrame();
171
//FindFailedDialog fd = new FindFailedDialog("Test");
172
FindFailedDialog fd = new FindFailedDialog("Test");
174
//f.setVisible(true);
175
//fd.setAlwaysOnTop(true);
181
// } catch (InterruptedException e) {
182
// e.printStackTrace();
187
Debug.log("" + fd.getResponse());