View Javadoc

1   /*******************************************************************************
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004, 2012 Artois University and CNRS
3    *
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the Eclipse Public License v1.0
6    * which accompanies this distribution, and is available at
7    *  http://www.eclipse.org/legal/epl-v10.html
8    *
9    * Alternatively, the contents of this file may be used under the terms of
10   * either the GNU Lesser General Public License Version 2.1 or later (the
11   * "LGPL"), in which case the provisions of the LGPL are applicable instead
12   * of those above. If you wish to allow use of your version of this file only
13   * under the terms of the LGPL, and not to allow others to use your version of
14   * this file under the terms of the EPL, indicate your decision by deleting
15   * the provisions above and replace them with the notice and other provisions
16   * required by the LGPL. If you do not delete the provisions above, a recipient
17   * may use your version of this file under the terms of the EPL or the LGPL.
18   *
19   * Based on the original MiniSat specification from:
20   *
21   * An extensible SAT solver. Niklas Een and Niklas Sorensson. Proceedings of the
22   * Sixth International Conference on Theory and Applications of Satisfiability
23   * Testing, LNCS 2919, pp 502-518, 2003.
24   *
25   * See www.minisat.se for the original solver in C++.
26   *
27   * Contributors:
28   *   CRIL - initial API and implementation
29   *******************************************************************************/
30  package org.sat4j.sat;
31  
32  import java.awt.BorderLayout;
33  import java.awt.Container;
34  import java.awt.event.ActionEvent;
35  import java.awt.event.ActionListener;
36  import java.awt.event.WindowAdapter;
37  import java.awt.event.WindowEvent;
38  import java.io.BufferedReader;
39  import java.io.IOException;
40  import java.io.InputStreamReader;
41  import java.net.URL;
42  
43  import javax.swing.Box;
44  import javax.swing.ButtonGroup;
45  import javax.swing.JFrame;
46  import javax.swing.JLabel;
47  import javax.swing.JMenu;
48  import javax.swing.JMenuBar;
49  import javax.swing.JMenuItem;
50  import javax.swing.JOptionPane;
51  import javax.swing.JRadioButtonMenuItem;
52  import javax.swing.JScrollPane;
53  
54  import org.sat4j.sat.visu.VisuPreferencesFrame;
55  import org.sat4j.specs.ILogAble;
56  
57  /**
58   * 
59   * JFrame for the remote control.
60   * 
61   * @author sroussel
62   * 
63   */
64  public class RemoteControlFrame extends JFrame implements ILogAble {
65  
66      /**
67  	 * 
68  	 */
69      private static final long serialVersionUID = 1L;
70  
71      private JMenuItem activateTracing;
72  
73      private JRadioButtonMenuItem gnuplotBasedRadio;
74      private JRadioButtonMenuItem jChartBasedRadio;
75  
76      private DetailedCommandPanel commandePanel;
77      private String filename;
78  
79      private String ramdisk;
80  
81      private String[] args;
82      private VisuPreferencesFrame visuFrame;
83  
84      private static final String ACTIVATE = "Activate Tracing";
85      private static final String DEACTIVATE = "Deactivate Tracing";
86  
87      public RemoteControlFrame(String filename, String ramdisk, String[] args) {
88          super("Remote Control");
89  
90          this.filename = filename;
91          this.ramdisk = ramdisk;
92          this.args = args.clone();
93  
94          JFrame.setDefaultLookAndFeelDecorated(true);
95  
96          createAndShowGUI();
97      }
98  
99      public RemoteControlFrame(String filename, String ramdisk) {
100         this(filename, ramdisk, new String[] {});
101     }
102 
103     public RemoteControlFrame(String filename) {
104         this(filename, "", new String[] {});
105     }
106 
107     public RemoteControlFrame(String filename, String[] args) {
108         this(filename, "", args);
109     }
110 
111     public void reinitialiser() {
112     }
113 
114     public void setActivateGnuplot(boolean b) {
115         this.activateTracing.setSelected(b);
116         activateTracing(b);
117     }
118 
119     private void createAndShowGUI() {
120         Container c = this.getContentPane();
121         c.setLayout(new BorderLayout());
122 
123         createMenuBar();
124 
125         this.commandePanel = new DetailedCommandPanel(this.filename,
126                 this.ramdisk, this.args, this);
127 
128         this.commandePanel.setChartBased(true);
129         this.commandePanel.activateGnuplotTracing(true);
130 
131         this.visuFrame = new VisuPreferencesFrame(
132                 this.commandePanel.getGnuplotPreferences());
133 
134         JScrollPane scrollPane = new JScrollPane(this.commandePanel);
135 
136         this.add(scrollPane);
137 
138         this.addWindowListener(new WindowAdapter() {
139 
140             @Override
141             public void windowClosing(WindowEvent e) {
142                 RemoteControlFrame.this.commandePanel.stopVisu();
143                 setVisible(false);
144                 dispose();
145             }
146 
147         });
148 
149         this.pack();
150         this.setVisible(true);
151     }
152 
153     public void clickOnAboutSolver() {
154         if (this.commandePanel.getSolver() != null) {
155             JOptionPane.showMessageDialog(this, this.commandePanel.getSolver()
156                     .toString());
157         } else {
158             JOptionPane.showMessageDialog(this,
159                     "No solver is running at the moment. Please start solver.");
160         }
161     }
162 
163     public void setActivateTracingEditableUnderCondition(boolean b) {
164         if (this.activateTracing.getText().equals(ACTIVATE)) {
165             this.activateTracing.setEnabled(b);
166         }
167     }
168 
169     public void setActivateTracingEditable(boolean b) {
170         this.activateTracing.setEnabled(b);
171     }
172 
173     public void setActivateRadioTracing(boolean activate) {
174         gnuplotBasedRadio.setEnabled(activate);
175         jChartBasedRadio.setEnabled(activate);
176     }
177 
178     public void createMenuBar() {
179         JMenuBar barreMenu = new JMenuBar();
180         JMenu menu = new JMenu("File");
181         barreMenu.add(menu);
182 
183         this.activateTracing = new JMenuItem(DEACTIVATE);
184         menu.add(this.activateTracing);
185 
186         this.activateTracing.addActionListener(new ActionListener() {
187             public void actionPerformed(ActionEvent e) {
188                 activateTracing(RemoteControlFrame.this.activateTracing
189                         .getText().equals(ACTIVATE));
190             }
191         });
192 
193         menu.addSeparator();
194 
195         gnuplotBasedRadio = new JRadioButtonMenuItem("Trace with Gnuplot");
196         jChartBasedRadio = new JRadioButtonMenuItem("Trace with Java");
197 
198         ButtonGroup visuGroup = new ButtonGroup();
199         visuGroup.add(gnuplotBasedRadio);
200         visuGroup.add(jChartBasedRadio);
201 
202         menu.add(gnuplotBasedRadio);
203 
204         gnuplotBasedRadio.addActionListener(new ActionListener() {
205             public void actionPerformed(ActionEvent e) {
206                 RemoteControlFrame.this.commandePanel.setGnuplotBased(true);
207                 RemoteControlFrame.this.commandePanel.setChartBased(false);
208                 RemoteControlFrame.this.commandePanel
209                         .activateGnuplotTracing(RemoteControlFrame.this.activateTracing
210                                 .getText().equals(DEACTIVATE));
211                 log("Use gnuplot tracing");
212             }
213         });
214         jChartBasedRadio.setSelected(true);
215 
216         menu.add(jChartBasedRadio);
217 
218         jChartBasedRadio.addActionListener(new ActionListener() {
219             public void actionPerformed(ActionEvent e) {
220                 RemoteControlFrame.this.commandePanel.setGnuplotBased(false);
221                 RemoteControlFrame.this.commandePanel.setChartBased(true);
222                 RemoteControlFrame.this.commandePanel
223                         .activateGnuplotTracing(RemoteControlFrame.this.activateTracing
224                                 .getText().equals(DEACTIVATE));
225                 log("Use java tracing");
226             }
227         });
228 
229         menu.addSeparator();
230 
231         JMenuItem quit = new JMenuItem("Exit");
232         menu.add(quit);
233 
234         quit.addActionListener(new ActionListener() {
235             public void actionPerformed(ActionEvent e) {
236                 RemoteControlFrame.this.commandePanel.stopVisu();
237                 System.exit(NORMAL);
238             }
239         });
240 
241         JMenu preferences = new JMenu("Preferences");
242         JMenuItem gnuplotPreferencesItem = new JMenuItem(
243                 "Visualisation preferences");
244 
245         gnuplotPreferencesItem.addActionListener(new ActionListener() {
246             public void actionPerformed(ActionEvent e) {
247                 RemoteControlFrame.this.visuFrame.setVisible(true);
248             }
249         });
250 
251         preferences.add(gnuplotPreferencesItem);
252 
253         barreMenu.add(preferences);
254 
255         barreMenu.add(Box.createHorizontalGlue());
256 
257         // ...create the rightmost menu...
258         JLabel version = new JLabel(getVersion());
259         barreMenu.add(version);
260 
261         this.setJMenuBar(barreMenu);
262 
263     }
264 
265     public void log(String message) {
266         this.commandePanel.log(message);
267     }
268 
269     private String getVersion() {
270         URL url = RemoteControlFrame.class.getResource("/sat4j.version"); //$NON-NLS-1$
271         String s = "";
272         if (url == null) {
273             s = "no version file found!!!"; //$NON-NLS-1$			
274         } else {
275             BufferedReader in = null;
276             try {
277                 in = new BufferedReader(new InputStreamReader(url.openStream()));
278                 s = "version " + in.readLine(); //$NON-NLS-1$
279             } catch (IOException e) {
280                 s = "c ERROR: " + e.getMessage();
281             } finally {
282                 if (in != null) {
283                     try {
284                         in.close();
285                     } catch (IOException e) {
286                         s = "c ERROR: " + e.getMessage();
287                     }
288                 }
289             }
290         }
291         return s;
292     }
293 
294     public void activateTracing(boolean b) {
295         if (b) {
296             log("Activated tracing");
297             this.activateTracing.setText(DEACTIVATE);
298             this.commandePanel.setPlotActivated(true);
299         } else {
300             log("Deactivated tracing.");
301             this.activateTracing.setText(ACTIVATE);
302             this.commandePanel.stopVisu();
303             this.commandePanel.setPlotActivated(false);
304         }
305         if (this.commandePanel.getStartStopText().equals("Stop")
306                 && this.activateTracing.getText().equals(ACTIVATE)) {
307             this.activateTracing.setEnabled(false);
308         } else {
309             this.activateTracing.setEnabled(true);
310         }
311     }
312 
313     public void setOptimisationMode(boolean optimizationMode) {
314         this.commandePanel.setOptimisationMode(optimizationMode);
315     }
316 
317 }