1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.tagging.ac;
|
---|
3 |
|
---|
4 | /**
|
---|
5 | * Describes the priority of an item in an autocompletion list.
|
---|
6 | * The selected flag is currently only used in plugins.
|
---|
7 | *
|
---|
8 | * Instances of this class are not modifiable.
|
---|
9 | * @since 1762
|
---|
10 | */
|
---|
11 | public class AutoCompletionItemPriority implements Comparable<AutoCompletionItemPriority> {
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Indicates, that the value is standard and it is found in the data.
|
---|
15 | * This has higher priority than some arbitrary standard value that is
|
---|
16 | * usually not used by the user.
|
---|
17 | */
|
---|
18 | public static final AutoCompletionItemPriority IS_IN_STANDARD_AND_IN_DATASET = new AutoCompletionItemPriority(true, true, false);
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Indicates that this is an arbitrary value from the data set, i.e.
|
---|
22 | * the value of a tag name=*.
|
---|
23 | */
|
---|
24 | public static final AutoCompletionItemPriority IS_IN_DATASET = new AutoCompletionItemPriority(true, false, false);
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Indicates that this is a standard value, i.e. a standard tag name
|
---|
28 | * or a standard value for a given tag name (from the presets).
|
---|
29 | */
|
---|
30 | public static final AutoCompletionItemPriority IS_IN_STANDARD = new AutoCompletionItemPriority(false, true, false);
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Indicates that this is a value from a selected object.
|
---|
34 | */
|
---|
35 | public static final AutoCompletionItemPriority IS_IN_SELECTION = new AutoCompletionItemPriority(false, false, true);
|
---|
36 |
|
---|
37 | /** Unknown priority. This is the lowest priority. */
|
---|
38 | public static final AutoCompletionItemPriority UNKNOWN = new AutoCompletionItemPriority(false, false, false);
|
---|
39 |
|
---|
40 | private static final int NO_USER_INPUT = Integer.MAX_VALUE;
|
---|
41 |
|
---|
42 | private final int userInput;
|
---|
43 | private final boolean inDataSet;
|
---|
44 | private final boolean inStandard;
|
---|
45 | private final boolean selected;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Constructs a new {@code AutoCompletionItemPriority}.
|
---|
49 | *
|
---|
50 | * @param inDataSet true, if the item is found in the currently active data layer
|
---|
51 | * @param inStandard true, if the item is a standard tag, e.g. from the presets
|
---|
52 | * @param selected true, if it is found on an object that is currently selected
|
---|
53 | * @param userInput null, if the user hasn't entered this tag so far. A number when
|
---|
54 | * the tag key / value has been entered by the user before. A lower number means
|
---|
55 | * this happened more recently and beats a higher number in priority.
|
---|
56 | */
|
---|
57 | public AutoCompletionItemPriority(boolean inDataSet, boolean inStandard, boolean selected, Integer userInput) {
|
---|
58 | this.inDataSet = inDataSet;
|
---|
59 | this.inStandard = inStandard;
|
---|
60 | this.selected = selected;
|
---|
61 | this.userInput = userInput == null ? NO_USER_INPUT : userInput;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Constructs a new {@code AutoCompletionItemPriority}.
|
---|
66 | *
|
---|
67 | * @param inDataSet true, if the item is found in the currently active data layer
|
---|
68 | * @param inStandard true, if the item is a standard tag, e.g. from the presets
|
---|
69 | * @param selected true, if it is found on an object that is currently selected
|
---|
70 | */
|
---|
71 | public AutoCompletionItemPriority(boolean inDataSet, boolean inStandard, boolean selected) {
|
---|
72 | this(inDataSet, inStandard, selected, NO_USER_INPUT);
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Determines if the item is found in the currently active data layer.
|
---|
77 | * @return {@code true} if the item is found in the currently active data layer
|
---|
78 | */
|
---|
79 | public boolean isInDataSet() {
|
---|
80 | return inDataSet;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Determines if the item is a standard tag, e.g. from the presets.
|
---|
85 | * @return {@code true} if the item is a standard tag, e.g. from the presets
|
---|
86 | */
|
---|
87 | public boolean isInStandard() {
|
---|
88 | return inStandard;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Determines if it is found on an object that is currently selected.
|
---|
93 | * @return {@code true} if it is found on an object that is currently selected
|
---|
94 | */
|
---|
95 | public boolean isSelected() {
|
---|
96 | return selected;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Returns a number when the tag key / value has been entered by the user before.
|
---|
101 | * A lower number means this happened more recently and beats a higher number in priority.
|
---|
102 | * @return a number when the tag key / value has been entered by the user before.
|
---|
103 | * {@code null}, if the user hasn't entered this tag so far.
|
---|
104 | */
|
---|
105 | public Integer getUserInput() {
|
---|
106 | return userInput == NO_USER_INPUT ? null : userInput;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Imposes an ordering on the priorities.
|
---|
111 | * Currently, being in the current DataSet is worth more than being in the Presets.
|
---|
112 | */
|
---|
113 | @Override
|
---|
114 | public int compareTo(AutoCompletionItemPriority other) {
|
---|
115 | int ui = Integer.compare(other.userInput, userInput);
|
---|
116 | if (ui != 0)
|
---|
117 | return ui;
|
---|
118 |
|
---|
119 | int sel = Boolean.compare(selected, other.selected);
|
---|
120 | if (sel != 0)
|
---|
121 | return sel;
|
---|
122 |
|
---|
123 | int ds = Boolean.compare(inDataSet, other.inDataSet);
|
---|
124 | if (ds != 0)
|
---|
125 | return ds;
|
---|
126 |
|
---|
127 | int std = Boolean.compare(inStandard, other.inStandard);
|
---|
128 | if (std != 0)
|
---|
129 | return std;
|
---|
130 |
|
---|
131 | return 0;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Merges two priorities.
|
---|
136 | * The resulting priority is always >= the original ones.
|
---|
137 | * @param other other priority
|
---|
138 | * @return the merged priority
|
---|
139 | */
|
---|
140 | public AutoCompletionItemPriority mergeWith(AutoCompletionItemPriority other) {
|
---|
141 | return new AutoCompletionItemPriority(
|
---|
142 | inDataSet || other.inDataSet,
|
---|
143 | inStandard || other.inStandard,
|
---|
144 | selected || other.selected,
|
---|
145 | Math.min(userInput, other.userInput));
|
---|
146 | }
|
---|
147 |
|
---|
148 | @Override
|
---|
149 | public String toString() {
|
---|
150 | return String.format("<Priority; userInput: %s, inDataSet: %b, inStandard: %b, selected: %b>",
|
---|
151 | userInput == NO_USER_INPUT ? "no" : Integer.toString(userInput), inDataSet, inStandard, selected);
|
---|
152 | }
|
---|
153 | }
|
---|