🚩 完善文件图标支持

This commit is contained in:
gewuyou 2023-10-10 14:07:44 +08:00
parent 69f0381167
commit 6920774e64
53 changed files with 197 additions and 30 deletions

View File

@ -1,5 +1,5 @@
<p align="center">
<img src="src/main/resources/img/icon.svg" alt="JNotepad Icon">
<img src="src/main/resources/jcnc/app/svg/icon.svg" alt="JNotepad Icon">
<h1 align="center" style="margin: 30px 0 30px; font-weight: bold;">JNotepad</h1>
<h4 align="center" style="margin: 30px 0 30px; font-weight: bold;">JavaFX开发插件驱动创造无限可能</h4>

View File

@ -21,7 +21,7 @@ public class UiResourceBundle {
/**
* resource目录下的i18n/i18nXXX.properties
*/
private static final String BASENAME = "i18n/i18n";
private static final String BASENAME = "jcnc/app/i18n/i18n";
/**
* 资源文件的观察者绑定
*/

View File

@ -106,7 +106,7 @@ public class ApplicationManager {
double width = AppConstants.SCREEN_WIDTH;
double length = AppConstants.SCREEN_LENGTH;
scene = new Scene(root, width, length);
scene.getStylesheets().add(Objects.requireNonNull(application.getClass().getResource("/css/styles.css")).toExternalForm());
scene.getStylesheets().add(Objects.requireNonNull(application.getClass().getResource("/jcnc/app/css/styles.css")).toExternalForm());
}
private void initPrimaryStage() {

View File

@ -33,7 +33,7 @@ public class AppConstants {
/**
* logo地址
*/
public static final String APP_ICON = "/img/icon.png";
public static final String APP_ICON = "/jcnc/app/images/appIcons/icon.png";
/**
* 默认标签页的正则

View File

@ -107,7 +107,7 @@ public class TextCodeArea extends CodeArea {
}
}
});
this.getStylesheets().add(Objects.requireNonNull(getClass().getResource("/css/java_code_styles.css")).toString());
this.getStylesheets().add(Objects.requireNonNull(getClass().getResource("/jcnc/app/css/java_code_styles.css")).toString());
}

View File

@ -1,5 +1,6 @@
package org.jcnc.jnotepad.model.entity;
import javafx.scene.Node;
import org.kordamp.ikonli.javafx.FontIcon;
import java.util.List;
@ -16,9 +17,9 @@ public class DirFileModel {
private String path;
private String name;
private FontIcon iconIsNotSelected;
private Node iconIsNotSelected;
private FontIcon iconIsSelected;
private Node iconIsSelected;
private List<DirFileModel> childFile;
@ -31,6 +32,14 @@ public class DirFileModel {
this.iconIsSelected = iconIsSelected;
}
public DirFileModel(String path, String name, List<DirFileModel> childFile, Node iconIsNotSelected, Node iconIsSelected) {
this.path = path;
this.name = name;
this.childFile = childFile;
this.iconIsNotSelected = iconIsNotSelected;
this.iconIsSelected = iconIsSelected;
}
public List<DirFileModel> getChildFile() {
return childFile;
@ -57,7 +66,7 @@ public class DirFileModel {
return name;
}
public FontIcon getIconIsNotSelected() {
public Node getIconIsNotSelected() {
return iconIsNotSelected;
}
@ -65,7 +74,7 @@ public class DirFileModel {
this.iconIsNotSelected = iconIsNotSelected;
}
public FontIcon getIconIsSelected() {
public Node getIconIsSelected() {
return iconIsSelected;
}

View File

@ -158,7 +158,7 @@ public class FileUtil {
dirFileModel.getChildFile().add(childDirFileModel);
} else {
// todo 在此监测文件后缀设置对应的图标
dirFileModel.getChildFile().add(new DirFileModel(f.getAbsolutePath(), f.getName(), null, new FontIcon(FILE), new FontIcon(FILE)));
dirFileModel.getChildFile().add(new DirFileModel(f.getAbsolutePath(), f.getName(), null, getIconCorrespondingToFileName(f.getName()), new FontIcon(FILE)));
}
}
}
@ -293,21 +293,13 @@ public class FileUtil {
/**
* Retrieves the icon corresponding to the given file name.
*
* @param tabTitle the title of the tab
* @return the icon node corresponding to the file name
* @param fileName the file name
* @return the corresponding icon for the file extension
*/
public static Node getIconCorrespondingToFileName(String tabTitle) {
public static Node getIconCorrespondingToFileName(String fileName) {
// todo 在此根据文件缀名获取对应的图标
String fileExtension = tabTitle.substring(tabTitle.lastIndexOf(".") + 1);
return switch (fileExtension.toLowerCase()) {
case "txt" -> FontIcon.of(FILE_TEXT);
case "doc" -> FontIcon.of(FILE_WORD);
case "pdf" -> FontIcon.of(FILE_PDF);
case "ppt" -> FontIcon.of(FILE_PPT);
case "xls" -> FontIcon.of(FILE_EXCEL);
case "md" -> FontIcon.of(FILE_MARKDOWN);
default -> FontIcon.of(FILE_UNKNOWN);
};
String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1);
return UiUtil.getIconMap().getOrDefault(fileExtension, FontIcon.of(FILE_UNKNOWN));
}
}

View File

@ -0,0 +1,70 @@
package org.jcnc.jnotepad.util;
import org.jcnc.jnotepad.JnotepadApp;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.Objects;
/**
* 资源工具
*
* @author gewuyou
*/
public class ResourceUtil {
public static final String MODULE_DIR = "/jcnc/app/";
private ResourceUtil() {
}
/**
* Retrieves an input stream for the specified resource.
*
* @param resource the path to the resource
* @return the input stream for the resource
*/
public static InputStream getResourceAsStream(String resource) {
String path = resolve(resource);
return Objects.requireNonNull(
JnotepadApp.class.getResourceAsStream(resolve(path)),
"Resource not found: " + path
);
}
/**
* Retrieves the resource with the specified path.
*
* @param resource the path of the resource to retrieve
* @return the URI of the retrieved resource
*/
public static URI getResource(String resource) {
String path = resolve(resource);
URL url = Objects.requireNonNull(JnotepadApp.class.getResource(resolve(path)), "Resource not found: " + path);
return URI.create(url.toExternalForm());
}
/**
* Resolves a resource path by checking if it starts with a "/". If it does,
* the resource path is returned as is. If it doesn't, the resource path is
* concatenated with the module directory path.
*
* @param resource the resource path to be resolved
* @param moduleDir the module directory path
* @return the resolved resource path
*/
public static String resolve(String resource, String moduleDir) {
Objects.requireNonNull(resource);
return resource.startsWith("/") ? resource : moduleDir + resource;
}
/**
* Resolve the given resource using the default module directory.
*
* @param resource the resource to resolve
* @return the resolved resource
*/
public static String resolve(String resource) {
return resolve(resource, MODULE_DIR);
}
}

View File

@ -1,12 +1,16 @@
package org.jcnc.jnotepad.util;
import atlantafx.base.theme.Styles;
import javafx.scene.Node;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Window;
import org.jcnc.jnotepad.app.manager.ApplicationManager;
import org.jcnc.jnotepad.common.constants.AppConstants;
import org.kordamp.ikonli.javafx.FontIcon;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import static org.kordamp.ikonli.antdesignicons.AntDesignIconsFilled.*;
@ -43,6 +47,10 @@ public class UiUtil {
private static final FontIcon QUESTION_ICON = FontIcon.of(QUESTION_CIRCLE);
private static final FontIcon SUCCESS_ICON = FontIcon.of(CHECK_CIRCLE);
/**
* 图标集合
*/
private static final Map<String, Node> ICON_MAP = new HashMap<>(32);
static {
// 暂时设置颜色
@ -51,6 +59,39 @@ public class UiUtil {
QUESTION_ICON.getStyleClass().addAll(Styles.ACCENT);
WARNING_ICON.getStyleClass().addAll(Styles.WARNING);
SUCCESS_ICON.getStyleClass().addAll(Styles.SUCCESS);
ICON_MAP.put("css", fileIconByPng("css"));
ICON_MAP.put("doc", fileIconByPng("doc"));
ICON_MAP.put("dll", fileIconByPng("dll"));
ICON_MAP.put("exe", fileIconByPng("exe"));
ICON_MAP.put("gif", fileIconByPng("gif"));
ICON_MAP.put("gitignore", fileIconByPng("git"));
ICON_MAP.put("html", fileIconByPng("html"));
ICON_MAP.put("json", fileIconByPng("json"));
ICON_MAP.put("md", fileIconByPng("markdown"));
ICON_MAP.put("pdf", FontIcon.of(FILE_PDF));
ICON_MAP.put("ppt", FontIcon.of(FILE_PPT));
ICON_MAP.put("png", fileIconByPng("png"));
ICON_MAP.put("sql", fileIconByPng("database"));
ICON_MAP.put("svg", fileIconByPng("svg"));
ICON_MAP.put("txt", FontIcon.of(FILE_TEXT));
ICON_MAP.put("xls", FontIcon.of(FILE_EXCEL));
ICON_MAP.put("xml", fileIconByPng("xml"));
// 编程语言
ICON_MAP.put("bat", fileIconByPng("bat"));
ICON_MAP.put("c", fileIconByPng("c"));
ICON_MAP.put("cs", fileIconByPng("csharp"));
ICON_MAP.put("cpp", fileIconByPng("cplusplus"));
ICON_MAP.put("go", fileIconByPng("golang"));
ICON_MAP.put("js", fileIconByPng("js"));
ICON_MAP.put("java", fileIconByPng("java"));
ICON_MAP.put("kt", fileIconByPng("kotlin"));
ICON_MAP.put("lua", fileIconByPng("lua"));
ICON_MAP.put("py", fileIconByPng("python"));
ICON_MAP.put("php", fileIconByPng("php"));
ICON_MAP.put("rb", fileIconByPng("ruby"));
ICON_MAP.put("sh", fileIconByPng("sh"));
}
private UiUtil() {
@ -121,5 +162,61 @@ public class UiUtil {
return ApplicationManager.getInstance().getWindow();
}
/**
* Generates an ImageView with the specified module directory, name, and format.
*
* @param moduleDir the directory where the module is located
* @param name the name of the icon
* @param format the format of the icon
* @return the generated ImageView
*/
public static ImageView icon(String moduleDir, String name, String format) {
return icon(moduleDir + name + format);
}
/**
* Generates an ImageView object with the image specified by the given path.
*
* @param path the path to the image file
* @return the ImageView object with the specified image
*/
public static ImageView icon(String path) {
var img = new Image(ResourceUtil.getResourceAsStream(path));
return new ImageView(img);
}
/**
* Generates an ImageView based on a PNG file.
*
* @param moduleDir the directory of the module
* @param name the name of the PNG file
* @return the generated ImageView
*/
public static ImageView iconByPng(String moduleDir, String name) {
return icon(moduleDir + name + ".png");
}
/**
* Generates an ImageView object for a file icon based on the given PNG name.
*
* @param name the name of the PNG file for the file icon
* @return the ImageView object representing the file icon
*/
public static ImageView fileIconByPng(String name) {
return iconByPng("images/fileIcons/", name);
}
/**
* Generates an ImageView object for a file icon based on the given PNG name.
*
* @param name the name of the PNG file for the file icon
* @return the ImageView object representing the file icon
*/
public static ImageView sidebarIconByPng(String name) {
return iconByPng("images/sidebarIcons/", name);
}
public static Map<String, Node> getIconMap() {
return ICON_MAP;
}
}

View File

@ -1,13 +1,12 @@
package org.jcnc.jnotepad.views.manager;
import javafx.scene.Node;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import org.jcnc.jnotepad.api.core.views.manager.AbstractManager;
import org.jcnc.jnotepad.api.core.views.manager.builder.SideBarButtonBuilder;
import org.jcnc.jnotepad.controller.event.handler.toolbar.DirTreeBtn;
import org.jcnc.jnotepad.controller.event.handler.toolbar.RunBtn;
import org.jcnc.jnotepad.controller.event.handler.toolbar.SetBtn;
import org.jcnc.jnotepad.util.UiUtil;
import org.jcnc.jnotepad.views.root.left.sidebar.tools.SidebarToolBar;
import java.util.ArrayList;
@ -42,7 +41,7 @@ public class SidebarToolBarManager extends AbstractManager<Node> {
registerNode(
new SideBarButtonBuilder()
.setButton(sidebarToolBar.getSetButton())
.setImageView(new ImageView(new Image("tools.png")))
.setImageView(UiUtil.sidebarIconByPng("tools"))
.setImageViewEssentialAttribute(10D, 10D, true, 2.5D, 2.5D)
.setButtonEssentialAttribute(20D, 20D)
.setEventHandler(new SetBtn()).build());
@ -50,7 +49,7 @@ public class SidebarToolBarManager extends AbstractManager<Node> {
registerNode(
new SideBarButtonBuilder()
.setButton(sidebarToolBar.getDirTreeButton())
.setImageView(new ImageView(new Image("directory.png")))
.setImageView(UiUtil.sidebarIconByPng("directory"))
.setImageViewEssentialAttribute(10D, 10D, true, 2.5D, 2.5D)
.setButtonEssentialAttribute(20D, 20D)
.setEventHandler(new DirTreeBtn()).build());
@ -60,7 +59,7 @@ public class SidebarToolBarManager extends AbstractManager<Node> {
registerNode(
new SideBarButtonBuilder()
.setButton(sidebarToolBar.getRunButton())
.setImageView(new ImageView(new Image("cmd.png")))
.setImageView(UiUtil.sidebarIconByPng("cmd"))
.setImageViewEssentialAttribute(10D, 10D, true, 2.5D, 2.5D)
.setButtonEssentialAttribute(20D, 20D)
.setEventHandler(new RunBtn()).build());

View File

Before

Width:  |  Height:  |  Size: 38 KiB

After

Width:  |  Height:  |  Size: 38 KiB

View File

Before

Width:  |  Height:  |  Size: 188 KiB

After

Width:  |  Height:  |  Size: 188 KiB

View File

Before

Width:  |  Height:  |  Size: 381 B

After

Width:  |  Height:  |  Size: 381 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 424 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 309 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 694 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 498 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 598 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 494 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 256 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 513 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 361 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 300 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 369 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 246 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 596 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 442 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 398 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 229 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 625 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 369 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 382 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 384 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 326 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 610 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 407 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 334 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 302 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 274 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 540 B

View File

Before

Width:  |  Height:  |  Size: 462 B

After

Width:  |  Height:  |  Size: 462 B

View File

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

Before

Width:  |  Height:  |  Size: 6.7 KiB

After

Width:  |  Height:  |  Size: 6.7 KiB

View File

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -6,6 +6,6 @@ jpackage \
--type app-image \
-m org.jcnc.jnotepad/org.jcnc.jnotepad.JnotepadApp \
--runtime-image ./target/JNotepad/ \
--icon src/main/resources/img/icon.ico \
--icon src/main/resources/jcnc/app/images/appIcons/icon.ico \
--app-version 1.1.14 \
--vendor "JCNC"

View File

@ -3,6 +3,6 @@ jpackage `
--type app-image `
-m org.jcnc.jnotepad/org.jcnc.jnotepad.JnotepadApp `
--runtime-image .\target\JNotepad\ `
--icon src/main/resources/img/icon.ico `
--icon src/main/resources/jcnc/app/images/appIcons/icon.ico `
--app-version 1.1.14 `
--vendor "JCNC"