001    
002    package gui;
003    
004    import java.awt.event.ActionListener;
005    import java.awt.event.ActionEvent;
006    
007    //import javax.swing.Action;
008    import javax.swing.JButton;
009    import javax.swing.JMenuItem;
010    import javax.swing.JTextField;
011    
012    /**
013     * Main class to send GUI actions to the TSP model.
014     * @author Heinz Kredel.
015     */
016    public class TSPguiActionControl implements ActionListener {
017    
018        TSPguiModel model;
019    
020    /**
021     * @param model the TSPguiModel.
022     */
023        public TSPguiActionControl(TSPguiModel model) {
024            this.model = model;
025        }
026    
027        public void actionPerformed(ActionEvent event) {
028            Object s = event.getSource();
029            //System.out.println("source = " + s);
030            if ( s instanceof JMenuItem ) {
031               JMenuItem source = (JMenuItem)s;
032               String was = source.getActionCommand();
033               if ( was == null ) {
034                   return;
035               }
036               if ( was.equals("generate") ) {
037                  model.generateProblem();
038                  model.doUpdate();
039                  return;
040               }
041               if ( was.equals("solve") ) {
042                  model.solveProblem();
043                  model.doUpdate();
044                  return;
045               }
046               if ( was.equals("stop") ) {
047                  model.stopProblem();
048                  model.doUpdate();
049                  return;
050               }
051               if ( was.equals("sequential") ) {
052                  model.setAlgorithm( was );
053                  model.doUpdate();
054                  return;
055               }
056               if ( was.equals("parallel") ) {
057                  model.setAlgorithm( was );
058                  model.doUpdate();
059                  return;
060               }
061               if ( was.equals("distributed") ) {
062                  model.setAlgorithm( was );
063                  model.doUpdate();
064                  return;
065               }
066               if ( was.equals("about") ) {
067                  TSPguiMain.doAboutDialog();
068                  model.doUpdate();
069                  return;
070               }
071               if ( was.equals("standAlone") ) {
072                  try {
073                      model.setStandAlone( ! model.getStandAlone() );
074                  } catch (NumberFormatException ignored) {
075                      //ignored.printStackTrace();
076                  }
077                  model.doUpdate();
078                  return;
079               }
080               if ( was.equals("exit") ) {
081                  if ( ! model.isDone() ) {
082                     if ( ! TSPguiMain.doExitDialog() ) {
083                        return;
084                     }
085                     model.setDone();
086                  }
087                  return;
088               }
089               return;
090            }
091            if ( s instanceof JTextField ) {
092               JTextField source = (JTextField)s;
093               System.out.println("source = " + event.getActionCommand() );
094               String was = event.getActionCommand();
095               if ( was == null ) {
096                   return;
097               }
098               if ( was.equals("size") ) {
099                  try {
100                      int i = Integer.parseInt( source.getText() );
101                      model.setProbSize(i);
102                  } catch (NumberFormatException ignored) {
103                  }
104                  model.doUpdate();
105                  return;
106               }
107               if ( was.equals("file") ) {
108                  // not implemented
109                  model.doUpdate();
110                  return;
111               }
112               if ( was.equals("algorithm") ) {
113                  model.setAlgorithm( source.getText() );
114                  model.doUpdate();
115                  return;
116               }
117               if ( was.equals("threads") ) {
118                  try {
119                      int i = Integer.parseInt( source.getText() );
120                      model.setThreads(i);
121                  } catch (NumberFormatException ignored) {
122                  }
123                  model.doUpdate();
124                  return;
125               }
126               if ( was.equals("maxiter") ) {
127                  try {
128                      long i;
129                      if ( source.getText().equals("max") ) {
130                          i = Long.MAX_VALUE;
131                      } else {
132                          i = Long.parseLong( source.getText() );
133                      }
134                      model.setMaxIterations(i);
135                  } catch (NumberFormatException ignored) {
136                      //ignored.printStackTrace();
137                  }
138                  model.doUpdate();
139                  return;
140               }
141               if ( was.equals("server") ) {
142                  model.setServer( source.getText() );
143                  model.doUpdate();
144                  return;
145               }
146               if ( was.equals("port") ) {
147                  try {
148                      int i = Integer.parseInt( source.getText() );
149                      model.setPort(i);
150                  } catch (NumberFormatException ignored) {
151                      //ignored.printStackTrace();
152                  }
153                  model.doUpdate();
154                  return;
155               }
156               return;
157            }
158            if ( s instanceof JButton ) {
159               JButton source = (JButton)s;
160               //System.out.println("source = " + event.getActionCommand() );
161               String was = event.getActionCommand();
162               if ( was == null ) {
163                   return;
164               }
165               return;
166            }
167        }
168    
169    }