diff --git a/protege-editor-core/src/main/java/org/protege/editor/core/ui/util/UIUtil.java b/protege-editor-core/src/main/java/org/protege/editor/core/ui/util/UIUtil.java index 974bc768a..b0dd495b9 100644 --- a/protege-editor-core/src/main/java/org/protege/editor/core/ui/util/UIUtil.java +++ b/protege-editor-core/src/main/java/org/protege/editor/core/ui/util/UIUtil.java @@ -7,8 +7,9 @@ import org.protege.editor.core.platform.apple.MacUIUtil; import org.protege.editor.core.prefs.Preferences; import org.protege.editor.core.prefs.PreferencesManager; -import org.protege.editor.core.ui.workspace.WorkspaceManager; +import javax.annotation.Nonnull; +import javax.swing.FocusManager; import javax.swing.*; import javax.swing.filechooser.FileFilter; import java.awt.*; @@ -18,6 +19,7 @@ import java.util.Collection; import java.util.Optional; import java.util.Set; +import java.util.stream.Stream; /** @@ -25,7 +27,7 @@ * The University Of Manchester
* Medical Informatics Group
* Date: Apr 2, 2006

- + * * matthew.horridge@cs.man.ac.uk
* www.cs.man.ac.uk/~horridgm

*/ @@ -39,32 +41,23 @@ public class UIUtil { public static final String FILE_URI_SCHEME = "file"; - public static String getCurrentFileDirectory() { - String dir = "~"; - Preferences p = PreferencesManager.getInstance().getApplicationPreferences(FILE_PREFERENCES_KEY); - dir = p.getString(CURRENT_FILE_DIRECTORY_KEY, dir); - return dir; - } - - - public static void setCurrentFileDirectory(String dir) { - Preferences p = PreferencesManager.getInstance().getApplicationPreferences(FILE_PREFERENCES_KEY); - p.putString(CURRENT_FILE_DIRECTORY_KEY, dir); - } - /** - * * @param parent * @param title * @param extensions * @deprecated Use openFile(Window parent, String title, final String description, final Set extensions) */ @Deprecated - public static File openFile(Component parent, String title, Set extensions) { + public static File openFile(Component parent, + String title, + Set extensions) { return openFile(parent, title, null, extensions); } - - public static File openFile(Component parent, String title, final String description, final Set extensions) { + + public static File openFile(Component parent, + String title, + final String description, + final Set extensions) { Window parentWindow; if(parent instanceof Window) { parentWindow = (Window) parent; @@ -76,11 +69,11 @@ else if(parent == null) { else { parentWindow = SwingUtilities.getWindowAncestor(parent); } - if (OSUtils.isOSX() && parentWindow != null) { + if(OSUtils.isOSX() && parentWindow != null) { return MacUIUtil.openFile(parentWindow, title, extensions); } JFileChooser fileDialog = new JFileChooser(getCurrentFileDirectory()); - if (extensions != null && !extensions.isEmpty()) { + if(extensions != null && !extensions.isEmpty()) { fileDialog.setFileFilter(new FileFilter() { @Override @@ -90,13 +83,13 @@ public String getDescription() { @Override public boolean accept(File f) { - if (extensions.isEmpty() || f.isDirectory()) { + if(extensions.isEmpty() || f.isDirectory()) { return true; } else { String name = f.getName(); - for (String ext : extensions) { - if (name.toLowerCase().endsWith(ext.toLowerCase())) { + for(String ext : extensions) { + if(name.toLowerCase().endsWith(ext.toLowerCase())) { return true; } } @@ -108,8 +101,8 @@ public boolean accept(File f) { fileDialog.setDialogType(JFileChooser.OPEN_DIALOG); int retVal = fileDialog.showOpenDialog(parent); File f; - if (retVal == JFileChooser.APPROVE_OPTION && (f = fileDialog.getSelectedFile()) != null) { - if (f.getParent() != null) { + if(retVal == JFileChooser.APPROVE_OPTION && (f = fileDialog.getSelectedFile()) != null) { + if(f.getParent() != null) { setCurrentFileDirectory(f.getParent()); } return f; @@ -119,8 +112,19 @@ public boolean accept(File f) { } } + public static String getCurrentFileDirectory() { + String dir = "~"; + Preferences p = PreferencesManager.getInstance().getApplicationPreferences(FILE_PREFERENCES_KEY); + dir = p.getString(CURRENT_FILE_DIRECTORY_KEY, dir); + return dir; + } + + public static void setCurrentFileDirectory(String dir) { + Preferences p = PreferencesManager.getInstance().getApplicationPreferences(FILE_PREFERENCES_KEY); + p.putString(CURRENT_FILE_DIRECTORY_KEY, dir); + } + /** - * * @param parent * @param title * @param extensions @@ -128,11 +132,18 @@ public boolean accept(File f) { * @deprecated Use saveFile(Window parent, String title, final String description, final Set extensions, String initialName) */ @Deprecated - public static File saveFile(Component parent, String title, Set extensions, String initialName) { + public static File saveFile(Component parent, + String title, + Set extensions, + String initialName) { return saveFile(parent, title, null, extensions, initialName); } - public static File saveFile(Component parent, String title, final String description, final Set extensions, String initialName) { + public static File saveFile(Component parent, + String title, + final String description, + final Set extensions, + String initialName) { Window parentWindow; if(parent instanceof Window) { parentWindow = (Window) parent; @@ -140,12 +151,12 @@ public static File saveFile(Component parent, String title, final String descrip else { parentWindow = SwingUtilities.getWindowAncestor(parent); } - if (OSUtils.isOSX() && parentWindow != null) { + if(OSUtils.isOSX() && parentWindow != null) { return MacUIUtil.saveFile(parentWindow, title, extensions, initialName); } JFileChooser fileDialog = new JFileChooser(getCurrentFileDirectory()); fileDialog.setDialogTitle(title); - if (extensions != null && !extensions.isEmpty()) { + if(extensions != null && !extensions.isEmpty()) { fileDialog.setFileFilter(new FileFilter() { @Override @@ -155,13 +166,13 @@ public String getDescription() { @Override public boolean accept(File f) { - if (extensions.isEmpty() || f.isDirectory()) { + if(extensions.isEmpty() || f.isDirectory()) { return true; } else { String name = f.getName(); - for (String ext : extensions) { - if (name.toLowerCase().endsWith(ext.toLowerCase())) { + for(String ext : extensions) { + if(name.toLowerCase().endsWith(ext.toLowerCase())) { return true; } } @@ -171,14 +182,14 @@ public boolean accept(File f) { }); } fileDialog.setDialogType(JFileChooser.SAVE_DIALOG); - if (initialName != null) { + if(initialName != null) { fileDialog.setSelectedFile(new File(initialName)); } int retVal = fileDialog.showSaveDialog(parent); File f = null; - if (retVal == JFileChooser.APPROVE_OPTION && (f = fileDialog.getSelectedFile()) != null) { - if (f.getParent() != null) { + if(retVal == JFileChooser.APPROVE_OPTION && (f = fileDialog.getSelectedFile()) != null) { + if(f.getParent() != null) { setCurrentFileDirectory(f.getParent()); } return f; @@ -192,17 +203,23 @@ public boolean accept(File f) { * @deprecated Use saveFile(Window parent, String title, String description, Set extensions) */ @Deprecated - public static File saveFile(Window parent, String title, Set extensions) { + public static File saveFile(Window parent, + String title, + Set extensions) { return saveFile(parent, title, null, extensions, null); } - public static File saveFile(Window parent, String title, String description, Set extensions) { + public static File saveFile(Window parent, + String title, + String description, + Set extensions) { return saveFile(parent, title, description, extensions, null); } - public static File chooseFolder(Component parent, String title) { - if (System.getProperty("os.name").contains("OS X")) { + public static File chooseFolder(Component parent, + String title) { + if(System.getProperty("os.name").contains("OS X")) { return MacUIUtil.chooseOSXFolder(parent, title); } JFileChooser chooser = new JFileChooser(); @@ -211,10 +228,10 @@ public static File chooseFolder(Component parent, String title) { chooser.setDialogTitle(title); chooser.setDialogType(JFileChooser.OPEN_DIALOG); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); - if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) { + if(chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) { File selectedDirectory = chooser.getSelectedFile(); - if (selectedDirectory != null) { - setCurrentFileDirectory(selectedDirectory.toString()); + if(selectedDirectory != null) { + setCurrentFileDirectory(selectedDirectory.toString()); } return selectedDirectory; } @@ -232,37 +249,36 @@ public static void openRequest(OpenRequestHandler handler) throws Exception { return; } } - int ret = JOptionPane.showConfirmDialog(handler.getCurrentWorkspace(), - "Do you want to open the ontology in the current window?", - "Open in current window", - JOptionPane.YES_NO_CANCEL_OPTION, - JOptionPane.QUESTION_MESSAGE); + int ret = JOptionPane.showConfirmDialog(handler.getCurrentWorkspace(), "Do you want to open the ontology in the current window?", "Open in current window", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); - if (ret == JOptionPane.YES_OPTION){ + if(ret == JOptionPane.YES_OPTION) { handler.openInCurrentWorkspace(); } - else if (ret == JOptionPane.NO_OPTION){ + else if(ret == JOptionPane.NO_OPTION) { handler.openInNewWorkspace(); } } - - public static Collection getComponentsExtending(Component component, Class clazz) { - Collection components = new ArrayList<>(); - addComponentsExtending(component, clazz, components); - return components; + + public static Collection getComponentsExtending(Component component, + Class clazz) { + Collection components = new ArrayList<>(); + addComponentsExtending(component, clazz, components); + return components; } - - private static void addComponentsExtending(Component component, Class clazz, Collection components) { - if (component instanceof Container) { + + private static void addComponentsExtending(Component component, + Class clazz, + Collection components) { + if(component instanceof Container) { Container container = (Container) component; int nSubcomponents = container.getComponentCount(); - for (int i = 0; i < nSubcomponents; ++i) { + for(int i = 0; i < nSubcomponents; ++i) { Component subComponent = container.getComponent(i); - if (clazz.isAssignableFrom(subComponent.getClass())) { - components.add(clazz.cast(subComponent)); + if(clazz.isAssignableFrom(subComponent.getClass())) { + components.add(clazz.cast(subComponent)); } else { - addComponentsExtending(subComponent, clazz, components); + addComponentsExtending(subComponent, clazz, components); } } } @@ -271,6 +287,7 @@ private static void addComponentsExtending(Component component, Classnull. * @return true if the URI represents a local file, otherwise false. */ @@ -285,12 +302,33 @@ public static boolean isLocalFile(URI uri) { /** * Determines whether the user is on Windows and if so whether the "high contrast" setting * is set to on. + * * @return {@code true} if on, otherwise {@code false} */ public static boolean isHighContrastOn() { Toolkit toolkit = Toolkit.getDefaultToolkit(); - Optional highContrast = Optional.ofNullable((Boolean)toolkit.getDesktopProperty( "win.highContrast.on" )); + Optional highContrast = Optional.ofNullable((Boolean) toolkit.getDesktopProperty("win.highContrast.on")); return highContrast.orElse(false); } + + /** + * Determines if the specified rectangle is (at least partially) visible on + * a screen. + * @param rectangle The rectangle. + * @return true if the specified rectangle is partially or fully visible on some screen, + * otherwise false. + */ + public static boolean isVisibleOnScreen(@Nonnull Rectangle rectangle) { + GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment(); + if(graphicsEnvironment.isHeadlessInstance()) { + return false; + } + GraphicsDevice[] screenDevices = graphicsEnvironment.getScreenDevices(); + return Stream + .of(screenDevices) + .map(screenDevice -> screenDevice.getDefaultConfiguration().getBounds()) + .anyMatch(screenBounds -> screenBounds.intersects(rectangle)); + } + } diff --git a/protege-editor-core/src/main/java/org/protege/editor/core/ui/workspace/WorkspaceFrame.java b/protege-editor-core/src/main/java/org/protege/editor/core/ui/workspace/WorkspaceFrame.java index 2a9dd8ebd..e41f8f3d1 100644 --- a/protege-editor-core/src/main/java/org/protege/editor/core/ui/workspace/WorkspaceFrame.java +++ b/protege-editor-core/src/main/java/org/protege/editor/core/ui/workspace/WorkspaceFrame.java @@ -7,6 +7,7 @@ import org.protege.editor.core.ui.action.ProtegeAction; import org.protege.editor.core.ui.menu.MenuBuilder; import org.protege.editor.core.ui.util.Icons; +import org.protege.editor.core.ui.util.UIUtil; import javax.swing.*; import java.awt.*; @@ -88,15 +89,20 @@ public void dispose() { protected void restoreMetrics() { - Preferences p = PreferencesManager.getInstance().getApplicationPreferences(getClass().getName()); - int w = p.getInt(SIZE_X, DEFAULT_WIDTH); - int h = p.getInt(SIZE_Y, DEFAULT_HEIGHT); + Preferences prefs = PreferencesManager.getInstance().getApplicationPreferences(getClass().getName()); + int w = prefs.getInt(SIZE_X, DEFAULT_WIDTH); + int h = prefs.getInt(SIZE_Y, DEFAULT_HEIGHT); setSize(w, h); Point defLoc = getDefaultLocation(); - int x = p.getInt(LOC_X, defLoc.x); - int y = p.getInt(LOC_Y, defLoc.y); - setLocation(x, y); - setSize(w, h); + int x = prefs.getInt(LOC_X, defLoc.x); + int y = prefs.getInt(LOC_Y, defLoc.y); + Rectangle desiredRectangle = new Rectangle(x, y, w, h); + if(UIUtil.isVisibleOnScreen(desiredRectangle)) { + setLocation(x, y); + } + else { + setLocation(defLoc.x, defLoc.y); + } } @@ -108,11 +114,12 @@ private Point getDefaultLocation() { protected void saveMetrics() { - Preferences p = PreferencesManager.getInstance().getApplicationPreferences(getClass().getName()); - p.putInt(LOC_X, getLocation().x); - p.putInt(LOC_Y, getLocation().y); - p.putInt(SIZE_X, getSize().width); - p.putInt(SIZE_Y, getSize().height); + Preferences prefs = PreferencesManager.getInstance().getApplicationPreferences(getClass().getName()); + Point location = getLocation(); + prefs.putInt(LOC_X, location.x); + prefs.putInt(LOC_Y, location.y); + prefs.putInt(SIZE_X, getSize().width); + prefs.putInt(SIZE_Y, getSize().height); }