View Javadoc

1   package org.sat4j.sat;
2   
3   import java.awt.BorderLayout;
4   import java.awt.FlowLayout;
5   import java.awt.event.ActionEvent;
6   import java.awt.event.ActionListener;
7   import java.util.ArrayList;
8   import java.util.List;
9   
10  import javax.swing.JButton;
11  import javax.swing.JComboBox;
12  import javax.swing.JLabel;
13  import javax.swing.JPanel;
14  import javax.swing.border.CompoundBorder;
15  import javax.swing.border.TitledBorder;
16  
17  import org.sat4j.minisat.core.IPhaseSelectionStrategy;
18  
19  public class PhaseCommandComponent extends CommandComponent {
20  
21      /**
22       * 
23       */
24      private static final long serialVersionUID = 1L;
25  
26      private String currentPhaseSelectionStrategy;
27  
28      private JComboBox phaseList;
29      private JLabel phaseListLabel;
30      private static final String PHASE_STRATEGY = "Choose phase strategy :";
31  
32      private JButton phaseApplyButton;
33      private static final String PHASE_APPLY = "Apply";
34  
35      private static final String PHASE_STRATEGY_CLASS = "org.sat4j.minisat.core.IPhaseSelectionStrategy";
36      private static final String PHASE_PATH_SAT = "org.sat4j.minisat.orders";
37  
38      private SolverController solverController;
39  
40      public PhaseCommandComponent(String name, SolverController commandPanel,
41              String initialPhaseStrategyName) {
42          this.currentPhaseSelectionStrategy = initialPhaseStrategyName;
43          this.solverController = commandPanel;
44          this.setName(name);
45          createPanel();
46      }
47  
48      @Override
49      public void createPanel() {
50          createPhasePanel();
51      }
52  
53      public void createPhasePanel() {
54  
55          this.setBorder(new CompoundBorder(new TitledBorder(null,
56                  this.getName(), TitledBorder.LEFT, TitledBorder.TOP),
57                  DetailedCommandPanel.BORDER5));
58  
59          this.setLayout(new BorderLayout());
60  
61          JPanel tmpPanel1 = new JPanel();
62          tmpPanel1.setLayout(new FlowLayout());
63  
64          this.phaseListLabel = new JLabel(PHASE_STRATEGY);
65  
66          this.phaseList = new JComboBox(getListOfPhaseStrategies().toArray());
67          this.phaseList.setSelectedItem(this.currentPhaseSelectionStrategy);
68  
69          tmpPanel1.add(this.phaseListLabel);
70          tmpPanel1.add(this.phaseList);
71  
72          this.phaseApplyButton = new JButton(PHASE_APPLY);
73  
74          this.phaseApplyButton.addActionListener(new ActionListener() {
75              public void actionPerformed(ActionEvent e) {
76                  hasClickedOnApplyPhase();
77              }
78          });
79  
80          JPanel tmpPanel2 = new JPanel();
81          tmpPanel2.add(this.phaseApplyButton);
82  
83          this.add(tmpPanel1, BorderLayout.CENTER);
84          this.add(tmpPanel2, BorderLayout.SOUTH);
85      }
86  
87      public void hasClickedOnApplyPhase() {
88          String phaseName = (String) this.phaseList.getSelectedItem();
89          this.currentPhaseSelectionStrategy = phaseName;
90          IPhaseSelectionStrategy phase = null;
91          try {
92              phase = (IPhaseSelectionStrategy) Class.forName(
93                      PHASE_PATH_SAT + "." + phaseName).newInstance();
94              phase.init(this.solverController.getNVar() + 1);
95              this.solverController.setPhaseSelectionStrategy(phase);
96  
97          } catch (ClassNotFoundException e) {
98              e.printStackTrace();
99          } catch (IllegalAccessException e) {
100             e.printStackTrace();
101         } catch (InstantiationException e) {
102             e.printStackTrace();
103         }
104 
105     }
106 
107     public void setPhasePanelEnabled(boolean enabled) {
108         this.phaseList.setEnabled(enabled);
109         this.phaseListLabel.setEnabled(enabled);
110         this.phaseApplyButton.setEnabled(enabled);
111         repaint();
112     }
113 
114     public List<String> getListOfPhaseStrategies() {
115         List<String> resultRTSI = RTSI.find(PHASE_STRATEGY_CLASS);
116         List<String> finalResult = new ArrayList<String>();
117 
118         for (String s : resultRTSI) {
119             if (!s.contains("Remote")) {
120                 finalResult.add(s);
121             }
122         }
123 
124         return finalResult;
125     }
126 
127     public void setPhaseListSelectedItem(String name) {
128         this.currentPhaseSelectionStrategy = name;
129         this.phaseList.setSelectedItem(this.currentPhaseSelectionStrategy);
130         repaint();
131     }
132 }