1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.tagging.ac;
|
---|
3 |
|
---|
4 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * Represents an entry in the list of auto completion values.
|
---|
8 | *
|
---|
9 | * An AutoCompletionListItem has a <em>priority</em> and a <em>value</em>.
|
---|
10 | *
|
---|
11 | * The priority helps to sort the auto completion items according to their importance. For instance,
|
---|
12 | * in an auto completion list for tag names, standard tag names would be assigned a higher
|
---|
13 | * priority than arbitrary tag names present in the current data set. There are three priority levels,
|
---|
14 | * {@link AutoCompletionItemPriority}.
|
---|
15 | *
|
---|
16 | * The value is a string which will be displayed in the auto completion list.
|
---|
17 | *
|
---|
18 | */
|
---|
19 | public class AutoCompletionListItem implements Comparable<AutoCompletionListItem> {
|
---|
20 |
|
---|
21 | /** the pritority of this item */
|
---|
22 | private AutoCompletionItemPriority priority;
|
---|
23 | /** the value of this item */
|
---|
24 | private String value;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Constructs a new {@code AutoCompletionListItem} with the given value and priority.
|
---|
28 | * @param value The value
|
---|
29 | * @param priority The priority
|
---|
30 | */
|
---|
31 | public AutoCompletionListItem(String value, AutoCompletionItemPriority priority) {
|
---|
32 | this.value = value;
|
---|
33 | this.priority = priority;
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Constructs a new {@code AutoCompletionListItem} with the given value and unknown priority.
|
---|
38 | * @param value The value
|
---|
39 | */
|
---|
40 | public AutoCompletionListItem(String value) {
|
---|
41 | this.value = value;
|
---|
42 | priority = AutoCompletionItemPriority.UNKNOWN;
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Constructs a new {@code AutoCompletionListItem}.
|
---|
47 | */
|
---|
48 | public AutoCompletionListItem() {
|
---|
49 | value = "";
|
---|
50 | priority = AutoCompletionItemPriority.UNKNOWN;
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Returns the priority.
|
---|
55 | * @return the priority
|
---|
56 | */
|
---|
57 | public AutoCompletionItemPriority getPriority() {
|
---|
58 | return priority;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Sets the priority.
|
---|
63 | * @param priority the priority
|
---|
64 | */
|
---|
65 | public void setPriority(AutoCompletionItemPriority priority) {
|
---|
66 | this.priority = priority;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Returns the value.
|
---|
71 | * @return the value
|
---|
72 | */
|
---|
73 | public String getValue() {
|
---|
74 | return value;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * sets the value
|
---|
79 | * @param value the value; must not be null
|
---|
80 | * @throws IllegalArgumentException if value if null
|
---|
81 | */
|
---|
82 | public void setValue(String value) {
|
---|
83 | CheckParameterUtil.ensureParameterNotNull(value, "value");
|
---|
84 | this.value = value;
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public String toString() {
|
---|
89 | StringBuilder sb = new StringBuilder();
|
---|
90 | sb.append("<val='")
|
---|
91 | .append(value)
|
---|
92 | .append("',")
|
---|
93 | .append(priority)
|
---|
94 | .append('>');
|
---|
95 | return sb.toString();
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Override
|
---|
99 | public int hashCode() {
|
---|
100 | final int prime = 31;
|
---|
101 | int result = 1;
|
---|
102 | result = prime * result
|
---|
103 | + ((priority == null) ? 0 : priority.hashCode());
|
---|
104 | result = prime * result + ((value == null) ? 0 : value.hashCode());
|
---|
105 | return result;
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | public boolean equals(Object obj) {
|
---|
110 | if (this == obj)
|
---|
111 | return true;
|
---|
112 | if (obj == null)
|
---|
113 | return false;
|
---|
114 | if (obj instanceof String)
|
---|
115 | return obj.equals(value);
|
---|
116 | if (getClass() != obj.getClass())
|
---|
117 | return false;
|
---|
118 | final AutoCompletionListItem other = (AutoCompletionListItem) obj;
|
---|
119 | if (priority == null) {
|
---|
120 | if (other.priority != null)
|
---|
121 | return false;
|
---|
122 | } else if (!priority.equals(other.priority))
|
---|
123 | return false;
|
---|
124 | if (value == null) {
|
---|
125 | if (other.value != null)
|
---|
126 | return false;
|
---|
127 | } else if (!value.equals(other.value))
|
---|
128 | return false;
|
---|
129 | return true;
|
---|
130 | }
|
---|
131 |
|
---|
132 | @Override
|
---|
133 | public int compareTo(AutoCompletionListItem other) {
|
---|
134 | int ret = other.priority.compareTo(priority); // higher priority items come first in the list
|
---|
135 | if (ret != 0)
|
---|
136 | return ret;
|
---|
137 | else
|
---|
138 | return this.value.compareTo(other.value);
|
---|
139 | }
|
---|
140 | }
|
---|