Changeset 10207 in josm for trunk/src/org/openstreetmap/josm/gui/widgets
- Timestamp:
- 2016-05-13T21:32:58+02:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/widgets/MultiSplitLayout.java
r10043 r10207 29 29 import java.beans.PropertyChangeListener; 30 30 import java.beans.PropertyChangeSupport; 31 import java.io.Reader;32 import java.io.StreamTokenizer;33 import java.io.StringReader;34 31 import java.util.ArrayList; 35 32 import java.util.Collections; … … 38 35 import java.util.List; 39 36 import java.util.ListIterator; 40 import java.util.Locale;41 37 import java.util.Map; 42 38 43 39 import javax.swing.UIManager; 44 40 45 import org.openstreetmap.josm.Main;46 41 import org.openstreetmap.josm.tools.CheckParameterUtil; 47 import org.openstreetmap.josm.tools.Utils;48 42 49 43 /** … … 104 98 } 105 99 100 /** 101 * Add property change listener. 102 * @param listener listener to add 103 */ 106 104 public void addPropertyChangeListener(PropertyChangeListener listener) { 107 105 if (listener != null) { … … 110 108 } 111 109 110 /** 111 * Remove property change listener. 112 * @param listener listener to remove 113 */ 112 114 public void removePropertyChangeListener(PropertyChangeListener listener) { 113 115 if (listener != null) { … … 116 118 } 117 119 120 /** 121 * Replies list of property change listeners. 122 * @return list of property change listeners 123 */ 118 124 public PropertyChangeListener[] getPropertyChangeListeners() { 119 125 return pcs.getPropertyChangeListeners(); … … 652 658 splitChildren.hasNext() ? (Divider) splitChildren.next() : null; 653 659 654 double childHeight = 0.0;660 double childHeight; 655 661 if (getFloatingDividers()) { 656 662 childHeight = preferredNodeSize(splitChild).getHeight(); … … 714 720 } 715 721 716 private void checkLayout(Node root) {722 private static void checkLayout(Node root) { 717 723 if (root instanceof Split) { 718 724 Split split = (Split) root; … … 792 798 } 793 799 794 private List<Divider> dividersThatOverlap(Node root, Rectangle r) {800 private static List<Divider> dividersThatOverlap(Node root, Rectangle r) { 795 801 if (nodeOverlapsRectangle(root, r) && (root instanceof Split)) { 796 802 List<Divider> dividers = new ArrayList<>(); … … 1138 1144 } 1139 1145 } 1140 1141 private static void throwParseException(StreamTokenizer st, String msg) throws Exception {1142 throw new Exception("MultiSplitLayout.parseModel Error: " + msg);1143 }1144 1145 private static void parseAttribute(String name, StreamTokenizer st, Node node) throws Exception {1146 if (st.nextToken() != '=') {1147 throwParseException(st, "expected '=' after " + name);1148 }1149 if ("WEIGHT".equalsIgnoreCase(name)) {1150 if (st.nextToken() == StreamTokenizer.TT_NUMBER) {1151 node.setWeight(st.nval);1152 } else {1153 throwParseException(st, "invalid weight");1154 }1155 } else if ("NAME".equalsIgnoreCase(name)) {1156 if (st.nextToken() == StreamTokenizer.TT_WORD) {1157 if (node instanceof Leaf) {1158 ((Leaf) node).setName(st.sval);1159 } else {1160 throwParseException(st, "can't specify name for " + node);1161 }1162 } else {1163 throwParseException(st, "invalid name");1164 }1165 } else {1166 throwParseException(st, "unrecognized attribute \"" + name + '\"');1167 }1168 }1169 1170 private static void addSplitChild(Split parent, Node child) {1171 List<Node> children = new ArrayList<>(parent.getChildren());1172 if (children.isEmpty()) {1173 children.add(child);1174 } else {1175 children.add(new Divider());1176 children.add(child);1177 }1178 parent.setChildren(children);1179 }1180 1181 private static void parseLeaf(StreamTokenizer st, Split parent) throws Exception {1182 Leaf leaf = new Leaf();1183 int token;1184 while ((token = st.nextToken()) != StreamTokenizer.TT_EOF) {1185 if (token == ')') {1186 break;1187 }1188 if (token == StreamTokenizer.TT_WORD) {1189 parseAttribute(st.sval, st, leaf);1190 } else {1191 throwParseException(st, "Bad Leaf: " + leaf);1192 }1193 }1194 addSplitChild(parent, leaf);1195 }1196 1197 private static void parseSplit(StreamTokenizer st, Split parent) throws Exception {1198 int token;1199 while ((token = st.nextToken()) != StreamTokenizer.TT_EOF) {1200 if (token == ')') {1201 break;1202 } else if (token == StreamTokenizer.TT_WORD) {1203 if ("WEIGHT".equalsIgnoreCase(st.sval)) {1204 parseAttribute(st.sval, st, parent);1205 } else {1206 addSplitChild(parent, new Leaf(st.sval));1207 }1208 } else if (token == '(') {1209 if ((token = st.nextToken()) != StreamTokenizer.TT_WORD) {1210 throwParseException(st, "invalid node type");1211 }1212 String nodeType = st.sval.toUpperCase(Locale.ENGLISH);1213 if ("LEAF".equals(nodeType)) {1214 parseLeaf(st, parent);1215 } else if ("ROW".equals(nodeType) || "COLUMN".equals(nodeType)) {1216 Split split = new Split();1217 split.setRowLayout("ROW".equals(nodeType));1218 addSplitChild(parent, split);1219 parseSplit(st, split);1220 } else {1221 throwParseException(st, "unrecognized node type '" + nodeType + '\'');1222 }1223 }1224 }1225 }1226 1227 private static Node parseModel(Reader r) {1228 StreamTokenizer st = new StreamTokenizer(r);1229 try {1230 Split root = new Split();1231 parseSplit(st, root);1232 return root.getChildren().get(0);1233 } catch (Exception e) {1234 Main.error(e);1235 } finally {1236 Utils.close(r);1237 }1238 return null;1239 }1240 1241 /**1242 * A convenience method that converts a string to a MultiSplitLayout model (a tree of Nodes) using a1243 * a simple syntax. Nodes are represented by parenthetical expressions whose first token1244 * is one of ROW/COLUMN/LEAF. ROW and COLUMN specify horizontal and vertical Split nodes respectively,1245 * LEAF specifies a Leaf node. A Leaf's name and weight can be specified with attributes,1246 * name=<i>myLeafName</i> weight=<i>myLeafWeight</i>.1247 * Similarly, a Split's weight can be specified with weight=<i>mySplitWeight</i>.1248 *1249 * <p> For example, the following expression generates a horizontal Split node with three children:1250 * the Leafs named left and right, and a Divider in between:1251 * <pre>1252 * (ROW (LEAF name=left) (LEAF name=right weight=1.0))1253 * </pre>1254 *1255 * <p> Dividers should not be included in the string, they're added automatcially as needed. Because1256 * Leaf nodes often only need to specify a name, one can specify a Leaf by just providing the name.1257 * The previous example can be written like this:1258 * <pre>1259 * (ROW left (LEAF name=right weight=1.0))1260 * </pre>1261 *1262 * <p>Here's a more complex example. One row with three elements, the first and last of which are columns1263 * with two leaves each:1264 * <pre>1265 * (ROW (COLUMN weight=0.5 left.top left.bottom)1266 * (LEAF name=middle)1267 * (COLUMN weight=0.5 right.top right.bottom))1268 * </pre>1269 *1270 * <p> This syntax is not intended for archiving or configuration files . It's just a convenience for1271 * examples and tests.1272 * @param s model as string1273 *1274 * @return the Node root of a tree based on s.1275 */1276 public static Node parseModel(String s) {1277 return parseModel(new StringReader(s));1278 }1279 1146 }
Note:
See TracChangeset
for help on using the changeset viewer.