View Javadoc
1   /*******************************************************************************
2    * jArduino: Arduino C++ Code Generation From Java
3    * Copyright 2020 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.jarduino.gui;
18  
19  import java.awt.BorderLayout;
20  import java.awt.Dimension;
21  import javax.swing.BorderFactory;
22  import javax.swing.Box;
23  import javax.swing.BoxLayout;
24  import javax.swing.JButton;
25  import javax.swing.JComponent;
26  import javax.swing.JFrame;
27  import javax.swing.JPanel;
28  import javax.swing.JScrollPane;
29  import javax.swing.border.Border;
30  
31  import net.sourceforge.jarduino.message.ArduinoMsgFilter;
32  import net.sourceforge.jarduino.message.ArduinoNamedObject;
33  import net.sourceforge.jarduino.message.ArduinoNode;
34  import net.sourceforge.jarduino.message.ArduinoSystem;
35  
36  /**
37   * The filter panel.
38   */
39  public class ArduinoPanelFilter {
40      /**
41       * The parent panel.
42       */
43      private final ArduinoPanelSource theParent;
44  
45      /**
46       * The panel.
47       */
48      private final JPanel thePanel;
49  
50      /**
51       * The basis button.
52       */
53      private final ArduinoScrollButton<ArduinoNamedObject> theBasis;
54  
55      /**
56       * The reset button.
57       */
58      private final JButton theReset;
59  
60      /**
61       * The public button.
62       */
63      private final JButton thePublic;
64  
65      /**
66       * The Filter Table.
67       */
68      private final ArduinoTableFilter theTable;
69  
70      /**
71       * Constructor.
72       * @param pParent the parent panel
73       * @param pFrame the frame
74       */
75      ArduinoPanelFilter(final ArduinoPanelSource pParent,
76                         final JFrame pFrame) {
77          /* Store parameters */
78          theParent = pParent;
79  
80          /* Create the table */
81          theTable = new ArduinoTableFilter(this);
82  
83          /* Create the source button */
84          final JButton mySource = new JButton();
85          mySource.setText("Message Filter");
86          mySource.addActionListener(e -> theParent.showSource());
87  
88          /* Create the basis button */
89          theBasis = new ArduinoScrollButton<>(pFrame);
90          theBasis.setFormatter(ArduinoPanelFilter::getNodeName);
91          theBasis.onSelect(this::setNode);
92  
93          /* Create the Reset button */
94          theReset = new JButton();
95          theReset.setText("Reset");
96          theReset.addActionListener(e -> setNode(theBasis.getSelectedItem()));
97  
98          /* Create the public button */
99          thePublic = new JButton();
100         thePublic.setText("Private");
101         thePublic.addActionListener(e -> {
102             final ArduinoMsgFilter myFilter = getFilter();
103             myFilter.togglePublicFields();
104             thePublic.setText(myFilter.publicFields() ? "Public" : "Private");
105             theParent.updateFilter();
106         });
107 
108         /* Create the basis panel */
109         final JPanel myBasisPanel = new JPanel();
110         myBasisPanel.setLayout(new BoxLayout(myBasisPanel, BoxLayout.X_AXIS));
111         myBasisPanel.add(Box.createHorizontalStrut(ArduinoPanelMain.STRUTSIZE));
112         myBasisPanel.add(Box.createHorizontalStrut(ArduinoPanelMain.STRUTSIZE));
113         myBasisPanel.add(theBasis.getComponent());
114         myBasisPanel.add(Box.createHorizontalGlue());
115         myBasisPanel.add(theReset);
116         myBasisPanel.add(Box.createHorizontalGlue());
117         Border myBorder = BorderFactory.createTitledBorder("Filter Basis");
118         myBasisPanel.setBorder(myBorder);
119 
120         /* Create the Field panel */
121         final JPanel myFieldPanel = new JPanel();
122         myFieldPanel.setLayout(new BoxLayout(myFieldPanel, BoxLayout.X_AXIS));
123         myFieldPanel.add(Box.createHorizontalStrut(ArduinoPanelMain.STRUTSIZE));
124         myFieldPanel.add(thePublic);
125         myFieldPanel.add(Box.createHorizontalStrut(ArduinoPanelMain.STRUTSIZE));
126         myBorder = BorderFactory.createTitledBorder("Fields");
127         myFieldPanel.setBorder(myBorder);
128 
129         /* Create the Source panel */
130         final JPanel mySourcePanel = new JPanel();
131         mySourcePanel.setLayout(new BoxLayout(mySourcePanel, BoxLayout.X_AXIS));
132         mySourcePanel.add(Box.createHorizontalStrut(ArduinoPanelMain.STRUTSIZE));
133         mySourcePanel.add(mySource);
134         mySourcePanel.add(Box.createHorizontalStrut(ArduinoPanelMain.STRUTSIZE));
135         myBorder = BorderFactory.createTitledBorder("View");
136         mySourcePanel.setBorder(myBorder);
137 
138         /* Create the control panel */
139         final JPanel myControl = new JPanel();
140         myControl.setLayout(new BoxLayout(myControl, BoxLayout.X_AXIS));
141         myControl.add(myBasisPanel);
142         myControl.add(myFieldPanel);
143         myControl.add(mySourcePanel);
144 
145         /* Create the message panel */
146         final JPanel myFilterPanel = new JPanel(new BorderLayout());
147         myFilterPanel.add(new JScrollPane(theTable.getComponent()), BorderLayout.CENTER);
148         myBorder = BorderFactory.createTitledBorder("Message Filter");
149         myFilterPanel.setBorder(myBorder);
150 
151         /* Create the panel */
152         thePanel = new JPanel(new BorderLayout());
153         thePanel.add(myControl, BorderLayout.PAGE_START);
154         thePanel.add(myFilterPanel, BorderLayout.CENTER);
155 
156         /* Set Dimensions */
157         thePanel.setPreferredSize(new Dimension(ArduinoPanelMain.WIDTH, ArduinoPanelMain.HEIGHT));
158     }
159 
160     /**
161      * Obtain the panel.
162      * @return the panel
163      */
164     JComponent getComponent() {
165         return thePanel;
166     }
167 
168     /**
169      * Obtain the filter.
170      * @return the filter
171      */
172     ArduinoMsgFilter getFilter() {
173         return theTable.getFilter();
174     }
175 
176     /**
177      * Obtain the node name.
178      * @param pNode the node
179      * @return the display name
180      */
181     private static String getNodeName(final ArduinoNamedObject pNode) {
182         return pNode instanceof ArduinoSystem ? "All" : pNode.getName();
183     }
184 
185     /**
186      * Set System.
187      * @param pSystem the system.
188      */
189     void setSystem(final ArduinoSystem pSystem) {
190         /* Declare system to table */
191         theTable.configureForSystem(pSystem);
192 
193         /* Reset the basis button */
194         theBasis.removeAll();
195         theBasis.add(pSystem);
196 
197         /* Add all the nodes to the button */
198         for (ArduinoNode myNode : pSystem.getNodes()) {
199             /* Add to the list (unless it is the nullNode) */
200             if (!myNode.getName().equals(ArduinoNode.NULL_NODE)) {
201                 theBasis.add(myNode);
202             }
203         }
204     }
205 
206     /**
207      * Set Node.
208      * @param pNode the node.
209      */
210     private void setNode(final ArduinoNamedObject pNode) {
211         /* Declare system to table */
212         theTable.configureForNode(pNode);
213     }
214 
215     /**
216      * Update the filter.
217      */
218     void updateFilter() {
219         /* Update source details */
220         theParent.updateFilter();
221 
222         /* Set reset visibility */
223         theReset.setVisible(theTable.isBespoke());
224     }
225 }