1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| /**
* Bind {@link Preferences} to {@link JFrame}.
* By default the frame will be placed on screen center
* and given default values if no corresponding preferences are found.
*
* @param prefs The {@link Preferences} to use.
* @param frame The {@link JFrame}.
* @param identifier The unique identifier (optional).
* @param defaultWidth The default width to use if no pref found.
* @param defaultHeight The default height to use if no pref found.
* @param defaultState The default extended state to use if no pref found.
*/
public static void bind(
final Preferences prefs,
final JFrame frame,
final String identifier,
final int defaultWidth,
final int defaultHeight,
final int defaultState) {
// - keys
final String keyWidth = buildKey(KEY_FRAME_WIDTH, identifier);
final String keyHeight = buildKey(KEY_FRAME_HEIGHT, identifier);
final String keyLocationX = buildKey(KEY_FRAME_LOCATION_X, identifier);
final String keyLocationY = buildKey(KEY_FRAME_LOCATION_Y, identifier);
final String keyExtendedState = buildKey(KEY_FRAME_EXTENDED_STATE, identifier);
// - set values
frame.setSize(prefs.getInt(keyWidth, defaultWidth), prefs.getInt(keyHeight, defaultHeight));
if (prefs.getInt(keyLocationX, -1) < 0 || prefs.getInt(keyLocationY, -1) < 0) {
locateOnScreen(frame);
} else {
locateOnScreen(frame, prefs.getInt(keyLocationX, 1), prefs.getInt(keyLocationY, 1));
}
frame.setExtendedState(prefs.getInt(keyExtendedState, defaultState));
// - bind values
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
frame.setExtendedState(prefs.getInt(keyExtendedState, defaultState));
}
@Override
public void windowClosing(WindowEvent e) {
try {
prefs.flush();
} catch (BackingStoreException e1) {
s_logger.warn("Cannot flush Preferences", e1);
}
}
@Override
public void windowStateChanged(WindowEvent e) {
prefs.putInt(keyExtendedState, frame.getExtendedState());
}
});
frame.addComponentListener(new ComponentListener() {
public void componentHidden(ComponentEvent e) { }
public void componentMoved(ComponentEvent e) {
prefs.putInt(keyExtendedState, frame.getExtendedState());
if ((frame.getExtendedState() & JFrame.MAXIMIZED_BOTH) != JFrame.MAXIMIZED_BOTH) {
prefs.putInt(keyLocationX, e.getComponent().getX());
prefs.putInt(keyLocationY, e.getComponent().getY());
}
}
public void componentResized(ComponentEvent e) {
prefs.putInt(keyExtendedState, frame.getExtendedState());
if ((frame.getExtendedState() & JFrame.MAXIMIZED_BOTH) != JFrame.MAXIMIZED_BOTH) {
prefs.putInt(keyWidth, e.getComponent().getWidth());
prefs.putInt(keyHeight, e.getComponent().getHeight());
}
}
public void componentShown(ComponentEvent e) { }
});
InputMap inputMap = frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, KeyEvent.CTRL_DOWN_MASK), "deleteParams");
frame.getRootPane().getActionMap().put("deleteParams", new AbstractAction("deleteParams") {
public void actionPerformed(ActionEvent e) {
try {
prefs.clear();
} catch (BackingStoreException e1) {
s_logger.warn("Cannot clear Preferences", e1);
}
}
});
}
/**
* Locates the window on the screen center.
*/
public static void locateOnScreen(Window window) {
Dimension paneSize = window.getSize();
Dimension screenSize = window.getToolkit().getScreenSize();
window.setLocation(
(screenSize.width - paneSize.width) / 2,
(screenSize.height - paneSize.height) / 2);
}
/**
* Locates the given component at (x,y)
* @param component
* @param x absisse coin sup gauche (si null --> centré)
* @param y ordonnée coin sup gauche (si null --> centré)
*/
public static void locateOnScreen(Component component, Integer x, Integer y) {
Dimension paneSize = component.getSize();
Dimension screenSize = component.getToolkit().getScreenSize();
component.setLocation((x != null) ? x : (screenSize.width - paneSize.width) / 2,
(y != null) ? y : (screenSize.height - paneSize.height) / 2);
} |
Partager