22
33import com .xwintop .xJavaFxTool .XJavaFxToolApplication ;
44import com .xwintop .xJavaFxTool .controller .index .PluginManageController ;
5+ import com .xwintop .xJavaFxTool .event .AppEvents ;
6+ import com .xwintop .xJavaFxTool .event .PluginEvent ;
7+ import com .xwintop .xJavaFxTool .model .PluginJarInfo ;
58import com .xwintop .xJavaFxTool .model .ToolFxmlLoaderConfiguration ;
9+ import com .xwintop .xJavaFxTool .newui .NewLauncherService ;
610import com .xwintop .xJavaFxTool .newui .PluginCategoryController ;
711import com .xwintop .xJavaFxTool .newui .PluginItemController ;
12+ import com .xwintop .xJavaFxTool .plugin .PluginManager ;
13+ import com .xwintop .xJavaFxTool .plugin .PluginParser ;
814import com .xwintop .xJavaFxTool .services .IndexService ;
915import com .xwintop .xJavaFxTool .services .index .PluginManageService ;
1016import com .xwintop .xJavaFxTool .services .index .SystemSettingService ;
2026import javafx .fxml .FXML ;
2127import javafx .fxml .FXMLLoader ;
2228import javafx .scene .Parent ;
29+ import javafx .scene .control .CheckMenuItem ;
2330import javafx .scene .control .ContextMenu ;
2431import javafx .scene .control .Menu ;
2532import javafx .scene .control .MenuItem ;
3037import lombok .extern .slf4j .Slf4j ;
3138import org .apache .commons .beanutils .BeanUtils ;
3239import org .apache .commons .lang .StringUtils ;
40+ import org .apache .commons .lang3 .BooleanUtils ;
3341import org .apache .commons .lang3 .time .DateFormatUtils ;
3442import org .dom4j .Attribute ;
3543import org .dom4j .Document ;
5361@ Getter
5462@ Setter
5563public class IndexController extends IndexView {
56-
5764 public static final String QQ_URL = "https://support.qq.com/product/127577" ;
5865
5966 public static final String STATISTICS_URL = "https://xwintop.gitee.io/maven/tongji/xJavaFxTool.html" ;
6067
68+ public static final String FAVORITE_CATEGORY_NAME = "置顶" ;
69+
6170 private Map <String , Menu > menuMap = new HashMap <>();
6271
6372 private Map <String , MenuItem > menuItemMap = new HashMap <>();
6473
6574 private IndexService indexService = new IndexService (this );
6675
6776 private ContextMenu contextMenu = new ContextMenu ();
77+ private ContextMenu itemContextMenu ;
6878
6979 // 实现搜索用
7080 private List <PluginItemController > pluginItemControllers = new ArrayList <>();
@@ -86,6 +96,7 @@ public void initialize(URL location, ResourceBundle resources) {
8696
8797 this .indexService .addWebView (XJavaFxToolApplication .RESOURCE_BUNDLE .getString ("feedback" ), QQ_URL , null );
8898 this .tongjiWebView .getEngine ().load (STATISTICS_URL );
99+ this .tabPaneMain .getSelectionModel ().select (0 );
89100 }
90101
91102 private void initNotepad () {
@@ -95,6 +106,7 @@ private void initNotepad() {
95106 }
96107
97108 private void initView () {
109+ initContextMenu ();
98110 menuMap .put ("moreToolsMenu" , moreToolsMenu );
99111 File libPath = new File ("libs/" );
100112 // 获取所有的.jar和.zip文件
@@ -116,12 +128,91 @@ private void initView() {
116128 private void initEvent () {
117129 mainMenuBar .setUseSystemMenuBar (true );
118130 myTextField .textProperty ().addListener ((observable , oldValue , newValue ) -> selectAction (newValue ));
119- myButton .setOnAction (arg0 -> {
120- selectAction (myTextField .getText ());
121- });
122131 }
123132
124133 private void initService () {
134+ NewLauncherService .getInstance ().setTabPane (tabPaneMain );
135+ loadPlugins (); // 加载插件列表到界面上
136+ AppEvents .addEventHandler (PluginEvent .PLUGIN_DOWNLOADED , pluginEvent -> {
137+ loadPlugins ();
138+ });
139+ }
140+
141+ private void initContextMenu () {
142+ CheckMenuItem chkFavorite = new CheckMenuItem ("置顶" );
143+ chkFavorite .setStyle ("-fx-padding: 0 35 0 0" );
144+ this .itemContextMenu = new ContextMenu (chkFavorite );
145+ chkFavorite .setOnAction (event -> {
146+ CheckMenuItem _this = (CheckMenuItem ) event .getSource ();
147+ PluginItemController pluginItemController = NewLauncherService .getInstance ().getCurrentPluginItem ();
148+ setFavorite (pluginItemController , _this .isSelected ());
149+ });
150+ }
151+ private void setFavorite (PluginItemController itemController , boolean isFavorite ) {
152+ if (itemController == null ) {
153+ return ;
154+ }
155+ itemController .getPluginJarInfo ().setIsFavorite (isFavorite );
156+ PluginManager .getInstance ().saveToFileQuietly ();
157+ loadPlugins ();
158+ }
159+
160+ /**
161+ * 加载/刷新插件列表
162+ */
163+ private void loadPlugins () {
164+ this .pluginCategories .getChildren ().clear ();
165+ this .pluginItemControllers .clear ();
166+ this .categoryControllers .clear ();
167+
168+ PluginManager pluginManager = PluginManager .getInstance ();
169+ pluginManager .loadLocalPlugins ();
170+ pluginManager .getEnabledPluginList ().forEach (this ::loadPlugin );
171+ }
172+
173+ /**
174+ * 加载单个插件到界面,要求插件已经经过 {@link PluginParser#parse(File, PluginJarInfo)} 解析
175+ *
176+ * @param jarInfo 插件信息
177+ */
178+ private void loadPlugin (PluginJarInfo jarInfo ) {
179+ if (!jarInfo .getFile ().exists ()) {
180+ log .info ("跳过插件 {}: 文件不存在" , jarInfo .getName ());
181+ return ;
182+ }
183+ if (BooleanUtils .isFalse (jarInfo .getIsEnable ())) {
184+ log .info ("跳过插件 {}: 插件未启用" , jarInfo .getName ());
185+ return ;
186+ }
187+ String menuParentTitle = jarInfo .getMenuParentTitle ();
188+ if (menuParentTitle == null ) {
189+ log .info ("跳过插件 {}: menuParentTitle 为空" , jarInfo .getName ());
190+ return ;
191+ }
192+ String categoryName = jarInfo .getIsFavorite () ? FAVORITE_CATEGORY_NAME : XJavaFxToolApplication .RESOURCE_BUNDLE .getString (menuParentTitle );
193+ PluginCategoryController category = categoryControllers .computeIfAbsent (
194+ categoryName , __ -> {
195+ PluginCategoryController _category = PluginCategoryController .newInstance (categoryName );
196+ addCategory (_category );
197+ return _category ;
198+ }
199+ );
200+
201+ PluginItemController item = PluginItemController .newInstance (jarInfo );
202+ item .setContextMenu (itemContextMenu );
203+ category .addItem (item );
204+
205+ if (!pluginItemControllers .contains (item )) {
206+ pluginItemControllers .add (item );
207+ }
208+ }
209+
210+ private void addCategory (PluginCategoryController category ) {
211+ if (category .lblCategoryName .getText ().equals (FAVORITE_CATEGORY_NAME )) {
212+ this .pluginCategories .getChildren ().add (0 , category .root );
213+ } else {
214+ this .pluginCategories .getChildren ().add (category .root );
215+ }
125216 }
126217
127218 public void addToolMenu (File file ) throws Exception {
@@ -230,11 +321,15 @@ private void addMenu(List<ToolFxmlLoaderConfiguration> toolList) {
230321 }
231322
232323 public void selectAction (String selectText ) {
233- if (contextMenu .isShowing ()) {
234- contextMenu .hide ();
235- }
236- contextMenu = indexService .getSelectContextMenu (selectText );
237- contextMenu .show (myTextField , null , 0 , myTextField .getHeight ());
324+ // if (contextMenu.isShowing()) {
325+ // contextMenu.hide();
326+ // }
327+ // contextMenu = indexService.getSelectContextMenu(selectText);
328+ // contextMenu.show(myTextField, null, 0, myTextField.getHeight());
329+ boolean notSearching = StringUtils .isBlank (selectText );
330+ pluginItemControllers .forEach (itemController -> {
331+ itemController .setVisible (notSearching || itemController .matchKeyword (selectText ));
332+ });
238333 }
239334
240335 @ FXML
0 commit comments