Ticket #6343: patch.diff

File patch.diff, 3.8 KB (added by Don-vip, 14 years ago)
  • core/src/org/openstreetmap/josm/gui/ExtendedDialog.java

     
    455455        if(rememberSizePref.length() != 0 && defaultWindowGeometry != null) {
    456456            if(visible) {
    457457                new WindowGeometry(rememberSizePref,
    458                         defaultWindowGeometry).applySafe(this);
     458                        defaultWindowGeometry).applySafeMultiScreen(this);
    459459            } else {
    460460                new WindowGeometry(this).remember(rememberSizePref);
    461461            }
  • core/src/org/openstreetmap/josm/tools/WindowGeometry.java

     
    55
    66import java.awt.Component;
    77import java.awt.Dimension;
     8import java.awt.GraphicsConfiguration;
     9import java.awt.GraphicsDevice;
     10import java.awt.GraphicsEnvironment;
    811import java.awt.Point;
     12import java.awt.Rectangle;
    913import java.awt.Toolkit;
    1014import java.awt.Window;
    1115import java.util.regex.Matcher;
     
    101105    protected int parseField(String preferenceKey, String preferenceValue, String field) throws WindowGeometryException {
    102106        String v = "";
    103107        try {
    104             Pattern p = Pattern.compile(field + "=(\\d+)",Pattern.CASE_INSENSITIVE);
     108            Pattern p = Pattern.compile(field + "=(-?\\d+)",Pattern.CASE_INSENSITIVE);
    105109            Matcher m = p.matcher(preferenceValue);
    106110            if (!m.find())
    107111                throw new WindowGeometryException(tr("Preference with key ''{0}'' does not include ''{1}''. Cannot restore window geometry from preferences.", preferenceKey, field));
     
    213217     */
    214218    public void applySafe(Window window) {
    215219        Point p = new Point(topLeft);
    216         if (p.x > Toolkit.getDefaultToolkit().getScreenSize().width - 10) {
    217             p.x  = 0;
     220        if (p.x < 0 || p.x > Toolkit.getDefaultToolkit().getScreenSize().width - 10) {
     221            p.x = 0;
    218222        }
    219         if (p.y > Toolkit.getDefaultToolkit().getScreenSize().height - 10) {
     223        if (p.y < 0 || p.y > Toolkit.getDefaultToolkit().getScreenSize().height - 10) {
    220224            p.y = 0;
    221225        }
    222226        window.setLocation(p);
    223227        window.setSize(extent);
    224228    }
     229   
     230    /**
     231     * Applies this geometry to a window. Makes sure that the window is not placed outside
     232     * of the coordinate range of all available screens.
     233     *
     234     * @param window the window
     235     */
     236    public void applySafeMultiScreen(Window window) {
     237        Point p = new Point(topLeft);
     238       
     239                Rectangle virtualBounds = new Rectangle();
     240                GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
     241                GraphicsDevice[] gs = ge.getScreenDevices();
     242                for (int j = 0; j < gs.length; j++) {
     243                        GraphicsDevice gd = gs[j];
     244                        GraphicsConfiguration[] gc = gd.getConfigurations();
     245                        for (int i = 0; i < gc.length; i++) {
     246                                virtualBounds = virtualBounds.union(gc[i].getBounds());
     247                        }
     248                }
     249               
     250        if (p.x < virtualBounds.x) {
     251            p.x = virtualBounds.x;
     252        } else if (p.x > virtualBounds.x + virtualBounds.width - extent.width) {
     253                p.x = virtualBounds.x + virtualBounds.width - extent.width;
     254        }
     255               
     256        if (p.y < virtualBounds.y) {
     257            p.y = virtualBounds.y;
     258        } else if (p.y > virtualBounds.y + virtualBounds.height - extent.height) {
     259                p.y = virtualBounds.y + virtualBounds.height - extent.height;
     260        }
     261       
     262        window.setLocation(p);
     263        window.setSize(extent);
     264    }
    225265}