View Javadoc
1   /*
2    * Copyright 2013-2023 Medical Information Systems Research Group (https://medical.zcu.cz),
3    * Department of Computer Science and Engineering, University of West Bohemia.
4    * Address: Univerzitni 8, 306 14 Plzen, Czech Republic.
5    *
6    * This file is part of Sparkle project.
7    *
8    * Sparkle is free software: you can redistribute it and/or modify
9    * it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License.
11   *
12   * Sparkle is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with Sparkle. If not, see <http://www.gnu.org/licenses/>.
19   */
20  package cz.zcu.mre.sparkle.gui.mainForm.mainTabPane;
21  
22  import cz.zcu.mre.sparkle.gui.evaluation.EvaluationTab;
23  import cz.zcu.mre.sparkle.gui.dialogs.ColorPickerDialog;
24  import cz.zcu.mre.sparkle.gui.mainForm.MainForm;
25  import cz.zcu.mre.sparkle.gui.mainForm.mainMenu.menuItems.MenuItemsQuery;
26  import cz.zcu.mre.sparkle.gui.query.QueryTab;
27  import cz.zcu.mre.sparkle.gui.query.other.SaverTab;
28  import cz.zcu.mre.sparkle.gui.query.queryTypes.subselect.SubSelectQueryTab;
29  import javafx.event.ActionEvent;
30  import javafx.scene.control.*;
31  import javafx.scene.paint.Color;
32  
33  /**
34   * Kontextove menu zalozky
35   *
36   * @author Jan Smucr
37   * @author Klara Hlavacova
38   * @author Petr Vcelak (vcelak@kiv.zcu.cz)
39   */
40  public class TabContextMenu {
41  
42      /**
43       * The method creates context menu for the tab in parameter.
44       *
45       * @param ownerTab Owner tab of created context menu.
46       * @param mainForm The main form.
47       */
48      public void getContextMenu(Tab ownerTab, MainForm mainForm) {
49  //TODO: rozdělit
50          final ContextMenu contextMenu = new ContextMenu();
51          SaverTab saverTab;
52          QueryTab mainQueryPart;
53  
54          if (ownerTab instanceof SaverTab) {
55              saverTab = (SaverTab) ownerTab;
56              mainQueryPart = ownerTab instanceof SubSelectQueryTab
57                      ? mainForm.getQueryTab(MainTabPane.getMainQueryPart((SubSelectQueryTab) ownerTab)) : null;
58  
59          } else if (ownerTab instanceof EvaluationTab) {
60              QueryTab parentQueryTab = ((EvaluationTab) ownerTab).getQueryTab();
61              mainQueryPart = parentQueryTab instanceof SubSelectQueryTab
62                      ? mainForm.getQueryTab(MainTabPane.getMainQueryPart((SubSelectQueryTab) parentQueryTab))
63                      : parentQueryTab;
64              saverTab = null;
65  
66          } else {
67              saverTab = null;
68              mainQueryPart = null;
69  
70          }
71  
72          final MenuItem viewMenuItem = new MenuItem("_View"); //$NON-NLS-1$
73          viewMenuItem.setOnAction((ActionEvent e) -> mainForm.getMainTabPane().getSelectionModel().select(ownerTab));
74  
75          final MenuItem colorTabMenuItem = setColorTabMenuItem(ownerTab, mainForm, mainQueryPart);
76  
77          contextMenu.getItems().addAll(viewMenuItem, new SeparatorMenuItem(), colorTabMenuItem);
78  
79          if (saverTab != null) {
80              final MenuItem saveContextMenuItem = setSaveMenuItem(saverTab, mainQueryPart);
81              final MenuItem saveAsContextMenuItem = setSaveAsMenuItem(mainForm);
82  
83              contextMenu.getItems().addAll(new SeparatorMenuItem(), saveContextMenuItem, saveAsContextMenuItem);
84          }
85          if (!(ownerTab instanceof SubSelectQueryTab)) {
86              setCloseMenuItem(ownerTab, mainForm, contextMenu);
87          }
88          ownerTab.setContextMenu(contextMenu);
89      }
90  
91      /**
92       *
93       * @param ownerTab
94       * @param mainForm
95       * @param contextMenu
96       */
97      private void setCloseMenuItem(Tab ownerTab, MainForm mainForm, ContextMenu contextMenu) {
98          final MenuItem closeMenuItem = new MenuItem("_Close"); //$NON-NLS-1$
99  
100         closeMenuItem.setOnAction((ActionEvent e) -> {
101 
102             System.out.println("ERROR missing TabPaneBehavior.closeTab(ownerTab)");
103 //            TabPaneBehavior tabBehavior = ((TabPaneSkin) mainForm.getMainTabPane().getSkin()).getBehavior();
104 //            if (tabBehavior.canCloseTab(ownerTab)) {
105 //                tabBehavior.closeTab(ownerTab);
106 //            }
107         });
108 
109         contextMenu.getItems().addAll(new SeparatorMenuItem(), closeMenuItem);
110     }
111 
112     /**
113      *
114      * @param mainForm
115      * @return
116      */
117     private MenuItem setSaveAsMenuItem(MainForm mainForm) {
118         final MenuItem saveAsContextMenuItem = new MenuItem("Save _as..."); //$NON-NLS-1$
119         saveAsContextMenuItem.setOnAction((ActionEvent e) -> mainForm.getMainMenuBar().saveAllOnAction());
120         saveAsContextMenuItem.disableProperty()
121                 .bind(mainForm.getMainMenuBar().getMenuItemsQuery()
122                         .get(MenuItemsQuery.getSaveAsOrdinal()).disableProperty());
123         return saveAsContextMenuItem;
124     }
125 
126     /**
127      *
128      * @param saverTab
129      * @param mainQueryPart
130      * @return
131      */
132     private MenuItem setSaveMenuItem(SaverTab saverTab, QueryTab mainQueryPart) {
133         final MenuItem saveContextMenuItem = new MenuItem("_Save"); //$NON-NLS-1$
134 
135         if (mainQueryPart != null) {
136             saveContextMenuItem.setOnAction((ActionEvent e) -> mainQueryPart.save());
137         } else {
138             saveContextMenuItem.setOnAction((ActionEvent e) -> saverTab.save());
139         }
140 
141         saveContextMenuItem.disableProperty().bind(saverTab.hasChangesProperty().not());
142         return saveContextMenuItem;
143     }
144 
145     /**
146      *
147      * @param ownerTab
148      * @param mainForm
149      * @param mainQueryPart
150      * @return
151      */
152     private MenuItem setColorTabMenuItem(Tab ownerTab, MainForm mainForm, QueryTab mainQueryPart) {
153         final ColorPicker colorPicker = TabColorPicker.getColorPicker(ownerTab, mainQueryPart);
154         final MenuItem colorTabMenuItem = new MenuItem("_Color tab..."); //$NON-NLS-1$
155 
156         colorTabMenuItem.setOnAction((ActionEvent e) -> {
157 
158             final Color selectedColor = ColorPickerDialog.open(mainForm.getStage(), colorPicker.getValue());
159 
160             if (selectedColor != null) {
161                 if (mainQueryPart != null) {
162                     TabColorPicker.changeQueryColor(mainQueryPart, selectedColor);
163                 } else {
164                     TabColorPicker.changeQueryColor((QueryTab) ownerTab, selectedColor);
165                 }
166             }
167         });
168         return colorTabMenuItem;
169     }
170 
171 }