~ubuntu-branches/ubuntu/utopic/sikuli/utopic

« back to all changes in this revision

Viewing changes to sikuli-script/src/main/java/org/sikuli/script/FindFailedDialog.java

  • Committer: Bazaar Package Importer
  • Author(s): Gilles Filippini
  • Date: 2011-04-16 00:23:53 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20110416002353-cn79cto3c03z5jx1
Tags: 1.0~x~rc2-dfsg1-1
* New upstream release:
  + Redesigned user interface for Sikuli-IDE
  + Support for extensions for Sikuli Script

* debian/control, debian/copyright:
  The package is now maintained by the Debian Java maintainers Team

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * 
 
3
 */
 
4
package org.sikuli.script;
 
5
 
 
6
import java.awt.BorderLayout;
 
7
import java.awt.Component;
 
8
import java.awt.Frame;
 
9
import java.awt.Image;
 
10
import java.awt.event.ActionEvent;
 
11
import java.awt.event.ActionListener;
 
12
import java.awt.event.WindowAdapter;
 
13
import java.awt.event.WindowEvent;
 
14
import java.io.File;
 
15
import java.io.IOException;
 
16
 
 
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;
 
23
 
 
24
 
 
25
 
 
26
//TODO:
 
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. 
 
35
 
 
36
class FindFailedDialog extends JDialog implements ActionListener {
 
37
 
 
38
   /**
 
39
    * 
 
40
    */
 
41
 
 
42
   JButton retryButton;
 
43
   JButton skipButton;
 
44
   JButton abortButton;
 
45
 
 
46
   FindFailedResponse _response;
 
47
 
 
48
   public <PSC> FindFailedDialog(PSC  target){
 
49
      setModal(true);
 
50
      //super(new Frame(),true);
 
51
 
 
52
      JPanel panel = new JPanel();
 
53
      panel.setLayout(new BorderLayout());
 
54
 
 
55
      Component targetComp = createTargetComponent(target);
 
56
 
 
57
      panel.add(targetComp,BorderLayout.NORTH);
 
58
 
 
59
      JPanel buttons = new JPanel();
 
60
 
 
61
      retryButton = new JButton("Retry");
 
62
      retryButton.addActionListener(this);
 
63
 
 
64
      skipButton = new JButton("Skip");
 
65
      skipButton.addActionListener(this);
 
66
 
 
67
      abortButton = new JButton("Abort");
 
68
      abortButton.addActionListener(this);
 
69
 
 
70
      buttons.add(retryButton);
 
71
      buttons.add(skipButton);
 
72
      buttons.add(abortButton);
 
73
 
 
74
      panel.add(buttons,BorderLayout.SOUTH);
 
75
 
 
76
      add(panel);
 
77
      setDefaultCloseOperation(DISPOSE_ON_CLOSE);
 
78
 
 
79
 
 
80
      addWindowListener(new WindowAdapter(){
 
81
         public void windowClosing(WindowEvent e){
 
82
            _response = FindFailedResponse.ABORT;
 
83
            //dispose();
 
84
         }
 
85
      });
 
86
 
 
87
      //pack(); // don't pack, doing so messes up AlwaysOnTop
 
88
      //setLocationRelativeTo(null);
 
89
 
 
90
   }
 
91
 
 
92
   @Override
 
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;
 
100
      }
 
101
      dispose();
 
102
   }
 
103
 
 
104
   public FindFailedResponse getResponse(){
 
105
      return _response;
 
106
   }
 
107
 
 
108
 
 
109
   <PSC> Component createTargetComponent(PSC target){
 
110
 
 
111
      if( target instanceof Pattern ){
 
112
         Pattern p = (Pattern) target;
 
113
         JLabel c = new JLabel("Sikuli can not find pattern :" + p);
 
114
         return c;
 
115
      }
 
116
      else if( target instanceof String){
 
117
 
 
118
         String s = (String) target;
 
119
         try{
 
120
            String filename = (new ImageLocator()).locate((String)target);
 
121
 
 
122
            Image image = null;
 
123
            try {
 
124
               image = ImageIO.read(new File(filename));
 
125
            } catch (IOException e) {
 
126
            }
 
127
 
 
128
            JPanel p = new JPanel();
 
129
            p.setLayout(new BorderLayout());
 
130
            JLabel iconLabel = new JLabel();
 
131
            iconLabel.setIcon(new ImageIcon(image));
 
132
 
 
133
            JLabel c = new JLabel("Sikuli is unable to find the target image.");
 
134
 
 
135
            p.add(c,BorderLayout.PAGE_START);
 
136
            p.add(new JLabel((String)target));
 
137
            p.add(iconLabel,BorderLayout.PAGE_END);
 
138
            return p;
 
139
 
 
140
         }
 
141
         catch(IOException e){
 
142
            JLabel c = new JLabel("Sikuli can not find text :" + s);
 
143
            return c;
 
144
 
 
145
         }
 
146
      }
 
147
 
 
148
      return null;
 
149
   }
 
150
 
 
151
   @Override
 
152
   public void setVisible(boolean flag){
 
153
 
 
154
      if (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.
 
158
         toFront();
 
159
         setAlwaysOnTop(true);
 
160
         pack();
 
161
         setResizable(false);
 
162
         setLocationRelativeTo(this);
 
163
      }
 
164
 
 
165
      super.setVisible(flag);
 
166
   }
 
167
 
 
168
   public static void main(String[] args) {
 
169
 
 
170
      //    JFrame f = new JFrame();
 
171
      //FindFailedDialog fd = new FindFailedDialog("Test");
 
172
      FindFailedDialog fd = new FindFailedDialog("Test");
 
173
      fd.setVisible(true);
 
174
      //f.setVisible(true);
 
175
      //fd.setAlwaysOnTop(true);
 
176
      //fd.waitResponse();
 
177
 
 
178
      //    synchronized(f){
 
179
      //       try {
 
180
      //          f.wait();
 
181
      //       } catch (InterruptedException e) {
 
182
      //          e.printStackTrace();
 
183
      //       }
 
184
      //    }
 
185
 
 
186
 
 
187
      Debug.log("" + fd.getResponse());
 
188
   }
 
189
}