Ignore:
Timestamp:
2018-01-04T11:05:03+01:00 (7 years ago)
Author:
rebeccas95
Message:

Update indoorhelper plugin to support the Simple Indoor Tagging scheme

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/indoorhelper/src/model/IndoorHelperModel.java

    r33887 r33974  
    1919package model;
    2020
    21 import java.util.ArrayList;
     21import static org.openstreetmap.josm.tools.I18n.tr;
     22
     23import java.util.Collection;
    2224import java.util.List;
    2325
     
    2527
    2628import org.openstreetmap.josm.Main;
     29import org.openstreetmap.josm.command.AddCommand;
    2730import org.openstreetmap.josm.command.ChangePropertyCommand;
    28 import org.openstreetmap.josm.data.osm.Filter;
    29 import org.openstreetmap.josm.data.osm.Filter.FilterPreferenceEntry;
     31import org.openstreetmap.josm.data.osm.DataSet;
     32import org.openstreetmap.josm.data.osm.OsmPrimitive;
     33import org.openstreetmap.josm.data.osm.Relation;
     34import org.openstreetmap.josm.data.osm.RelationMember;
    3035import org.openstreetmap.josm.data.osm.Tag;
    3136import org.openstreetmap.josm.gui.MainApplication;
    32 import org.openstreetmap.josm.gui.dialogs.FilterDialog;
    33 import org.openstreetmap.josm.gui.dialogs.FilterTableModel;
    3437
    3538import model.TagCatalog.IndoorObject;
     
    4043 *
    4144 * @author egru
     45 * @author rebsc
    4246 */
    43 public class IndoorHelperModel {
    44 
    45     private java.util.List<IndoorLevel> levelList;
    46     private int workingLevel;
    47     private int workingIndex;
     47public class IndoorHelperModel{
     48
    4849    private TagCatalog tags;
    4950    private PresetCounter counter;
    5051
    5152    /**
    52      * Constructor for the {@link IndoorHelperModel} which sets the current
    53      * workingLevel to 0 and creates the {@link TagCatalog}.
     53     * Constructor for the {@link IndoorHelperModel} which creates the {@link TagCatalog}
     54     * and {@link PresetCounter}.
    5455     */
    5556    public IndoorHelperModel() {
    56         this.workingLevel = 0;
    57         this.levelList = new ArrayList<>();
    5857        this.tags = new TagCatalog();
    5958        this.counter = new PresetCounter();
     
    6160
    6261    /**
    63      * Method to create a list of levels for the current building.
    64      * It also creates the filters which are needed to execute the indoor mapping.
    65      * minLevel should be lower than maxLevel or the same.
    66      *
    67      * @param minLevel the lowest level of the building
    68      * @param maxLevel the highest level of the building
    69      * @return boolean which indicates if the creation of the levelList was successful
    70      */
    71     public boolean setBuildingLevels(int minLevel, int maxLevel) {
    72 
    73         if (minLevel < maxLevel) {
    74 
    75             for (int i = minLevel; i <= maxLevel; i++) {
    76 
    77                 IndoorLevel level = new IndoorLevel(i);
    78                 levelList.add(level);
    79 
    80                 // Get the filter dialog
    81                 FilterDialog filterDialog = MainApplication.getMap().getToggleDialog(FilterDialog.class);
    82 
    83                 if (filterDialog != null) {
    84                     // Create a new filter
    85                     //Filter filter = new Filter("\"indoor:level\"=\""+i+"\"", SearchMode.add, false, false, false);
    86                     FilterPreferenceEntry entry = new FilterPreferenceEntry();
    87                     entry.case_sensitive = false;
    88                     entry.enable = false;
    89                     entry.hiding = false;
    90                     entry.inverted = false;
    91                     entry.mapCSS_search = false;
    92                     entry.mode = "add";
    93                     entry.text = "\"indoor:level\"=\""+i+"\"";
    94                     Filter filter = new Filter(entry);
    95 
    96                     FilterTableModel filterTableModel = filterDialog.getFilterModel();
    97 
    98                     boolean exists = false;
    99 
    100                     // Search if the filter exists already.
    101                     for (Filter listFilter : filterTableModel.getFilters()) {
    102                         if (listFilter.equals(filter)) {
    103                             exists = true;
    104                         }
    105                     }
    106 
    107                     // Only add the filter if it is not already in the filter dialog.
    108                     if (exists == false) {
    109                         filterTableModel.addFilter(filter);
    110                     }
    111 
    112                 } else {
    113                     //Show error message if filter dialog is null.
    114                     JOptionPane.showMessageDialog(null, "Filter Dialog is null.", "Error", JOptionPane.ERROR_MESSAGE);
    115                 }
    116             }
    117 
    118             return true;
    119 
    120         } else if (minLevel == maxLevel) {
    121 
    122             IndoorLevel level = new IndoorLevel(minLevel);
    123             levelList.add(level);
    124 
    125             // Get the filter dialog
    126             FilterDialog filterDialog = MainApplication.getMap().getToggleDialog(FilterDialog.class);
    127 
    128             if (filterDialog != null) {
    129                 // Create a new filter
    130                 //Filter filter = new Filter("\"indoor:level\"=\""+minLevel+"\"", SearchMode.add, false, false, false);
    131 
    132                 FilterPreferenceEntry entry = new FilterPreferenceEntry();
    133                 entry.case_sensitive = false;
    134                 entry.enable = false;
    135                 entry.hiding = false;
    136                 entry.inverted = false;
    137                 entry.mapCSS_search = false;
    138                 entry.mode = "add";
    139                 entry.text = "\"indoor:level\"=\""+minLevel+"\"";
    140                 Filter filter = new Filter(entry);
    141 
    142                 FilterTableModel filterTableModel = filterDialog.getFilterModel();
    143 
    144                 boolean exists = false;
    145 
    146                 // Search if the filter exists already.
    147                 for (Filter listFilter : filterTableModel.getFilters()) {
    148                     if (listFilter.equals(filter)) {
    149                         exists = true;
    150                     }
    151                 }
    152 
    153                 // Only add the filter if it is not already in the filter dialog.
    154                 if (exists == false) {
    155                     filterTableModel.addFilter(filter);
    156                 }
    157             } else {
    158                 JOptionPane.showMessageDialog(null, "Filter Dialog is null.", "Error", JOptionPane.ERROR_MESSAGE);
    159             }
    160 
    161             return true;
    162         }
    163 
    164         return false;
    165     }
    166 
    167     /**
    168      * Getter for the levelList of the model.
    169      *
    170      * @return the levelList, or null if no levelList was created yet
    171      */
    172     public java.util.List<IndoorLevel> getLevelList() {
    173         return this.levelList;
    174     }
    175 
    176     /**
    177      * Function to set the level the user wants to work on (with the level index) and activates the corresponding filter.
    178      *
    179      * @param index the index of the level the user wants to work on
    180      */
    181     public void setWorkingLevel(int index) {
    182         this.workingIndex = index;
    183         this.workingLevel = this.getLevelNumberFromIndex(index);
    184 
    185         FilterDialog filterDialog = MainApplication.getMap().getToggleDialog(FilterDialog.class);
    186         FilterTableModel filterTableModel = filterDialog.getFilterModel();
    187 
    188 
    189         for (Filter filter : filterTableModel.getFilters()) {
    190             // disable the filter for the current level
    191             if (filter.text.equals("\"indoor:level\"=\""+workingLevel+"\"")) {
    192                 filterTableModel.setValueAt(false, filterTableModel.getFilters().indexOf(filter), FilterTableModel.COL_ENABLED);
    193                 filterTableModel.setValueAt(false, filterTableModel.getFilters().indexOf(filter), FilterTableModel.COL_HIDING);
    194             } else if (filter.text.startsWith("\"indoor:level\"=\"")) {
    195                 filterTableModel.setValueAt(true, filterTableModel.getFilters().indexOf(filter), FilterTableModel.COL_ENABLED);
    196                 filterTableModel.setValueAt(true, filterTableModel.getFilters().indexOf(filter), FilterTableModel.COL_HIDING);
    197             }
    198         }
    199     }
    200 
    201     /**
    202      * Function to get the current working level of the plug-in
    203      *
    204      * @return {@link Integer} which represents the current working level
    205      */
    206     public int getWorkingLevel() {
    207         return this.workingLevel;
    208     }
    209 
    210     /**
    211      * Method to get the index of the current working level of the plug-in.
    212      *
    213      * @return {@link Integer} which represents the index
    214      */
    215     public int getWorkingIndex() {
    216         return this.workingIndex;
    217     }
    218 
    219     /**
    220      * Returns the level number which is corresponding to a specific index.
    221      *
    222      * @param index index of the level
    223      * @return a level number as an {@link Integer}
    224      */
    225     public int getLevelNumberFromIndex(int index) {
    226         return levelList.get(index).getLevelNumber();
    227     }
    228 
    229     /**
    230      * Function to set the nameTag of a specific level.
    231      *
    232      * @param levelNumber number of the level
    233      * @param levelName tag which the user wants to set
    234      * @return boolean which indicates if the level was found in the levelList
    235      */
    236     public void setLevelName(int levelIndex, String levelName) {
    237         if ((levelName.length() > 0) && (levelName != null)) {
    238             levelList.get(levelIndex).setNameTag(levelName);
    239         }
    240     }
    241 
    242     /**
    24362     * Function to get a tag-set out of the {@link TagCatalog}.
    244      *
     63     * ClipboardUtils.copy(Main.getLayerManager().getEditDataSet(),Main.getLayerManager().getEditDataSet().getKey());
    24564     * @param object the {@link IndoorObject} from which you want to get the tag-set
    24665     * @return a {@link List} of {@link Tag}s
     
    25069    }
    25170
    252 
    253     /**
    254      * Method which adds the selected tag-set to the currently selected OSM data.
    255      * It also adds the level tag corresponding to the current working level.
     71    /**
     72     * Method which adds the selected tag-set to the currently selected OSM data. If OSM data is a relation add tag-set
     73     * directly to the relation otherwise add it to nodes and/or ways.
    25674     *
    25775     * @param object the object which defines the tag-set you want to add
    25876     * @param userTags the tags which are given by the user input
    259      */
    260     public void addTagsToOSM(IndoorObject object, List<Tag> userTags) {
     77     * @author rebsc
     78     */
     79        public void addTagsToOSM(IndoorObject object, List<Tag> userTags) {
    26180        if (!MainApplication.getLayerManager().getEditDataSet().selectionEmpty() && !Main.main.getInProgressSelection().isEmpty()) {
    26281
     82                DataSet ds = Main.main.getEditDataSet();
    26383            List<Tag> tags = this.getObjectTags(object);
     84            Collection<Relation> relations = ds.getRelations();
     85            Relation relationToAdd = null;
     86
    26487            tags.addAll(userTags);
    265             tags.add(new Tag("indoor:level", Integer.toString(workingLevel)));
    266 
    267             if (!this.getLevelList().get(workingIndex).hasEmptyName()) {
    268                 tags.add(this.getLevelList().get(workingIndex).getNameTag());
     88
     89            // Increment the counter for the presets
     90            this.counter.count(object);
     91
     92            // Put value on {@link relationToAdd} if selected object is a relation.
     93            relationToAdd = getRelationFromDataSet(ds,relations);
     94
     95            if(relationToAdd != null) {
     96                //Add tags to relation
     97                for (Tag t : tags) {
     98                                Main.main.undoRedo.add(new ChangePropertyCommand(relationToAdd, t.getKey(), t.getValue()));
     99                }
     100            }else{
     101                //Add tags to ways or nodes
     102                    for (Tag t : tags) {
     103                        Main.main.undoRedo.add(new ChangePropertyCommand(Main.main.getInProgressSelection(), t.getKey(), t.getValue()));
     104                    }
    269105            }
    270 
    271             // Increment the counter for the presets
     106        //If the selected dataset is empty
     107        } else if (MainApplication.getLayerManager().getEditDataSet().selectionEmpty()) {
     108
     109                JOptionPane.showMessageDialog(null, tr("No data selected."), tr("Error"), JOptionPane.ERROR_MESSAGE);
     110        }
     111    }
     112
     113    /**
     114     * Method which adds a object {@link IndoorObject} to the currently selected OSM data (to nodes and/or ways).
     115     *
     116     * @param object the object which defines the tag-set you want to add
     117     */
     118        public void addTagsToOSM(IndoorObject object) {
     119
     120        if (!MainApplication.getLayerManager().getEditDataSet().selectionEmpty() && !Main.main.getInProgressSelection().isEmpty()) {
     121            List<Tag> tags = this.getObjectTags(object);
     122
     123            //Increment the counter for the presets
    272124            this.counter.count(object);
    273125
     
    276128                Main.main.undoRedo.add(new ChangePropertyCommand(Main.main.getInProgressSelection(), t.getKey(), t.getValue()));
    277129            }
    278 
     130        //If the selected dataset ist empty
    279131        } else if (MainApplication.getLayerManager().getEditDataSet().selectionEmpty()) {
    280 
    281             JOptionPane.showMessageDialog(null, "No data selected.", "Error", JOptionPane.ERROR_MESSAGE);
    282         }
    283     }
    284 
    285     /**
    286      * Method which adds the selected tag-set to the currently selected OSM data.
    287      * It also adds the level tag corresponding to the current working level.
    288      *
    289      * @param object the object which defines the tag-set you want to add
    290      */
    291     public void addTagsToOSM(IndoorObject object) {
     132            JOptionPane.showMessageDialog(null, tr("No data selected."), tr("Error"), JOptionPane.ERROR_MESSAGE);
     133        }
     134    }
     135
     136    /**
     137     * Method which adds a list of tag-sets to the currently selected OSM data. Tags directly to ways and/or nodes.
     138     *
     139     * @param userTags the tags which are given by the user input
     140     * @author rebsc
     141     */
     142        public void addTagsToOSM(List<Tag> userTags) {
    292143
    293144        if (!MainApplication.getLayerManager().getEditDataSet().selectionEmpty() && !Main.main.getInProgressSelection().isEmpty()) {
    294             List<Tag> tags = this.getObjectTags(object);
    295             tags.add(new Tag("indoor:level", Integer.toString(workingLevel)));
    296 
    297             // Increment the counter for the presets
    298             this.counter.count(object);
    299145
    300146            //Add the tags to the current selection
    301             for (Tag t : tags) {
     147            for (Tag t : userTags) {
    302148                Main.main.undoRedo.add(new ChangePropertyCommand(Main.main.getInProgressSelection(), t.getKey(), t.getValue()));
    303149            }
    304         } else if (MainApplication.getLayerManager().getEditDataSet().selectionEmpty()) {
    305             JOptionPane.showMessageDialog(null, "No data selected.", "Error", JOptionPane.ERROR_MESSAGE);
    306         }
    307     }
    308 
    309     /**
     150        }
     151        else if (MainApplication.getLayerManager().getEditDataSet().selectionEmpty()) {
     152            JOptionPane.showMessageDialog(null, tr("No data selected."), tr("Error"), JOptionPane.ERROR_MESSAGE);
     153        }
     154    }
     155
     156    /**
     157     * Method which adds the relation to OSM data. Also adds the selected tag-set to relation object.
     158     *
     159     * @param String the Multipolygon Role as String
     160     * @author rebsc
     161     */
     162        public void addRelation(String role){
     163        Relation newRelation = new Relation();
     164        RelationMember newMember;
     165        DataSet ds = Main.main.getEditDataSet();
     166
     167        // Create new relation and add a new member with specific role
     168        if(!MainApplication.getLayerManager().getEditDataSet().selectionEmpty()) {
     169                for (OsmPrimitive osm : ds.getSelected()) {
     170                 newMember = new RelationMember(role == null ? "" : role, osm);
     171                 newRelation.addMember(newMember);
     172            }
     173        }
     174        // Add relation to OSM data
     175        MainApplication.undoRedo.add(new AddCommand(MainApplication.getLayerManager().getEditDataSet(), newRelation));
     176    }
     177
     178    /**
     179     * Method which edits the selected object to the currently selected OSM data (relations).
     180     *
     181     * @param role The Multipolygon Role as String
     182     * @param relation
     183     * @author rebsc
     184     */
     185        public void editRelation(String role, Collection<OsmPrimitive> innerRelation){
     186
     187        RelationMember newMember;
     188        DataSet ds = Main.main.getEditDataSet();
     189        Collection<Relation> relations = ds.getRelations();
     190        Relation relation = getRelationFromDataSet(ds,relations);
     191
     192        if (!MainApplication.getLayerManager().getEditDataSet().selectionEmpty() && !Main.main.getInProgressSelection().isEmpty() &&
     193                        !innerRelation.isEmpty() && getRole(ds,relations).equals("outer")) {
     194
     195                //Add new relation member to selected relation
     196            for (OsmPrimitive osm : innerRelation) {
     197                 newMember = new RelationMember(role == null ? "" : role, osm);
     198                 relation.addMember(newMember);
     199            };
     200
     201        //Check if dataset is not empty or if {@link innerRelation} has no value
     202        }else if (MainApplication.getLayerManager().getEditDataSet().selectionEmpty() || innerRelation.isEmpty()) {
     203            JOptionPane.showMessageDialog(null, tr("No data selected."), tr("Error"), JOptionPane.ERROR_MESSAGE);
     204
     205        //If selected object is not a relation member or not a relation member with role "outer"
     206        }else if(!getRole(ds,relations).equals("outer")) {
     207                JOptionPane.showMessageDialog(null, tr("No relation or no relation member with role \"outer\" selected."), tr("Error"), JOptionPane.ERROR_MESSAGE);
     208        }
     209
     210    }
     211
     212  /**
    310213     * Returns the current ranking of the preset counter, which includes the 4 most used items.
    311214     *
     
    315218        return counter.getRanking();
    316219    }
     220
     221
     222
     223/*************************************************
     224* HELPER METHODS
     225*
     226*/
     227
     228    /**
     229     * Function which returns the the relation (if any) of the currently selected object.
     230     * If not returns null.
     231     * @param ds actual working dataset
     232     * @param relations collection of relations in the dataset
     233     * @return relation of currently selected dataset
     234     * @author rebsc
     235     */
     236    private Relation getRelationFromDataSet(DataSet ds, Collection<Relation> relations) {
     237        for(Relation r: relations) {
     238                for(RelationMember rm: r.getMembers()) {
     239                        for(OsmPrimitive osm: ds.getSelected()) {
     240                                if(rm.refersTo(osm)) {
     241                                        return r;
     242                                }
     243                        }
     244                }
     245        }
     246        return null;
     247    }
     248
     249    /**
     250     * Function which returns the relation role (if any) of the currently selected object.
     251     * If object is not a relation returns empty string.
     252     * @param ds active dataset
     253     * @param relations collection of relations in the dataset
     254     * @return role of currently selected relation member if any
     255     * @author rebsc
     256     */
     257    private String getRole(DataSet ds, Collection<Relation> relations) {
     258
     259        if(isRelationMember(ds,relations)) {
     260                for(Relation r: relations) {
     261                        for(RelationMember rm: r.getMembers()) {
     262                                for(OsmPrimitive osm: ds.getSelected()) {
     263                                        if(rm.refersTo(osm)) {
     264                                                return rm.getRole();
     265                                        }
     266                                }
     267                        }
     268                }
     269        }
     270        return "";
     271    }
     272
     273    /**
     274     * Function which returns true if the currently selected object is a relation
     275     * @param ds active dataset
     276     * @return true if selected object is a relation
     277     * @author rebsc
     278     */
     279    private boolean isRelationMember(DataSet ds, Collection<Relation> relations) {
     280        for(Relation r: relations) {
     281                for(RelationMember rm: r.getMembers()) {
     282                        for(OsmPrimitive osm: ds.getSelected()) {
     283                                if(rm.refersTo(osm)) {
     284                                        return true;
     285                                }
     286                        }
     287                }
     288        }
     289        return false;
     290    }
     291
     292
     293/**
     294*
     295*
     296*
     297*
     298*
     299*
     300*
     301*
     302*
     303*/
    317304}
Note: See TracChangeset for help on using the changeset viewer.