001 /******************************************************************************
002 * Copyright (C) MActor Developers. All rights reserved. *
003 * ---------------------------------------------------------------------------*
004 * This file is part of MActor. *
005 * *
006 * MActor is free software; you can redistribute it and/or modify *
007 * it under the terms of the GNU General Public License as published by *
008 * the Free Software Foundation; either version 2 of the License, or *
009 * (at your option) any later version. *
010 * *
011 * MActor is distributed in the hope that it will be useful, *
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
014 * GNU General Public License for more details. *
015 * *
016 * You should have received a copy of the GNU General Public License *
017 * along with MActor; if not, write to the Free Software *
018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
019 ******************************************************************************/
020 package org.mactor.ui.gui.project.editors;
021
022 import java.util.Collection;
023 import java.util.Iterator;
024 import java.util.LinkedList;
025 import java.util.List;
026 import java.util.Map;
027 import java.util.Set;
028 import java.util.TreeMap;
029 import java.util.TreeSet;
030
031 import org.dom4j.Element;
032 import org.mactor.framework.MactorException;
033 import org.mactor.ui.gui.project.ProjectNodeType;
034
035 public class NodeEditorConfig {
036 private String editor;
037 private ProjectNodeType nodeType;
038 private Map<String, CommandType> commandTypesMap = new TreeMap<String, CommandType>();
039 private String tip;
040 public NodeEditorConfig(ProjectNodeType nodeType) {
041 this.nodeType = nodeType;
042 }
043 public Collection<CommandType> getCommandTypes() {
044 return commandTypesMap.values();
045 }
046 public void load(Element element) {
047 if (editor == null)
048 editor = element.attributeValue("editor");
049 if (tip == null)
050 tip = element.attributeValue("tip");
051 loadCommandTypes(element);
052 }
053 public NodeEditor createEditor() throws MactorException {
054 if (editor == null)
055 throw new MactorException("Editor not specified in config");
056 try {
057 NodeEditor ne = (NodeEditor) Class.forName(editor).newInstance();
058 ne.setConfig(this);
059 return ne;
060 } catch (Exception e) {
061 throw new MactorException("Unable to initiate editor '" + editor + "'", e);
062 }
063 }
064 public void loadCommandTypes(Element nodeTypeElement) {
065 Iterator typeIt = nodeTypeElement.elementIterator("command_type");
066 while (typeIt.hasNext()) {
067 Element e = (Element) typeIt.next();
068 String name = e.attributeValue("name");
069 CommandType config = commandTypesMap.get(name);
070 if (config == null) {
071 config = new CommandType();
072 commandTypesMap.put(name, config);
073 }
074 config.load(e);
075 }
076 }
077 public static class CommandType {
078 private String name;
079 private String tip;
080 private boolean acceptParameters = true;
081 private Set<CommandPrototype> prototypes = new TreeSet<CommandPrototype>();
082 public void load(Element element) {
083 name = element.attributeValue("name");
084 tip = element.attributeValue("tip");
085 if (element.attributeValue("accept_paramters") != null)
086 acceptParameters = Boolean.parseBoolean(element.attributeValue("accept_paramters"));
087 Iterator typeIt = element.elementIterator("command_prototype");
088 while (typeIt.hasNext()) {
089 prototypes.add(CommandPrototype.loadConfig((Element) typeIt.next()));
090 }
091 }
092 public String getName() {
093 return name;
094 }
095 public String getTip() {
096 return tip;
097 }
098 @Override
099 public String toString() {
100 return getName();
101 }
102 public Set<CommandPrototype> getPrototypes() {
103 return prototypes;
104 }
105 public boolean isAcceptParameters() {
106 return acceptParameters;
107 }
108 }
109 public static class CommandPrototype implements Comparable<CommandPrototype> {
110 private String name;
111 private String tip;
112 private boolean acceptParameters;
113 private List<String> params = new LinkedList<String>();
114 public static CommandPrototype loadConfig(Element element) {
115 CommandPrototype cp = new CommandPrototype();
116 cp.name = element.attributeValue("name");
117 cp.tip = element.attributeValue("tip");
118 cp.acceptParameters = Boolean.parseBoolean(element.attributeValue("accept_paramters"));
119 Iterator<Element> it = element.elementIterator("param");
120 while (it.hasNext())
121 cp.params.add(it.next().getText());
122 return cp;
123 }
124 public int compareTo(CommandPrototype other) {
125 if (this == other)
126 return 0;
127 return name.compareTo(other.name);
128 }
129 public String getName() {
130 return name;
131 }
132 public String getTip() {
133 return tip;
134 }
135 @Override
136 public String toString() {
137 return getName();
138 }
139 public List<String> getParams() {
140 return params;
141 }
142 }
143 public String getEditor() {
144 return editor;
145 }
146 public ProjectNodeType getNodeType() {
147 return nodeType;
148 }
149 public String getTip() {
150 return tip;
151 }
152 }