Skip to content

Commit

Permalink
Potential fix for #804
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhorridge committed Jan 16, 2019
1 parent e7e789a commit d32f8e0
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -18,14 +19,15 @@
import java.util.Collection;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;


/**
* Author: Matthew Horridge<br>
* The University Of Manchester<br>
* Medical Informatics Group<br>
* Date: Apr 2, 2006<br><br>
*
* matthew.horridge@cs.man.ac.uk<br>
* www.cs.man.ac.uk/~horridgm<br><br>
*/
Expand All @@ -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<String> extensions)
*/
@Deprecated
public static File openFile(Component parent, String title, Set<String> extensions) {
public static File openFile(Component parent,
String title,
Set<String> extensions) {
return openFile(parent, title, null, extensions);
}

public static File openFile(Component parent, String title, final String description, final Set<String> extensions) {

public static File openFile(Component parent,
String title,
final String description,
final Set<String> extensions) {
Window parentWindow;
if(parent instanceof Window) {
parentWindow = (Window) parent;
Expand All @@ -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
Expand All @@ -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;
}
}
Expand All @@ -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;
Expand All @@ -119,33 +112,51 @@ 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
* @param initialName
* @deprecated Use saveFile(Window parent, String title, final String description, final Set<String> extensions, String initialName)
*/
@Deprecated
public static File saveFile(Component parent, String title, Set<String> extensions, String initialName) {
public static File saveFile(Component parent,
String title,
Set<String> extensions,
String initialName) {
return saveFile(parent, title, null, extensions, initialName);
}

public static File saveFile(Component parent, String title, final String description, final Set<String> extensions, String initialName) {
public static File saveFile(Component parent,
String title,
final String description,
final Set<String> extensions,
String initialName) {
Window parentWindow;
if(parent instanceof Window) {
parentWindow = (Window) parent;
}
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
Expand All @@ -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;
}
}
Expand All @@ -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;
Expand All @@ -192,17 +203,23 @@ public boolean accept(File f) {
* @deprecated Use saveFile(Window parent, String title, String description, Set<String> extensions)
*/
@Deprecated
public static File saveFile(Window parent, String title, Set<String> extensions) {
public static File saveFile(Window parent,
String title,
Set<String> extensions) {
return saveFile(parent, title, null, extensions, null);
}

public static File saveFile(Window parent, String title, String description, Set<String> extensions) {
public static File saveFile(Window parent,
String title,
String description,
Set<String> 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();
Expand All @@ -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;
}
Expand All @@ -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 <T> Collection<T> getComponentsExtending(Component component, Class<? extends T> clazz) {
Collection<T> components = new ArrayList<>();
addComponentsExtending(component, clazz, components);
return components;

public static <T> Collection<T> getComponentsExtending(Component component,
Class<? extends T> clazz) {
Collection<T> components = new ArrayList<>();
addComponentsExtending(component, clazz, components);
return components;
}

private static <T> void addComponentsExtending(Component component, Class<? extends T> clazz, Collection<T> components) {
if (component instanceof Container) {

private static <T> void addComponentsExtending(Component component,
Class<? extends T> clazz,
Collection<T> 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);
}
}
}
Expand All @@ -271,6 +287,7 @@ private static <T> void addComponentsExtending(Component component, Class<? exte

/**
* Tests to see if a URI represents a local file.
*
* @param uri The URI. May be <code>null</code>.
* @return <code>true</code> if the URI represents a local file, otherwise <code>false</code>.
*/
Expand All @@ -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<Boolean> highContrast = Optional.ofNullable((Boolean)toolkit.getDesktopProperty( "win.highContrast.on" ));
Optional<Boolean> 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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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);
}
}


Expand All @@ -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);
}


Expand Down

0 comments on commit d32f8e0

Please sign in to comment.