public FileSave(JComponent aAppFrame, DatabaseHolder aHolder, Preferences aPrefs) { super(GuiUtil.getText("action.filesave")); dbHolder = aHolder; application = aAppFrame; prefs = aPrefs; // Install the icon. final ImageIcon lUIDIcon = new ImageIcon(FileSave.class.getClassLoader().getResource("assets/save.png")); putValue(SMALL_ICON, lUIDIcon); // Decide if the action is enabled or not right now. // Initialize current state using the current database. if (dbHolder.getCurrentDatabase() != null) { setEnabled(dbHolder.getCurrentDatabase().isChanged()); dbHolder .getCurrentDatabase() .addPropertyChangeListener( new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { // Decide if the action is enabled or not right now. if (dbHolder.getCurrentDatabase() != null) setEnabled(dbHolder.getCurrentDatabase().isChanged()); } }); } // Add a listener chain so that the state is maintained if the database changes. dbHolder.addPropertyChangeListener( new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent aHolderEvt) { final ChangeViewDatabase lOldDb = (ChangeViewDatabase) aHolderEvt.getOldValue(); final ChangeViewDatabase lNewDb = (ChangeViewDatabase) aHolderEvt.getNewValue(); // We must not forget to stop listening to the old database. if (lOldDb != null) { lOldDb.removePropertyChangeListener(this); } // We can now investigate the state of the new database and // add a listener to the new database. if (lNewDb != null) { setEnabled(lNewDb.isChanged()); lNewDb.addPropertyChangeListener( new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent aDbEvent) { // Decide if the action is enabled or not right now. if (dbHolder.getCurrentDatabase() != null) setEnabled(dbHolder.getCurrentDatabase().isChanged()); } }); } } }); }
protected void initializeAction( JComponent aAppFrame, DatabaseHolder aHolder, Preferences aPrefs) { dbHolder = aHolder; application = aAppFrame; prefs = aPrefs; // Decide if the action is enabled or not right now. // Initialize current state using the current database. if (dbHolder.getCurrentDatabase() != null) { setEnabled(true); dbHolder .getCurrentDatabase() .addPropertyChangeListener( new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { // Decide if the action is enabled or not right now. setEnabled(dbHolder.getCurrentDatabase() != null); } }); } // Add a listener chain so that the state is maintained if the database changes. dbHolder.addPropertyChangeListener( new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent aHolderEvt) { final ChangeViewDatabase lOldDb = (ChangeViewDatabase) aHolderEvt.getOldValue(); final ChangeViewDatabase lNewDb = (ChangeViewDatabase) aHolderEvt.getNewValue(); // We must not forget to stop listening to the old database. if (lOldDb != null) { lOldDb.removePropertyChangeListener(this); } // We can now investigate the state of the new database and // add a listener to the new database. if (lNewDb != null) { setEnabled(true); lNewDb.addPropertyChangeListener( new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent aDbEvent) { // Decide if the action is enabled or not right now. setEnabled(dbHolder.getCurrentDatabase() != null); } }); } } }); }