1 | package genius.gui.domainrepository;
|
---|
2 |
|
---|
3 | import java.awt.Font;
|
---|
4 | import java.awt.event.MouseAdapter;
|
---|
5 | import java.awt.event.MouseEvent;
|
---|
6 | import java.io.File;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.net.MalformedURLException;
|
---|
9 | import java.net.URL;
|
---|
10 |
|
---|
11 | import javax.swing.JMenuItem;
|
---|
12 | import javax.swing.JOptionPane;
|
---|
13 | import javax.swing.JPopupMenu;
|
---|
14 | import javax.swing.JTree;
|
---|
15 | import javax.swing.tree.DefaultTreeModel;
|
---|
16 | import javax.swing.tree.TreePath;
|
---|
17 | import javax.swing.tree.TreeSelectionModel;
|
---|
18 |
|
---|
19 | import genius.core.Domain;
|
---|
20 | import genius.core.DomainImpl;
|
---|
21 | import genius.core.repository.DomainRepItem;
|
---|
22 | import genius.core.repository.ProfileRepItem;
|
---|
23 | import genius.core.repository.RepItem;
|
---|
24 | import genius.core.repository.Repository;
|
---|
25 | import genius.core.repository.RepositoryFactory;
|
---|
26 | import genius.core.utility.AdditiveUtilitySpace;
|
---|
27 | import genius.core.xml.SimpleElement;
|
---|
28 | import genius.gui.GeniusAppInterface;
|
---|
29 | import genius.gui.tree.DomainAndProfileEditorPanel;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * A user interface to the domain repository
|
---|
33 | *
|
---|
34 | */
|
---|
35 | @SuppressWarnings("serial")
|
---|
36 | public class DomainRepositoryUI extends JTree {
|
---|
37 | private Repository<DomainRepItem> domainrepository;
|
---|
38 | private MyTreeNode root = new MyTreeNode(null);
|
---|
39 | private DefaultTreeModel scenarioTreeModel;
|
---|
40 | private GeniusAppInterface mainPanel;
|
---|
41 |
|
---|
42 | public DomainRepositoryUI(GeniusAppInterface negoView) {
|
---|
43 | domainrepository = RepositoryFactory.get_domain_repos();
|
---|
44 | initTree();
|
---|
45 | setModel(scenarioTreeModel);
|
---|
46 | this.mainPanel = negoView;
|
---|
47 |
|
---|
48 | setMinimumSize(new java.awt.Dimension(100, 100));
|
---|
49 | setName("treeDomains");
|
---|
50 |
|
---|
51 | addMouseListener(new java.awt.event.MouseAdapter() {
|
---|
52 | @Override
|
---|
53 | public void mouseClicked(java.awt.event.MouseEvent evt) {
|
---|
54 | treeDomainsMouseClicked(evt);
|
---|
55 | }
|
---|
56 | });
|
---|
57 |
|
---|
58 | }
|
---|
59 |
|
---|
60 | private void initTree() {
|
---|
61 | // for all domains in the domain repository
|
---|
62 | for (DomainRepItem repitem : domainrepository.getItems()) {
|
---|
63 | DomainRepItem dri = repitem;
|
---|
64 | MyTreeNode domainNode = new MyTreeNode(dri);
|
---|
65 | // add all preference profiles of the domain as nodes
|
---|
66 | for (ProfileRepItem profileitem : dri.getProfiles()) {
|
---|
67 | domainNode.add(new MyTreeNode(profileitem));
|
---|
68 | }
|
---|
69 | root.add(domainNode);
|
---|
70 | }
|
---|
71 |
|
---|
72 | scenarioTreeModel = new DefaultTreeModel(root);
|
---|
73 | setModel(scenarioTreeModel);
|
---|
74 | Font currentFont = getFont();
|
---|
75 | Font bigFont = new Font(currentFont.getName(), currentFont.getStyle(),
|
---|
76 | currentFont.getSize() + 2);
|
---|
77 | setRowHeight(23);
|
---|
78 | setFont(bigFont);
|
---|
79 | setRootVisible(false);
|
---|
80 | setShowsRootHandles(true);
|
---|
81 | getSelectionModel()
|
---|
82 | .setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
|
---|
83 | addMouseListener(new MouseAdapter() {
|
---|
84 |
|
---|
85 | // for Windows
|
---|
86 | @Override
|
---|
87 | public void mouseReleased(MouseEvent e) {
|
---|
88 | mouseCode(e);
|
---|
89 | }
|
---|
90 |
|
---|
91 | // for Linux
|
---|
92 | @Override
|
---|
93 | public void mousePressed(MouseEvent e) {
|
---|
94 | mouseCode(e);
|
---|
95 | }
|
---|
96 |
|
---|
97 | private void mouseCode(MouseEvent e) {
|
---|
98 | if (e.getButton() == MouseEvent.BUTTON3) {
|
---|
99 | TreePath selPath = getPathForLocation(e.getX(), e.getY());
|
---|
100 | if (selPath == null) {
|
---|
101 | JPopupMenu popup = createPopupMenu(null);
|
---|
102 | popup.show(e.getComponent(), e.getX(), e.getY());
|
---|
103 | return;
|
---|
104 | }
|
---|
105 |
|
---|
106 | MyTreeNode node = (MyTreeNode) selPath
|
---|
107 | .getLastPathComponent();
|
---|
108 | setSelectionPath(selPath);
|
---|
109 | if (e.isPopupTrigger()
|
---|
110 | && e.getComponent() instanceof JTree) {
|
---|
111 | JPopupMenu popup = createPopupMenu(node);
|
---|
112 | popup.show(e.getComponent(), e.getX(), e.getY());
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 | });
|
---|
117 | }
|
---|
118 |
|
---|
119 | private JPopupMenu createPopupMenu(final MyTreeNode node) {
|
---|
120 | JPopupMenu popup = new JPopupMenu();
|
---|
121 |
|
---|
122 | JMenuItem newDomain = new JMenuItem("New domain");
|
---|
123 | newDomain.addActionListener(new java.awt.event.ActionListener() {
|
---|
124 | @Override
|
---|
125 | public void actionPerformed(java.awt.event.ActionEvent evt) {
|
---|
126 | addDomain();
|
---|
127 | }
|
---|
128 | });
|
---|
129 |
|
---|
130 | popup.add(newDomain);
|
---|
131 |
|
---|
132 | if (node != null) {
|
---|
133 | if (node.getRepositoryItem() instanceof ProfileRepItem) {
|
---|
134 | JMenuItem deletePP = new JMenuItem("Delete preference profile");
|
---|
135 | deletePP.addActionListener(new java.awt.event.ActionListener() {
|
---|
136 | @Override
|
---|
137 | public void actionPerformed(
|
---|
138 | java.awt.event.ActionEvent evt) {
|
---|
139 | deleteProfile(node);
|
---|
140 | }
|
---|
141 | });
|
---|
142 | popup.add(deletePP);
|
---|
143 | } else {
|
---|
144 | JMenuItem newPP = new JMenuItem("New preference profile");
|
---|
145 | newPP.addActionListener(new java.awt.event.ActionListener() {
|
---|
146 | @Override
|
---|
147 | public void actionPerformed(
|
---|
148 | java.awt.event.ActionEvent evt) {
|
---|
149 | if (domainHasIssues(node)) {
|
---|
150 | newPreferenceProfile(node);
|
---|
151 | } else {
|
---|
152 | JOptionPane.showMessageDialog(null,
|
---|
153 | "Before creating a preference profile, the domain must be saved with at least one isue.",
|
---|
154 | "Domain error", 0);
|
---|
155 | }
|
---|
156 | }
|
---|
157 | });
|
---|
158 | popup.add(newPP);
|
---|
159 | JMenuItem deleteDomain = new JMenuItem("Delete domain");
|
---|
160 | deleteDomain
|
---|
161 | .addActionListener(new java.awt.event.ActionListener() {
|
---|
162 | @Override
|
---|
163 | public void actionPerformed(
|
---|
164 | java.awt.event.ActionEvent evt) {
|
---|
165 | deleteDomain(node);
|
---|
166 | }
|
---|
167 | });
|
---|
168 | popup.add(deleteDomain);
|
---|
169 | }
|
---|
170 | }
|
---|
171 | return popup;
|
---|
172 | }
|
---|
173 |
|
---|
174 | protected boolean domainHasIssues(MyTreeNode node) {
|
---|
175 | // get the directory of the domain
|
---|
176 | DomainRepItem dri = (DomainRepItem) node.getRepositoryItem();
|
---|
177 | String fullPath = dri.getURL().toString().substring(5);
|
---|
178 | Domain domain = null;
|
---|
179 | try {
|
---|
180 | domain = new DomainImpl(fullPath);
|
---|
181 | } catch (Exception e) {
|
---|
182 | e.printStackTrace();
|
---|
183 | }
|
---|
184 | return domain != null && domain.getIssues().size() > 0;
|
---|
185 | }
|
---|
186 |
|
---|
187 | private void newPreferenceProfile(MyTreeNode node) {
|
---|
188 |
|
---|
189 | DomainRepItem dri = (DomainRepItem) node.getRepositoryItem();
|
---|
190 | String domainName = dri.getURL().toString().replace("file:", "");
|
---|
191 | String cleanName = domainName.replace(".xml", "").replace("_domain",
|
---|
192 | "");
|
---|
193 | String path = "";
|
---|
194 |
|
---|
195 | int i = 1;
|
---|
196 | boolean found = false;
|
---|
197 | while (!found && i < 100) {
|
---|
198 | path = cleanName + "_util" + i + ".xml";
|
---|
199 | File file = new File(path);
|
---|
200 | if (!file.exists()) {
|
---|
201 | found = true;
|
---|
202 | }
|
---|
203 | i++;
|
---|
204 | }
|
---|
205 | String url = "file:" + path;
|
---|
206 | ProfileRepItem newPref = null;
|
---|
207 |
|
---|
208 | try {
|
---|
209 | newPref = new ProfileRepItem(new URL(url), dri);
|
---|
210 | dri.getProfiles().add(newPref);
|
---|
211 | MyTreeNode newNode = new MyTreeNode(newPref);
|
---|
212 | scenarioTreeModel.insertNodeInto(newNode, node,
|
---|
213 | node.getChildCount());
|
---|
214 | domainrepository.save();
|
---|
215 |
|
---|
216 | Domain domain = null;
|
---|
217 | try {
|
---|
218 | domain = new DomainImpl(domainName);
|
---|
219 | } catch (Exception e1) {
|
---|
220 | e1.printStackTrace();
|
---|
221 | }
|
---|
222 | AdditiveUtilitySpace space = null;
|
---|
223 | try {
|
---|
224 | space = new AdditiveUtilitySpace(domain, "");
|
---|
225 | } catch (Exception e) {
|
---|
226 | e.printStackTrace();
|
---|
227 | }
|
---|
228 | try {
|
---|
229 | space.toXML().saveToFile(path);
|
---|
230 | } catch (IOException e) {
|
---|
231 | e.printStackTrace();
|
---|
232 | }
|
---|
233 | showRepositoryItemInTab(newPref, newNode);
|
---|
234 | } catch (MalformedURLException e) {
|
---|
235 | e.printStackTrace();
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | private void addDomain() {
|
---|
240 |
|
---|
241 | String result = new CreateNewDomain(mainPanel.getMainFrame())
|
---|
242 | .getResult();
|
---|
243 | if (result != null) {
|
---|
244 | String dirName = "etc" + File.separator + "templates"
|
---|
245 | + File.separator + result;
|
---|
246 |
|
---|
247 | File dir = new File(dirName);
|
---|
248 | if (!dir.exists()) {
|
---|
249 | dir.mkdir();
|
---|
250 | }
|
---|
251 |
|
---|
252 | String path = dirName + File.separator + result + ".xml";
|
---|
253 | DomainRepItem dri = null;
|
---|
254 | try {
|
---|
255 | dri = new DomainRepItem(new URL("file:" + path));
|
---|
256 | } catch (MalformedURLException e) {
|
---|
257 | e.printStackTrace();
|
---|
258 | }
|
---|
259 | domainrepository.getItems().add(dri);
|
---|
260 | domainrepository.save();
|
---|
261 | MyTreeNode newNode = new MyTreeNode(dri);
|
---|
262 | scenarioTreeModel.insertNodeInto(newNode, root,
|
---|
263 | root.getChildCount());
|
---|
264 | saveDomainAsFile(path, result);
|
---|
265 | showRepositoryItemInTab(dri, newNode);
|
---|
266 | updateUI();
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 | private void saveDomainAsFile(String relativePath, String domainName) {
|
---|
271 | SimpleElement template = new SimpleElement("negotiation_template");
|
---|
272 | SimpleElement utilSpace = new SimpleElement("utility_space");
|
---|
273 | SimpleElement objective = new SimpleElement("objective");
|
---|
274 | objective.setAttribute("index", "0");
|
---|
275 | objective.setAttribute("description", "");
|
---|
276 | objective.setAttribute("name", domainName);
|
---|
277 | objective.setAttribute("type", "objective");
|
---|
278 | objective.setAttribute("etype", "objective");
|
---|
279 | utilSpace.addChildElement(objective);
|
---|
280 | template.addChildElement(utilSpace);
|
---|
281 | template.saveToFile(relativePath);
|
---|
282 | }
|
---|
283 |
|
---|
284 | private void deleteDomain(MyTreeNode node) {
|
---|
285 | DomainRepItem dri = (DomainRepItem) node.getRepositoryItem();
|
---|
286 | scenarioTreeModel.removeNodeFromParent(node);
|
---|
287 | domainrepository.getItems().remove(dri);
|
---|
288 | domainrepository.save();
|
---|
289 | }
|
---|
290 |
|
---|
291 | private void deleteProfile(MyTreeNode node) {
|
---|
292 | ProfileRepItem pri = (ProfileRepItem) node.getRepositoryItem();
|
---|
293 | scenarioTreeModel.removeNodeFromParent(node);
|
---|
294 | domainrepository.removeProfileRepItem(pri);
|
---|
295 | domainrepository.save();
|
---|
296 | }
|
---|
297 |
|
---|
298 | private void treeDomainsMouseClicked(java.awt.event.MouseEvent evt) {
|
---|
299 | int selRow = getRowForLocation(evt.getX(), evt.getY());
|
---|
300 | TreePath selPath = getPathForLocation(evt.getX(), evt.getY());
|
---|
301 | if (selRow != -1) {
|
---|
302 | if (evt.getClickCount() == 2) {
|
---|
303 | if (selPath != null) {
|
---|
304 | MyTreeNode node = (MyTreeNode) (selPath
|
---|
305 | .getLastPathComponent());
|
---|
306 | RepItem repItem = node.getRepositoryItem();
|
---|
307 | showRepositoryItemInTab(repItem, node);
|
---|
308 | }
|
---|
309 | }
|
---|
310 | }
|
---|
311 | }
|
---|
312 |
|
---|
313 | public void showRepositoryItemInTab(RepItem repItem, MyTreeNode node) {
|
---|
314 | DomainAndProfileEditorPanel tf;
|
---|
315 | if (repItem instanceof DomainRepItem) {
|
---|
316 | try {
|
---|
317 | boolean hasNoProfiles = ((DomainRepItem) repItem).getProfiles()
|
---|
318 | .size() == 0;
|
---|
319 | String filename = ((DomainRepItem) repItem).getURL().getFile();
|
---|
320 | DomainImpl domain = new DomainImpl(filename);
|
---|
321 | tf = new DomainAndProfileEditorPanel(domain, hasNoProfiles);
|
---|
322 | mainPanel.addTab(StripExtension(GetPlainFileName(filename)),
|
---|
323 | tf);
|
---|
324 | } catch (Exception e) {
|
---|
325 | e.printStackTrace();
|
---|
326 | }
|
---|
327 | } else if (repItem instanceof ProfileRepItem) {
|
---|
328 | try {
|
---|
329 | MyTreeNode parentNode = (MyTreeNode) (node.getParent());
|
---|
330 | String filename = ((ProfileRepItem) repItem).getURL().getFile();
|
---|
331 |
|
---|
332 | DomainImpl domain = new DomainImpl(
|
---|
333 | ((DomainRepItem) (parentNode.getRepositoryItem()))
|
---|
334 | .getURL().getFile());
|
---|
335 |
|
---|
336 | // we can handle only AdditiveUtilitySpace in TreeFrame anyway
|
---|
337 | // so we guess it's that...
|
---|
338 | AdditiveUtilitySpace utilitySpace = new AdditiveUtilitySpace(
|
---|
339 | domain, filename);
|
---|
340 |
|
---|
341 | tf = new DomainAndProfileEditorPanel(domain, utilitySpace);
|
---|
342 | mainPanel.addTab(StripExtension(GetPlainFileName(filename)),
|
---|
343 | tf);
|
---|
344 | } catch (Exception e) {
|
---|
345 | e.printStackTrace();
|
---|
346 | }
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | /**
|
---|
351 | * @param filename
|
---|
352 | * @return part of filename following the last slash, or full filename if
|
---|
353 | * there is no slash.
|
---|
354 | */
|
---|
355 | public String GetPlainFileName(String filename) {
|
---|
356 | int i = filename.lastIndexOf('/');
|
---|
357 | if (i == -1)
|
---|
358 | return filename;
|
---|
359 | return filename.substring(i + 1);
|
---|
360 | }
|
---|
361 |
|
---|
362 | /**
|
---|
363 | * @param filename
|
---|
364 | * @return filename stripped of its extension (the part after the last dot).
|
---|
365 | */
|
---|
366 | public String StripExtension(String filename) {
|
---|
367 | int i = filename.lastIndexOf('.');
|
---|
368 | if (i == -1)
|
---|
369 | return filename;
|
---|
370 | return filename.substring(0, i);
|
---|
371 | }
|
---|
372 |
|
---|
373 | } |
---|