Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: rcsdesign/AlertDialog.java


1   package rcsdesign;
2   
3   import java.awt.*;
4   import java.awt.event.*;
5   import java.util.*;
6   
7   /*
8    *
9    * AlertDialog
10   *
11   */
12  public class AlertDialog extends Dialog implements Runnable, ActionListener, WindowListener
13  {
14  
15    TextArea msgArea = null;
16    Button okButton = null;
17    FlowLayout lm = null;
18    Dimension d = new Dimension(480,240);
19    static public boolean debug_on = false;
20    static int count=0;
21  
22    Thread timeoutThread = null;
23    public boolean running = false;
24    public boolean done = false;
25  
26    public void run()
27    {
28      if(debug_on)
29        {
30    System.out.println("AlertDialog.timeoutThread started.\n");
31        }
32      try
33        {
34    for(int i = 0; i < 1000; i++)
35      {
36        Thread.sleep(10);
37        System.out.print(i/10.0+"%\r");
38      }
39    System.out.println("         ");
40        }
41      catch(InterruptedException ie)
42        {
43        }
44      catch(Exception e)
45        {
46    e.printStackTrace();
47        }
48      if(running)
49        {
50    //synchronized(this) { done = true; notifyAll(); };
51    done=true;
52    System.out.println("done=true;");
53    dispose();
54    count--;
55    System.out.println("AlertDialog timeout.");
56        }
57      running=false;
58    }
59  
60    public AlertDialog(Frame parent, String title, String message)
61    {
62      super(parent, false);
63      count++;
64      init(title,message);
65    }
66    
67    public void init(String title, String message)
68    {
69      setVisible(false);
70      setTitle(title);
71      lm = new FlowLayout();
72      setLayout(lm);
73      int text_length = 10;
74      int text_width = 50;
75    
76      /*
77      try
78        {
79    Font f = getFont();
80    int font_height = 10;
81    int font_width = 10;
82    if(null != f)
83      {
84        FontMetrics fm = getFontMetrics(f);
85        if(null != fm)
86          {
87      font_height = fm.getHeight();
88      if(font_height < 10)
89        {
90          font_height = 10;
91        }
92      int font_widths[] = fm.getWidths();
93      font_width = 0;
94      int nonzero_fontwidths = 0;
95      int fontwidth_sum = 0;
96      for(int i = 0; i < font_widths.length; i++)
97        {
98          if(font_widths[i] > 0)
99            {
100       nonzero_fontwidths++;
101       fontwidth_sum += font_widths[i];
102           }
103       }
104     font_width = fontwidth_sum/nonzero_fontwidths+1;
105     if(font_width < 10)
106       {
107         font_width = 10;
108       }
109         }
110     }
111   if(debug_on)
112     {
113       System.out.println("font_height = "+font_height);
114       System.out.println("font_width = "+font_width);
115     }
116   text_length = d.height/font_height;
117   if(text_length < 8)
118     {
119       text_length = 8;
120     }
121   text_width = d.width/font_width;
122   if(text_width < 20)
123     {
124       text_width = 20;
125     }
126       }
127     catch(Exception e)
128       {
129   e.printStackTrace();
130       }
131       */
132     
133     msgArea = new AutoSizedTextArea(message,d.width-20,d.height -100,100,20);
134     text_width = msgArea.getColumns();
135     text_length = msgArea.getRows();
136     if(debug_on)
137       {
138   System.out.println("text_length = "+text_length);
139   System.out.println("text_width = "+text_width);
140       }
141     if(message.length() > text_width)
142       {
143   StringTokenizer messageTokenizer = new StringTokenizer(message,"\r\n");
144   String new_message = "";
145   while(messageTokenizer.hasMoreTokens())
146     {
147       String line = messageTokenizer.nextToken();
148       if(line.length() > text_width)
149         {
150     for(int offset = text_width-4; offset < line.length(); offset+=(text_width-4))
151       {
152         line = line.substring(0,offset)+"- \\\n"+line.substring(offset);
153       }
154         }
155       new_message += line +"\n";
156     }
157   message = new_message;
158       }
159 
160     msgArea.setEditable(false);
161     add(msgArea);
162     okButton = new Button("OK");
163     add(okButton);
164     okButton.addActionListener(this);
165 
166     setSize(d);
167     int tries = 0;
168     if(debug_on)
169       {
170   System.out.println("Showing AlertDialog with message = "+message);
171       }
172     //timeoutThread = new Thread(this);
173     //timeoutThread.start();
174     running = true;
175     addWindowListener(this);
176     pack();
177   } 
178  
179   public void actionPerformed(ActionEvent evt)
180   {
181     if(debug_on)
182       {
183   System.out.println("AlertDialog.actionPerformed("+evt+")");
184       }
185     if(evt.getSource() == okButton)
186       {
187   //synchronized(this) { done = true; notifyAll(); };
188   done=true;
189   dispose();
190   count--;
191   running=false;
192   try
193     {
194       if(null != timeoutThread)
195         {
196     timeoutThread.interrupt();
197     timeoutThread = null;
198         }
199     }
200    catch(Exception e)
201     {
202       e.printStackTrace();
203     }
204   return;
205       }
206   }
207 
208   public Dimension getPreferredSize()
209   {
210     return d;
211   }
212 
213   public Dimension getMinimumSize()
214   {
215     return d;
216   }
217 
218    /*
219    * The functions windowOpened, windowClosing, windowClosed, windowActivated,
220    * windowDeactivated, windowIconified, and windowDeiconified are needed
221    * to implement WindowListener and basically replace handleEvent 
222    * from JDK 1.0.x
223    * 
224    */
225 
226   public void windowOpened(WindowEvent evt)
227   {
228   }
229   
230   public void windowClosing(WindowEvent evt)
231   {
232     if(debug_on)
233       {
234   System.out.println("windowClosing("+evt+")");
235       }
236     try
237       {
238   done = true;
239   dispose();
240   count--;
241   running=false;
242   try
243     {
244       if(null != timeoutThread)
245         {
246     timeoutThread.interrupt();
247     timeoutThread = null;
248         }
249     }
250    catch(Exception e)
251     {
252       e.printStackTrace();
253     }
254       }
255     catch(Exception e)
256       {
257   e.printStackTrace();
258       }
259   }
260 
261   public void windowClosed(WindowEvent evt)
262   {
263   }
264   
265   public void windowIconified(WindowEvent evt)
266   {
267   }
268 
269   public void windowDeiconified(WindowEvent evt)
270   {
271   }
272 
273   public void windowActivated(WindowEvent evt)
274   {
275   }
276   
277   public void windowDeactivated(WindowEvent evt)
278   {
279   }
280 
281   public static  void main(String args[])
282   {
283     Frame f = new Frame();
284     System.out.println("AlertDialog test program started.");
285     AlertDialog ad = new AlertDialog(f,"Test","TestMsg");
286     System.out.println("AlertDialog object created.");
287     ad.show();
288     System.out.println("AlertDialog object show() returned.");
289     while(!ad.done)
290       {
291   try 
292     {
293       Thread.sleep(100);
294     }
295   catch(Exception e)
296     {
297     }
298       }
299     System.out.println("ad.done");
300     System.exit(0);
301   }
302     
303     
304 }
305