写了书签的支持类,尚未测试

This commit is contained in:
free will committed 2020-11-24 21:11:58 +08:00
1 parent fce3012745
commit 289a072b5b
5 files changed
+283 -8

No files matched your search

@@ -0,0 +1,71 @@
package cn.minbrowser.model;
/*
* @author wefree
*/
import java.util.ArrayList;
import java.util.List;
/**
* 存储所有标签数据
*/
public class Bookmark {
/**
* 存储所有标签页数据
*/
private List<BookmarkModel> bookmarks = new ArrayList<>();
/**
* 获取所有标签页数据
* @return
*/
public List<BookmarkModel> getBookmarks() {
return bookmarks;
}
public void setBookmarks(List<BookmarkModel> bookmarks) {
this.bookmarks = bookmarks;
}
/**
* 放入一个新的标签页
* @param name
* @param url
*/
public void addOneBookmark(String name, String url) {
bookmarks.add(new BookmarkModel(name, url));
}
public void addOneBookmark(BookmarkModel bookmarkModel){
bookmarks.add(bookmarkModel);
}
/**
* 存储单个标签页数据
*/
public static class BookmarkModel {
String name;
String url;
public BookmarkModel(String name, String url) {
this.name = name;
this.url = url;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return this.url;
}
public void setUrl(String url) {
this.url = url;
}
}
}
@@ -0,0 +1,132 @@
package cn.minbrowser.util;
/*
* @author wefree
*/
import cn.minbrowser.model.Bookmark;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
/**
* 书签管理支持类
*/
public class BookMarkUtil {
// 书签数据保存到的地址
public static String bookmarksFilePath=SettingPropertyUtil.settingPath +"BookmarksData";
/**
* 从本地文件中读取所有标签页数据
*/
private static Bookmark readBookMarks(){
Bookmark bookmark=new Bookmark();
try {
FileInputStream inputStream = new FileInputStream(bookmarksFilePath);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String str;
while((str = bufferedReader.readLine()) != null)
{
System.out.println("要解析的标签页数据:"+str);
bookmark.addOneBookmark(readOneBookMark(str));
}
//close
inputStream.close();
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
return bookmark;
}
/**
* 从字符串中解析一条标签页数据
*/
private static Bookmark.BookmarkModel readOneBookMark(String bookmarkString){
String [] arr = bookmarkString.split("\\s+");
return new Bookmark.BookmarkModel(arr[0],arr[1]);
}
/**
* 将所有标签页数据覆盖性写入原文件
* @return
*/
private static void saveWholeBookmarks(Bookmark bookmark){
List<Bookmark.BookmarkModel> bookmarks=bookmark.getBookmarks();
for(int i=0;i<bookmarks.size();i++){
saveNewBookMark(bookmarks.get(i));
}
}
/**
* 存一个标签页的信息到本地
*/
private static void saveNewBookMark(Bookmark.BookmarkModel bookmarkModel){
// 向文件中写入新的标签页
try {
File file = new File(bookmarksFilePath);
FileOutputStream fos = null;
if (!file.exists()) {
file.createNewFile();//如果文件不存在,就创建该文件
fos = new FileOutputStream(file);//首次写入获取
} else {
//如果文件已存在,那么就在文件末尾追加写入
fos = new FileOutputStream(file, true);//这里构造方法多了一个参数true,表示在文件末尾追加写入
}
OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);//指定以UTF-8格式写入文件
osw.write(URLEncoder.encode(bookmarkModel.getName())+" "
+URLEncoder.encode(bookmarkModel.getUrl()));
osw.write("\r\n");
//写入完成关闭流
osw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 存一个标签页的信息到本地
*/
public static void saveBookMark(Bookmark.BookmarkModel bookmarkModel){
// 读取本地标签页,判断新标签页是否已经存在
boolean flag=false;
Bookmark bookmark=readBookMarks();
List<Bookmark.BookmarkModel> bookmarks=bookmark.getBookmarks();
for(int i=0;i<bookmarks.size();i++){
if(bookmarks.get(i).getUrl().equals(bookmarkModel.getUrl())){
flag=true;
}
}
// 不存在,则向文件中写入新的标签页
if(!flag){
saveNewBookMark(bookmarkModel);
}
}
/**
* 从本地文件中移除某个标签页的数据
*/
public static void rmBookMark(Bookmark.BookmarkModel bookmarkModel){
// 读取本地标签页,判断该标签页是否已经存在
boolean flag=false;
Bookmark bookmark=readBookMarks();
List<Bookmark.BookmarkModel> bookmarks=bookmark.getBookmarks();
for(int i=0;i<bookmarks.size();i++){
if(bookmarks.get(i).getUrl().equals(bookmarkModel.getUrl())){
flag=true;
bookmarks.remove(i);
i--;
}
}
// 存在,则移除它(删除该Bookmark文件中的这条标签,然后覆盖掉原文件)
if(flag){
bookmark.setBookmarks(bookmarks);
saveWholeBookmarks(bookmark);
}
}
}
@@ -29,6 +29,5 @@ public class LoggerUtil {
} catch (Exception e) {
e.printStackTrace();
}
}
}
@@ -11,9 +11,7 @@ import javafx.application.Platform;
import javafx.collections.ListChangeListener;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
@@ -40,6 +38,19 @@ public class NavigationBarController implements Initializable {
private final Double tabHeaderWidth = 200D;
// 标记“三点”列表的显示状态
private boolean isShowingSettingList;
// 标记收藏标签页的弹窗的显示状态
private boolean isShowingCollectingBookmark;
// 收藏标签页
@FXML
private TextField bookmarkName;
@FXML
private TextField bookmarkAddress;
@FXML
private AnchorPane bookmarkPane;
public NavigationBarController() {
}
@@ -72,6 +83,7 @@ public class NavigationBarController implements Initializable {
setTabPaneListener();
// 不显示设置页面
hideSettingPane();
hideBookmark();
}
/**
@@ -246,25 +258,71 @@ public class NavigationBarController implements Initializable {
hideSettingPane();
}
/**
* 设置列表控件**********************************************************
*/
/**
* 展示设置列表
*/
public void showSettingPane(){
AnchorPane.setRightAnchor(SettingPane,30D);
SettingPane.setVisible(true);
if(!isShowingSettingList) {
AnchorPane.setRightAnchor(SettingPane, 30D);
SettingPane.setVisible(true);
isShowingSettingList = true;
}else{
hideSettingPane();
}
}
/**
* 隐藏设置列表
*/
public void hideSettingPane(){
SettingPane.setVisible(false);
isShowingSettingList=false;
}
/**
* 退出系统
*/
public void exitSystem(){
System.exit(0);
}
/**
* 收藏标签页控件*****************************
*/
/**
* 显示书签管理窗口
* @param name
* @param url
* @param userData 编辑传需要修改的节点
*/
public void showBookmarkPane(String name, String url, Object userData) {
if(!isShowingCollectingBookmark) {
Double width = bookmarkPane.getScene().getWidth();
AnchorPane.setLeftAnchor(bookmarkPane, 500D);
bookmarkAddress.setText(url);
bookmarkName.setText(name);
bookmarkPane.setVisible(true);
isShowingCollectingBookmark=true;
bookmarkPane.setUserData(userData);
}else{
hideBookmark();
}
}
/**
* 隐藏书签管理窗口
*/
@FXML
public void hideBookmark() {
bookmarkPane.setVisible(false);
isShowingCollectingBookmark=false;
}
/**
* 保存书签
*/
@FXML
private void saveBookmark() {
}
}
@@ -27,6 +27,21 @@
</ImageView>
<Button fx:id="settingButton" mnemonicParsing="false" onAction="#showSettingPane" opacity="0.1" prefHeight="40.0" prefWidth="30.0" text="S" AnchorPane.rightAnchor="18.0" AnchorPane.topAnchor="41.0" />
<Button fx:id="addTabButton" mnemonicParsing="false" text="+" AnchorPane.leftAnchor="10.0" AnchorPane.topAnchor="8.0" />
<AnchorPane fx:id="bookmarkPane" layoutY="89.0" prefHeight="139.0" prefWidth="344.0" styleClass="inner_pane" visible="false" AnchorPane.leftAnchor="500.0" AnchorPane.topAnchor="80.0">
<children>
<Label alignment="CENTER" contentDisplay="CENTER" layoutX="2.0" layoutY="2.0" prefHeight="30.0" prefWidth="342.0" text="书签管理" textAlignment="CENTER" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="1.0" AnchorPane.topAnchor="1.0">
<font>
<Font size="15.0" />
</font>
</Label>
<Label layoutX="6.0" layoutY="34.0" text="名称" AnchorPane.leftAnchor="6.0" AnchorPane.topAnchor="34.0" />
<Label layoutX="6.0" layoutY="69.0" text="地址" AnchorPane.leftAnchor="6.0" AnchorPane.topAnchor="69.0" />
<TextField fx:id="bookmarkName" layoutX="40.0" layoutY="34.0" prefHeight="23.0" prefWidth="299.0" AnchorPane.topAnchor="34.0" />
<TextField fx:id="bookmarkAddress" layoutX="40.0" layoutY="69.0" prefHeight="23.0" prefWidth="299.0" AnchorPane.topAnchor="69.0" />
<Button layoutX="145.0" layoutY="104.0" mnemonicParsing="false" onAction="#saveBookmark" text="保存" AnchorPane.topAnchor="104.0" />
<Button layoutX="284.0" layoutY="3.0" mnemonicParsing="false" onAction="#hideBookmark" text="X" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
<AnchorPane fx:id="SettingPane" prefHeight="350.0" prefWidth="250.0" AnchorPane.rightAnchor="30.0" AnchorPane.topAnchor="80.0">
<children>
<ScrollPane prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">