Skip to content
Browse files

added file information to context

  • Loading branch information...
1 parent e9b2654 commit 652d0ed5381044b370fedf36efdefbb9d7cf68f8 @fuxi83 committed
View
6 Droplet/src/com/stefanbrenner/droplet/model/IDropletContext.java
@@ -21,6 +21,8 @@
import gnu.io.CommPortIdentifier;
+import java.io.File;
+
/**
* @author Stefan Brenner
*/
@@ -38,4 +40,8 @@
public abstract IDroplet getDroplet();
+ public abstract void setFile(File file);
+
+ public abstract File getFile();
+
}
View
2 Droplet/src/com/stefanbrenner/droplet/model/internal/DropletContext.java
@@ -38,10 +38,12 @@
private IDroplet droplet;
+ @Override
public File getFile() {
return file;
}
+ @Override
public void setFile(File file) {
this.file = file;
}
View
2 Droplet/src/com/stefanbrenner/droplet/ui/DropletMenu.java
@@ -90,7 +90,7 @@ private void buildNewMenu() {
// create actions
newAction = new NewAction(dropletContext);
openAction = new OpenFileAction(this, fileChooser, dropletContext);
- saveAction = new SaveFileAction(dropletContext);
+ saveAction = new SaveFileAction(this, fileChooser, dropletContext);
saveAsAction = new SaveAsFileAction(this, fileChooser, dropletContext);
// create menu items
View
1 Droplet/src/com/stefanbrenner/droplet/ui/actions/NewAction.java
@@ -47,6 +47,7 @@ public void actionPerformed(ActionEvent event) {
// TODO brenner: remove this initialization
droplet.initializeWithDefaults();
getDropletContext().setDroplet(droplet);
+ getDropletContext().setFile(null);
}
}
View
1 Droplet/src/com/stefanbrenner/droplet/ui/actions/OpenFileAction.java
@@ -74,6 +74,7 @@ public void actionPerformed(ActionEvent event) {
// set droplet to context
getDropletContext().setDroplet(droplet);
+ getDropletContext().setFile(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
View
21 Droplet/src/com/stefanbrenner/droplet/ui/actions/SaveAsFileAction.java
@@ -20,7 +20,6 @@
package com.stefanbrenner.droplet.ui.actions;
import java.awt.event.ActionEvent;
-import java.io.File;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
@@ -33,14 +32,8 @@
@SuppressWarnings("serial")
public class SaveAsFileAction extends SaveFileAction {
- private final JComponent parent;
- private final JFileChooser fileChooser;
-
public SaveAsFileAction(JComponent parent, JFileChooser fileChooser, IDropletContext dropletContext) {
- super(dropletContext, "Save As...");
-
- this.fileChooser = fileChooser;
- this.parent = parent;
+ super("Save As...", parent, fileChooser, dropletContext);
putValue(SHORT_DESCRIPTION, "Save Droplet Configuration in new file");
@@ -48,17 +41,7 @@ public SaveAsFileAction(JComponent parent, JFileChooser fileChooser, IDropletCon
@Override
public void actionPerformed(ActionEvent event) {
- int returnVal = fileChooser.showSaveDialog(parent);
- if (returnVal == JFileChooser.APPROVE_OPTION) {
-
- // Get the selected file
- File file = fileChooser.getSelectedFile();
-
- // TODO brenner: automatically add file extension
- // TODO brenner: warn before overwrite
- saveFile(file);
-
- }
+ showFileChooser();
}
};
View
39 Droplet/src/com/stefanbrenner/droplet/ui/actions/SaveFileAction.java
@@ -26,6 +26,8 @@
import java.io.FileWriter;
import java.io.IOException;
+import javax.swing.JComponent;
+import javax.swing.JFileChooser;
import javax.xml.bind.JAXBException;
import com.stefanbrenner.droplet.model.IDroplet;
@@ -39,26 +41,47 @@
@SuppressWarnings("serial")
public class SaveFileAction extends AbstractDropletAction {
- public SaveFileAction(IDropletContext dropletContext) {
- this(dropletContext, "Save");
+ private final JComponent parent;
+ private final JFileChooser fileChooser;
+
+ public SaveFileAction(JComponent parent, JFileChooser fileChooser, IDropletContext dropletContext) {
+ this("Save", parent, fileChooser, dropletContext);
putValue(ACCELERATOR_KEY, UiUtils.getAccelerator(KeyEvent.VK_S));
putValue(SHORT_DESCRIPTION, "Save Droplet Configuration");
}
- public SaveFileAction(IDropletContext dropletContext, String name) {
+ public SaveFileAction(String name, JComponent parent, JFileChooser fileChooser, IDropletContext dropletContext) {
super(dropletContext, name);
+ this.parent = parent;
+ this.fileChooser = fileChooser;
}
@Override
public void actionPerformed(ActionEvent event) {
+ File file = getDropletContext().getFile();
+ if (file == null) {
+ showFileChooser();
+ } else {
+ saveFile(file);
+ }
+ }
+
+ protected void showFileChooser() {
+ int returnVal = fileChooser.showSaveDialog(parent);
+ if (returnVal == JFileChooser.APPROVE_OPTION) {
- // TODO brenner: where do we store the file information?
- // TODO brenner: show file chooser if we don't already have a file
- // information
- File file = new File("test.drp");
- saveFile(file);
+ // Get the selected file
+ File file = fileChooser.getSelectedFile();
+ // add file to context
+ getDropletContext().setFile(file);
+
+ // TODO brenner: automatically add file extension
+ // TODO brenner: warn before overwrite
+ saveFile(file);
+
+ }
}
protected void saveFile(File file) {

0 comments on commit 652d0ed

Please sign in to comment.
Something went wrong with that request. Please try again.