Skip to content

Commit

Permalink
添加另存文件功能
Browse files Browse the repository at this point in the history
  • Loading branch information
plter committed Nov 24, 2015
1 parent c5adaf1 commit 4653e07
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 13 deletions.
Binary file modified ANETool2015/ExampleFlashAppForAndroidHello/libs/FlashLib.ane
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,11 @@ public void btnSaveConfigFileClickedHandler(ActionEvent actionEvent) {

if (currentConfigFile != null) {
try {
if (!currentConfigFile.exists()) {
currentConfigFile.createNewFile();
}

FileOutputStream fos = new FileOutputStream(currentConfigFile);
fos.write(makeAneConfigInfo().toJSONString().getBytes(Config.DEFAULT_CHARSET));
fos.flush();
fos.close();

FileTool.writeContentToFile(makeAneConfigInfo().toJSONString(),currentConfigFile);
Log.info("保存文件成功");
} catch (IOException e) {
e.printStackTrace();
Log.error("保存文件失败");
}
}
}
Expand Down Expand Up @@ -237,4 +230,33 @@ public void btnOpenConfigFileClickedHandler(ActionEvent actionEvent) {
syncUIStatusWithConfigFile(file);
}
}

public void btnSaveNewConfigFileClickedHandler(ActionEvent actionEvent) {
if (currentConfigFile==null){
btnSaveConfigFileClickedHandler(null);
}else {
FileChooser fc = new FileChooser();
fc.setTitle("请选择配置文件保存地址");
fc.setInitialFileName("CopyOf"+currentConfigFile.getName());
File result = fc.showSaveDialog(getWindow());

if (result!=null){
currentConfigFile = result;

try {
FileTool.writeContentToFile(makeAneConfigInfo().toJSONString(),currentConfigFile);

RecentFilesListCellData newItem = new RecentFilesListCellData(currentConfigFile);
lvRecentConfigFiles.addItem(newItem);
lvRecentConfigFiles.getSelectionModel().select(newItem);

Log.info("另存文件成功");
} catch (IOException e) {
e.printStackTrace();

Log.error("另存文件失败");
}
}
}
}
}
19 changes: 19 additions & 0 deletions ANETool2015/src/com/plter/anetool/utils/FileTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,23 @@ public static String getFileContent(File file) throws IOException {
return getFileContent(file, "UTF-8");
}


public static void writeContentToFile(String content,String charset,File file) throws IOException {
FileOutputStream fos = new FileOutputStream(file);
fos.write(content.getBytes(charset));
fos.flush();
fos.close();
}


/**
* Write string data to file with charset utf-8
* @param content
* @param file
* @throws IOException
*/
public static void writeContentToFile(String content,File file) throws IOException {
writeContentToFile(content,"UTF-8",file);
}

}
1 change: 1 addition & 0 deletions ANETool2015/src/com/plter/anetool/views/MainView.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<HBox style="-fx-spacing: 5px;-fx-alignment: center-left">
<Button text="打开" onAction="#btnOpenConfigFileClickedHandler"/>
<Button text="保存" onAction="#btnSaveConfigFileClickedHandler"/>
<Button text="另存为" onAction="#btnSaveNewConfigFileClickedHandler"/>
<Button text="捐助" onAction="#btnDonateClickHandler"/>
<Button text="关于" onAction="#btnAboutClickHandler"/>

Expand Down
10 changes: 6 additions & 4 deletions ANETool2015/src/com/plter/anetool/views/RecentFilesListView.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,26 @@ public void addItem(RecentFilesListCellData data) {

int index = items.indexOf(data);

if (index != -1) {
if (index > 0) {
items.remove(index);
items.add(0, data);
}else if (index==-1){
items.add(0, data);
}

items.add(0, data);
saveRecentFiles();
}

private void saveRecentFiles() {
ObservableList<RecentFilesListCellData> items = getItems();
if (items.size()>0) {
if (items.size() > 0) {
prefs.put("recentFiles", ArrayTool.join(items.stream().map(RecentFilesListCellData::getAbsolutePath).collect(Collectors.toList()), "\n"));
try {
prefs.flush();
} catch (BackingStoreException e) {
e.printStackTrace();
}
}else {
} else {
try {
prefs.clear();
} catch (BackingStoreException e) {
Expand Down

0 comments on commit 4653e07

Please sign in to comment.