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.util;
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  
33  
34  public class ModifierDialog extends JDialog 
35                              implements ActionListener, ItemListener {
36  
37    protected static final String  CANCEL     = "Cancel";
38    protected static final String  OK         = "Ok";
39  
40    protected static final String  VARIABLE   = "Is a variable";
41    protected static final String  NEGATIVE   = "Not";
42    protected static final String  READONLY   = "Strict Precondition (read only - not consumed)";
43    protected static final String  LOCAL      = "Must be in local database";
44    protected static final String  REPLACED   = "Is replaced after use";
45    protected static final String  SIDEEFFECT = "Is a side-effect only";
46  
47    protected JButton okButton;
48    protected JButton cancelButton;
49  
50    protected JCheckBox isVariable   = new JCheckBox(VARIABLE,false);
51    protected JCheckBox isNegative   = new JCheckBox(NEGATIVE,false);
52    protected JCheckBox isReadOnly   = new JCheckBox(READONLY,false);
53    protected JCheckBox isLocal      = new JCheckBox(LOCAL,false);
54    protected JCheckBox isReplaced   = new JCheckBox(REPLACED,false);
55    protected JCheckBox isSideEffect = new JCheckBox(SIDEEFFECT,false);
56  
57    protected FactModifier caller = null;
58  
59    public ModifierDialog(Frame parent, String title) {
60      super(parent,title,true);
61  
62      JPanel pane = (JPanel)getContentPane();
63      pane.setLayout( new BorderLayout() );
64  
65      
66      JPanel p1 = new JPanel();
67      p1.setLayout(new GridLayout(1,2,10,10));
68      okButton = new JButton(OK);
69      cancelButton = new JButton(CANCEL);
70      p1.add(okButton);
71      p1.add(cancelButton);
72      
73      GridBagLayout gb = new GridBagLayout();
74      GridBagConstraints gbc = new GridBagConstraints();
75      gbc.insets = new Insets(10,0,10,0);
76      gbc.anchor = GridBagConstraints.NORTHWEST;
77      gbc.fill = GridBagConstraints.HORIZONTAL;
78      gbc.gridwidth = GridBagConstraints.REMAINDER;
79      gbc.weightx = 1;
80      
81      JPanel p0 = new JPanel();
82      JSeparator s1 = new JSeparator();
83      p0.setLayout(gb);
84      gb.setConstraints(s1,gbc);
85      p0.add(s1);
86      
87      gbc.anchor = GridBagConstraints.CENTER;
88      gbc.fill = GridBagConstraints.NONE;
89      gbc.insets = new Insets(0,0,10,0);
90      gb.setConstraints(p1,gbc);
91      p0.add(p1);
92  
93      JPanel p3 = new JPanel();
94      p3.setBackground(Color.lightGray);
95      CompoundBorder cbr = new CompoundBorder(new EtchedBorder(),
96                                              new EmptyBorder(5,5,5,5));
97      p3.setBorder(cbr);
98      p3.setLayout(new GridLayout(6,1,5,5));
99  
100     p3.add(isVariable);
101     p3.add(isNegative);
102     p3.add(isReadOnly);
103     p3.add(isLocal);
104     p3.add(isReplaced);
105     p3.add(isSideEffect);
106 
107     isVariable.setBackground(Color.lightGray);
108     isNegative.setBackground(Color.lightGray);
109     isReadOnly.setBackground(Color.lightGray);
110     isLocal.setBackground(Color.lightGray);
111     isReplaced.setBackground(Color.lightGray);
112     isSideEffect.setBackground(Color.lightGray);
113 
114     pane.add("South",p0);
115     pane.add("Center",p3);
116     
117     // Event handling
118     okButton.addActionListener(this);
119     cancelButton.addActionListener(this);
120 
121     isVariable.addItemListener(this);
122     isNegative.addItemListener(this);
123     isReadOnly.addItemListener(this);
124     isLocal.addItemListener(this);
125     isReplaced.addItemListener(this);
126     isSideEffect.addItemListener(this);
127 
128     this.addWindowListener(
129        new WindowAdapter() {
130           public void windowClosing(WindowEvent evt) { setVisible(false); }
131        }
132     );
133     this.pack();
134   }
135   
136   public void actionPerformed(ActionEvent evt) {
137     Object source = evt.getSource();
138     if ( source == okButton ) {
139        this.setVisible(false);
140        if ( caller != null ) {
141           int modifier = 0;
142           modifier = Fact.setIsVariable(modifier,   isVariable.isSelected());
143           modifier = Fact.setIsNegative(modifier,   isNegative.isEnabled() &&
144                                                     isNegative.isSelected());
145           modifier = Fact.setIsReadOnly(modifier,   isReadOnly.isEnabled() &&
146                                                     isReadOnly.isSelected());
147           modifier = Fact.setIsLocal(modifier,      isLocal.isEnabled() &&
148                                                     isLocal.isSelected());
149           modifier = Fact.setIsReplaced(modifier,   isReplaced.isEnabled() && 
150                                                     isReplaced.isSelected());
151           modifier = Fact.setIsSideEffect(modifier, isSideEffect.isEnabled() &&
152                                                     isSideEffect.isSelected());
153 
154           caller.factModifiersChanged(modifier);
155        }
156     }
157     else if ( source == cancelButton ) {
158        this.setVisible(false);
159     }
160   }
161 
162   public void itemStateChanged(ItemEvent e) {
163     Object src = e.getSource();
164     boolean state = (e.getStateChange() == ItemEvent.SELECTED);
165     if ( src == isNegative ) {
166        isReadOnly.setEnabled(!state);
167        isLocal.setEnabled(!state);
168        isReplaced.setEnabled(!state);
169     }
170     else if ( src == isReadOnly ) {
171        isNegative.setEnabled(!state);
172        isReplaced.setEnabled(!state);
173     }
174     else if ( src == isLocal ) {
175        isNegative.setEnabled(!state);
176     }
177     else if ( src == isReplaced ) {
178        isNegative.setEnabled(!state);
179        isReadOnly.setEnabled(!state);
180     }
181   }
182 
183   public void display(FactModifier caller, int modifier, int type) {
184      this.caller = caller;
185 
186      isVariable.setSelected(Fact.isVariable(modifier));
187      isVariable.setEnabled(false);
188 
189      if ( type == FactPanel.PRECONDITION ) {
190         isNegative.setEnabled(true);
191         isReadOnly.setEnabled(true);
192         isLocal.setEnabled(true);
193         isReplaced.setEnabled(true);
194         isSideEffect.setEnabled(false);
195 
196         isNegative.setSelected(Fact.isNegative(modifier));
197         isReadOnly.setSelected(Fact.isReadOnly(modifier));
198         isLocal.setSelected(Fact.isLocal(modifier));
199         isReplaced.setSelected(Fact.isReplaced(modifier));
200         isSideEffect.setSelected(false);
201      }
202      else {
203         isNegative.setSelected(false);
204         isReadOnly.setSelected(false);
205         isLocal.setSelected(false);
206         isReplaced.setSelected(false);
207         isSideEffect.setSelected(Fact.isSideEffect(modifier));
208 
209         isNegative.setEnabled(false);
210         isReadOnly.setEnabled(false);
211         isLocal.setEnabled(false);
212         isReplaced.setEnabled(false);
213         isSideEffect.setEnabled(true);
214      }
215      setVisible(true);
216      pack();
217   }
218 
219 }