Changeset 187

Show
Ignore:
Timestamp:
02/20/10 17:40:55 (2 years ago)
Author:
stefan
Message:

implemented issue #178: save and restore last window position and size

Location:
trunk
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • trunk/CHANGELOG

    r186 r187  
    33 
    44//== 0.7.9 == 
     5 
     62010-02-20 
     7* [stefan]  * implemented issue #178: save and restore last window position and size 
    58 
    69== 0.7.8 == 
  • trunk/scripts/create_versions_xml

    r142 r187  
    66echo '<?xml version="1.0" encoding="UTF-8"?>' 
    77echo '<files>' 
    8 for file in `print **/(*~versions.xml)(.)`; do 
     8for file in `print **/(*~versions.xml)(.D)`; do 
    99  md5=`md5sum $file | cut -d " " -f 1` 
    1010  len=`du --apparent-size -b $file | cut -f 1` 
  • trunk/src/org/jergometer/gui/MainWindow.java

    r179 r187  
    374374      openSettingsWindow(); 
    375375    } else if (e.getSource() == quitMenuItem) { 
    376       jergometer.quit(); 
     376      System.exit(0); 
    377377    } else if (e.getSource() == aboutMenuItem) { 
    378378      new AboutDialog().showMe(); 
  • trunk/src/org/jergometer/Jergometer.java

    r183 r187  
    1515 
    1616import javax.swing.*; 
    17 import java.awt.event.ActionEvent; 
    18 import java.awt.event.ActionListener; 
    19 import java.awt.event.WindowEvent; 
    20 import java.awt.event.WindowListener; 
     17import java.awt.event.*; 
     18import java.io.File; 
    2119import java.io.IOException; 
    2220import java.util.ArrayList; 
     
    132130    mainWindow.getProgramTree().setModel(programTree); 
    133131    // maximize the main window 
    134     mainWindow.pack(); 
    135     mainWindow.setExtendedState(mainWindow.getExtendedState()|JFrame.MAXIMIZED_BOTH); 
     132    mainWindow.setBounds(jergometerSettings.getMainWindowBounds()); 
     133    mainWindow.setExtendedState(mainWindow.getExtendedState() | jergometerSettings.getMainWindowMaximizedState()); 
    136134    mainWindow.addWindowListener(this); 
    137135    mainWindow.setVisible(true); 
     
    143141    switchToUser(jergometerSettings.getLastUserName()); 
    144142 
    145     /* 
    146     int u = 130; 
    147     int v = 80; 
    148     int w = 100; 
    149     for(int i = 0; i < 3600; i++) { 
    150       u += Math.random() *3 - 1; 
    151       if(u < 30) u = 30; 
    152       if(u > 210) u = 210; 
    153       mainWindow.getDiagram().addValue("pulse", i, u); 
    154       v += Math.random() *3 - 1; 
    155       if(v < 30) v = 30; 
    156       if(v > 210) v = 210; 
    157       mainWindow.getDiagram().addValue("pedalRPM", i, v); 
    158       w += Math.random() *3 - 1; 
    159       if(w < 30) w = 30; 
    160       if(w > 210) w = 210; 
    161       mainWindow.getDiagram().addValue("power", i, w); 
    162     } 
    163     */ 
     143    Runtime.getRuntime().addShutdownHook(new Thread(){ 
     144      @Override 
     145      public void run() { 
     146        quit(); 
     147      } 
     148    }); 
    164149  } 
    165150 
     
    209194  private void save() { 
    210195    stopRecording(); 
     196    jergometerSettings.setMainWindowBounds(mainWindow.getBounds()); 
     197    jergometerSettings.setMainWindowMaximizedState(mainWindow.getExtendedState() & JFrame.MAXIMIZED_BOTH); 
    211198    jergometerSettings.save(); 
    212199    userSettings.save(); 
     
    575562  public void quit() { 
    576563    save(); 
    577     mainWindow.dispose(); 
    578  
    579     System.exit(0); 
     564    //mainWindow.dispose(); 
    580565  } 
    581566 
     
    640625 
    641626  public void windowClosing(WindowEvent e) { 
    642     quit(); 
     627    System.exit(0); 
    643628  } 
    644629 
  • trunk/src/org/jergometer/JergometerSettings.java

    r126 r187  
    11package org.jergometer; 
    22 
     3import de.endrullis.utils.BetterProperties2; 
    34import de.endrullis.utils.StreamUtils; 
    45import de.endrullis.xml.XMLDocument; 
     
    67import de.endrullis.xml.XMLParser; 
    78 
     9import javax.swing.*; 
     10import java.awt.*; 
    811import java.io.*; 
    912import java.util.ArrayList; 
     
    2225  public static final String jergometerExampleProgramsDirName = "programs"; 
    2326  // files 
    24   public static final String settingsFileName = jergometerDirName + "/settings.xml"; 
     27  public static final File oldSettingsFile = new File(jergometerDirName + "/settings.xml"); 
     28  public static final File settingsFile = new File(jergometerDirName + "/settings.properties"); 
     29 
     30  // BetterProperties2 constants 
     31  public static final BetterProperties2.Range INT           = BetterProperties2.INT; 
     32  public static final BetterProperties2.Range INT_GT_0      = BetterProperties2.INT_GT_0; 
     33  public static final BetterProperties2.Range DOUBLE        = BetterProperties2.DOUBLE; 
     34  public static final BetterProperties2.Range DOUBLE_GT_0   = BetterProperties2.DOUBLE_GT_0; 
     35  public static final BetterProperties2.Range DOUBLE_0_TO_1 = BetterProperties2.DOUBLE_0_TO_1; 
     36  public static final BetterProperties2.Range BOOLEAN       = BetterProperties2.BOOLEAN; 
     37  public static final BetterProperties2.Range STRING        = BetterProperties2.STRING; 
     38  public static final BetterProperties2.Range SHORTCUT      = BetterProperties2.SHORTCUT; 
    2539 
    2640 
    2741// dynamic 
    2842 
     43  private BetterProperties2 properties = new BetterProperties2(); 
    2944  private boolean checkForUpdatesOnStart = true; 
     45  private Rectangle mainWindowBounds; 
     46  private int mainWindowMaximizedState; 
    3047  private ArrayList<String> userNames = new ArrayList<String>(); 
    3148  private String lastUserName; 
     
    5976    } 
    6077 
     78    defineProperties(); 
     79 
    6180    // load settings 
    6281    load(); 
    6382  } 
    6483 
     84  private void defineProperties() { 
     85    // set default for the properties file 
     86    properties.addEntry(new BetterProperties2.Comment("\n## General properties")); 
     87    //properties.addEntry(new BetterProperties2.Comment(" Check for updates")); 
     88    properties.addEntry(new BetterProperties2.Def("check_for_updates", BOOLEAN, "true")); 
     89    properties.addEntry(new BetterProperties2.Def("last_user", STRING, null)); 
     90    properties.addEntry(new BetterProperties2.Def("comport", STRING, null)); 
     91    properties.addEntry(new BetterProperties2.Def("xml_editor", STRING, null)); 
     92 
     93    properties.addEntry(new BetterProperties2.Comment("\n## Window properties")); 
     94    properties.addEntry(new BetterProperties2.Comment(" Position, width, and height of the main window")); 
     95    properties.addEntry(new BetterProperties2.Def("main_window.x", INT_GT_0, "0")); 
     96    properties.addEntry(new BetterProperties2.Def("main_window.y", INT_GT_0, "0")); 
     97    properties.addEntry(new BetterProperties2.Def("main_window.width", INT_GT_0, "700")); 
     98    properties.addEntry(new BetterProperties2.Def("main_window.height", INT_GT_0, "500")); 
     99    properties.addEntry(new BetterProperties2.Def("main_window.maximized", INT_GT_0, "" + JFrame.MAXIMIZED_BOTH)); 
     100    /* 
     101    properties.addEntry(new BetterProperties2.Comment(" Width of the symbols panel as part of the main window")); 
     102    properties.addEntry(new BetterProperties2.Def("symbols_panel.width", DOUBLE_0_TO_1, "0.25")); 
     103    properties.addEntry(new BetterProperties2.Comment(" Height of the tools panel as part of the main window")); 
     104    properties.addEntry(new BetterProperties2.Def("tools_panel.height", DOUBLE_0_TO_1, "0.15")); 
     105    */ 
     106 
     107    properties.addEntry(new BetterProperties2.Comment("\n## Shortcuts")); 
     108    properties.addEntry(new BetterProperties2.Comment(" File menu")); 
     109    properties.addEntry(new BetterProperties2.Def("shortcut.new", SHORTCUT, "control N")); 
     110    properties.addEntry(new BetterProperties2.Def("shortcut.open", SHORTCUT, "control O")); 
     111    properties.addEntry(new BetterProperties2.Def("shortcut.save", SHORTCUT, "control S")); 
     112    properties.addEntry(new BetterProperties2.Def("shortcut.close", SHORTCUT, "control W")); 
     113    properties.addEntry(new BetterProperties2.Def("shortcut.exit", SHORTCUT, "")); 
     114 
     115  } 
     116 
    65117  public void load() { 
    66     File settingsFile = new File(settingsFileName); 
    67118    if (settingsFile.exists()) { 
     119      try { 
     120        properties.load(new FileReader(settingsFile)); 
     121      } catch (IOException e) { 
     122        properties.loadDefaults(); 
     123        e.printStackTrace(); 
     124      } 
     125    } else { 
     126      properties.loadDefaults(); 
     127    } 
     128 
     129    // extract variables 
     130    checkForUpdatesOnStart = properties.getBoolean("check_for_updates"); 
     131    mainWindowBounds = new Rectangle( 
     132        properties.getInt("main_window.x"), 
     133        properties.getInt("main_window.y"), 
     134        properties.getInt("main_window.width"), 
     135        properties.getInt("main_window.height") 
     136    ); 
     137    mainWindowMaximizedState = properties.getInt("main_window.maximized"); 
     138 
     139    lastUserName = properties.getString("last_user"); 
     140    comPort = properties.getString("comport"); 
     141    xmlEditor = properties.getString("xml_editor"); 
     142 
     143    if (oldSettingsFile.exists()) { 
    68144      XMLParser parser = new XMLParser(); 
    69145      try { 
    70         XMLDocument doc = parser.parse(StreamUtils.readXmlStream(new FileInputStream(settingsFile))); 
     146        XMLDocument doc = parser.parse(StreamUtils.readXmlStream(new FileInputStream(oldSettingsFile))); 
    71147        XMLElement root = doc.getRootElement(); 
    72148 
     
    81157      } catch (Exception ignored) { 
    82158      } 
    83     } 
    84   } 
     159      save(); 
     160      oldSettingsFile.delete(); 
     161    } 
     162  } 
     163 
     164  /* 
     165  */ 
    85166 
    86167  public void save() { 
    87     XMLElement root = new XMLElement("settings"); 
    88     root.setAttribute("version", "1"); 
    89  
    90     { 
    91       XMLElement update = new XMLElement("update"); 
    92       root.addChildElement(update); 
    93       update.setAttribute("checkOnStart", checkForUpdatesOnStart ? "true" : "false"); 
    94  
    95       XMLElement users = new XMLElement("users"); 
    96       root.addChildElement(users); 
    97       if (lastUserName != null) { 
    98         users.setAttribute("lastUser", lastUserName); 
    99       } 
    100  
    101       XMLElement comport = new XMLElement("comport"); 
    102       root.addChildElement(comport); 
    103       if (comPort != null) { 
    104         comport.setAttribute("name", comPort); 
    105       } 
    106        
    107       XMLElement xmlEditor = new XMLElement("xmlEditor"); 
    108       root.addChildElement(xmlEditor); 
    109       if (this.xmlEditor != null) { 
    110         xmlEditor.setAttribute("name", this.xmlEditor); 
    111       } 
    112     } 
    113  
     168    properties.setBoolean("check_for_updates", checkForUpdatesOnStart); 
     169    properties.setInt("main_window.x", mainWindowBounds.x); 
     170    properties.setInt("main_window.y", mainWindowBounds.y); 
     171    properties.setInt("main_window.width", mainWindowBounds.width); 
     172    properties.setInt("main_window.height", mainWindowBounds.height); 
     173    properties.setInt("main_window.maximized", mainWindowMaximizedState); 
     174 
     175    properties.setString("last_user", lastUserName); 
     176    properties.setString("comport", comPort); 
     177    properties.setString("xml_editor", xmlEditor); 
     178 
     179    settingsFile.getParentFile().mkdirs(); 
     180    try { 
     181      properties.store(new FileOutputStream(settingsFile), 
     182          " JErgometer properties\n" + 
     183          " Default values will be automatically commented out.\n"); 
     184    } catch (Exception e) { 
     185      e.printStackTrace(); 
     186    } 
     187 
     188    /* 
    114189    // write the document 
    115190    XMLDocument doc = new XMLDocument(); 
     
    121196    } catch (IOException ignored) { 
    122197    } 
     198    */ 
    123199  } 
    124200 
     
    133209  } 
    134210 
     211  public Rectangle getMainWindowBounds() { 
     212    return mainWindowBounds; 
     213  } 
     214 
     215  public void setMainWindowBounds(Rectangle mainWindowBounds) { 
     216    this.mainWindowBounds = mainWindowBounds; 
     217  } 
     218 
     219  public int getMainWindowMaximizedState() { 
     220    return mainWindowMaximizedState; 
     221  } 
     222 
     223  public void setMainWindowMaximizedState(int mainWindowMaximizedState) { 
     224    this.mainWindowMaximizedState = mainWindowMaximizedState; 
     225  } 
     226 
    135227  public ArrayList<String> getUserNames() { 
    136228    return userNames;