Categories
discuss

Prevent “title” attribute of parent element from causing a browser flyover

The good browsers all work such that an empty “title” attribute for an element means, “don’t bother to show a title flyover here”. That makes sense, as a little white flyover with nothing in it (edit — or nothing but a space character) is, for most people, completely useless.

The designers of IE do not agree.

My problem is that I’ve got a little “what’s this?” mechanism on a site that involves an absolutely-positioned <div> containing a little graphic of a question mark. That <div> has a “title” attribute with a question like, “What does the ‘History Eraser Button’ do?” When you click on the question mark, a little “Help” bubble pops out and you can read about the topic. The idea of the “title” is that if the user mouses over the question mark, they see a question that’s (hopefully) one they might be wondering about.

Well the problem with the “title” is that the pop-out “help” balloon is inside the absolutely-positioned <div> so that it is correctly situated on the page. In other words, the absolutely-positioned <div> just has “position: absolute”, but it’s allowed to be dropped on the page in the “right” place without any offset computation. The “help” balloon is thus in the right place sort-of automatically. But: that “title” on the parent <div> is pesky because the browser pops it up after the balloon is open. Why? because the “help” balloon is lexically contained in the outer <div>, even though the outer <div> is just a little bitty thing with a question mark in it.

Thus solution #1 was to give the “help” balloon <div> its own “title” attribute, with nothing in it (edit — I got that wrong; it’s not nothing in the title, it’s a space character). That works great, except in IE. In IE now, that empty “title” attribute causes the browser to put up a little blank rectangle. Helpful.

I could of course fiddle with the Javascript and just nuke the “title” attribute off the parent <div> while the balloon is showing, but I’m curious about possible ways to “override” the parent element “title” in IE that can be done with nothing but markup. If it’s not possible, then oh well.

Simple demo page: http://gutfullofbeer.net/title.html (try it with IE 7 or 8 to see the little empty white box)

edit — ha ha ha – when I change my code so that I explicitly null out the “title” attributes on all the parent elements, IE shows a flyover with the word “null” in it 🙂

Answer

I ended up augmenting my existing code that opens/closes the “help” bubble so that it runs up the chain of parent elements and stashes the “title” code with the jQuery “data” facility. When the bubble closes, the titles are restored.

Categories
discuss

How to detect Swing thread policy violations

I am looking for an automatic way to detect violations of the Swing’s single threaded policy in my code. I’m looking for something along the lines of some AOP code you drop into the VM while the swing app is running and have it log any places where a swing component is modified outside of the EDT.

I’m not an AOP guy but I would imagine creating an AOP proxy around every java.swing.* class which looks like

AOP_before(Method m, Object args[]) {
 if (!isEventDispatchThread(Thread.currentThread()) {
  logStack(new RuntimeException("violation!"));
 }

 invoke(m, args);
}

Anyone know of a project or utility that does this?

Answer

I haven’t used this particular one, but this CheckThreadViolationRepaintManager should do the trick.

It does have the requirement of adding:

RepaintManager.setCurrentManager(new CheckThreadViolationRepaintManager());

to your code however.

Categories
discuss

How to get current working directory in Java?

Let’s say I have my main class in C:UsersJustianDocuments. How can I get my program to show that it’s in C:UsersJustianDocuments?

Hard-Coding is not an option- it needs to be adaptable if it’s moved to another location.

I want to dump a bunch of CSV files in a folder, have the program recognize all the files, then load the data and manipulate them. I really just want to know how to navigate to that folder.

Answer

One way would be to use the system property System.getProperty("user.dir"); this will give you “The current working directory when the properties were initialized”. This is probably what you want. to find out where the java command was issued, in your case in the directory with the files to process, even though the actual .jar file might reside somewhere else on the machine. Having the directory of the actual .jar file isn’t that useful in most cases.

The following will print out the current directory from where the command was invoked regardless where the .class or .jar file the .class file is in.

public class Test
{
    public static void main(final String[] args)
    {
        final String dir = System.getProperty("user.dir");
        System.out.println("current dir = " + dir);
    }
}  

if you are in /User/me/ and your .jar file containing the above code is in /opt/some/nested/dir/ the command java -jar /opt/some/nested/dir/test.jar Test will output current dir = /User/me.

You should also as a bonus look at using a good object oriented command line argument parser. I highly recommend JSAP, the Java Simple Argument Parser. This would let you use System.getProperty("user.dir") and alternatively pass in something else to over-ride the behavior. A much more maintainable solution. This would make passing in the directory to process very easy to do, and be able to fall back on user.dir if nothing was passed in.

Categories
discuss

HTML5 Files and FileLists path

I am wondering where the file path is stored in a File object in HTML javascript.

I used the Webkit DevTools and got this:

FileList
0: File
    fileName: "script.js"
    fileSize: 71268
    name: "script.js"
    size: 71268
    type: "application/x-javascript"
    __proto__: File
length: 1
__proto__: FileList

The file name, size and types are there(anyone knows why name and size have 2 variables), but the path is not.

Is there any way to find path of the file, and if not, how does the browser and javascript read the file(such as POST methods & determining the type and size)?

Answer

As you can read in the WHATWG HTML spec,

[f]or historical reasons, the value IDL attribute prefixes the filename with the string “C:fakepath”. Some legacy user agents actually included the full path (which was a security vulnerability).

Reading on on MDC, we can see that Mozilla’s implementation of the File object has a (non-standard) property named mozFullPath, containing

[t]he full path of the referenced file; available only to code with UniversalFileRead privileges in chrome.

That page also answers your question about the redundant data in the File object: properties fileName and fileSize are deprecated. Also look at W3C’s File API Working Draft, where those are not mentioned.

To answer the second part of your question:

if not, how does the browser and JavaScript read the file (such as POST methods & determining the type and size)?

Of course, internally the complete file path can be accessed (and is in several browsers shown to the user), but it’s not accessible to JavaScript scripts running in a web page.

By the way, a few years ago there was a discussion about this on the WHATWG mailing list.

Categories
discuss

Webview load html from assets directory

I’m trying to load a html page from the assets directory. I tried this, but it fails.

public class ViewWeb extends Activity {  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        WebView wv;  
        wv = (WebView) findViewById(R.id.webView1);  
        wv.loadUrl("file:///android_asset/aboutcertified.html");   // fails here
        setContentView(R.layout.webview);  
    }  
}

I don’t really get any telling errors in LogCat…

Answer

You are getting the WebView before setting the Content view so the wv is probably null.

public class ViewWeb extends Activity {  
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);
            setContentView(R.layout.webview);  
            WebView wv;  
            wv = (WebView) findViewById(R.id.webView1);  
            wv.loadUrl("file:///android_asset/aboutcertified.html");   // now it will not fail here
        }  
    }
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..