Skip to content

Commit

Permalink
Added Preferences support to the EditStatus class. Zoom factor, last …
Browse files Browse the repository at this point in the history
…used directory and bands on are now saved as preferences when exiting the application.
  • Loading branch information
[email protected] committed Jul 11, 2011
1 parent e5e5710 commit cf763ac
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
38 changes: 38 additions & 0 deletions picedit/src/com/agifans/picedit/EditStatus.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.agifans.picedit;

import java.awt.Point;
import java.io.File;
import java.util.LinkedList;
import java.util.prefs.Preferences;

/**
* This class holds everything about the current picture editing status.
Expand All @@ -20,6 +22,8 @@ public class EditStatus {

public static final int LAST_VALUE_NONE = Integer.MIN_VALUE;

private Preferences prefs;

private ToolType tool;

private int visualColour;
Expand Down Expand Up @@ -128,14 +132,40 @@ public class EditStatus {
* The name of the picture being edited.
*/
private String pictureName;

/**
* The name of the most recently used directory.
*/
private String lastUsedDirectory;

/**
* Constructor for EditStatus.
*/
public EditStatus() {
clear();
loadPreferences();
}

/**
* Loads and applies the user preferences related to the Edit Status.
*/
public void loadPreferences() {
prefs = Preferences.userNodeForPackage(this.getClass());

this.lastUsedDirectory = prefs.get("LAST_USED_DIRECTORY", new File(".").getAbsolutePath());
this.zoomFactor = prefs.getInt("ZOOM_FACTOR", 3);
this.bandsOn = prefs.getBoolean("BANDS_ON", false);
}

/**
* Saves the user preferences related to the Edit Status.
*/
public void savePreferences() {
prefs.put("LAST_USED_DIRECTORY", this.lastUsedDirectory);
prefs.putInt("ZOOM_FACTOR", this.zoomFactor);
prefs.putBoolean("BANDS_ON", this.bandsOn);
}

public void clear() {
clear(true);
}
Expand Down Expand Up @@ -678,6 +708,14 @@ public void setPictureName(String pictureName) {
this.pictureName = pictureName;
}

public String getLastUsedDirectory() {
return this.lastUsedDirectory;
}

public void setLastUsedDirectory(String lastUsedDirectory) {
this.lastUsedDirectory = lastUsedDirectory;
}

/**
* Adjusts the mouse point to the coordinate system of the AGI
* picture canvas. The EditStatus doesn't care about anything outside
Expand Down
9 changes: 8 additions & 1 deletion picedit/src/com/agifans/picedit/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Menu(EditStatus editStatus, PicGraphics picGraphics, Picture picture, Pic
// Set up a single JFileChooser for use with all open and save dialogs. This
// allows the directory to be remembered between uses. Default to the current
// directory.
this.fileChooser = new JFileChooser(new File(".").getAbsolutePath());
this.fileChooser = new JFileChooser(editStatus.getLastUsedDirectory());

createMenuItems();
}
Expand Down Expand Up @@ -134,12 +134,15 @@ private void createMenuItems() {
specialMenu.setMnemonic(KeyEvent.VK_S);
JMenuItem backgroundMenuItem = new JCheckBoxMenuItem(MenuOption.BACKGROUND.getDisplayValue());
backgroundMenuItem.setMnemonic(KeyEvent.VK_G);
backgroundMenuItem.setSelected(editStatus.isBackgroundEnabled());
JMenuItem bandsMenuItem = new JCheckBoxMenuItem(MenuOption.BANDS.getDisplayValue());
bandsMenuItem.setMnemonic(KeyEvent.VK_B);
bandsMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, acceleratorKey));
bandsMenuItem.setSelected(editStatus.isBandsOn());
JMenuItem dualModeMenuItem = new JCheckBoxMenuItem(MenuOption.DUAL_MODE.getDisplayValue());
dualModeMenuItem.setMnemonic(KeyEvent.VK_D);
dualModeMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, acceleratorKey));
dualModeMenuItem.setSelected(editStatus.isDualModeEnabled());
backgroundMenuItem.addActionListener(this);
bandsMenuItem.addActionListener(this);
dualModeMenuItem.addActionListener(this);
Expand Down Expand Up @@ -253,6 +256,7 @@ private boolean processMenuSelection(MenuOption menuOption) {
Object[] quitOptions = { "Quit", "Cancel" };
int quitAnswer = JOptionPane.showOptionDialog(application, "Are you sure you want to Quit?", "", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, quitOptions, quitOptions[0]);
if (quitAnswer == JOptionPane.YES_OPTION) {
editStatus.savePreferences();
System.exit(0);
}
break;
Expand Down Expand Up @@ -309,6 +313,9 @@ private boolean processMenuSelection(MenuOption menuOption) {
break;
}

// Store the current directory in the edit status so it is saved in preferences.
editStatus.setLastUsedDirectory(fileChooser.getCurrentDirectory().getAbsolutePath());

return success;
}
}
10 changes: 8 additions & 2 deletions picedit/src/com/agifans/picedit/PicEdit.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.agifans.picedit;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.*;
import javax.swing.*;

Expand Down Expand Up @@ -49,7 +51,6 @@ public final class PicEdit extends JApplet {
@SuppressWarnings("unchecked")
public PicEdit() {
this.editStatus = new EditStatus();
this.editStatus.setZoomFactor(3);
this.picGraphics = new PicGraphics(this, 25);
this.picture = new Picture(editStatus, picGraphics);
this.picturePanel = new PicturePanel(editStatus, picGraphics, picture, this);
Expand Down Expand Up @@ -123,9 +124,14 @@ public void paint(Graphics g) {
* Run PICEDIT as a standalone Java application.
*/
public static void main(String[] args) {
PicEdit app = new PicEdit();
final PicEdit app = new PicEdit();

JFrame frame = new JFrame();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
app.editStatus.savePreferences();
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.getContentPane().add(app, BorderLayout.CENTER);
Expand Down

0 comments on commit cf763ac

Please sign in to comment.