ProgressMonitorInputStream test

A

Andrew Thompson

ProgressMonitorInputStream: "..a progress monitor to monitor
the progress of reading the input stream. If it's taking a
while, a ProgressDialog will be popped up to inform the user."

Sounds neat, huh? Wrap a plain old Inputstream in one of
these, and the user is kept informed. But I cannot get
it to work!

The code (below) uses it, and offers two URL's, the first
being the entire class list of the Java 6 API, the second
being the trivially small content of the frames based
index page (it would be large if the JEP was interpreting
the input as HTML, but it is displayed as plain text).

Yet while loading the first URL here, I get:-

Wed Sep 26 22:14:42 ACT 2007 Start load
Wed Sep 26 22:18:54 ACT 2007 End load

Over 4 minutes duration, but no appearance of the progress
dialog!

What does 'a while' mean?
Does this code work (pop a progress dialog) for you?
Assuming 'no' for the previous question.
Are there variants of this code that *do* pop the dialog?

If you have an very fast connection, try setting
the URL to point at something much bigger, the
JComboBox is editable.

Thoughts, comments, bug report URLs and solutions
all welcome.

<sscce>
// this is what we are testing..
import javax.swing.ProgressMonitorInputStream;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
import javax.swing.SwingUtilities;

import java.io.IOException;
import java.io.InputStream;

import java.net.URL;
import java.net.MalformedURLException;

import java.util.Date;

class TestStreamProgress extends JFrame {

JEditorPane output;

TestStreamProgress() throws MalformedURLException {
super("ProgressMonitorInputStream - Test");
output = new JEditorPane();
output.setText("Choose a resource to load");

this.getContentPane().add(
new JScrollPane(output)
);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setSize(600,400);
setLocationRelativeTo(null);
setVisible(true);

String[] resourceLocator = {
"http://java.sun.com/javase/6"+
"/docs/api/allclasses-frame.html",
"http://java.sun.com/javase/6/"+
"docs/api/index.html", // a small resource..
"other (type URL)"
};
JComboBox resourceSelector =
new JComboBox(resourceLocator);
resourceSelector.setEditable(true);
JOptionPane.showMessageDialog(null, resourceSelector);
String resource =
(String)resourceSelector.getSelectedItem();
URL url = new URL( resource );
loadResource(url);
}

public void loadResource(final URL location) {
output.setText("Loading..");
Thread t = new Thread() {
public void run() {
try {
final InputStream is = location.openStream();
ProgressMonitorInputStream pmis =
new ProgressMonitorInputStream(
output,
"Loading..",
is
);
System.out.println( new Date() +
" \tStart load" );
output.read( pmis, null );
System.out.println( new Date() +
" \tEnd load" );
} catch(Exception e) {
e.printStackTrace();
}
}
};
SwingUtilities.invokeLater(t);
}

public static void main(String[] args) {
Thread t = new Thread() {
public void run() {
try {
TestStreamProgress tsp =
new TestStreamProgress();
} catch(MalformedURLException e) {
e.printStackTrace();
}
}
};
SwingUtilities.invokeLater(t);
}
}
</sscce>
 
A

Andrew Thompson

Andrew said:
ProgressMonitorInputStream: ...
Thoughts, comments, bug report URLs ...

A search of the bug DB on ProgressMonitorInputStream
shows the following hits:

<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4075058>
Mentioned as an example in support of interruptible streams.

<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4140706>
One string in a list data object, not otherwise used.

<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4209604>
Seems to have been used inside the EDT.
Solution - don't block the EDT. 'Closed, will not be fixed'.

<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6456237>
"Remove synchronized keyword from method declarations in Swing"
PMIS is listed as one of the classes that uses it. I cannot
see how removing the synchronized keyword would fix this,
but I might be wrong.

<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5089193>
"JTextArea.setText() wastes megabytes of memory!"
Uh-huh. This is a little more informative in that it gets
the underlying ProgressMonitor* and adjusts the params.
I don't know how I missed the getProgressMonitor()
method! (Except of course, the obvious answer that I
am a moron who does not take enough care to RTFM.)
Will check this out further.
(though the actual bug report does not relate to PMIS,
and is closed as a duplicate).

* This has answered one of my questions, namely..
What does 'a while' mean?

2.5 seconds is the default. (2 secs. pass before it even
considers appearing, then, if the read is expected to take
less than half a second, it will not pop up).

These questions are still open..
Does this code work (pop a progress dialog) for you?
Assuming 'no' for the previous question.
Are there variants of this code that *do* pop the dialog?

..continuing the investigation.
 
A

Andrew Thompson

Andrew said:

Finally thought to trawl through the Java Tutorial and
see what it had to say about the PMIS.

The tutorial examples work nicely. They put the 'long'
task into a sub-class of SwingWorker and implement
propertyChangeListeners in the GUI. Quite different to
the way I was going about it.

This thread is resolved.

Final Score:
Java Tutorial: 1 | Me: 0
 
A

Andrew Thompson

Roedy said:
I have not used that particular feature, but I got progress bars to
work fine using the other features.
...
Yeah, I had previously used a JProgresBar in The Giffer
with good effect. The advantage is that I was accessing
the API at a slightly 'lower' level than is offered by the
ProgressMonitorInputStream. The technique I outlined
in the first post works* for JPB.

* With a few additional lines to manually update
the progress.

Thanks for your attention, and the links.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200709/1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top