1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 /*****************************************************************************
25 * AgentEditor.java
26 *
27 * Main Frame for the agent editing panels
28 ***************************************************************************/
29
30 package zeus.generator.agent;
31
32 import java.util.*;
33 import java.awt.*;
34 import java.awt.event.*;
35 import javax.swing.*;
36 import javax.swing.border.*;
37 import javax.swing.event.*;
38
39 import zeus.util.*;
40 import zeus.concepts.*;
41 import zeus.gui.help.*;
42 import zeus.generator.*;
43 import zeus.generator.util.*;
44
45
46 public class AgentEditor extends JFrame
47 implements Editor {
48
49 protected DefinitionPanel definitionPanel;
50 protected OrganisationPanel organisationPanel;
51 protected CoordinationPanel coordinationPanel;
52 protected RestrictionPanel restrictionPanel;
53 protected JTabbedPane tabbedPane;
54
55 protected AgentGenerator generator;
56 protected GeneratorModel genmodel;
57 protected OntologyDb ontologyDb;
58 protected HelpWindow helpWin;
59
60 protected boolean changed;
61 protected EventListenerList changeListeners = new EventListenerList();
62 protected AgentDescription currentAgent;
63
64 static final String[] MESSAGE = {
65
66
67
68
69 };
70
71
72 public AgentEditor(AgentGenerator generator, GeneratorModel genmodel,
73 OntologyDb ontologyDb, AgentDescription agent) {
74
75 this.generator = generator;
76 this.genmodel = genmodel;
77 this.currentAgent = agent;
78 this.ontologyDb = ontologyDb;
79
80 changed = false;
81 genmodel.addChangeListener(this);
82
83 getContentPane().setBackground(java.awt.Color.gray);
84 getContentPane().setLayout(new BorderLayout());
85
86 String sep = System.getProperty("file.separator");
87 String path = SystemProps.getProperty("gif.dir") + "generator" + sep;
88 ImageIcon icon = new ImageIcon(path + "edit.gif");
89 setIconImage(icon.getImage());
90
91 tabbedPane = new JTabbedPane();
92 definitionPanel = new DefinitionPanel(generator,genmodel,ontologyDb,
93 this,agent);
94 organisationPanel = new OrganisationPanel(generator,genmodel,ontologyDb,
95 this,agent);
96 coordinationPanel = new CoordinationPanel(generator,genmodel,ontologyDb,
97 this,agent);
98 restrictionPanel = new RestrictionPanel(generator, genmodel, ontologyDb,
99 this, agent);
100
101
102 tabbedPane.addTab("Agent Definition", definitionPanel);
103 tabbedPane.addTab("Agent Organisation", organisationPanel);
104 tabbedPane.addTab("Agent Coordination", coordinationPanel);
105 tabbedPane.addTab("Value Restrictions", restrictionPanel);
106 tabbedPane.setSelectedIndex(0);
107
108 tabbedPane.setTabPlacement(JTabbedPane.BOTTOM);
109 getContentPane().add(tabbedPane,BorderLayout.CENTER);
110
111 this.addWindowListener(new WindowAdapter() {
112 public void windowClosing(WindowEvent evt) { closeDown(); }
113 }
114 );
115 String name = genmodel.getAgentName(agent.getName());
116 setTitle(MESSAGE[2] + name);
117
118 validate();
119 pack();
120 }
121
122
123 public void previous() {
124 if ( (tabbedPane.getSelectedIndex() > 0) )
125 tabbedPane.setSelectedIndex(tabbedPane.getSelectedIndex() - 1);
126 }
127 public void next() {
128 if ( (tabbedPane.getSelectedIndex() + 1 < tabbedPane.getTabCount() ) )
129 tabbedPane.setSelectedIndex(tabbedPane.getSelectedIndex() + 1);
130 }
131 public void help(AbstractButton helpBtn) {
132 if ( helpBtn.isSelected() ) {
133 Point dispos = getLocation();
134 helpWin = new HelpWindow(this, dispos, "generator", "Agent Design Approach");
135 helpWin.setSource(helpBtn);
136 }
137 else
138 helpWin.dispose();
139 }
140
141 public void save() {
142 definitionPanel.save();
143 organisationPanel.save();
144 coordinationPanel.save();
145 restrictionPanel.save();
146 genmodel.updateAgent(currentAgent);
147 changed = false;
148 fireChanged();
149 }
150
151 public AgentDescription getCurrentAgent() {
152 return currentAgent;
153 }
154 public String getObjectName() {
155 return genmodel.getAgentName(currentAgent.getName());
156 }
157
158 public boolean hasChanged() {
159 return changed;
160 }
161
162 public void stateChanged(ChangeEvent evt) {
163 if ( evt.getSource() != genmodel )
164 changed = true;
165 else if ( evt.getSource() == genmodel ) {
166 String agentName = genmodel.getAgentName(currentAgent.getName());
167 setTitle(MESSAGE[2] + agentName);
168 }
169 fireChanged();
170 }
171
172 public void closeDown() {
173 if ( changed ) {
174 int answer = JOptionPane.showConfirmDialog(this,MESSAGE[0],
175 MESSAGE[1],JOptionPane.YES_NO_CANCEL_OPTION);
176 if ( answer == JOptionPane.YES_OPTION )
177 save();
178 else if ( answer == JOptionPane.CANCEL_OPTION )
179 return;
180 }
181 generator.agentEditorClosed(currentAgent.getName());
182 dispose();
183 }
184
185
186 public void addChangeListener(ChangeListener x) {
187 changeListeners.add(ChangeListener.class, x);
188 }
189 public void removeChangeListener(ChangeListener x) {
190 changeListeners.remove(ChangeListener.class, x);
191 }
192
193 protected void fireChanged() {
194 ChangeEvent c = new ChangeEvent(this);
195 Object[] listeners = changeListeners.getListenerList();
196 for(int i = listeners.length-2; i >= 0; i -=2) {
197 if (listeners[i] == ChangeListener.class) {
198 ChangeListener cl = (ChangeListener)listeners[i+1];
199 cl.stateChanged(c);
200 }
201 }
202 }
203 }