summaryrefslogtreecommitdiff
path: root/java/ImageProcessing/framework/FileBrowser.java
blob: 27245b1f2f4e3dde0dacb0b3f283b7c88e4ad3ba (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
package imaging.framework;

import java.awt.*;
import java.awt.image.*;
import java.net.*;
import java.applet.*;
import gjt.Separator;
import gjt.ComponentScroller;

class FileNode extends HierarchyAdapter
{
  public FileNode (String name, 
		   Icon icon, 
		   boolean root,
		   ImageApp app)
  {
    super (name, icon, root);
    this.app_ = app;
  }

  public FileNode (String name, 
		   Icon icon, 
		   ListFiles list,
		   ImageApp app)
  {
    super (name, icon);
    this.list_ = list;
    this.app_ = app;
  }

  public ImageApp app ()
  {
    return this.app_;
  }

  public void handleEvent ()
  {
    String s = null;
    String pString = "";
    Hierarchy p = this.getHierarchyParent ();

    while (p != null)
      {
	pString = p.getName () + pString;
	p = p.getHierarchyParent ();
      }

    if (pString.endsWith ("/"))
      s = "http://" +  pString + this.getName ();
    else
      s = "http://" + pString + "/" + this.getName ();

    System.out.println ("Opening: " + s);

    // If list is null, then it is a file, else it is a directory and
    // use list to get the rest of the directory.
    if (this.list_ == null)
      this.app_.openURL (s);            // It is a file
    else
      this.list_.listFiles (s, this);	// It is a directory. 
  }

  private ListFiles list_ = null;
  private ImageApp app_ = null;
}

class BrowserPanel extends Panel
{
  public BrowserPanel (ImageApp parent)
  {
    this.resize (300, 300);
    this.parent_ = parent;
    this.setLayout (new BorderLayout ());
  }

  public int initialize (String url, ListFiles list)
  {
    String directory = null;
    int index = -1;

    String pString = list.stripProtocolHeader (url);
    if (!pString.endsWith ("/"))
      pString = pString + "/";

    try
      {
	Icon dirIcon = new Icon (this.parent_.getCodeBase () + 
				 "../ImageProcessing/framework/" +
				 "file03.gif", (Applet) this.parent_);
	System.out.println (this.parent_.getCodeBase () + 
			    "../ImageProcessing/framework/" +
			    "file03.gif");
	this.root_ = new FileNode (pString,
				   dirIcon, true, this.parent_);
      }
    catch (MalformedURLException e)
      {
      }

    int count = list.listFiles (url, this.root_);
    //    System.out.println ("Count: " + count);
    if (count > 0)
      {
	// Add the root to the component scroller and then add the
	// component scroller to the panel.
	this.scroller_ = new ComponentScroller (this.root_);
	this.add ("Center", this.scroller_);
      }
    return count;
  }

  private FileNode root_ = null;
  private ImageApp parent_;
  private ComponentScroller scroller_;
}

class FileBrowser extends Frame
{
  public FileBrowser (String title, ImageApp parent)
  {
    super (title);
    this.resize (300, 300);
    this.browser_ = new BrowserPanel (parent);
    this.setLayout (new BorderLayout ());

    this.cancelButton_ = new Button (" cancel ");
    Panel buttonPanel = new Panel ();
    buttonPanel.add (this.cancelButton_);
    buttonPanel.resize (100, 100);

    Panel southPanel = new Panel ();
    southPanel.setLayout (new BorderLayout ());
    southPanel.add ("North", new Separator ());
    southPanel.add ("South", buttonPanel);
    this.add ("South", southPanel);
    this.add ("Center", this.browser_);
  }

  public int initialize (String url, ListFiles list)
  {
    return this.browser_.initialize (url, list);
  }

  // Handle window destroy events
  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.cancelButton_)
	  {
	    this.dispose ();
	  }
	validate ();
	return true;
      }
    else
      return false;
  }

  private Button cancelButton_;;
  private BrowserPanel browser_;
}