View Javadoc

1   package org.sat4j.sat.visu;
2   
3   import java.awt.BorderLayout;
4   import java.awt.Color;
5   import java.awt.Container;
6   import java.awt.GridLayout;
7   import java.awt.event.ActionEvent;
8   import java.awt.event.ActionListener;
9   
10  import javax.swing.BoxLayout;
11  import javax.swing.JButton;
12  import javax.swing.JCheckBox;
13  import javax.swing.JColorChooser;
14  import javax.swing.JFrame;
15  import javax.swing.JLabel;
16  import javax.swing.JPanel;
17  import javax.swing.JScrollPane;
18  import javax.swing.JTextField;
19  import javax.swing.border.CompoundBorder;
20  import javax.swing.border.EmptyBorder;
21  import javax.swing.border.TitledBorder;
22  
23  /*
24   * DialogDemo.java requires these files:
25   *   CustomDialog.java
26   *   images/middle.gif
27   */
28  public class VisuPreferencesFrame extends JFrame {
29  
30      private static final GridLayout GRIDLAYOUT = new GridLayout(0, 2, 5, 5);
31  
32      private static final EmptyBorder BORDER5 = new EmptyBorder(5, 5, 5, 5);
33  
34      private static final long serialVersionUID = 1L;
35  
36      private VisuPreferences preferences;
37      private JPanel mainPanel;
38  
39      private static final String BACKGROUND_COLOR = "Background color: ";
40      private JButton bgButton;
41  
42      private static final String BORDER_COLOR = "Border color: ";
43      private JButton borderButton;
44  
45      private JLabel nbLinesReadLabel;
46      private static final String NB_LINE = "Number of lines that should be displayed: ";
47      private JTextField nbLinesTextField;
48  
49      private static final String REFRESH_TIME = "Refresh Time (in ms): ";
50  
51      private static final String TIME_BEFORE_LAUNCHING = "Time before launching gnuplot (in ms): ";
52  
53      private JCheckBox displayRestartsCheckBox;
54      private static final String DISPLAY_RESTARTS = "Display restarts";
55  
56      private JLabel restartColorLabel;
57      private static final String RESTART_COLOR = "Restart color";
58      private JButton restartButton;
59  
60      private JCheckBox slidingWindows;
61      private static final String SLIDING_WINDOWS = "Use sliding windows";
62  
63      private JCheckBox displayDecisionIndexesCB;
64      private static final String DECISION_INDEX = "Show index of decision variables";
65      private JCheckBox displaySpeedCB;
66      private static final String SPEED = "Show number of propagations per second";
67      private JCheckBox displayConflictsTrailCB;
68      private static final String CONFLICTS_TRAIL = "Show trail level when a conflict occurs";
69      private JCheckBox displayConflictsDecisionCB;
70      private static final String CONFLICTS_DECISION = "Show decision level when a conflict occurs";
71      private JCheckBox displayVariablesEvaluationCB;
72      private static final String VARIABLE_EVALUATION = "Show variables evaluation";
73      private JCheckBox displayClausesEvaluationCB;
74      private static final String CLAUSES_EVALUATION = "Show clauses evauluation";
75      private JCheckBox displayClausesSizeCB;
76      private static final String CLAUSES_SIZE = "Show size of learned clauses";
77  
78      private static final String OK = "OK";
79  
80      private JPanel gnuplotOptionsPanel;
81  
82      private JPanel generalOptionsPanel;
83  
84      public VisuPreferencesFrame() {
85          this(new VisuPreferences());
86      }
87  
88      public VisuPreferencesFrame(VisuPreferences pref) {
89          super("Visualisation preferences");
90          this.preferences = pref;
91          createGUI();
92      }
93  
94      private void createGUI() {
95          Container c = this.getContentPane();
96          c.setLayout(new BorderLayout());
97  
98          createMainPanel();
99  
100         JScrollPane scrollPane = new JScrollPane(this.mainPanel);
101         this.add(scrollPane);
102 
103         this.pack();
104         this.setVisible(false);
105     }
106 
107     private void createGeneralOptionsPanel() {
108         this.generalOptionsPanel = new JPanel();
109 
110         this.generalOptionsPanel.setName("General options");
111         this.generalOptionsPanel.setBorder(new CompoundBorder(new TitledBorder(
112                 null, this.generalOptionsPanel.getName(), TitledBorder.LEFT,
113                 TitledBorder.TOP), BORDER5));
114 
115         this.generalOptionsPanel.setLayout(GRIDLAYOUT);
116 
117         JLabel backgroundColorLabel;
118 
119         backgroundColorLabel = new JLabel(BACKGROUND_COLOR);
120         this.bgButton = new JButton("");
121         this.bgButton.setOpaque(true);
122         this.bgButton.setBorderPainted(false);
123         this.bgButton.setBackground(this.preferences.getBackgroundColor());
124 
125         this.bgButton.addActionListener(new ActionListener() {
126             public void actionPerformed(ActionEvent e) {
127                 Color color = JColorChooser.showDialog(getFrame(),
128                         "Background Color",
129                         VisuPreferencesFrame.this.bgButton.getBackground());
130                 VisuPreferencesFrame.this.preferences.setBackgroundColor(color);
131                 VisuPreferencesFrame.this.bgButton.setBackground(color);
132             }
133         });
134 
135         this.generalOptionsPanel.add(backgroundColorLabel);
136         this.generalOptionsPanel.add(this.bgButton);
137 
138         JLabel borderColorLabel = new JLabel(BORDER_COLOR);
139 
140         this.borderButton = new JButton("");
141         this.borderButton.setOpaque(true);
142         this.borderButton.setBorderPainted(false);
143         this.borderButton.setBackground(this.preferences.getBorderColor());
144 
145         this.borderButton.addActionListener(new ActionListener() {
146             public void actionPerformed(ActionEvent e) {
147                 Color color = JColorChooser.showDialog(getFrame(),
148                         "Border Color",
149                         VisuPreferencesFrame.this.borderButton.getBackground());
150                 VisuPreferencesFrame.this.preferences.setBorderColor(color);
151                 VisuPreferencesFrame.this.borderButton.setBackground(color);
152             }
153         });
154 
155         this.generalOptionsPanel.add(borderColorLabel);
156         this.generalOptionsPanel.add(this.borderButton);
157 
158         this.restartColorLabel = new JLabel(RESTART_COLOR);
159         this.restartButton = new JButton("");
160         this.restartButton.setOpaque(true);
161         this.restartButton.setBorderPainted(false);
162         this.restartButton.setBackground(this.preferences.getRestartColor());
163 
164         this.restartButton.addActionListener(new ActionListener() {
165             public void actionPerformed(ActionEvent e) {
166                 Color color = JColorChooser
167                         .showDialog(getFrame(), "Restart Color",
168                                 VisuPreferencesFrame.this.restartButton
169                                         .getBackground());
170                 VisuPreferencesFrame.this.preferences.setRestartColor(color);
171                 VisuPreferencesFrame.this.restartButton.setBackground(color);
172             }
173         });
174 
175         this.generalOptionsPanel.add(this.restartColorLabel);
176         this.generalOptionsPanel.add(this.restartButton);
177     }
178 
179     private void createGnuplotOptionsPanel() {
180         this.gnuplotOptionsPanel = new JPanel();
181 
182         this.gnuplotOptionsPanel.setName("Gnuplot options");
183         this.gnuplotOptionsPanel.setBorder(new CompoundBorder(new TitledBorder(
184                 null, this.gnuplotOptionsPanel.getName(), TitledBorder.LEFT,
185                 TitledBorder.TOP), BORDER5));
186 
187         this.gnuplotOptionsPanel.setLayout(GRIDLAYOUT);
188 
189         this.slidingWindows = new JCheckBox(SLIDING_WINDOWS);
190         this.slidingWindows.setSelected(this.preferences.isSlidingWindows());
191 
192         this.slidingWindows.addActionListener(new ActionListener() {
193             public void actionPerformed(ActionEvent e) {
194                 VisuPreferencesFrame.this.nbLinesReadLabel
195                         .setEnabled(VisuPreferencesFrame.this.slidingWindows
196                                 .isSelected());
197                 VisuPreferencesFrame.this.nbLinesTextField
198                         .setEnabled(VisuPreferencesFrame.this.slidingWindows
199                                 .isSelected());
200                 VisuPreferencesFrame.this.preferences
201                         .setSlidingWindows(VisuPreferencesFrame.this.slidingWindows
202                                 .isSelected());
203             }
204         });
205 
206         this.gnuplotOptionsPanel.add(this.slidingWindows);
207         this.gnuplotOptionsPanel.add(new JLabel());
208 
209         this.nbLinesReadLabel = new JLabel(NB_LINE);
210         this.nbLinesTextField = new JTextField(
211                 this.preferences.getNbLinesRead() + "");
212 
213         this.nbLinesReadLabel.setEnabled(this.slidingWindows.isSelected());
214         this.nbLinesTextField.setEnabled(this.slidingWindows.isSelected());
215 
216         this.gnuplotOptionsPanel.add(this.nbLinesReadLabel);
217         this.gnuplotOptionsPanel.add(this.nbLinesTextField);
218 
219         JLabel refreshTimeLabel = new JLabel(REFRESH_TIME);
220         JTextField refreshTimeField = new JTextField(
221                 this.preferences.getRefreshTime() + "");
222 
223         this.gnuplotOptionsPanel.add(refreshTimeLabel);
224         this.gnuplotOptionsPanel.add(refreshTimeField);
225 
226         JLabel timeBeforeLaunchLabel = new JLabel(TIME_BEFORE_LAUNCHING);
227         JTextField timeBeforeLaunchField = new JTextField(
228                 this.preferences.getTimeBeforeLaunching() + "");
229 
230         this.gnuplotOptionsPanel.add(timeBeforeLaunchLabel);
231         this.gnuplotOptionsPanel.add(timeBeforeLaunchField);
232 
233         this.displayRestartsCheckBox = new JCheckBox(DISPLAY_RESTARTS);
234         this.displayRestartsCheckBox.setSelected(this.preferences
235                 .isDisplayRestarts());
236 
237         this.displayRestartsCheckBox.addActionListener(new ActionListener() {
238             public void actionPerformed(ActionEvent e) {
239                 VisuPreferencesFrame.this.restartColorLabel
240                         .setEnabled(VisuPreferencesFrame.this.displayRestartsCheckBox
241                                 .isSelected());
242                 VisuPreferencesFrame.this.restartButton
243                         .setEnabled(VisuPreferencesFrame.this.displayRestartsCheckBox
244                                 .isSelected());
245                 VisuPreferencesFrame.this.preferences
246                         .setDisplayRestarts(VisuPreferencesFrame.this.displayRestartsCheckBox
247                                 .isSelected());
248             }
249         });
250 
251         this.gnuplotOptionsPanel.add(this.displayRestartsCheckBox);
252         this.gnuplotOptionsPanel.add(new JLabel());
253 
254     }
255 
256     public void createMainPanel() {
257         this.mainPanel = new JPanel();
258         this.mainPanel.setLayout(new BorderLayout());
259 
260         JPanel graphPanel;
261 
262         createGeneralOptionsPanel();
263 
264         createGnuplotOptionsPanel();
265 
266         JButton okButton;
267 
268         okButton = new JButton(OK);
269         okButton.addActionListener(new ActionListener() {
270             public void actionPerformed(ActionEvent e) {
271                 getFrame().setVisible(false);
272             }
273         });
274 
275         graphPanel = new JPanel();
276         graphPanel.setLayout(new BoxLayout(graphPanel, BoxLayout.Y_AXIS));
277 
278         graphPanel.setName("Possible Graphs");
279         graphPanel.setBorder(new CompoundBorder(new TitledBorder(null,
280                 graphPanel.getName(), TitledBorder.LEFT, TitledBorder.TOP),
281                 BORDER5));
282 
283         this.displayClausesEvaluationCB = new JCheckBox(CLAUSES_EVALUATION);
284         graphPanel.add(this.displayClausesEvaluationCB);
285         this.displayClausesEvaluationCB.setSelected(this.preferences
286                 .isDisplayClausesEvaluation());
287         this.displayClausesEvaluationCB.addActionListener(new ActionListener() {
288             public void actionPerformed(ActionEvent e) {
289                 VisuPreferencesFrame.this.preferences
290                         .setDisplayClausesEvaluation(VisuPreferencesFrame.this.displayClausesEvaluationCB
291                                 .isSelected());
292             }
293         });
294 
295         this.displayClausesSizeCB = new JCheckBox(CLAUSES_SIZE);
296         graphPanel.add(this.displayClausesSizeCB);
297         this.displayClausesSizeCB.setSelected(this.preferences
298                 .isDisplayClausesSize());
299         this.displayClausesSizeCB.addActionListener(new ActionListener() {
300             public void actionPerformed(ActionEvent e) {
301                 VisuPreferencesFrame.this.preferences
302                         .setDisplayClausesSize(VisuPreferencesFrame.this.displayClausesSizeCB
303                                 .isSelected());
304             }
305         });
306 
307         this.displayConflictsDecisionCB = new JCheckBox(CONFLICTS_DECISION);
308         graphPanel.add(this.displayConflictsDecisionCB);
309         this.displayConflictsDecisionCB.setSelected(this.preferences
310                 .isDisplayConflictsDecision());
311         this.displayConflictsDecisionCB.addActionListener(new ActionListener() {
312             public void actionPerformed(ActionEvent e) {
313                 VisuPreferencesFrame.this.preferences
314                         .setDisplayConflictsDecision(VisuPreferencesFrame.this.displayConflictsDecisionCB
315                                 .isSelected());
316             }
317         });
318 
319         this.displayConflictsTrailCB = new JCheckBox(CONFLICTS_TRAIL);
320         graphPanel.add(this.displayConflictsTrailCB);
321         this.displayConflictsTrailCB.setSelected(this.preferences
322                 .isDisplayConflictsTrail());
323         this.displayConflictsTrailCB.addActionListener(new ActionListener() {
324             public void actionPerformed(ActionEvent e) {
325                 VisuPreferencesFrame.this.preferences
326                         .setDisplayConflictsTrail(VisuPreferencesFrame.this.displayConflictsTrailCB
327                                 .isSelected());
328             }
329         });
330 
331         this.displayDecisionIndexesCB = new JCheckBox(DECISION_INDEX);
332         graphPanel.add(this.displayDecisionIndexesCB);
333         this.displayDecisionIndexesCB.setSelected(this.preferences
334                 .isDisplayDecisionIndexes());
335         this.displayDecisionIndexesCB.addActionListener(new ActionListener() {
336             public void actionPerformed(ActionEvent e) {
337                 VisuPreferencesFrame.this.preferences
338                         .setDisplayDecisionIndexes(VisuPreferencesFrame.this.displayDecisionIndexesCB
339                                 .isSelected());
340             }
341         });
342 
343         this.displaySpeedCB = new JCheckBox(SPEED);
344         graphPanel.add(this.displaySpeedCB);
345         this.displaySpeedCB.setSelected(this.preferences.isDisplaySpeed());
346         this.displaySpeedCB.addActionListener(new ActionListener() {
347             public void actionPerformed(ActionEvent e) {
348                 VisuPreferencesFrame.this.preferences
349                         .setDisplaySpeed(VisuPreferencesFrame.this.displaySpeedCB
350                                 .isSelected());
351             }
352         });
353 
354         this.displayVariablesEvaluationCB = new JCheckBox(VARIABLE_EVALUATION);
355         graphPanel.add(this.displayVariablesEvaluationCB);
356         this.displayVariablesEvaluationCB.setSelected(this.preferences
357                 .isDisplayVariablesEvaluation());
358         this.displayVariablesEvaluationCB
359                 .addActionListener(new ActionListener() {
360                     public void actionPerformed(ActionEvent e) {
361                         VisuPreferencesFrame.this.preferences
362                                 .setDisplayVariablesEvaluation(VisuPreferencesFrame.this.displayVariablesEvaluationCB
363                                         .isSelected());
364                     }
365                 });
366 
367         JPanel topPanel = new JPanel();
368         topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
369 
370         topPanel.add(this.generalOptionsPanel);
371         topPanel.add(this.gnuplotOptionsPanel);
372 
373         this.mainPanel.add(topPanel, BorderLayout.NORTH);
374         this.mainPanel.add(graphPanel, BorderLayout.CENTER);
375         this.mainPanel.add(okButton, BorderLayout.SOUTH);
376     }
377 
378     public JFrame getFrame() {
379         return this;
380     }
381 
382 }