[1] | 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;
|
---|
[10] | 23 | import genius.core.repository.RepItem;
|
---|
[1] | 24 | import genius.core.repository.Repository;
|
---|
| 25 | import genius.core.repository.RepositoryFactory;
|
---|
| 26 | import genius.core.utility.AdditiveUtilitySpace;
|
---|
[84] | 27 | import genius.core.utility.UtilitySpace;
|
---|
[1] | 28 | import genius.core.xml.SimpleElement;
|
---|
[16] | 29 | import genius.gui.GeniusAppInterface;
|
---|
[83] | 30 | import genius.gui.tree.DomainAndProfileEditorPanel;
|
---|
[1] | 31 |
|
---|
| 32 | /**
|
---|
| 33 | * A user interface to the domain repository
|
---|
| 34 | *
|
---|
| 35 | */
|
---|
[10] | 36 | @SuppressWarnings("serial")
|
---|
[9] | 37 | public class DomainRepositoryUI extends JTree {
|
---|
[1] | 38 | private Repository<DomainRepItem> domainrepository;
|
---|
| 39 | private MyTreeNode root = new MyTreeNode(null);
|
---|
| 40 | private DefaultTreeModel scenarioTreeModel;
|
---|
[16] | 41 | private GeniusAppInterface mainPanel;
|
---|
[1] | 42 |
|
---|
[16] | 43 | public DomainRepositoryUI(GeniusAppInterface negoView) {
|
---|
[1] | 44 | domainrepository = RepositoryFactory.get_domain_repos();
|
---|
| 45 | initTree();
|
---|
[9] | 46 | setModel(scenarioTreeModel);
|
---|
[11] | 47 | this.mainPanel = negoView;
|
---|
[9] | 48 |
|
---|
| 49 | setMinimumSize(new java.awt.Dimension(100, 100));
|
---|
| 50 | setName("treeDomains");
|
---|
[10] | 51 |
|
---|
| 52 | addMouseListener(new java.awt.event.MouseAdapter() {
|
---|
| 53 | @Override
|
---|
| 54 | public void mouseClicked(java.awt.event.MouseEvent evt) {
|
---|
| 55 | treeDomainsMouseClicked(evt);
|
---|
| 56 | }
|
---|
| 57 | });
|
---|
| 58 |
|
---|
[1] | 59 | }
|
---|
| 60 |
|
---|
| 61 | private void initTree() {
|
---|
| 62 | // for all domains in the domain repository
|
---|
| 63 | for (DomainRepItem repitem : domainrepository.getItems()) {
|
---|
[9] | 64 | DomainRepItem dri = repitem;
|
---|
[1] | 65 | MyTreeNode domainNode = new MyTreeNode(dri);
|
---|
| 66 | // add all preference profiles of the domain as nodes
|
---|
| 67 | for (ProfileRepItem profileitem : dri.getProfiles()) {
|
---|
| 68 | domainNode.add(new MyTreeNode(profileitem));
|
---|
| 69 | }
|
---|
| 70 | root.add(domainNode);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | scenarioTreeModel = new DefaultTreeModel(root);
|
---|
[9] | 74 | setModel(scenarioTreeModel);
|
---|
| 75 | Font currentFont = getFont();
|
---|
| 76 | Font bigFont = new Font(currentFont.getName(), currentFont.getStyle(),
|
---|
| 77 | currentFont.getSize() + 2);
|
---|
| 78 | setRowHeight(23);
|
---|
| 79 | setFont(bigFont);
|
---|
| 80 | setRootVisible(false);
|
---|
| 81 | setShowsRootHandles(true);
|
---|
| 82 | getSelectionModel()
|
---|
| 83 | .setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
|
---|
| 84 | addMouseListener(new MouseAdapter() {
|
---|
[1] | 85 |
|
---|
| 86 | // for Windows
|
---|
| 87 | @Override
|
---|
| 88 | public void mouseReleased(MouseEvent e) {
|
---|
| 89 | mouseCode(e);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | // for Linux
|
---|
| 93 | @Override
|
---|
| 94 | public void mousePressed(MouseEvent e) {
|
---|
| 95 | mouseCode(e);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | private void mouseCode(MouseEvent e) {
|
---|
| 99 | if (e.getButton() == MouseEvent.BUTTON3) {
|
---|
[9] | 100 | TreePath selPath = getPathForLocation(e.getX(), e.getY());
|
---|
[1] | 101 | if (selPath == null) {
|
---|
| 102 | JPopupMenu popup = createPopupMenu(null);
|
---|
| 103 | popup.show(e.getComponent(), e.getX(), e.getY());
|
---|
| 104 | return;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[9] | 107 | MyTreeNode node = (MyTreeNode) selPath
|
---|
| 108 | .getLastPathComponent();
|
---|
| 109 | setSelectionPath(selPath);
|
---|
| 110 | if (e.isPopupTrigger()
|
---|
| 111 | && e.getComponent() instanceof JTree) {
|
---|
[1] | 112 | JPopupMenu popup = createPopupMenu(node);
|
---|
| 113 | popup.show(e.getComponent(), e.getX(), e.getY());
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 | });
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | private JPopupMenu createPopupMenu(final MyTreeNode node) {
|
---|
| 121 | JPopupMenu popup = new JPopupMenu();
|
---|
| 122 |
|
---|
| 123 | JMenuItem newDomain = new JMenuItem("New domain");
|
---|
| 124 | newDomain.addActionListener(new java.awt.event.ActionListener() {
|
---|
[9] | 125 | @Override
|
---|
[1] | 126 | public void actionPerformed(java.awt.event.ActionEvent evt) {
|
---|
| 127 | addDomain();
|
---|
| 128 | }
|
---|
| 129 | });
|
---|
| 130 |
|
---|
| 131 | popup.add(newDomain);
|
---|
| 132 |
|
---|
| 133 | if (node != null) {
|
---|
| 134 | if (node.getRepositoryItem() instanceof ProfileRepItem) {
|
---|
| 135 | JMenuItem deletePP = new JMenuItem("Delete preference profile");
|
---|
| 136 | deletePP.addActionListener(new java.awt.event.ActionListener() {
|
---|
[9] | 137 | @Override
|
---|
| 138 | public void actionPerformed(
|
---|
| 139 | java.awt.event.ActionEvent evt) {
|
---|
[1] | 140 | deleteProfile(node);
|
---|
| 141 | }
|
---|
| 142 | });
|
---|
| 143 | popup.add(deletePP);
|
---|
| 144 | } else {
|
---|
| 145 | JMenuItem newPP = new JMenuItem("New preference profile");
|
---|
| 146 | newPP.addActionListener(new java.awt.event.ActionListener() {
|
---|
[9] | 147 | @Override
|
---|
| 148 | public void actionPerformed(
|
---|
| 149 | java.awt.event.ActionEvent evt) {
|
---|
[1] | 150 | if (domainHasIssues(node)) {
|
---|
| 151 | newPreferenceProfile(node);
|
---|
| 152 | } else {
|
---|
[84] | 153 | showError(
|
---|
| 154 | "Before creating a preference profile, the domain must be saved with at least one isue.");
|
---|
[1] | 155 | }
|
---|
| 156 | }
|
---|
| 157 | });
|
---|
| 158 | popup.add(newPP);
|
---|
| 159 | JMenuItem deleteDomain = new JMenuItem("Delete domain");
|
---|
[9] | 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 | });
|
---|
[1] | 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:", "");
|
---|
[9] | 191 | String cleanName = domainName.replace(".xml", "").replace("_domain",
|
---|
| 192 | "");
|
---|
[1] | 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);
|
---|
[9] | 212 | scenarioTreeModel.insertNodeInto(newNode, node,
|
---|
| 213 | node.getChildCount());
|
---|
[1] | 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 | }
|
---|
[10] | 233 | showRepositoryItemInTab(newPref, newNode);
|
---|
[1] | 234 | } catch (MalformedURLException e) {
|
---|
| 235 | e.printStackTrace();
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | private void addDomain() {
|
---|
| 240 |
|
---|
[11] | 241 | String result = new CreateNewDomain(mainPanel.getMainFrame())
|
---|
[9] | 242 | .getResult();
|
---|
[1] | 243 | if (result != null) {
|
---|
[9] | 244 | String dirName = "etc" + File.separator + "templates"
|
---|
| 245 | + File.separator + result;
|
---|
[1] | 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);
|
---|
[9] | 262 | scenarioTreeModel.insertNodeInto(newNode, root,
|
---|
| 263 | root.getChildCount());
|
---|
[1] | 264 | saveDomainAsFile(path, result);
|
---|
[10] | 265 | showRepositoryItemInTab(dri, newNode);
|
---|
[9] | 266 | updateUI();
|
---|
[1] | 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 | }
|
---|
[10] | 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) {
|
---|
[83] | 314 | DomainAndProfileEditorPanel tf;
|
---|
[10] | 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);
|
---|
[83] | 321 | tf = new DomainAndProfileEditorPanel(domain, hasNoProfiles);
|
---|
[11] | 322 | mainPanel.addTab(StripExtension(GetPlainFileName(filename)),
|
---|
| 323 | tf);
|
---|
[10] | 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...
|
---|
[84] | 338 | UtilitySpace profile = ((ProfileRepItem) repItem).create();
|
---|
| 339 | if (!(profile instanceof AdditiveUtilitySpace)) {
|
---|
| 340 | showError(
|
---|
| 341 | "Selected profile is not an Additive Linear profile, you can not edit this.");
|
---|
| 342 | return;
|
---|
| 343 | }
|
---|
| 344 | AdditiveUtilitySpace utilitySpace = (AdditiveUtilitySpace) profile;
|
---|
[10] | 345 |
|
---|
[83] | 346 | tf = new DomainAndProfileEditorPanel(domain, utilitySpace);
|
---|
[11] | 347 | mainPanel.addTab(StripExtension(GetPlainFileName(filename)),
|
---|
| 348 | tf);
|
---|
[10] | 349 | } catch (Exception e) {
|
---|
| 350 | e.printStackTrace();
|
---|
| 351 | }
|
---|
| 352 | }
|
---|
| 353 | }
|
---|
| 354 |
|
---|
[84] | 355 | private void showError(String string) {
|
---|
| 356 | JOptionPane.showMessageDialog(null, string, "Domain error", 0);
|
---|
| 357 | }
|
---|
| 358 |
|
---|
[10] | 359 | /**
|
---|
| 360 | * @param filename
|
---|
| 361 | * @return part of filename following the last slash, or full filename if
|
---|
| 362 | * there is no slash.
|
---|
| 363 | */
|
---|
| 364 | public String GetPlainFileName(String filename) {
|
---|
| 365 | int i = filename.lastIndexOf('/');
|
---|
| 366 | if (i == -1)
|
---|
| 367 | return filename;
|
---|
| 368 | return filename.substring(i + 1);
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 | /**
|
---|
| 372 | * @param filename
|
---|
| 373 | * @return filename stripped of its extension (the part after the last dot).
|
---|
| 374 | */
|
---|
| 375 | public String StripExtension(String filename) {
|
---|
| 376 | int i = filename.lastIndexOf('.');
|
---|
| 377 | if (i == -1)
|
---|
| 378 | return filename;
|
---|
| 379 | return filename.substring(0, i);
|
---|
| 380 | }
|
---|
| 381 |
|
---|
[1] | 382 | } |
---|