Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2001-2008 Aqris Software AS. All rights reserved.
 * 
 * This program is dual-licensed under both the Common Development
 * and Distribution License ("CDDL") and the GNU General Public
 * License ("GPL"). You may elect to use one or the other of these
 * licenses.
 */

import javax.swing.SwingUtilities;

import java.awt.Container;

import java.awt.Point;
import java.awt.Rectangle;

public class Main {
    public static Point positionToClickPoint(Container component, int caretPosition, Container invokedIn) {
        if (component == null) {
            return null;
        }

        //System.err.println("Checking: " + component.getClass().getName());
        if (component.getClass().getName().indexOf("EditorPane") >= 0) {
            try {
                java.lang.reflect.Method pointGetter = component.getClass().getMethod("modelToView",
                        new Class[] { Integer.TYPE });
                Rectangle rec = (Rectangle) pointGetter.invoke(component,
                        new Object[] { new Integer(caretPosition) });
                //System.err.println("Before: " + (int)rec.getY());
                // FIXME: somehow it fails here to convert point from scrollable component
                Point point = SwingUtilities.convertPoint(component, (int) rec.getX(), (int) rec.getY() + 10,
                        invokedIn);
                // FIXME: ugly hack :(
                if (point.getY() > 1024) {
                    point = new Point((int) point.getX(), 250);
                }
                //System.err.println("After: " + (int)point.getY());
                return point;
            } catch (Exception e) {
                System.err.println("Method invocation exception caught");
                e.printStackTrace();

                //FIXME: BUG
                return null;
                //throw new RuntimeException("Method invocation exception caught");
            }
        }

        for (int i = 0; i < component.getComponentCount(); i++) {
            java.awt.Component childComponent = component.getComponent(i);
            if (childComponent instanceof javax.swing.JComponent) {
                Point point = positionToClickPoint((javax.swing.JComponent) childComponent, caretPosition,
                        invokedIn);
                if (point != null) {
                    return point;
                }
            }
        }

        return null;
    }
}