Example #1
0
  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());
                    }
                  });
            }
          }
        });
  }
Example #2
0
  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);
                    }
                  });
            }
          }
        });
  }
Example #3
0
 public void actionPerformed(ActionEvent e) {
   try {
     // Try to write the database.
     final PwsDatabase lDb = dbHolder.getCurrentDatabase();
     if (lDb.getFile() == null) {
       // File is not known, we have to ask it first.
       File lNewFile = GuiUtil.browseForSafe(application, prefs);
       if (lNewFile != null) {
         lDb.setFile(lNewFile);
         GuiUtil.manageBackups(lDb.getFile(), prefs);
         lDb.getCodec().write(lDb);
         lDb.setChanged(false);
       }
     } else {
       // File is known, we can write immediately.
       GuiUtil.manageBackups(lDb.getFile(), prefs);
       lDb.getCodec().write(lDb);
       lDb.setChanged(false);
     }
   } catch (CodecException eCodec) {
     JOptionPane.showMessageDialog(
         application,
         eCodec.getFormattedMessage(),
         GuiUtil.getText("general.error"),
         JOptionPane.ERROR_MESSAGE);
   }
 }
Example #4
0
 public void actionPerformed(ActionEvent e) {
   try {
     if (GuiUtil.continueAndExport(application, prefs)) {
       // Try to write the database.
       final PwsDatabase lDb = dbHolder.getCurrentDatabase();
       final File lExport = askFile();
       if (lExport != null) {
         getCodec().write(new PwsDatabaseFileWrapper(lDb, lExport));
       }
     }
   } catch (CodecException eCodec) {
     JOptionPane.showMessageDialog(
         application, eCodec.getFormattedMessage(), "Error", JOptionPane.ERROR_MESSAGE);
   }
 }