| StrategyPanel.java |
1 // ============================================================================
2 // Copyright 2006, 2007, 2008 Daniel W. Dyer
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 // ============================================================================
16 package org.uncommons.watchmaker.examples.travellingsalesman;
17
18 import java.awt.BorderLayout;
19 import javax.swing.BorderFactory;
20 import javax.swing.ButtonGroup;
21 import javax.swing.JPanel;
22 import javax.swing.JRadioButton;
23
24 /**
25 * Panel for configuring a route-finding strategy for the travelling
26 * salesman problem.
27 * @author Daniel Dyer
28 */
29 final class StrategyPanel extends JPanel
30 {
31 private final DistanceLookup distances;
32 private final JRadioButton bruteForceOption;
33
34 /**
35 * Creates a panel with components for controlling the route-finding
36 * strategy.
37 * @param distances Data used by the strategy in order to calculate
38 * shortest routes.
39 */
40 public StrategyPanel(DistanceLookup distances)
41 {
42 super(new BorderLayout());
43 this.distances = distances;
44 bruteForceOption = new JRadioButton("Brute Force", true);
45 bruteForceOption.setName("BruteForce"); // Helps to find the radio button from a unit test.
46 ButtonGroup strategyGroup = new ButtonGroup();
47 strategyGroup.add(bruteForceOption);
48 add(bruteForceOption, BorderLayout.SOUTH);
49 setBorder(BorderFactory.createTitledBorder("Route-Finding Strategy"));
50 }
51
52
53 public TravellingSalesmanStrategy getStrategy()
54 {
55 return new BruteForceTravellingSalesman(distances);
56 }
57
58
59 @Override
60 public void setEnabled(boolean b)
61 {
62 bruteForceOption.setEnabled(b);
63 super.setEnabled(b);
64 }
65 }
66