View Javadoc
1   /*******************************************************************************
2    * jhunters: Pool League
3    * Copyright 2015 Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   ********************************************************************************/
17  package net.sourceforge.jhunters.pool.ui;
18  
19  import java.util.List;
20  
21  import javafx.beans.value.ChangeListener;
22  import javafx.beans.value.ObservableValue;
23  import javafx.event.ActionEvent;
24  import javafx.event.EventHandler;
25  import javafx.scene.control.Menu;
26  import javafx.scene.control.MenuBar;
27  import javafx.scene.control.MenuItem;
28  import javafx.scene.control.Tab;
29  import javafx.scene.control.TabPane;
30  import net.sourceforge.jhunters.pool.PoolException;
31  import net.sourceforge.jhunters.pool.data.DataSet;
32  
33  /**
34   * Main tab view.
35   */
36  public class MainTabView extends TabPane {
37      /**
38       * The competition tab text.
39       */
40      private static final String TAB_COMPETITION = GuiResource.TAB_COMPETITION.getValue();
41  
42      /**
43       * The maintenance tab text.
44       */
45      private static final String TAB_MAINT = GuiResource.TAB_MAINT.getValue();
46  
47      /**
48       * The margin.
49       */
50      private static final int WIDTH_MARGIN = 20;
51  
52      /**
53       * The competition panel.
54       */
55      private final CompetitionPanel theCompetitions;
56  
57      /**
58       * The maintenance Panel.
59       */
60      private final MaintenancePanel theMaint;
61  
62      /**
63       * The save MenuItem.
64       */
65      private final MenuItem theSaveMenu;
66  
67      /**
68       * Constructor.
69       * @param pControl the control
70       * @throws PoolException on error
71       */
72      protected MainTabView(final AppControl pControl) throws PoolException {
73          /* Access child list */
74          List<Tab> myTabs = getTabs();
75  
76          /* Set the width */
77          setPrefWidth(HuntersPool.WIDTH_STAGE - WIDTH_MARGIN);
78          setPrefHeight(HuntersPool.HEIGHT_STAGE - WIDTH_MARGIN);
79  
80          /* Create the CompetitionPanel */
81          theCompetitions = new CompetitionPanel(pControl);
82  
83          /* Add it as a tab */
84          Tab myTab = new Tab();
85          myTab.setText(TAB_COMPETITION);
86          myTab.setContent(theCompetitions);
87          myTabs.add(myTab);
88  
89          /* Create the MaintenancePanel */
90          theMaint = new MaintenancePanel(pControl, theCompetitions);
91  
92          /* Add it as a tab */
93          myTab = new Tab();
94          myTab.setText(TAB_MAINT);
95          myTab.setContent(theMaint);
96          myTabs.add(myTab);
97  
98          /* Don't let the user close tabs */
99          setTabClosingPolicy(TabClosingPolicy.UNAVAILABLE);
100 
101         /* Create the save menu item */
102         theSaveMenu = new MenuItem(GuiResource.MENUITEM_SAVE.getValue());
103     }
104 
105     /**
106      * Set data.
107      * @param pData the dataSet
108      */
109     protected void setData(final DataSet pData) {
110         /* Declare data to subPanels */
111         theCompetitions.setData(pData);
112         theMaint.setData(pData);
113 
114         /* Ensure save menu is in correct state */
115         theSaveMenu.setDisable(!pData.isUpdated());
116 
117         /* Add listener */
118         pData.updatedProperty().addListener(
119                 new ChangeListener<Boolean>() {
120                     public void changed(final ObservableValue<? extends Boolean> pValue,
121                                         final Boolean pOldState,
122                                         final Boolean pNewState) {
123                         theSaveMenu.setDisable(!pNewState);
124                     }
125                 });
126     }
127 
128     /**
129      * Build menuBar.
130      * @param pControl the control
131      * @return the menuBar
132      */
133     protected MenuBar buildMenuBar(final AppControl pControl) {
134         /* Create the menuBar */
135         MenuBar myMenuBar = new MenuBar();
136         Menu myFileMenu = new Menu(GuiResource.MENU_FILE.getValue());
137         Menu myHelpMenu = new Menu(GuiResource.MENU_HELP.getValue());
138         myMenuBar.getMenus().addAll(myFileMenu, myHelpMenu);
139         MenuItem myNewItem = new MenuItem(GuiResource.MENUITEM_NEW.getValue());
140         myNewItem.setOnAction(new EventHandler<ActionEvent>() {
141             @Override
142             public void handle(final ActionEvent e) {
143                 pControl.createNewData();
144             }
145         });
146         MenuItem myOpenItem = new MenuItem(GuiResource.MENUITEM_OPEN.getValue());
147         myOpenItem.setOnAction(new EventHandler<ActionEvent>() {
148             @Override
149             public void handle(final ActionEvent e) {
150                 pControl.chooseAndLoadFile();
151             }
152         });
153         theSaveMenu.setOnAction(new EventHandler<ActionEvent>() {
154             @Override
155             public void handle(final ActionEvent e) {
156                 pControl.saveToFile();
157             }
158         });
159         MenuItem mySaveAsItem = new MenuItem(GuiResource.MENUITEM_SAVEAS.getValue());
160         mySaveAsItem.setOnAction(new EventHandler<ActionEvent>() {
161             @Override
162             public void handle(final ActionEvent e) {
163                 pControl.saveAsFile();
164             }
165         });
166         myFileMenu.getItems().addAll(myNewItem, myOpenItem, theSaveMenu, mySaveAsItem);
167         MenuItem myAboutItem = new MenuItem(GuiResource.MENUITEM_ABOUT.getValue());
168         myAboutItem.setOnAction(new EventHandler<ActionEvent>() {
169             @Override
170             public void handle(final ActionEvent e) {
171                 AboutBox myAbout = new AboutBox(pControl.getStage());
172                 myAbout.show();
173             }
174         });
175         myHelpMenu.getItems().add(myAboutItem);
176         theSaveMenu.setDisable(true);
177         return myMenuBar;
178     }
179 }