View Javadoc
1   /*******************************************************************************
2    * Copyhacked (H) 2012-2020.
3    * This program and the accompanying materials
4    * are made available under no term at all, use it like
5    * you want, but share and discuss about it
6    * every time possible with every body.
7    * 
8    * Contributors:
9    *      ron190 at ymail dot com - initial implementation
10   ******************************************************************************/
11  package com.jsql.view.swing.tree.model;
12  
13  import com.jsql.model.bean.database.Table;
14  import com.jsql.model.suspendable.AbstractSuspendable;
15  import com.jsql.view.swing.menubar.JMenuItemWithMargin;
16  import com.jsql.view.swing.text.JPopupTextField;
17  import com.jsql.view.swing.tree.CheckBoxMenuItemIconCustom;
18  import com.jsql.view.swing.tree.ImageObserverAnimated;
19  import com.jsql.view.swing.tree.ImageOverlap;
20  import com.jsql.view.swing.tree.PanelNode;
21  import com.jsql.view.swing.util.I18nViewUtil;
22  import com.jsql.view.swing.util.MediatorHelper;
23  import com.jsql.view.swing.util.UiUtil;
24  
25  import javax.swing.*;
26  import javax.swing.plaf.basic.BasicCheckBoxMenuItemUI;
27  import javax.swing.plaf.basic.BasicRadioButtonMenuItemUI;
28  import javax.swing.tree.DefaultMutableTreeNode;
29  import javax.swing.tree.DefaultTreeModel;
30  import javax.swing.tree.TreePath;
31  import java.awt.*;
32  
33  /**
34   * Table model displaying the table icon on the label.
35   */
36  public class NodeModelTable extends AbstractNodeModel {
37      
38      /**
39       * Node as a table model.
40       * @param table Element table coming from model
41       */
42      public NodeModelTable(Table table) {
43          super(table);
44      }
45  
46      @Override
47      protected Icon getLeafIcon(boolean leaf) {
48          if (leaf) {
49              return UiUtil.ICON_TABLE_GO;
50          } else {
51              return UiUtil.ICON_TABLE;
52          }
53      }
54  
55      @Override
56      protected void displayProgress(PanelNode panelNode, DefaultMutableTreeNode currentNode) {
57          if ("information_schema".equals(this.getParent().toString())) {
58              
59              panelNode.showLoader();
60              AbstractSuspendable suspendableTask = MediatorHelper.model().getMediatorUtils().getThreadUtil().get(this.getElementDatabase());
61              
62              if (suspendableTask != null && suspendableTask.isPaused()) {
63                  
64                  ImageIcon animatedGifPaused = new ImageOverlap(UiUtil.PATH_PROGRESSBAR, UiUtil.PATH_PAUSE);
65                  animatedGifPaused.setImageObserver(new ImageObserverAnimated(MediatorHelper.treeDatabase(), currentNode));
66                  panelNode.setLoaderIcon(animatedGifPaused);
67              }
68          } else {
69              super.displayProgress(panelNode, currentNode);
70          }
71      }
72  
73      @Override
74      public void runAction() {
75          
76          if (this.isRunning()) {  // Prevent double thread run
77              return;
78          }
79              
80          DefaultMutableTreeNode treeNode = MediatorHelper.treeDatabase().getTreeNodeModels().get(this.getElementDatabase());
81          treeNode.removeAllChildren();
82          
83          DefaultTreeModel treeModel = (DefaultTreeModel) MediatorHelper.treeDatabase().getModel();
84          treeModel.reload(treeNode);
85  
86          new SwingWorker<>() {
87  
88              @Override
89              protected Object doInBackground() throws Exception {
90  
91                  Thread.currentThread().setName("SwingWorkerNodeModelTable");
92                  var selectedTable = (Table) NodeModelTable.this.getElementDatabase();
93  
94                  return MediatorHelper.model().getDataAccess().listColumns(selectedTable);
95              }
96          }.execute();
97          
98          this.setRunning(true);
99      }
100 
101     @Override
102     protected void buildMenu(JPopupMenuCustomExtract tablePopupMenu, final TreePath path) {
103         
104         this.addCheckUncheckItems(tablePopupMenu, path);
105 //        this.addCustomLoadItems(tablePopupMenu);
106     }
107 
108     private void addCustomLoadItems(JPopupMenuCustomExtract tablePopupMenu) {
109         
110         var menuCustomLoad = new JMenu("Custom load");
111         
112         var buttonGroupLoadRows = new ButtonGroup();
113         
114         JMenuItem menuItemLoadAllRows = new JRadioButtonMenuItem("Load all rows (default)", true);
115         JMenuItem menuItemLoadOneRow = new JRadioButtonMenuItem("Load first row only");
116         JMenuItem menuItemDump = new JCheckBoxMenuItem("Dump to a file");
117         
118         var panelCustomFromRow = new JPanel(new BorderLayout());
119         final JTextField inputCustomFromRow = new JPopupTextField("no.", "1").getProxy();
120         inputCustomFromRow.setHorizontalAlignment(SwingConstants.TRAILING);
121         var d = new Dimension(
122             (int) inputCustomFromRow.getPreferredSize().getWidth() + 50,
123             (int) inputCustomFromRow.getPreferredSize().getHeight()
124         );
125         inputCustomFromRow.setPreferredSize(d);
126 
127         final var radioCustomFromRow = new JCheckBox("<html><pre style=\"font-family:'Segoe UI';padding-left: 1px;\">Load from row no.&#9;</pre></html>");
128         radioCustomFromRow.setBorder(BorderFactory.createEmptyBorder(0, 6, 0, 0));
129         radioCustomFromRow.setIcon(new CheckBoxMenuItemIconCustom());
130         radioCustomFromRow.setFocusPainted(false);
131         
132         panelCustomFromRow.add(radioCustomFromRow, BorderLayout.LINE_START);
133         panelCustomFromRow.add(inputCustomFromRow, BorderLayout.CENTER);
134         
135         var panelCustomToRow = new JPanel(new BorderLayout());
136         final JTextField inputCustomToRow = new JPopupTextField("no.", "65565").getProxy();
137         inputCustomToRow.setHorizontalAlignment(SwingConstants.TRAILING);
138         inputCustomToRow.setPreferredSize(d);
139 
140         final var radioCustomToRow = new JCheckBox("<html><pre style=\"font-family:'Segoe UI';padding-left: 1px;\">Load to row no.&#9;&#9;&#9;&#9;&#9;&#9;</pre></html>");
141         radioCustomToRow.setBorder(BorderFactory.createEmptyBorder(0, 6, 0, 0));
142         radioCustomToRow.setIcon(new CheckBoxMenuItemIconCustom());
143         radioCustomToRow.setFocusPainted(false);
144         
145         panelCustomToRow.add(radioCustomToRow, BorderLayout.LINE_START);
146         panelCustomToRow.add(inputCustomToRow, BorderLayout.CENTER);
147         
148         var panelCustomFromChar = new JPanel(new BorderLayout());
149         final JTextField inputCustomFromChar = new JPopupTextField("no.", "1").getProxy();
150         inputCustomFromChar.setHorizontalAlignment(SwingConstants.TRAILING);
151         inputCustomFromChar.setPreferredSize(d);
152 
153         final var radioCustomFromChar = new JCheckBox("<html><pre style=\"font-family:'Segoe UI';padding-left: 1px;\">Load from char no.</pre></html>");
154         radioCustomFromChar.setBorder(BorderFactory.createEmptyBorder(0, 6, 0, 0));
155         radioCustomFromChar.setIcon(new CheckBoxMenuItemIconCustom());
156         radioCustomFromChar.setFocusPainted(false);
157         
158         panelCustomFromChar.add(radioCustomFromChar, BorderLayout.LINE_START);
159         panelCustomFromChar.add(inputCustomFromChar, BorderLayout.CENTER);
160         
161         var panelCustomToChar = new JPanel(new BorderLayout());
162         final JTextField inputCustomToChar = new JPopupTextField("no.", "65565").getProxy();
163         inputCustomToChar.setHorizontalAlignment(SwingConstants.TRAILING);
164         inputCustomToChar.setPreferredSize(d);
165 
166         final var radioCustomToChar = new JCheckBox("<html><pre style=\"font-family:'Segoe UI';padding-left: 1px;\">Load to char no.&#9;&#9;&#9;&#9;&#9;</pre></html>");
167         radioCustomToChar.setBorder(BorderFactory.createEmptyBorder(0, 6, 0, 0));
168         radioCustomToChar.setIcon(new CheckBoxMenuItemIconCustom());
169         radioCustomToChar.setFocusPainted(false);
170         
171         panelCustomToChar.add(radioCustomToChar, BorderLayout.LINE_START);
172         panelCustomToChar.add(inputCustomToChar, BorderLayout.CENTER);
173 
174         buttonGroupLoadRows.add(menuItemLoadAllRows);
175         buttonGroupLoadRows.add(menuItemLoadOneRow);
176       
177         menuCustomLoad.add(menuItemLoadAllRows);
178         menuCustomLoad.add(menuItemLoadOneRow);
179         menuCustomLoad.add(new JSeparator());
180         menuCustomLoad.add(panelCustomFromRow);
181         menuCustomLoad.add(panelCustomToRow);
182         menuCustomLoad.add(panelCustomFromChar);
183         menuCustomLoad.add(panelCustomToChar);
184         menuCustomLoad.add(new JSeparator());
185         menuCustomLoad.add(menuItemDump);
186         
187         for (JMenuItem menuItem: new JMenuItem[]{menuItemLoadAllRows, menuItemLoadOneRow}) {
188             
189             menuItem.setUI(
190                 new BasicRadioButtonMenuItemUI() {
191                     
192                     @Override
193                     protected void doClick(MenuSelectionManager msm) {
194                         
195                         this.menuItem.doClick(0);
196                     }
197                 }
198             );
199         }
200         
201         menuItemDump.setUI(
202             new BasicCheckBoxMenuItemUI() {
203                 
204                 @Override
205                 protected void doClick(MenuSelectionManager msm) {
206                     
207                     this.menuItem.doClick(0);
208                 }
209             }
210         );
211 
212         tablePopupMenu.add(new JSeparator());
213         tablePopupMenu.add(menuCustomLoad);
214         
215         tablePopupMenu.setButtonGroupLoadRows(buttonGroupLoadRows);
216         tablePopupMenu.setRadioCustomFromChar(radioCustomFromChar);
217         tablePopupMenu.setRadioCustomToChar(radioCustomToChar);
218         tablePopupMenu.setRadioCustomFromRow(radioCustomFromRow);
219         tablePopupMenu.setRadioCustomToRow(radioCustomToRow);
220     }
221 
222     private void addCheckUncheckItems(JPopupMenuCustomExtract tablePopupMenu, final TreePath path) {
223         
224         JMenuItem menuItemCheckAll = new JMenuItemWithMargin(I18nViewUtil.valueByKey("COLUMNS_CHECK_ALL"), 'C');
225         I18nViewUtil.addComponentForKey("COLUMNS_CHECK_ALL", menuItemCheckAll);
226         
227         JMenuItem menuItemUncheckAll = new JMenuItemWithMargin(I18nViewUtil.valueByKey("COLUMNS_UNCHECK_ALL"), 'U');
228         I18nViewUtil.addComponentForKey("COLUMNS_UNCHECK_ALL", menuItemUncheckAll);
229 
230         if (!this.isLoaded()) {
231             
232             menuItemCheckAll.setEnabled(false);
233             menuItemUncheckAll.setEnabled(false);
234         }
235 
236         menuItemCheckAll.addActionListener(new ActionCheckbox(true, path));
237         menuItemUncheckAll.addActionListener(new ActionCheckbox(false, path));
238 
239         tablePopupMenu.add(new JSeparator());
240         tablePopupMenu.add(menuItemCheckAll);
241         tablePopupMenu.add(menuItemUncheckAll);
242     }
243     
244     @Override
245     public boolean isPopupDisplayable() {
246         return this.isLoaded() || !this.isLoaded() && this.isRunning();
247     }
248 }