source: src/main/java/genius/gui/domainrepository/DomainRepositoryUI.java@ 346

Last change on this file since 346 was 85, checked in by Wouter Pasman, 6 years ago

#28 attached save button

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