1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
35
36
37
38
39
40 public class TabContextMenu {
41
42
43
44
45
46
47
48 public void getContextMenu(Tab ownerTab, MainForm mainForm) {
49
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");
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
94
95
96
97 private void setCloseMenuItem(Tab ownerTab, MainForm mainForm, ContextMenu contextMenu) {
98 final MenuItem closeMenuItem = new MenuItem("_Close");
99
100 closeMenuItem.setOnAction((ActionEvent e) -> {
101
102 System.out.println("ERROR missing TabPaneBehavior.closeTab(ownerTab)");
103
104
105
106
107 });
108
109 contextMenu.getItems().addAll(new SeparatorMenuItem(), closeMenuItem);
110 }
111
112
113
114
115
116
117 private MenuItem setSaveAsMenuItem(MainForm mainForm) {
118 final MenuItem saveAsContextMenuItem = new MenuItem("Save _as...");
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
129
130
131
132 private MenuItem setSaveMenuItem(SaverTab saverTab, QueryTab mainQueryPart) {
133 final MenuItem saveContextMenuItem = new MenuItem("_Save");
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
148
149
150
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...");
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 }