|
| 1 | +package org.hvdw.jexiftoolgui.view; |
| 2 | + |
| 3 | +import org.hvdw.jexiftoolgui.Application; |
| 4 | +import org.hvdw.jexiftoolgui.Utils; |
| 5 | +import org.hvdw.jexiftoolgui.facades.IPreferencesFacade; |
| 6 | + |
| 7 | +import javax.swing.*; |
| 8 | + |
| 9 | +import java.awt.*; |
| 10 | + |
| 11 | +import static org.hvdw.jexiftoolgui.facades.IPreferencesFacade.PreferenceKey.*; |
| 12 | +import static org.slf4j.LoggerFactory.getLogger; |
| 13 | + |
| 14 | +public class GuiSettings { |
| 15 | + |
| 16 | + public static int guiWidth; |
| 17 | + public static int guiHeight; |
| 18 | + |
| 19 | + private final static IPreferencesFacade prefs = IPreferencesFacade.defaultInstance; |
| 20 | + private final static ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) getLogger(GuiSettings.class); |
| 21 | + |
| 22 | + /** |
| 23 | + * Gets the current width and height from the rootPanel (not the frame) |
| 24 | + * @param rootPanel |
| 25 | + */ |
| 26 | + public static void GuiSize(JPanel rootPanel){ |
| 27 | + guiWidth = rootPanel.getWidth(); |
| 28 | + guiHeight = rootPanel.getHeight(); |
| 29 | + logger.debug("rootPanel size {} x {}", guiWidth, guiHeight); |
| 30 | + } |
| 31 | + public static void GuiSize(JFrame frame){ |
| 32 | + guiWidth = frame.getWidth(); |
| 33 | + guiHeight = frame.getHeight(); |
| 34 | + logger.debug("frame size {} x {}", guiWidth, guiHeight); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * SplitPaneDividerPercentage returns the position as a value like 0.33 (450/1350) or so |
| 39 | + * @param splitpane |
| 40 | + */ |
| 41 | + private static double SplitPaneDividerPercentage( JSplitPane splitpane) { |
| 42 | + double SplitPositionPercentage; |
| 43 | + // getDividerLocation is int, setDividerLocation is double |
| 44 | + double splitposition = splitpane.getDividerLocation() / |
| 45 | + (double) (splitpane.getWidth() - splitpane.getDividerSize()); |
| 46 | + return SplitPositionPercentage = ((int) (100 * (splitposition + 0.005))) / (double) 100; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * SaveGuiConfig saves Width, Height and JSplitpane position to Preferences |
| 51 | + * This method is called when the user nicely uses the File -> Exit method |
| 52 | + * @param rootPanel |
| 53 | + * @param splitpanel |
| 54 | + */ |
| 55 | + public static void SaveGuiConfig(JFrame frame, JPanel rootPanel, JSplitPane splitpanel) { |
| 56 | + //GuiSize(rootPanel); |
| 57 | + GuiSize(frame); |
| 58 | + double SPP = SplitPaneDividerPercentage(splitpanel); |
| 59 | + prefs.storeByKey(GUI_WIDTH, String.valueOf(guiWidth)); |
| 60 | + prefs.storeByKey(GUI_HEIGHT, String.valueOf(guiHeight)); |
| 61 | + prefs.storeByKey(SPLITPANEL_POSITION, String.valueOf(SPP)); |
| 62 | + logger.info("Save Gui Settings: Width x Height {}x{}; splitpanel postion {}", guiWidth, guiHeight, String.valueOf(SPP)); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * This SaveGuiConfig method saves only the frame size and is invoked when the user presses the exit button in the menubar |
| 67 | + * @param frame |
| 68 | + */ |
| 69 | + public static void SaveGuiConfig(JFrame frame) { |
| 70 | + //GuiSize(rootPanel); |
| 71 | + GuiSize(frame); |
| 72 | + prefs.storeByKey(GUI_WIDTH, String.valueOf(guiWidth)); |
| 73 | + prefs.storeByKey(GUI_HEIGHT, String.valueOf(guiHeight)); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + /** |
| 78 | + * LoadGuiConfig loads Width, Height and JSplitpane postion from Preferences and restores the Gui |
| 79 | + * to the state when the user exited the application |
| 80 | + * @param rootPanel |
| 81 | + * @param splitpanel |
| 82 | + */ |
| 83 | + public static void LoadGuiConfig(JPanel rootPanel, JSplitPane splitpanel) { |
| 84 | + String strWidth; |
| 85 | + String strHeight; |
| 86 | + int Width; |
| 87 | + int Height; |
| 88 | + |
| 89 | + strWidth = prefs.getByKey(GUI_WIDTH, "1"); // 1450 Apple, 1360 others |
| 90 | + strHeight = prefs.getByKey(GUI_HEIGHT, "1"); //850 |
| 91 | + Application.OS_NAMES os = Utils.getCurrentOsName(); |
| 92 | + if ("1".equals(strWidth)) { |
| 93 | + if (os == Application.OS_NAMES.APPLE) { |
| 94 | + Width = 1450; |
| 95 | + } else { |
| 96 | + Width = 850; |
| 97 | + } |
| 98 | + } else { |
| 99 | + Width = Integer.parseInt(strWidth); |
| 100 | + } |
| 101 | + if ("1".equals(strHeight)) { |
| 102 | + Height = 850; |
| 103 | + } else { |
| 104 | + Height = Integer.parseInt(strHeight); |
| 105 | + } |
| 106 | + logger.info("rootPanel.setSize {} x {}", Width, Height); |
| 107 | + rootPanel.setPreferredSize(new Dimension(Width, Height)); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * |
| 112 | + * @param splitpanel |
| 113 | + */ |
| 114 | + public static void SetSplitPaneDivider(JSplitPane splitpanel) { |
| 115 | + String strSplitPanePosition = prefs.getByKey(SPLITPANEL_POSITION, "1"); |
| 116 | + if ("1".equals(strSplitPanePosition)) { |
| 117 | + splitpanel.setDividerLocation(0.33); |
| 118 | + } else { |
| 119 | + splitpanel.setDividerLocation(Double.parseDouble(strSplitPanePosition)); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * This version of the LoadGuiConfig sets the total frame size |
| 125 | + * @param frame |
| 126 | + */ |
| 127 | + public static void LoadGuiConfig(JFrame frame) { |
| 128 | + String strWidth; |
| 129 | + String strHeight; |
| 130 | + int Width; |
| 131 | + int Height; |
| 132 | + |
| 133 | + strWidth = prefs.getByKey(GUI_WIDTH, "1"); // 1450 Apple, 1360 others |
| 134 | + strHeight = prefs.getByKey(GUI_HEIGHT, "1"); //850 |
| 135 | + Application.OS_NAMES os = Utils.getCurrentOsName(); |
| 136 | + if ("1".equals(strWidth)) { |
| 137 | + if (os == Application.OS_NAMES.APPLE) { |
| 138 | + Width = 1450; |
| 139 | + } else { |
| 140 | + Width = 850; |
| 141 | + } |
| 142 | + } else { |
| 143 | + Width = Integer.parseInt(strWidth); |
| 144 | + } |
| 145 | + if ("1".equals(strHeight)) { |
| 146 | + Height = 850; |
| 147 | + } else { |
| 148 | + Height = Integer.parseInt(strHeight); |
| 149 | + } |
| 150 | + logger.info("frame.setSize {} x {}", Width, Height); |
| 151 | + frame.setSize(Width, Height); |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | +} |
0 commit comments