001package arcommon.persistens;
002
003import java.awt.Frame;
004
005import arcommon.ui.other.GUIUtil;
006
007public class FrameSettings extends AbstractWindowSettings implements Cloneable {
008    
009    private static final long serialVersionUID = -43470332725489411L;
010    
011    public static final String EVENT_FRAME_SETTINGS_CHANGED = "Frame settings changed";
012    public static final String EVENT_STATE_CHANGED = "State changed";
013    
014    private int state;
015    
016    private transient boolean changedInProcess;
017
018    public FrameSettings() {
019        ;
020    }
021
022    public FrameSettings(int width, int height, int x, int y, int pstate) {
023        super(width, height, x, y);
024        
025        state = pstate;
026    }
027
028    public void loadFrameSettings(final Frame frame) {
029        if (GUIUtil.inFullScreenMode()) {
030            return ;
031        }
032        
033        if (!isInitialized()) {
034            GUIUtil.packAndCenterOnScreen(frame);
035            return ;
036        }
037        
038        changedInProcess = true;
039        frame.setExtendedState(Frame.NORMAL);
040        loadWindowSettings(frame, frame.isResizable());
041        if (state != Frame.NORMAL && state != Frame.ICONIFIED) {
042            frame.setExtendedState(state);
043        }
044        
045        changedInProcess = false;
046    }
047    
048    public void saveFrameSettings(Frame frame) {
049        if (changedInProcess) {
050            return ;
051        }
052        
053        if (GUIUtil.inFullScreenMode()) {
054            return ;
055        }
056        
057        boolean notify = false;
058        if (state != frame.getExtendedState()) {
059            state = frame.getExtendedState();
060            notify = true;
061        }
062        
063        if (state == Frame.NORMAL) {
064            boolean changed = saveWindowSettings(frame, frame.isResizable());
065            if (changed) {
066                notify = false; // If the call to saveWindowSettings() changed something, it also sends notification, therefore it is not necessary to notify here.
067            }
068        }
069        
070        if (notify) {
071            fireActionListeners(EVENT_FRAME_SETTINGS_CHANGED);
072        }
073    }
074    
075    @Override
076    public Object clone() {
077        Object result;
078        try {
079            result = super.clone();
080            ((FrameSettings)result).removeAllActionListeners();
081        } catch (Exception e) {
082            throw new RuntimeException(e);
083        }
084        return result;
085    }
086    
087    public String toString() {
088        return super.toString() + " state: " + state;
089    }
090    
091    public int getState() {
092        return state;
093    }
094
095    public void setState(int pstate) {
096        if (state == pstate) {
097            return ;
098        }
099        
100        state = pstate;
101        fireActionListeners(EVENT_STATE_CHANGED);
102    }
103}