View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.component;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: UI Swing Common
6    * %%
7    * Copyright (C) 2017 - 2019 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Affero General Public License as published by
11   * the Free Software Foundation, either version 3 of the License, or
12   * (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU Affero General Public License
20   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21   * #L%
22   */
23  
24  import javax.swing.Icon;
25  import javax.swing.JToggleButton;
26  import java.awt.event.ActionEvent;
27  import java.awt.event.ActionListener;
28  
29  /**
30   * @author peck7 on 25/07/2019.
31   */
32  public class ToggleButton extends JToggleButton implements ActionListener {
33  
34      private String defaultText;
35      private String defaultToolTipText;
36      private Icon defaultIcon;
37      private String toggledText;
38      private String toggledToolTipText;
39      private Icon toggledIcon;
40  
41      @Override
42      protected void init(String text, Icon icon) {
43          super.init(text, icon);
44          addActionListener(this);
45      }
46  
47      @Override
48      public void setText(String text) {
49          defaultText = text;
50          super.setText(text);
51      }
52  
53      @Override
54      public void setToolTipText(String text) {
55          defaultToolTipText = text;
56          super.setToolTipText(text);
57      }
58  
59      @Override
60      public void setIcon(Icon defaultIcon) {
61          this.defaultIcon = defaultIcon;
62          super.setIcon(defaultIcon);
63      }
64  
65      @Override
66      public void actionPerformed(ActionEvent e) {
67          updateComponents();
68      }
69  
70      public void updateComponents() {
71          if (isSelected()) {
72              super.setText(toggledText);
73              super.setToolTipText(toggledToolTipText);
74              super.setIcon(toggledIcon);
75          } else {
76              super.setText(defaultText);
77              super.setToolTipText(defaultToolTipText);
78              super.setIcon(defaultIcon);
79          }
80      }
81  
82      public String getToggledText() {
83          return toggledText;
84      }
85  
86      public void setToggledText(String toggledText) {
87          this.toggledText = toggledText;
88      }
89  
90      public String getToggledToolTipText() {
91          return toggledToolTipText;
92      }
93  
94      public void setToggledToolTipText(String toggledToolTipText) {
95          this.toggledToolTipText = toggledToolTipText;
96      }
97  
98      public Icon getToggledIcon() {
99          return toggledIcon;
100     }
101 
102     public void setToggledIcon(Icon toggledIcon) {
103         this.toggledIcon = toggledIcon;
104     }
105 }