diff --git a/core/src/main/java/org/jivesoftware/LoginDialog.java b/core/src/main/java/org/jivesoftware/LoginDialog.java index 9ae9f0f64..789a63682 100644 --- a/core/src/main/java/org/jivesoftware/LoginDialog.java +++ b/core/src/main/java/org/jivesoftware/LoginDialog.java @@ -1284,10 +1284,6 @@ JOptionPane.ERROR_MESSAGE); Workspace workspace = Workspace.getInstance(); LayoutSettings settings = LayoutSettingsManager.getLayoutSettings(); - int x = settings.getMainWindowX(); - int y = settings.getMainWindowY(); - int width = settings.getMainWindowWidth(); - int height = settings.getMainWindowHeight(); LocalPreferences pref = SettingsManager.getLocalPreferences(); if (pref.isDockingEnabled()) { @@ -1310,15 +1306,18 @@ JOptionPane.ERROR_MESSAGE); mainWindow.getContentPane().add(workspace.getCardPanel(), BorderLayout.CENTER); } - if (x == 0 && y == 0) { + final Rectangle mainWindowBounds = settings.getMainWindowBounds(); + if ( mainWindowBounds == null || mainWindowBounds.width <= 0 || mainWindowBounds.height <= 0 ) + { // Use Default size mainWindow.setSize(310, 520); // Center Window on Screen GraphicUtils.centerWindowOnScreen(mainWindow); } - else { - mainWindow.setBounds(x, y, width, height); + else + { + mainWindow.setBounds( mainWindowBounds ); } if (loginDialog.isVisible()) { diff --git a/core/src/main/java/org/jivesoftware/MainWindow.java b/core/src/main/java/org/jivesoftware/MainWindow.java index d73732311..db29ec6fc 100644 --- a/core/src/main/java/org/jivesoftware/MainWindow.java +++ b/core/src/main/java/org/jivesoftware/MainWindow.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (C) 2004-2011 Jive Software. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -32,7 +32,6 @@ import org.jivesoftware.spark.util.*; import org.jivesoftware.spark.util.SwingWorker; import org.jivesoftware.spark.util.log.Log; import org.jivesoftware.sparkimpl.plugin.alerts.InputTextAreaDialog; -import org.jivesoftware.sparkimpl.plugin.layout.LayoutSettings; import org.jivesoftware.sparkimpl.plugin.layout.LayoutSettingsManager; import org.jivesoftware.sparkimpl.plugin.manager.Enterprise; import org.jivesoftware.sparkimpl.settings.JiveInfo; @@ -126,14 +125,17 @@ public final class MainWindow extends ChatFrame implements ActionListener { // Add Workspace Container getContentPane().setLayout(new BorderLayout()); - LayoutSettings settings = LayoutSettingsManager.getLayoutSettings(); - if (settings.getMainWindowX() == 0 && settings.getMainWindowY() == 0) { + setMinimumSize( new Dimension( 100, 200 ) ); + final Rectangle mainWindowBounds = LayoutSettingsManager.getLayoutSettings().getMainWindowBounds(); + if ( mainWindowBounds == null || mainWindowBounds.width <= 0 || mainWindowBounds.height <= 0 ) + { // Use default settings. setSize(300, 500); GraphicUtils.centerWindowOnScreen(this); } - else { - setBounds(settings.getMainWindowX(), settings.getMainWindowY(), settings.getMainWindowWidth(), settings.getMainWindowHeight()); + else + { + setBounds( mainWindowBounds ); } // Add menubar @@ -143,37 +145,26 @@ public final class MainWindow extends ChatFrame implements ActionListener { setTitle(title); setIconImage(icon.getImage()); + addComponentListener( new ComponentAdapter() + { + @Override + public void componentResized( ComponentEvent e ) + { + LayoutSettingsManager.getLayoutSettings().setMainWindowBounds( getBounds() ); + } + @Override + public void componentMoved( ComponentEvent e ) + { + LayoutSettingsManager.getLayoutSettings().setMainWindowBounds( getBounds() ); + } + } ); // Setup WindowListener to be the proxy to the actual window listener // which cannot normally be used outside of the Window component because // of protected access. addWindowListener(new WindowAdapter() { - /** - * This event fires when the window has become active. - * - * @param e WindowEvent is not used. - */ - public void windowActivated(WindowEvent e) { - fireWindowActivated(); - } - - /** - * Invoked when a window is de-activated. - */ - public void windowDeactivated(WindowEvent e) { - } - - /** - * This event fires whenever a user minimizes the window - * from the toolbar. - * - * @param e WindowEvent is not used. - */ - public void windowIconified(WindowEvent e) { - } - /** * This event fires when the application is closing. * This allows Plugins to do any persistence or other @@ -182,7 +173,6 @@ public final class MainWindow extends ChatFrame implements ActionListener { * @param e WindowEvent is never used. */ public void windowClosing(WindowEvent e) { - saveLayout(); setVisible(false); } }); @@ -894,23 +884,6 @@ public final class MainWindow extends ChatFrame implements ActionListener { frame.setVisible(true); } - /** - * Saves the layout on closing of the main window. - */ - public void saveLayout() { - try { - LayoutSettings settings = LayoutSettingsManager.getLayoutSettings(); - settings.setMainWindowHeight(getHeight()); - settings.setMainWindowWidth(getWidth()); - settings.setMainWindowX(getX()); - settings.setMainWindowY(getY()); - LayoutSettingsManager.saveLayoutSettings(); - } - catch (Exception e) { - // Don't let this cause a real problem shutting down. - } - } - /** * Return true if the MainWindow is docked. * diff --git a/core/src/main/java/org/jivesoftware/spark/ui/ChatContainer.java b/core/src/main/java/org/jivesoftware/spark/ui/ChatContainer.java index 779a69a61..34a0a86b6 100644 --- a/core/src/main/java/org/jivesoftware/spark/ui/ChatContainer.java +++ b/core/src/main/java/org/jivesoftware/spark/ui/ChatContainer.java @@ -1243,11 +1243,8 @@ public class ChatContainer extends SparkTabbedPane implements MessageListener, C public void windowClosing(WindowEvent windowEvent) { - // Save layout - chatFrame.saveLayout(); SparkManager.getChatManager().getChatContainer() .closeAllChatRooms(); - } }); diff --git a/core/src/main/java/org/jivesoftware/spark/ui/ChatFrame.java b/core/src/main/java/org/jivesoftware/spark/ui/ChatFrame.java index 209cd884c..1615c57ff 100644 --- a/core/src/main/java/org/jivesoftware/spark/ui/ChatFrame.java +++ b/core/src/main/java/org/jivesoftware/spark/ui/ChatFrame.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (C) 2004-2011 Jive Software. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,7 +21,6 @@ import org.jivesoftware.resource.Res; import org.jivesoftware.spark.SparkManager; import org.jivesoftware.spark.util.GraphicUtils; import org.jivesoftware.spark.util.log.Log; -import org.jivesoftware.sparkimpl.plugin.layout.LayoutSettings; import org.jivesoftware.sparkimpl.plugin.layout.LayoutSettingsManager; import org.jivesoftware.sparkimpl.settings.local.SettingsManager; @@ -80,16 +79,41 @@ public class ChatFrame extends JFrame implements WindowFocusListener { getContentPane().setLayout(new BorderLayout()); getContentPane().add(SparkManager.getChatManager().getChatContainer(), BorderLayout.CENTER); - LayoutSettings settings = LayoutSettingsManager.getLayoutSettings(); - if (settings.getChatFrameX() == 0 && settings.getChatFrameY() == 0) { + setMinimumSize( new Dimension( 300, 300 ) ); + final Rectangle chatFrameBounds = LayoutSettingsManager.getLayoutSettings().getChatFrameBounds(); + if (chatFrameBounds == null || chatFrameBounds.width <= 0 || chatFrameBounds.height <= 0) + { // Use default settings. setSize(500, 400); GraphicUtils.centerWindowOnScreen(this); } - else { - setBounds(settings.getChatFrameX(), settings.getChatFrameY(), settings.getChatFrameWidth(), settings.getChatFrameHeight()); + else + { + setBounds( chatFrameBounds ); } + addComponentListener( new ComponentAdapter() + { + @Override + public void componentResized( ComponentEvent e ) + { + // Don't do this for subclasses. + if ( e.getComponent().getClass().getSimpleName().equalsIgnoreCase( "ChatFrame" ) ) + { + LayoutSettingsManager.getLayoutSettings().setChatFrameBounds( getBounds() ); + } + } + + @Override + public void componentMoved( ComponentEvent e ) + { + // Don't do this for subclasses. + if ( e.getComponent().getClass().getSimpleName().equalsIgnoreCase( "ChatFrame" ) ) + { + LayoutSettingsManager.getLayoutSettings().setChatFrameBounds( getBounds() ); + } + } + } ); addWindowFocusListener(this); @@ -187,18 +211,6 @@ public class ChatFrame extends JFrame implements WindowFocusListener { return System.currentTimeMillis() - inactiveTime; } - /** - * Saves the layout on closing of the chat frame. - */ - public void saveLayout() { - LayoutSettings settings = LayoutSettingsManager.getLayoutSettings(); - settings.setChatFrameHeight(getHeight()); - settings.setChatFrameWidth(getWidth()); - settings.setChatFrameX(getX()); - settings.setChatFrameY(getY()); - LayoutSettingsManager.saveLayoutSettings(); - } - /** * Brings the ChatFrame into focus on the desktop. */ diff --git a/core/src/main/java/org/jivesoftware/spark/ui/ContactItem.java b/core/src/main/java/org/jivesoftware/spark/ui/ContactItem.java index 0e9894c93..38457888e 100644 --- a/core/src/main/java/org/jivesoftware/spark/ui/ContactItem.java +++ b/core/src/main/java/org/jivesoftware/spark/ui/ContactItem.java @@ -226,9 +226,9 @@ public class ContactItem extends JPanel { int nickLength = displayName.length(); LayoutSettings settings = LayoutSettingsManager.getLayoutSettings(); - int windowWidth = settings.getMainWindowWidth(); + int windowWidth = settings.getMainWindowBounds() != null ? settings.getMainWindowBounds().width : 50; - if (nickLength > windowWidth) { + if (nickLength > windowWidth) { // FIXME comparing pixel-width with character count - that can't be good. displayNameLabel.setText(XmppStringUtils.unescapeLocalpart(displayName).substring(0, windowWidth) + "..."); } else { displayNameLabel.setText(XmppStringUtils.unescapeLocalpart(displayName)); diff --git a/core/src/main/java/org/jivesoftware/sparkimpl/plugin/layout/LayoutPlugin.java b/core/src/main/java/org/jivesoftware/sparkimpl/plugin/layout/LayoutPlugin.java index 6cb816c74..24b0f55d5 100644 --- a/core/src/main/java/org/jivesoftware/sparkimpl/plugin/layout/LayoutPlugin.java +++ b/core/src/main/java/org/jivesoftware/sparkimpl/plugin/layout/LayoutPlugin.java @@ -5,7 +5,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -19,53 +19,55 @@ import org.jivesoftware.MainWindow; import org.jivesoftware.MainWindowListener; import org.jivesoftware.spark.SparkManager; import org.jivesoftware.spark.plugin.Plugin; +import org.jivesoftware.spark.ui.ChatFrame; -public class LayoutPlugin implements Plugin { +public class LayoutPlugin implements Plugin +{ - public void initialize() { + public void initialize() + { final MainWindow mainWindow = SparkManager.getMainWindow(); - SparkManager.getMainWindow().addMainWindowListener(new MainWindowListener() { - public void shutdown() { - int x = mainWindow.getX(); - int y = mainWindow.getY(); - int width = mainWindow.getWidth(); - int height = mainWindow.getHeight(); - - LayoutSettings settings = LayoutSettingsManager.getLayoutSettings(); - - settings.setMainWindowHeight(height); - settings.setMainWindowWidth(width); - settings.setMainWindowX(x); - settings.setMainWindowY(y); - if (mainWindow.isDocked()){ - settings.setSplitPaneDividerLocation(mainWindow.getSplitPane().getDividerLocation()); + SparkManager.getMainWindow().addMainWindowListener( new MainWindowListener() + { + public void shutdown() + { + LayoutSettingsManager.getLayoutSettings().setMainWindowBounds( mainWindow.getBounds() ); + if ( mainWindow.isDocked() ) + { + LayoutSettingsManager.getLayoutSettings().setSplitPaneDividerLocation( mainWindow.getSplitPane().getDividerLocation() ); } - else{ - settings.setSplitPaneDividerLocation(-1); + else + { + LayoutSettingsManager.getLayoutSettings().setSplitPaneDividerLocation( -1 ); } LayoutSettingsManager.saveLayoutSettings(); } - public void mainWindowActivated() { + public void mainWindowActivated() + { } - public void mainWindowDeactivated() { + public void mainWindowDeactivated() + { } - }); + } ); } - public void shutdown() { + public void shutdown() + { } - public boolean canShutDown() { + public boolean canShutDown() + { return true; } - public void uninstall() { + public void uninstall() + { // Do nothing. } } diff --git a/core/src/main/java/org/jivesoftware/sparkimpl/plugin/layout/LayoutSettings.java b/core/src/main/java/org/jivesoftware/sparkimpl/plugin/layout/LayoutSettings.java index d2a9ae8bd..8f479e954 100644 --- a/core/src/main/java/org/jivesoftware/sparkimpl/plugin/layout/LayoutSettings.java +++ b/core/src/main/java/org/jivesoftware/sparkimpl/plugin/layout/LayoutSettings.java @@ -1,11 +1,11 @@ -/** +/* * Copyright (C) 2004-2011 Jive Software. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -15,143 +15,54 @@ */ package org.jivesoftware.sparkimpl.plugin.layout; -import java.awt.Toolkit; +import java.awt.*; -public class LayoutSettings { +public class LayoutSettings +{ + private Rectangle mainWindowBounds; + private Rectangle chatFrameBounds; + private Rectangle preferencesBounds; - private int mainWindowX; - private int mainWindowY; - private int mainWindowWidth; - private int mainWindowHeight; + private int splitPaneDividerLocation; - private int chatFrameX; - private int chatFrameY; - private int chatFrameWidth; - private int chatFrameHeight; - - private int preferencesFrameX; - private int preferencesFrameY; - private int preferencesFrameWidth; - private int preferencesFrameHeight; - private int dividerLocation = -1; - - public int getMainWindowX() { - return mainWindowX; + public Rectangle getMainWindowBounds() + { + return mainWindowBounds; } - public void setMainWindowX(int mainWindowX) { - this.mainWindowX = mainWindowX; + public void setMainWindowBounds( Rectangle mainWindowBounds ) + { + this.mainWindowBounds = mainWindowBounds; } - public int getMainWindowY() { - return mainWindowY; + public Rectangle getChatFrameBounds() + { + return chatFrameBounds; } - public void setMainWindowY(int mainWindowY) { - this.mainWindowY = mainWindowY; + public void setChatFrameBounds( Rectangle chatFrameBounds ) + { + this.chatFrameBounds = chatFrameBounds; } - public int getMainWindowWidth() { - if (mainWindowWidth < 100) { - mainWindowWidth = 100; - } else if (mainWindowWidth > Toolkit.getDefaultToolkit() - .getScreenSize().width) { - mainWindowWidth = Toolkit.getDefaultToolkit().getScreenSize().width - 50; - } - return mainWindowWidth; + public Rectangle getPreferencesBounds() + { + return preferencesBounds; } - public void setMainWindowWidth(int mainWindowWidth) { - this.mainWindowWidth = mainWindowWidth; + public void setPreferencesBounds( Rectangle preferencesBounds ) + { + this.preferencesBounds = preferencesBounds; } - public int getMainWindowHeight() { - if (mainWindowHeight < 200) { - mainWindowHeight = 500; - } else if (mainWindowHeight > Toolkit.getDefaultToolkit() - .getScreenSize().height) { - mainWindowHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 50; - } - return mainWindowHeight; + public int getSplitPaneDividerLocation() + { + return splitPaneDividerLocation; } - public void setMainWindowHeight(int mainWindowHeight) { - this.mainWindowHeight = mainWindowHeight; + public void setSplitPaneDividerLocation( int splitPaneDividerLocation ) + { + this.splitPaneDividerLocation = splitPaneDividerLocation; } - - public int getChatFrameX() { - return chatFrameX; - } - - public void setChatFrameX(int chatFrameX) { - this.chatFrameX = chatFrameX; - } - - public int getChatFrameY() { - return chatFrameY; - } - - public void setChatFrameY(int chatFrameY) { - this.chatFrameY = chatFrameY; - } - - public int getChatFrameWidth() { - return chatFrameWidth < 300 ? 300 : chatFrameWidth; - } - - public void setChatFrameWidth(int chatFrameWidth) { - this.chatFrameWidth = chatFrameWidth; - } - - public int getChatFrameHeight() { - return chatFrameHeight < 300 ? 300 : chatFrameHeight; - } - - public void setChatFrameHeight(int chatFrameHeight) { - this.chatFrameHeight = chatFrameHeight; - } - - public void setSplitPaneDividerLocation(int dividerLocation) { - this.dividerLocation = dividerLocation; - - } - - public int getPreferencesFrameX() { - return preferencesFrameX; - } - - public void setPreferencesFrameX(int preferencesFrameX) { - this.preferencesFrameX = preferencesFrameX; - } - - public int getPreferencesFrameY() { - return preferencesFrameY; - } - - public void setPreferencesFrameY(int preferencesFrameY) { - this.preferencesFrameY = preferencesFrameY; - } - - public int getPreferencesFrameWidth() { - return preferencesFrameWidth < 600 ? 600 : preferencesFrameWidth; - } - - public void setPreferencesFrameWidth(int preferencesFrameWidth) { - this.preferencesFrameWidth = preferencesFrameWidth; - } - - public int getPreferencesFrameHeight() { - return preferencesFrameHeight < 600 ? 600 : preferencesFrameHeight; - } - - public void setPreferencesFrameHeight(int preferencesFrameHeight) { - this.preferencesFrameHeight = preferencesFrameHeight; - } - - public int getSplitPaneDividerLocation() { - return dividerLocation; - } - - } diff --git a/core/src/main/java/org/jivesoftware/sparkimpl/plugin/layout/LayoutSettingsManager.java b/core/src/main/java/org/jivesoftware/sparkimpl/plugin/layout/LayoutSettingsManager.java index 49cb17abd..8042e91dc 100644 --- a/core/src/main/java/org/jivesoftware/sparkimpl/plugin/layout/LayoutSettingsManager.java +++ b/core/src/main/java/org/jivesoftware/sparkimpl/plugin/layout/LayoutSettingsManager.java @@ -1,11 +1,11 @@ -/** +/* * Copyright (C) 2004-2011 Jive Software. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -15,25 +15,25 @@ */ package org.jivesoftware.sparkimpl.plugin.layout; -import java.awt.Dimension; -import java.awt.Rectangle; -import java.awt.Toolkit; +import org.jivesoftware.Spark; +import org.jivesoftware.spark.util.GraphicUtils; +import org.jivesoftware.spark.util.log.Log; + +import java.awt.*; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties; -import org.jivesoftware.Spark; -import org.jivesoftware.spark.util.GraphicUtils; -import org.jivesoftware.spark.util.log.Log; - /** - * Responsbile for the loading and persisting of LocalSettings. + * Responsible for the loading and persisting of layout-settings. */ -public class LayoutSettingsManager { +public class LayoutSettingsManager +{ private static LayoutSettings layoutSettings; - private LayoutSettingsManager() { + private LayoutSettingsManager() + { } /** @@ -41,15 +41,18 @@ public class LayoutSettingsManager { * * @return the LayoutSettings for this agent. */ - public static LayoutSettings getLayoutSettings() { - if (!exists() && layoutSettings == null) { + public static LayoutSettings getLayoutSettings() + { + if ( !exists() && layoutSettings == null ) + { layoutSettings = new LayoutSettings(); } - if (layoutSettings == null) { + if ( layoutSettings == null ) + { // Do Initial Load from FileSystem. File settingsFile = getSettingsFile(); - layoutSettings = load(settingsFile); + layoutSettings = load( settingsFile ); } return layoutSettings; } @@ -57,45 +60,45 @@ public class LayoutSettingsManager { /** * Persists the settings to the local file system. */ - public static void saveLayoutSettings() { + public static void saveLayoutSettings() + { final Properties props = new Properties(); - String mainWindowX = Integer.toString(layoutSettings.getMainWindowX()); - String mainWindowY = Integer.toString(layoutSettings.getMainWindowY()); - String mainWindowHeight = Integer.toString(layoutSettings.getMainWindowHeight()); - String mainWindowWidth = Integer.toString(layoutSettings.getMainWindowWidth()); - String chatFrameX = Integer.toString(layoutSettings.getChatFrameX()); - String chatFrameY = Integer.toString(layoutSettings.getChatFrameY()); - String chatFrameWidth = Integer.toString(layoutSettings.getChatFrameWidth()); - String chatFrameHeight = Integer.toString(layoutSettings.getChatFrameHeight()); - String splitDividerLocation = Integer.toString(layoutSettings.getSplitPaneDividerLocation()); - String preferencesFrameX = Integer.toString(layoutSettings.getPreferencesFrameX()); - String preferencesFrameY = Integer.toString(layoutSettings.getPreferencesFrameY()); - String preferencesFrameWidth = Integer.toString(layoutSettings.getPreferencesFrameWidth()); - String preferencesFrameHeight = Integer.toString(layoutSettings.getPreferencesFrameHeight()); - - props.setProperty("mainWindowX", mainWindowX); - props.setProperty("mainWindowY", mainWindowY); - props.setProperty("mainWindowHeight", mainWindowHeight); - props.setProperty("mainWindowWidth", mainWindowWidth); - - props.setProperty("chatFrameX", chatFrameX); - props.setProperty("chatFrameY", chatFrameY); - props.setProperty("chatFrameWidth", chatFrameWidth); - props.setProperty("chatFrameHeight", chatFrameHeight); - - props.setProperty("preferencesFrameX", preferencesFrameX); - props.setProperty("preferencesFrameY", preferencesFrameY); - props.setProperty("preferencesFrameWidth", preferencesFrameWidth); - props.setProperty("preferencesFrameHeight", preferencesFrameHeight); - - props.setProperty("splitDividerLocation", splitDividerLocation); - - try { - props.store(new FileOutputStream(getSettingsFile()), "Storing Spark Layout Settings"); + final Rectangle mainWindow = layoutSettings.getMainWindowBounds(); + if ( mainWindow != null ) + { + props.setProperty( "mainWindowX", Integer.toString( mainWindow.x ) ); + props.setProperty( "mainWindowY", Integer.toString( mainWindow.y ) ); + props.setProperty( "mainWindowHeight", Integer.toString( mainWindow.height ) ); + props.setProperty( "mainWindowWidth", Integer.toString( mainWindow.width ) ); } - catch (Exception e) { - Log.error("Error saving settings.", e); + + final Rectangle chatFrame = layoutSettings.getChatFrameBounds(); + if ( chatFrame != null ) + { + props.setProperty( "chatFrameX", Integer.toString( chatFrame.x ) ); + props.setProperty( "chatFrameY", Integer.toString( chatFrame.y ) ); + props.setProperty( "chatFrameWidth", Integer.toString( chatFrame.width ) ); + props.setProperty( "chatFrameHeight", Integer.toString( chatFrame.height ) ); + } + + final Rectangle preferences = layoutSettings.getPreferencesBounds(); + if ( preferences != null ) + { + props.setProperty( "preferencesFrameX", Integer.toString( preferences.x ) ); + props.setProperty( "preferencesFrameY", Integer.toString( preferences.y ) ); + props.setProperty( "preferencesFrameWidth", Integer.toString( preferences.width ) ); + props.setProperty( "preferencesFrameHeight", Integer.toString( preferences.height ) ); + } + props.setProperty( "splitDividerLocation", Integer.toString( layoutSettings.getSplitPaneDividerLocation() ) ); + + try + { + props.store( new FileOutputStream( getSettingsFile() ), "Storing Spark Layout Settings" ); + } + catch ( Exception e ) + { + Log.error( "Error saving settings.", e ); } } @@ -104,7 +107,8 @@ public class LayoutSettingsManager { * * @return true if the settings file exists.('settings.xml') */ - public static boolean exists() { + public static boolean exists() + { return getSettingsFile().exists(); } @@ -113,164 +117,128 @@ public class LayoutSettingsManager { * * @return the settings file. */ - public static File getSettingsFile() { - File file = new File(Spark.getSparkUserHome()); - if (!file.exists()) { + public static File getSettingsFile() + { + File file = new File( Spark.getSparkUserHome() ); + if ( !file.exists() ) + { file.mkdirs(); } - return new File(file, "layout.settings"); + return new File( file, "layout.settings" ); } - private static LayoutSettings load(File file) { + public static int asInt( Properties props, String propertyName ) + { + return Integer.parseInt( props.getProperty( propertyName, "-1" ) ); + } + + private static LayoutSettings load( File file ) + { final Properties props = new Properties(); - try { - props.load(new FileInputStream(file)); + try + { + props.load( new FileInputStream( file ) ); - - LayoutSettings settings; - String mainWindowX = props.getProperty("mainWindowX"); - String mainWindowY = props.getProperty("mainWindowY"); - String mainWindowHeight = props.getProperty("mainWindowHeight"); - String mainWindowWidth = props.getProperty("mainWindowWidth"); - String chatFrameX = props.getProperty("chatFrameX"); - String chatFrameY = props.getProperty("chatFrameY"); - String chatFrameWidth = props.getProperty("chatFrameWidth"); - String chatFrameHeight = props.getProperty("chatFrameHeight"); - String splitDividerLocation = props.getProperty("splitDividerLocation"); - String preferencesFrameX = props.getProperty("preferencesFrameX"); - String preferencesFrameY = props.getProperty("preferencesFrameY"); - String preferencesFrameWidth = props.getProperty("preferencesFrameWidth"); - String preferencesFrameHeight = props.getProperty("preferencesFrameHeight"); - - settings = new LayoutSettings(); + final LayoutSettings settings = new LayoutSettings(); final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); - int height = (int)screenSize.getHeight(); - int width = (int)screenSize.getWidth(); - int mainWindowXInt = Integer.parseInt(mainWindowX); - int mainWindowYInt = Integer.parseInt(mainWindowY); - int mainWindowHeightInt = Integer.parseInt(mainWindowHeight); - int mainWindowWidthInt = Integer.parseInt(mainWindowWidth); + // Main Window + final Dimension mainWindowDimension = new Dimension( + asInt( props, "mainWindowWidth" ), + asInt( props, "mainWindowHeight" ) + ); - if (!isValidWindowPosition(mainWindowXInt, mainWindowYInt, - mainWindowWidthInt, mainWindowHeightInt)) { - mainWindowXInt = (width - mainWindowWidthInt) / 2; - mainWindowYInt = (height - mainWindowHeightInt) / 2; - } - - int chatFrameXInt = Integer.parseInt(chatFrameX); - int chatFrameYInt = Integer.parseInt(chatFrameY); - int chatFrameWidthInt = Integer.parseInt(chatFrameWidth); - int chatFrameHeightInt = Integer.parseInt(chatFrameHeight); - - if (!isValidWindowPosition(chatFrameXInt, chatFrameYInt, - chatFrameWidthInt, chatFrameHeightInt)) { - chatFrameXInt = (width - chatFrameWidthInt) / 2; - chatFrameYInt = (height - chatFrameHeightInt) / 2; - } - - int preferencesFrameXInt = preferencesFrameX == null ? -1 : Integer.parseInt(preferencesFrameX); - int preferencesFrameYInt = preferencesFrameY == null ? -1 : Integer.parseInt(preferencesFrameY); - int preferencesFrameWidthInt = preferencesFrameWidth == null ? -1 : Integer.parseInt(preferencesFrameWidth); - int preferencesFrameHeightInt = preferencesFrameHeight == null ? -1 : Integer.parseInt(preferencesFrameHeight); - - if (!isValidWindowPosition(preferencesFrameXInt, preferencesFrameYInt, - preferencesFrameWidthInt, preferencesFrameHeightInt)) { - preferencesFrameXInt = (width - preferencesFrameWidthInt) / 2; - preferencesFrameYInt = (height - preferencesFrameHeightInt) / 2; - } - - int splitDividerLocationInt = splitDividerLocation == null ? -1 : Integer.parseInt(splitDividerLocation); - - if (chatFrameHeightInt < 100) { - chatFrameHeightInt = 100; - } - if (chatFrameWidthInt < 100) { - chatFrameWidthInt = 100; - } - if (preferencesFrameWidthInt < 600) { - preferencesFrameWidthInt = 600; - } - if (preferencesFrameHeightInt < 600) { - preferencesFrameHeightInt = 600; + if ( mainWindowDimension.width > screenSize.width ) + { + mainWindowDimension.width = screenSize.width - 50; } - settings.setMainWindowX(mainWindowXInt); - settings.setMainWindowY(mainWindowYInt); - settings.setMainWindowHeight(mainWindowHeightInt); - settings.setMainWindowWidth(mainWindowWidthInt); + if ( mainWindowDimension.height > screenSize.height ) + { + mainWindowDimension.height = screenSize.height - 50; + } + + final Point mainWindowLocation = ensureValidWindowPosition( + new Point( + asInt( props, "mainWindowX" ), + asInt( props, "mainWindowY" ) + ), + mainWindowDimension ); + + settings.setMainWindowBounds( new Rectangle( mainWindowLocation, mainWindowDimension ) ); + + // Chat Frame + final Dimension chatFrameDimension = new Dimension( + asInt( props, "chatFrameWidth" ), + asInt( props, "chatFrameHeight" ) + ); + final Point chatFrameLocation = ensureValidWindowPosition( + new Point( + asInt( props, "chatFrameX" ), + asInt( props, "chatFrameY" ) + ), + chatFrameDimension ); + + settings.setChatFrameBounds( new Rectangle( chatFrameLocation, chatFrameDimension ) ); + + // Preferences + final Dimension preferencesDimension = new Dimension( + asInt( props, "preferencesFrameWidth" ), + asInt( props, "preferencesFrameHeight" ) + ); + final Point preferencesLocation = ensureValidWindowPosition( + new Point( + asInt( props, "preferencesFrameX" ), + asInt( props, "preferencesFrameY" ) + ), + chatFrameDimension ); + + settings.setPreferencesBounds( new Rectangle( preferencesLocation, preferencesDimension ) ); + + // Split Divider + settings.setSplitPaneDividerLocation( asInt( props, "splitDividerLocation" ) ); - settings.setChatFrameX(chatFrameXInt); - settings.setChatFrameY(chatFrameYInt); - settings.setChatFrameWidth(chatFrameWidthInt); - settings.setChatFrameHeight(chatFrameHeightInt); - settings.setSplitPaneDividerLocation(splitDividerLocationInt); - - settings.setPreferencesFrameX(preferencesFrameXInt); - settings.setPreferencesFrameY(preferencesFrameYInt); - settings.setPreferencesFrameWidth(preferencesFrameWidthInt); - settings.setPreferencesFrameHeight(preferencesFrameHeightInt); - return settings; } - catch (Exception e) { - Log.error(e); + catch ( Exception e ) + { + Log.error( e ); return new LayoutSettings(); } } - - protected static boolean isValidWindowPosition(int x, int y, int width, int height) { - Rectangle windowTitleBounds = new Rectangle(x,y,width,20); + + protected static boolean isValidWindowPosition( Point location, Dimension dimension ) + { + Rectangle windowTitleBounds = new Rectangle( location.x, location.y, dimension.width, 20 ); double windowTitleArea = windowTitleBounds.getWidth() * windowTitleBounds.getHeight(); - + Rectangle[] screenBounds = GraphicUtils.getScreenBounds(); - for (int i = 0; i < screenBounds.length; i++) { - Rectangle screen = screenBounds[i].getBounds(); - Rectangle intersection = screen.intersection(windowTitleBounds); - double visibleArea = intersection.getWidth() * intersection.getHeight(); - - // if 25% of it is visible in the device, then it is good - if ((visibleArea/windowTitleArea) > 0.25) - return true; - + for ( Rectangle screenBound : screenBounds ) + { + Rectangle screen = screenBound.getBounds(); + Rectangle intersection = screen.intersection( windowTitleBounds ); + double visibleArea = intersection.getWidth() * intersection.getHeight(); + + // if 25% of it is visible in the device, then it is good + if ( ( visibleArea / windowTitleArea ) > 0.25 ) + { + return true; + } } - + return false; } - - /** - * converts a Rectangle to a String - * @param r - * @return - */ - public static String rectangleToString(Rectangle r) { - return r.x + "," + r.y + "," + r.width + "," + r.height; + + protected static Point ensureValidWindowPosition( Point location, Dimension dimension ) + { + if ( isValidWindowPosition( location, dimension ) ) + { + return location; + } + + // Center + final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); + return new Point( (screenSize.width - dimension.width) / 2, (screenSize.height - dimension.height) / 2 ); } - - /** - * converts a String to a Rectangle - * @param s - * @return - */ - public static Rectangle stringToRectangle(String s) { - - if(s == null) - { - return new Rectangle(0,0,0,0); - } - - if (!s.matches("[0-9]*,[0-9]*,[0-9]*,[0-9]*")) { - return new Rectangle(0,0,0,0); - } else { - String[] arr = s.split(","); - - - return new Rectangle(Integer.parseInt(arr[0]), - Integer.parseInt(arr[1]), Integer.parseInt(arr[2]), - Integer.parseInt(arr[3])); - } - - } - } diff --git a/core/src/main/java/org/jivesoftware/sparkimpl/preference/PreferenceDialog.java b/core/src/main/java/org/jivesoftware/sparkimpl/preference/PreferenceDialog.java index bdebf3475..dc8037739 100644 --- a/core/src/main/java/org/jivesoftware/sparkimpl/preference/PreferenceDialog.java +++ b/core/src/main/java/org/jivesoftware/sparkimpl/preference/PreferenceDialog.java @@ -1,11 +1,11 @@ -/** +/* * Copyright (C) 2004-2011 Jive Software. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -16,125 +16,122 @@ package org.jivesoftware.sparkimpl.preference; -import java.awt.BorderLayout; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; +import org.jivesoftware.resource.Res; +import org.jivesoftware.spark.SparkManager; +import org.jivesoftware.sparkimpl.plugin.layout.LayoutSettingsManager; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; -import javax.swing.JButton; -import javax.swing.JDialog; -import javax.swing.JFrame; -import javax.swing.JOptionPane; -import javax.swing.JPanel; - -import org.jivesoftware.resource.Res; -import org.jivesoftware.spark.SparkManager; -import org.jivesoftware.sparkimpl.plugin.layout.LayoutSettings; -import org.jivesoftware.sparkimpl.plugin.layout.LayoutSettingsManager; - -public class PreferenceDialog implements PropertyChangeListener { +public class PreferenceDialog implements PropertyChangeListener +{ private JDialog preferenceDialog; private JOptionPane pane = null; private PreferencesPanel prefPanel; - public void invoke(JFrame parentFrame, PreferencesPanel contentPane) { - + public void invoke( JFrame parentFrame, PreferencesPanel contentPane ) + { this.prefPanel = contentPane; // Construct main panel w/ layout. final JPanel mainPanel = new JPanel(); - mainPanel.setLayout(new BorderLayout()); + mainPanel.setLayout( new BorderLayout() ); // Construct Dialog - preferenceDialog = new JDialog(parentFrame, - Res.getString("title.preferences"), - false); + preferenceDialog = new JDialog( parentFrame, + Res.getString( "title.preferences" ), + false ); - JButton btn_apply = new JButton(Res.getString("apply")); - JButton btn_save = new JButton(Res.getString("save")); - JButton btn_close = new JButton(Res.getString("close")); - - btn_close.addActionListener( e -> { - saveLayout(); - preferenceDialog.setVisible(false); - preferenceDialog.dispose(); - } ); - btn_save.addActionListener( e -> { - boolean okToClose = prefPanel.closing(); -if (okToClose) { -saveLayout(); -preferenceDialog.setVisible(false); -preferenceDialog.dispose(); -} -else { -pane.setValue(JOptionPane.UNINITIALIZED_VALUE); -} - } ); - btn_apply.addActionListener( e -> { - boolean okToClose = prefPanel.closing(); -if (!okToClose) { -pane.setValue(JOptionPane.UNINITIALIZED_VALUE); -} - } ); - - Object[] options = {btn_apply, btn_save, btn_close}; - pane = new JOptionPane(contentPane, JOptionPane.PLAIN_MESSAGE, - JOptionPane.OK_CANCEL_OPTION, null, options, options[0]); - mainPanel.add(pane, BorderLayout.CENTER); - preferenceDialog.setContentPane(mainPanel); - preferenceDialog.pack(); - - LayoutSettings settings = LayoutSettingsManager.getLayoutSettings(); - if ((settings.getPreferencesFrameX() == 0 && settings.getPreferencesFrameY() == 0) - || settings.getPreferencesFrameHeight() < 100 - || settings.getPreferencesFrameWidth() < 100) { + preferenceDialog.setMinimumSize( new Dimension( 600, 600 ) ); + + + JButton btn_apply = new JButton( Res.getString( "apply" ) ); + JButton btn_save = new JButton( Res.getString( "save" ) ); + JButton btn_close = new JButton( Res.getString( "close" ) ); + + btn_close.addActionListener( e -> + { + preferenceDialog.setVisible( false ); + preferenceDialog.dispose(); + } ); + + btn_save.addActionListener( e -> + { + boolean okToClose = prefPanel.closing(); + if ( okToClose ) + { + preferenceDialog.setVisible( false ); + preferenceDialog.dispose(); + } + else + { + pane.setValue( JOptionPane.UNINITIALIZED_VALUE ); + } + } ); + + btn_apply.addActionListener( e -> + { + boolean okToClose = prefPanel.closing(); + if ( !okToClose ) + { + pane.setValue( JOptionPane.UNINITIALIZED_VALUE ); + } + } ); + + Object[] options = { btn_apply, btn_save, btn_close }; + pane = new JOptionPane( contentPane, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, options, options[ 0 ] ); + mainPanel.add( pane, BorderLayout.CENTER ); + preferenceDialog.setContentPane( mainPanel ); + + final Rectangle preferencesBounds = LayoutSettingsManager.getLayoutSettings().getPreferencesBounds(); + if ( preferencesBounds == null || preferencesBounds.width <= 0 || preferencesBounds.height <= 0 ) + { // Use default settings. - preferenceDialog.setSize(750, 550); - preferenceDialog.setLocationRelativeTo(SparkManager.getMainWindow()); + preferenceDialog.setSize( 750, 550 ); + preferenceDialog.setLocationRelativeTo( SparkManager.getMainWindow() ); } - else { - preferenceDialog.setBounds(settings.getPreferencesFrameX(), settings.getPreferencesFrameY(), settings.getPreferencesFrameWidth(), settings.getPreferencesFrameHeight()); + else + { + preferenceDialog.setBounds( preferencesBounds ); } - pane.addPropertyChangeListener(this); + pane.addPropertyChangeListener( this ); - preferenceDialog.setVisible(true); + preferenceDialog.pack(); + preferenceDialog.setVisible( true ); preferenceDialog.toFront(); - - preferenceDialog.addWindowListener(new WindowAdapter() { - public void windowClosing(WindowEvent e) { - saveLayout(); - } - }); + + preferenceDialog.addComponentListener( new ComponentAdapter() + { + @Override + public void componentResized( ComponentEvent e ) + { + LayoutSettingsManager.getLayoutSettings().setPreferencesBounds( preferenceDialog.getBounds() ); + } + + @Override + public void componentMoved( ComponentEvent e ) + { + LayoutSettingsManager.getLayoutSettings().setPreferencesBounds( preferenceDialog.getBounds() ); + } + } ); } - public void propertyChange(PropertyChangeEvent e) { - if (pane.getValue() instanceof Integer) { - saveLayout(); - pane.removePropertyChangeListener(this); + public void propertyChange( PropertyChangeEvent e ) + { + if ( pane.getValue() instanceof Integer ) + { + pane.removePropertyChangeListener( this ); preferenceDialog.dispose(); } } - public JDialog getDialog() { + public JDialog getDialog() + { return preferenceDialog; } - - /** - * Saves the layout on closing of the main window. - */ - private void saveLayout() { - try { - LayoutSettings settings = LayoutSettingsManager.getLayoutSettings(); - settings.setPreferencesFrameHeight(preferenceDialog.getHeight()); - settings.setPreferencesFrameWidth(preferenceDialog.getWidth()); - settings.setPreferencesFrameX(preferenceDialog.getX()); - settings.setPreferencesFrameY(preferenceDialog.getY()); - LayoutSettingsManager.saveLayoutSettings(); - } - catch (Exception e) { - // Don't let this cause a real problem shutting down. - } - } }