summaryrefslogtreecommitdiff
path: root/java/ImageProcessing/framework/DialogManager.java
blob: cca0c037af180bc8bbefc1f2afb0aa5673e174ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package imaging.framework;

import java.awt.*;
import java.awt.image.*;
import java.applet.*;
import java.util.*;
import java.io.*;
import java.net.*;

class DialogManager
{
  public static void initialize (Applet parent)
  {
    codeBase_ = parent.getCodeBase ().toString ();
    aboutFrame_ = new AboutFrame ();
    helpFrame_ = new HelpFrame (codeBase_);
  }

  public static void popDialog (int type, String message)
  {
    Frame frame = null;
    
    switch (type)
      {
      case DialogType.ABOUT:
	aboutFrame_.show ();
	break;
      case DialogType.HELP:
	helpFrame_.show ();
	break;
      case DialogType.MALFORMED_URL:
      case DialogType.NOT_SUPPORTED:
      case DialogType.URL_NOT_FOUND:
	frame = new MessageFrame ("Error", message);
	break;
      case DialogType.NOT_YET_IMPLEMENTED:
	frame = new MessageFrame ("", message);
	break;
      }
    Dimension d = Toolkit.getDefaultToolkit ().getScreenSize ();
    frame.move ((d.width - frame.size ().width)/2,
		(d.height - frame.size ().height)/2);
    frame.show ();
  }

  private static String codeBase_ = "";
  private static AboutFrame aboutFrame_;
  private static HelpFrame helpFrame_;
}

class MessageFrame extends Frame
{
  public MessageFrame (String title, String message)
  {
    super (title);

    this.resize (message.length () * 8, 100);
    this.setLayout (new BorderLayout ());

    this.text_ = new TextField (message);
    this.text_.setEditable (false);

    Panel okButtonPanel = new Panel ();
    okButtonPanel.add (this.okButton_);
    okButtonPanel.resize (100, 100);
    
    this.add ("Center", this.text_);
    this.add ("South", okButtonPanel);
  }

  public boolean handleEvent (Event evt)
  {
    if (evt.id == Event.WINDOW_DESTROY)
      {
	this.dispose ();
	return true;
      }
    return super.handleEvent (evt);
  }

  // Handle all action events
  public boolean action (Event e, Object arg)
  {
    if (e.target instanceof Button)
      {
	if (e.target == this.okButton_)
	  {
	    this.dispose ();
	  }
	return true;
      }
    else
      return false;
  }

  private Button okButton_ = new Button (" ok ");
  private TextField text_ = null;
}

class AboutFrame extends Frame
{
  public AboutFrame ()
  {
    super ("About");
    this.resize (300,300);
    this.setLayout (new BorderLayout ());

    Panel okButtonPanel = new Panel ();
    okButtonPanel.add (this.okButton_);
    AboutFrameTextPanel textPanel = new AboutFrameTextPanel ();

    this.add ("Center", textPanel);    
    this.add ("South", okButtonPanel);
  }

  // Handle window destroy events
  public boolean handleEvent (Event evt)
  {
    if (evt.id == Event.WINDOW_DESTROY)
      {
	this.hide ();
	return true;
      }
    return super.handleEvent (evt);
  }

  // Handle all action events
  public boolean action (Event e, Object arg)
  {
    if (e.target instanceof Button)
      {
	if (e.target == this.okButton_)
	  {
	    this.hide ();
	  }
	return true;
      }
    else
      return false;
  }
  private Button okButton_ = new Button (" ok ");
}

class AboutFrameTextPanel extends Panel
{
  public void paint (Graphics g)
  {
    int width = this.size ().width;
    int height = this.size ().height;

    g.clearRect (0, 0, width, height);
    this.setBackground (Color.cyan);

    // First draw the title
    g.setFont (new Font ("TimesRoman", Font.BOLD | Font.ITALIC, 48));
    FontMetrics fontMetrics = g.getFontMetrics ();
    int x = (width - fontMetrics.stringWidth (title_))/2;
    int y = 100;

    Color color = g.getColor ();
    g.setColor (Color.orange);
    g.drawString(title_, x+2, y+2);
    g.setColor (color);
    g.drawString(title_, x, y);

    // Then draw author's name
    g.setFont (new Font ("TimesRoman", Font.ITALIC, 24));
    fontMetrics = g.getFontMetrics ();
    x = (width - fontMetrics.stringWidth (by_))/2;
    y += 50;
    g.drawString(by_, x, y);

    x = (width - fontMetrics.stringWidth (author_))/2;
    y += 50;
    g.drawString(author_, x, y);

    // Finally draw other information -- version number etc.
    g.setFont (new Font ("TimesRoman", Font.ITALIC, 18));
    fontMetrics = g.getFontMetrics ();
    x = (width - fontMetrics.stringWidth (info_))/2;
    y += 50;
    g.drawString(info_, x, y);
  }

  private String title_ = "MedJava";
  private String by_ = "by";
  private String author_ = "Prashant Jain";
  private String info_ = "Version 1.0";
}

class HelpFrame extends Frame
{
  public HelpFrame (String codeBase)
  {
    super ("Help");
    this.setBackground (Color.white);
    this.text_.setEditable (false);
    Font defaultFont = new Font ("TimesRoman", Font.PLAIN, 14);
    this.text_.setFont (defaultFont);

    try
      {
	URL url = new URL (codeBase + "../ImageProcessing/framework/help.conf");
	String delim = "\n";
	
	// Get the input stream and pipe it to a DataInputStream
	DataInputStream iStream = new DataInputStream (url.openStream ());
	
	// Keep reading the data until we are done
	String tempString = iStream.readLine ();
	while (tempString != null)
	  {
	    if (tempString.startsWith ("<START>"))
	      delim = "";
	    else if (tempString.startsWith ("<END>"))
	      delim = "\n";
	    else if (tempString.startsWith ("<TAB>"))
	      this.text_.appendText ("\t");
	    else if (tempString.startsWith ("<P>"))
	      this.text_.appendText ("\n");
	    else
	      {
		this.text_.appendText (tempString);
		this.text_.appendText (delim);
	      }
	    tempString = iStream.readLine ();		
	  }
      }
    catch (MalformedURLException e)
      {
	System.err.println (e);
      }
    catch (IOException e)
      {
	System.err.println (e);
      }
    
    this.resize (600,700);
    this.setLayout (new BorderLayout ());
    
    Panel okButtonPanel = new Panel ();
    okButtonPanel.add (this.okButton_);
    this.add ("South", okButtonPanel);
    this.add ("Center", this.text_);
  }

  // Handle window destroy events
  public boolean handleEvent (Event evt)
  {
    if (evt.id == Event.WINDOW_DESTROY)
      {
	this.hide ();
	return true;
      }
    return super.handleEvent (evt);
  }

  // Handle all action events
  public boolean action (Event e, Object arg)
  {
    if (e.target instanceof Button)
      {
	if (e.target == this.okButton_)
	  {
	    this.hide ();
	  }
	return true;
      }
    else
      return false;
  }

  private Vector helpInfo_ = new Vector ();
  private Button okButton_ = new Button (" ok ");
  private TextArea text_ = new TextArea ();
}