View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  
24  package zeus.generator.task;
25  
26  import java.awt.*;
27  import java.awt.event.*;
28  import javax.swing.*;
29  import javax.swing.border.*;
30  
31  import zeus.concepts.Fact;
32  import zeus.generator.util.BasicFactModel;
33  
34  public class OrderingDialog extends JDialog implements ActionListener {
35  
36    protected static final String OK = "OK";
37    protected static final String CANCEL = "Cancel";
38  
39    protected JList lhsList;
40    protected JList rhsList;
41    protected JButton okButton;
42    protected JButton cancelButton;
43    protected BasicFactModel model;
44    protected OrderingSelector selector;
45  
46    public OrderingDialog(Frame parent, String title, BasicFactModel model) {
47      super(parent,title,true);
48  
49      this.model = model;
50  
51      JPanel pane = (JPanel)getContentPane();
52      pane.setLayout( new BorderLayout() );
53      
54      JPanel p1 = new JPanel();
55      p1.setLayout(new GridLayout(1,2,10,10));
56      okButton = new JButton(OK);
57      cancelButton = new JButton(CANCEL);
58      p1.add(okButton);
59      p1.add(cancelButton);
60      
61      GridBagLayout gb = new GridBagLayout();
62      GridBagConstraints gbc = new GridBagConstraints();
63      gbc.insets = new Insets(10,0,10,0);
64      gbc.anchor = GridBagConstraints.NORTHWEST;
65      gbc.fill = GridBagConstraints.HORIZONTAL;
66      gbc.gridwidth = GridBagConstraints.REMAINDER;
67      gbc.weightx = 1;
68      
69      JPanel p0 = new JPanel();
70      JSeparator s1 = new JSeparator();
71      p0.setLayout(gb);
72      gb.setConstraints(s1,gbc);
73      p0.add(s1);
74      
75      gbc.anchor = GridBagConstraints.CENTER;
76      gbc.fill = GridBagConstraints.NONE;
77      gbc.insets = new Insets(0,0,10,0);
78      gb.setConstraints(p1,gbc);
79      p0.add(p1);
80  
81      JPanel lhsPanel = new JPanel();
82      TitledBorder border = BorderFactory.createTitledBorder("Before");
83      border.setTitlePosition(TitledBorder.TOP);
84      border.setTitleJustification(TitledBorder.RIGHT);
85      border.setTitleFont(new Font("Helvetica", Font.BOLD, 14));
86      border.setTitleColor(Color.blue);
87      lhsPanel.setBorder(border);
88  
89      lhsList = new JList();
90      lhsList.setSelectionModel(new DefaultListSelectionModel());
91      lhsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
92      lhsList.setPreferredSize(new Dimension(100,100));
93      JScrollPane scrollpane1 = new JScrollPane();
94      scrollpane1.getViewport().setView(lhsList);
95      lhsList.setBackground(Color.white);
96  
97      JPanel rhsPanel = new JPanel();
98      border = BorderFactory.createTitledBorder("After");
99      border.setTitlePosition(TitledBorder.TOP);
100     border.setTitleJustification(TitledBorder.RIGHT);
101     border.setTitleFont(new Font("Helvetica", Font.BOLD, 14));
102     border.setTitleColor(Color.blue);
103     rhsPanel.setBorder(border);
104 
105     rhsList = new JList();
106     rhsList.setSelectionModel(new DefaultListSelectionModel());
107     rhsList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
108     rhsList.setPreferredSize(new Dimension(100,100));
109     JScrollPane scrollpane2 = new JScrollPane();
110     scrollpane2.getViewport().setView(rhsList);
111     rhsList.setBackground(Color.white);
112 
113     JPanel p3 = new JPanel();
114     p3.setLayout(new GridLayout(1,2,5,5));
115     lhsPanel.add(scrollpane1);
116     rhsPanel.add(scrollpane2);
117     p3.add(lhsPanel);
118     p3.add(rhsPanel);
119 
120     pane.add("South",p0);
121     pane.add("Center",p3);
122 
123     // Event handling
124     okButton.addActionListener(this);
125     cancelButton.addActionListener(this);
126     this.addWindowListener(
127        new WindowAdapter() {
128           public void windowClosing(WindowEvent evt) { setVisible(false); }
129        }
130     );
131   }
132 
133   public void actionPerformed(ActionEvent evt) {
134     Object source = evt.getSource();
135 
136     if ( source == okButton ) {
137        String lhsItem = (String)lhsList.getSelectedValue();
138        Object[] data = rhsList.getSelectedValues();
139        String[] rhsItem = new String[data.length];
140        for(int i = 0; i < rhsItem.length; i++ )
141           rhsItem[i] = (String)data[i];
142 
143        if ( selector != null && lhsItem != null && rhsItem.length != 0 ) {
144           for(int i = 0; i < rhsItem.length; i++ ) {
145 	     if ( lhsItem.equals(rhsItem[i]) ) {
146                 JOptionPane.showMessageDialog(this,
147 	           "The left and right items must be different:\n" +
148 		   lhsItem + " < " + rhsItem[i],
149                    "Error", JOptionPane.ERROR_MESSAGE);
150 	        return;
151              }
152           }
153           this.setVisible(false);
154 	  selector.orderingSelected(lhsItem,rhsItem);
155        }
156        else
157           this.setVisible(false);
158     }
159     else if ( source == cancelButton ) {
160        this.setVisible(false);
161     }
162   }
163 
164   public void display(OrderingSelector selector) {
165      this.selector = selector;
166      Fact[] data = model.getData();
167      String[] items = new String[data.length];
168      for(int i = 0; i < data.length; i++ )
169         items[i] = data[i].getId();
170 
171      lhsList.setListData(items);
172      rhsList.setListData(items);
173 
174      pack();
175      setVisible(true);
176   }  
177 }