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/controller/IndoorHelperController.java

    r33887 r33974  
    1919package controller;
    2020
     21import static org.openstreetmap.josm.tools.I18n.tr;
     22
    2123import java.awt.event.ActionEvent;
    2224import java.awt.event.ActionListener;
    2325import java.awt.event.ItemEvent;
    2426import java.awt.event.ItemListener;
     27import java.awt.event.KeyEvent;
     28import java.awt.event.WindowEvent;
     29import java.awt.event.WindowListener;
    2530import java.util.ArrayList;
    2631import java.util.Collection;
     
    2934import java.util.List;
    3035import java.util.Map;
    31 
     36import java.util.Optional;
     37
     38import javax.swing.AbstractAction;
    3239import javax.swing.JOptionPane;
    3340
    3441import org.openstreetmap.josm.Main;
    3542import org.openstreetmap.josm.actions.ValidateAction;
     43import org.openstreetmap.josm.actions.mapmode.DrawAction;
     44import org.openstreetmap.josm.actions.mapmode.SelectAction;
     45import org.openstreetmap.josm.data.osm.OsmPrimitive;
    3646import org.openstreetmap.josm.data.osm.Tag;
    3747import org.openstreetmap.josm.data.validation.OsmValidator;
    3848import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker;
    3949import org.openstreetmap.josm.gui.MainApplication;
    40 import org.openstreetmap.josm.gui.dialogs.FilterDialog;
    41 import org.openstreetmap.josm.gui.dialogs.FilterTableModel;
     50import org.openstreetmap.josm.gui.MapFrame;
     51import org.openstreetmap.josm.gui.help.HelpBrowser;
    4252import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
     53import org.openstreetmap.josm.spi.preferences.MapListSetting;
     54import org.openstreetmap.josm.spi.preferences.Setting;
     55import org.openstreetmap.josm.tools.Shortcut;
    4356
    4457import model.IndoorHelperModel;
    4558import model.TagCatalog.IndoorObject;
    46 import views.FittingView;
    4759import views.LevelSelectorView;
    4860import views.ToolBoxView;
    4961
    5062/**
    51  *
    52  * Class for the Controller which provides the communication between
    53  * the IndoorHelperModel and the different views.
    54  *
    55  * @author egru
    56  *
    57  */
     63*
     64* Class for the Controller which provides the communication between
     65* the IndoorHelperModel and the different views.
     66*
     67* @author egru
     68* @author rebsc
     69*
     70*/
    5871public class IndoorHelperController {
    5972
    60     private IndoorHelperModel model;
    61     private ToolBoxView toolboxView;
    62     private FittingView fittingView;
    63     private LevelSelectorView selectorView;
    64     private String sep = System.getProperty("file.separator");
    65 
    66 
    67     private int lastLevelIndex;
    68 
    69     /**
    70      * Constructor for the {@link IndoorHelperController} which initiates model and views.
    71      *
    72      */
    73     public IndoorHelperController() {
    74         this.model = new IndoorHelperModel();
    75         this.toolboxView = new ToolBoxView();
    76 
    77         this.lastLevelIndex = 0;
    78 
    79         addToolboxListeners();
    80         MainApplication.getMap().addToggleDialog(toolboxView);
    81     }
     73   private IndoorHelperModel model;
     74   private ToolBoxView toolboxView;
     75   private String sep;
     76   private String levelValue, levelNum;
     77   private MapFrame map;
     78   private DrawAction drawAction;
     79   private SelectAction selectAction;
     80   private SpaceAction SpaceAction;
     81   private transient Shortcut SpaceShortcut;
     82   private EnterAction EnterAction;
     83   private transient Shortcut EnterShortcut;
     84   private boolean OuterHelp,InnerHelp,LevelHelp;
     85   private Collection<OsmPrimitive> innerRelation;
     86   private LevelSelectorView selectorView;
     87
     88   /**
     89    * Constructor for the {@link IndoorHelperController} which initiates model and views.
     90    *
     91    */
     92   public IndoorHelperController() {
     93
     94           this.model = new IndoorHelperModel();
     95       this.toolboxView = new ToolBoxView();
     96
     97       this.sep = System.getProperty("file.separator");
     98       setPluginPreferences(true);
     99
     100       // Multipolygon actions
     101       this.drawAction = new DrawAction();
     102       this.map = MainApplication.getMap();
     103       this.selectAction = new SelectAction(map);
     104
     105       // Ui elements
     106       toolboxView.setAllUiElementsEnabled(true);
     107       toolboxView.setROUiElementsEnabled(false);
     108
     109       addToolboxListeners();
     110       MainApplication.getMap().addToggleDialog(toolboxView);
     111
     112       // Shortcuts
     113       SpaceShortcut = Shortcut.registerShortcut("mapmode:space",
     114               tr(""), KeyEvent.VK_SPACE, Shortcut.DIRECT);
     115       this.SpaceAction = new SpaceAction();
     116       MainApplication.registerActionShortcut(SpaceAction,SpaceShortcut);
     117
     118       EnterShortcut = Shortcut.registerShortcut("mapmode:ALT",
     119               tr(""), KeyEvent.VK_ENTER, Shortcut.DIRECT);
     120       this.EnterAction = new EnterAction();
     121       MainApplication.registerActionShortcut(EnterAction,EnterShortcut);
     122
     123       // Helper
     124       OuterHelp = false;
     125       InnerHelp = false;
     126       LevelHelp = false;
     127           innerRelation = null;
     128           levelValue = new String();
     129           levelNum = new String();
     130
     131   }
     132
    82133
    83134    /**
     
    87138
    88139        if (this.toolboxView != null) {
    89             this.toolboxView.setPowerButtonListener(new ToolPowerButtonListener());
    90140            this.toolboxView.setApplyButtonListener(new ToolApplyButtonListener());
    91             this.toolboxView.setLevelItemListener(new ToolLevelItemListener());
     141            this.toolboxView.setLevelCheckBoxListener(new ToolLevelCheckBoxListener());
     142            this.toolboxView.setHelpButtonListener(new ToolHelpButtonListener());
     143            this.toolboxView.setAddLevelButtonListener(new ToolAddLevelButtonListener());
    92144            this.toolboxView.setObjectItemListener(new ToolObjectItemListener());
     145            this.toolboxView.setOuterButtonListener(new ToolOuterButtonListener());
     146            this.toolboxView.setInnerButtonListener(new ToolInnerButtonListener());
     147            this.toolboxView.setMultiCheckBoxListener(new ToolMultiCheckBoxListener());
    93148            this.toolboxView.setPreset1Listener(new Preset1Listener());
    94149            this.toolboxView.setPreset2Listener(new Preset2Listener());
     
    103158    private void addLevelSelectorListeners() {
    104159        if (this.selectorView != null) {
    105             this.selectorView.setOkButtonListener(new LevelOkButtonListener());
    106             this.selectorView.setCancelButtonListener(new LevelCancelButtonListener());
     160            this.selectorView.setOkButtonListener(new ToolLevelOkButtonListener());
     161            this.selectorView.setCancelButtonListener(new ToolLevelCancelButtonListener());
     162            this.selectorView.setSelectorWindowListener(new ToolSelectorWindowSListener());
    107163        }
     164
    108165    }
    109166
    110     /**
    111      * Adds the button-listeners to the {@link FittingView}.
    112      */
    113     private void addFittingListeners() {
    114         if (this.fittingView != null) {
    115             this.fittingView.setOkButtonListener(new FittingOkButtonListener());
    116         }
     167/*************************************************
     168* TOOLBOX LISTENER
     169*
     170*/
     171
     172   /**
     173    * The listener which provides the handling of the apply button.
     174    * Gets the texts which were written by the user and writes them to the OSM-data.
     175    * After that it checks the tagged data  with the built-in validator file.
     176    *
     177    * @author egru
     178    * @author rebsc
     179    */
     180   class ToolApplyButtonListener implements ActionListener {
     181
     182       @Override
     183       public void actionPerformed(ActionEvent e) {
     184
     185           IndoorObject indoorObject = toolboxView.getSelectedObject();
     186
     187                   // collecting all tags
     188               List<Tag> tags = new ArrayList<>();
     189               if (toolboxView.getLevelCheckBoxStatus() == false && !levelValue.equals("")) {
     190                   tags.add(new Tag(tr("level"),levelValue));
     191               }
     192                   if (!toolboxView.getLevelNameText().isEmpty() && !toolboxView.getLevelCheckBoxStatus()) {
     193                           tags.add(new Tag(tr("level_name"),toolboxView.getLevelNameText()));
     194                   }
     195               if (!toolboxView.getNameText().isEmpty()) {
     196                   tags.add(new Tag(tr("name"), toolboxView.getNameText()));
     197               }
     198               if (!toolboxView.getRefText().isEmpty()) {
     199                   tags.add(new Tag(tr("ref"), toolboxView.getRefText()));
     200               }
     201               if (!toolboxView.getRepeatOnText().isEmpty()){
     202                                tags.add(new Tag(tr("repeat_on"),toolboxView.getRepeatOnText()));
     203                   }
     204                   if (!toolboxView.getLevelNameText().isEmpty() && !toolboxView.getLevelCheckBoxStatus()) {
     205                           tags.add(new Tag(tr("level_name"),toolboxView.getLevelNameText()));
     206                   }
     207
     208           // Tagging to OSM Data
     209           model.addTagsToOSM(indoorObject, tags);
     210
     211           // Reset ui elements
     212           toolboxView.resetUiElements();
     213
     214           //Do the validation process
     215           ValidateAction validateAction = new ValidateAction();
     216           validateAction.doValidate(true);
     217
     218           refreshPresets();
     219
     220       }
     221   }
     222
     223   /**
     224    * The listener which is called when a new item in the object list is selected.
     225    *
     226    * @author egru
     227    * @author rebsc
     228    *
     229    */
     230   class ToolObjectItemListener implements ItemListener {
     231
     232       @Override
     233       public void itemStateChanged(ItemEvent e) {
     234           if (toolboxView.getSelectedObject().equals(IndoorObject.ROOM)) {
     235               toolboxView.setNRUiElementsEnabled(true);
     236               toolboxView.setROUiElementsEnabled(false);
     237           }
     238           else if (toolboxView.getSelectedObject().equals(IndoorObject.STEPS) ||
     239                        toolboxView.getSelectedObject().equals(IndoorObject.ELEVATOR)) {
     240                        toolboxView.setROUiElementsEnabled(true);
     241                        toolboxView.setNRUiElementsEnabled(true);
     242                }
     243           else {
     244               toolboxView.setROUiElementsEnabled(false);
     245           }
     246       }
     247   }
     248
     249   /**
     250    * The listener which is called when the LevelCheckBox is selected.
     251    *
     252    * @author rebsc
     253    */
     254   class ToolLevelCheckBoxListener implements ItemListener{
     255       @Override
     256       public void itemStateChanged(ItemEvent e) {
     257           if(e.getStateChange() == ItemEvent.SELECTED) {
     258                   toolboxView.setLVLUiElementsEnabled(false);
     259           }
     260           else {
     261                   toolboxView.setLVLUiElementsEnabled(true);
     262           }
     263       }
     264   }
     265
     266   /**
     267    * The listener which is called when the helpbutton got pushed.
     268    *
     269    * @author rebsc
     270    */
     271   static class ToolHelpButtonListener implements ActionListener{
     272
     273           @Override
     274           public void actionPerformed(ActionEvent e) {
     275                   String topic = tr("ToolB");
     276                   HelpBrowser.setUrlForHelpTopic(Optional.ofNullable(topic).orElse(tr("/")));
     277           }
     278   }
     279
     280   /**
     281    * The listener which is called when the addlevelbutton got pushed.
     282    *
     283    * @author rebsc
     284    */
     285   class ToolAddLevelButtonListener implements ActionListener{
     286
     287           @Override
     288           public void actionPerformed(ActionEvent e) {
     289
     290                   if(selectorView == null) {
     291                           selectorView = new LevelSelectorView();
     292                           addLevelSelectorListeners();
     293
     294                           //Show LevelSelectorView
     295                           selectorView.setVisible(true);
     296                   } else {
     297                           //Put focus back on LevelSelectorView
     298                           selectorView.toFront();
     299                   }
     300
     301           }
     302   }
     303
     304   /**
     305    * The listener which is called when the MultiCheckBox is selected.
     306    *
     307    * @author rebsc
     308    */
     309   class ToolMultiCheckBoxListener implements ItemListener{
     310       @Override
     311       public void itemStateChanged(ItemEvent e) {
     312           if(e.getStateChange() == ItemEvent.SELECTED) {
     313                   toolboxView.setMultiUiElementsEnabled(false);
     314           }
     315           else {
     316                   toolboxView.setMultiUiElementsEnabled(true);
     317           }
     318       }
     319   }
     320
     321   /**
     322    * The listener which is called when the OUTER Button got pushed.
     323    *
     324    * @author rebsc
     325    */
     326   class ToolOuterButtonListener implements ActionListener{
     327
     328           @Override
     329           public void actionPerformed(ActionEvent e) {
     330                   // Select drawing action
     331                   map.selectMapMode(drawAction);
     332
     333                   // For space shortcut to add the relation after spacebar got pushed {@link SpaceAction}
     334                   OuterHelp = true;
     335                   InnerHelp = false;
     336           }
     337   }
     338
     339   /**
     340    * The listener which is called when the INNER Button got pushed.
     341    *
     342    * @author rebsc
     343    */
     344   class ToolInnerButtonListener implements ActionListener {
     345           @Override
     346           public void actionPerformed(ActionEvent e) {
     347                   // Select drawing action
     348                   map.selectMapMode(drawAction);
     349
     350                   // For space shortcut to edit the relation after enter got pushed {@link SpaceAction}{@link EnterAction}
     351                   InnerHelp = true;
     352                   OuterHelp = false;
     353
     354           }
     355   }
     356
     357   /**
     358    * Listener for preset button 1.
     359    * @author egru
     360    *
     361    */
     362   class Preset1Listener implements ActionListener {
     363
     364       @Override
     365       public void actionPerformed(ActionEvent e) {
     366           model.addTagsToOSM(toolboxView.getPreset1());
     367       }
     368   }
     369
     370   /**
     371    * Listener for preset button 2.
     372    * @author egru
     373    *
     374    */
     375   class Preset2Listener implements ActionListener {
     376
     377       @Override
     378       public void actionPerformed(ActionEvent e) {
     379           model.addTagsToOSM(toolboxView.getPreset2());
     380       }
     381   }
     382
     383   /**
     384    * Listener for preset button 3.
     385    * @author egru
     386    *
     387    */
     388   class Preset3Listener implements ActionListener {
     389
     390       @Override
     391       public void actionPerformed(ActionEvent e) {
     392           model.addTagsToOSM(toolboxView.getPreset3());
     393       }
     394   }
     395
     396   /**
     397    * Listener for preset button 4.
     398    * @author egru
     399    *
     400    */
     401   class Preset4Listener implements ActionListener {
     402
     403       @Override
     404       public void actionPerformed(ActionEvent e) {
     405           model.addTagsToOSM(toolboxView.getPreset4());
     406       }
     407   }
     408
     409   /**
     410    * Updates the preset button from the current ranking.
     411    */
     412   private void refreshPresets() {
     413       toolboxView.setPresetButtons(model.getPresetRanking());
     414   }
     415
     416/*************************************************
     417* LEVEL SELCTOR VIEW LISTENER
     418*
     419*/
     420   /**
     421    * Specific listener for the applyButton
     422    * @author rebsc
     423    *
     424    */
     425   class ToolLevelOkButtonListener implements ActionListener {
     426
     427       @Override
     428       public void actionPerformed(ActionEvent e) {
     429           LevelHelp = true;
     430
     431           //Get insert level number out of SelectorView
     432           if(!selectorView.getLevelNumber().equals("")) {
     433                   levelNum = selectorView.getLevelNumber();
     434
     435                   //Unset visibility
     436                   selectorView.dispose();
     437                   //Select draw-action
     438                   map.selectMapMode(drawAction);
     439
     440           }else {
     441                   JOptionPane.showMessageDialog(null, tr("Please insert a value."), tr("Error"), JOptionPane.ERROR_MESSAGE);
     442           }
     443
     444           selectorView = null;
     445       }
     446   }
     447
     448   /**
     449    * Specific listener for the cancelButton
     450    * @author rebsc
     451    *
     452    */
     453   class ToolLevelCancelButtonListener implements ActionListener {
     454
     455       @Override
     456       public void actionPerformed(ActionEvent e) {
     457           selectorView.dispose();
     458           selectorView = null;
     459       }
     460   }
     461
     462   /**
     463    * General listener for LevelSelectorView window
     464    * @author rebsc
     465    *
     466    */
     467   class ToolSelectorWindowSListener implements WindowListener{
     468
     469        @Override
     470        public void windowClosed(WindowEvent e) {
     471                selectorView = null;
     472        }
     473
     474        @Override
     475        public void windowClosing(WindowEvent e) {
     476                selectorView = null;
     477        }
     478
     479        @Override
     480        public void windowActivated(WindowEvent arg0) {
     481                // TODO Auto-generated method stub
     482
     483        }
     484
     485        @Override
     486        public void windowDeactivated(WindowEvent arg0) {
     487                // TODO Auto-generated method stub
     488
     489        }
     490
     491        @Override
     492        public void windowDeiconified(WindowEvent arg0) {
     493                // TODO Auto-generated method stub
     494
     495        }
     496
     497        @Override
     498        public void windowIconified(WindowEvent arg0) {
     499                // TODO Auto-generated method stub
     500
     501        }
     502
     503        @Override
     504        public void windowOpened(WindowEvent arg0) {
     505                // TODO Auto-generated method stub
     506
     507        }
     508   }
     509
     510
     511/*************************************************
     512* SHORTCUT METHODS
     513*
     514*/
     515   /**
     516    * Shortcut for Spacebar
     517    * @author rebsc
     518    */
     519   private class SpaceAction extends AbstractAction {
     520
     521        private static final long serialVersionUID = 1L;
     522
     523        @Override
     524       public void actionPerformed(ActionEvent e) {
     525           if(OuterHelp) {
     526
     527                   //Create new relation and add the currently drawn object to it
     528                   model.addRelation("outer");
     529                   map.selectMapMode(selectAction);
     530                   OuterHelp = false;
     531
     532                   //Clear currently selection
     533                   MainApplication.getLayerManager().getEditDataSet().clearSelection();
     534           }
     535           else if(InnerHelp) {
     536
     537                   //Save new drawn relation for adding
     538                   innerRelation = MainApplication.getLayerManager().getEditDataSet().getAllSelected();
     539                           map.selectMapMode(selectAction);
     540
     541                   //Clear currently selection
     542                   MainApplication.getLayerManager().getEditDataSet().clearSelection();
     543           }
     544           else if(LevelHelp) {
     545
     546               List<Tag> tags = new ArrayList<>();
     547                   tags.add(new Tag(tr("level"),levelNum));
     548
     549                   //Add level tag
     550                   model.addTagsToOSM(tags);
     551
     552                   //Change action
     553                   map.selectMapMode(selectAction);
     554                   LevelHelp = false;
     555           }
     556       }
     557   }
     558
     559   /**
     560    * Shortcut for Enter
     561    * @author rebsc
     562    */
     563   private class EnterAction extends AbstractAction {
     564
     565        private static final long serialVersionUID = 1L;
     566
     567        @Override
     568       public void actionPerformed(ActionEvent e) {
     569
     570           if(InnerHelp && !OuterHelp) {
     571
     572                   // Edit the new drawn relation member to selected relation
     573                   model.editRelation("inner", innerRelation);
     574                   InnerHelp = false;
     575
     576           }else if((InnerHelp && OuterHelp) || (OuterHelp && !InnerHelp)){
     577                   JOptionPane.showMessageDialog(null, tr("Please press spacebar first to add \"outer\" object to relation."), tr("Relation-Error"), JOptionPane.ERROR_MESSAGE);
     578                   resetHelper();
     579           }
     580       }
     581   }
     582
     583/*************************************************
     584* FILTER METHODS
     585*
     586*/
     587   /**
     588    * Function which unset the disabled state of currently hidden and/or disabled objects which have a
     589    * specific tag (key). Just unsets the disabled state if object has a tag-value which is part of the
     590    * current working level.
     591    * Specific example: key: repeat_on ; value: 1-4;
     592    *                                   If current selected workinglevel is "3" all objects with the leveltag "level=3" are hidden but the
     593    *                                   objects with the key "repeat_on" and the value which includes "3" or is (minValue+1) - maxValue (4-...),
     594    *                                   because repeat_on tag starts on the current workinglevel+1.
     595    * @param key sepcific key to unset hidden objects which contains it
     596    * @author rebsc
     597    */
     598   public void unsetSpecificKeyFilter(String key) {
     599
     600         Collection<OsmPrimitive> p = Main.main.getEditDataSet().allPrimitives();
     601         Map<String, String> tags = new HashMap<>();
     602         Integer level = Integer.parseInt(levelValue);
     603         Integer firstVal, secVal;
     604
     605         //Find all primitives with the specific tag and check if value is part of the current
     606         //workinglevel. After that unset the disabled status.
     607         for(OsmPrimitive osm: p) {
     608                 if((osm.isDisabledAndHidden() || osm.isDisabled()) && osm.hasKey(key)) {
     609
     610                         tags = osm.getInterestingTags();
     611
     612                         for(Map.Entry<String, String> e: tags.entrySet()) {
     613                                if(e.getKey().equals(key)) {
     614                                        String val = e.getValue();
     615
     616                                        //Extract values
     617                                        if(val.indexOf("-") == 0) {
     618                                                firstVal = (Integer.parseInt(val.split("-",2)[1].split("-",2)[0]))*-1;
     619                                                secVal = Integer.parseInt(val.split("-",2)[1].split("-",2)[1]);
     620                                        }else {
     621                                                firstVal = Integer.parseInt(val.split("-")[0]);
     622                                                secVal = Integer.parseInt(val.split("-")[1]);
     623                                        }
     624
     625                                        //Compare values to current working level
     626                                        if(level >= ((firstVal)-1) && level <= secVal) {
     627                                                osm.unsetDisabledState();
     628                                        }else {
     629                                                osm.setDisabledState(true);
     630                                        }
     631                                }
     632
     633                         }
     634
     635                 }
     636         }
     637
     638   }
     639
     640  /**
     641   * Function which updates the current working level tag
     642   *
     643   * @param indoorLevel: current working level
     644   */
     645   public void setIndoorLevel(String indoorLevel) {
     646           this.toolboxView.setLevelLabel(indoorLevel);
     647   }
     648
     649   /**
     650    * Function which gets the current working level tag
     651    *
     652    * @param indoorLevel: current working level
     653    */
     654    public void getIndoorLevel(String indoorLevel) {
     655        levelValue = indoorLevel;
     656
    117657    }
    118658
    119     //********************************************************************
    120     //*********************   TOOLBOX LISTENERS   ************************
    121     //********************************************************************
    122 
    123     /**
    124      * The listener which handles the power button.
    125      *
    126      * @author egru
    127      *
    128      */
    129     class ToolPowerButtonListener implements ActionListener {
    130 
    131         @Override
    132         public void actionPerformed(ActionEvent e) {
    133             if (toolboxView.getPowerButtonState()) {
    134                 selectorView = new LevelSelectorView();
    135                 addLevelSelectorListeners();
    136                 selectorView.setVisible(true);
    137                 setPluginPreferences(true);
    138             } else if (!toolboxView.getPowerButtonState()) {
    139                 model = new IndoorHelperModel();
    140                 selectorView.dispose();
    141                 toolboxView.reset();
    142                 setPluginPreferences(false);
    143 
    144                 // Delete the indoor filters
    145                 FilterDialog filterDialog = MainApplication.getMap().getToggleDialog(FilterDialog.class);
    146 
    147                 if (filterDialog != null) {
    148                     FilterTableModel filterTableModel = filterDialog.getFilterModel();
    149 
    150                     for (int i = filterTableModel.getRowCount()-1; i > -1; i--) {
    151                         if (filterTableModel.getFilter(i).text.startsWith("\"indoor:level\"=\"")) {
    152                             filterTableModel.removeFilter(i);
    153                         }
    154                     }
    155                 }
    156             }
    157         }
    158     }
    159 
    160     /**
    161      * The listener which provides the handling of the apply button.
    162      * Gets the texts which were written by the user and writes them to the OSM-data.
    163      * After that it checks the tagged data  with the built-in validator file.
    164      *
    165      * @author egru
    166      */
    167     class ToolApplyButtonListener implements ActionListener {
    168 
    169         @Override
    170         public void actionPerformed(ActionEvent e) {
    171             IndoorObject indoorObject = toolboxView.getSelectedObject();
    172             if (toolboxView.getNameText().isEmpty() && toolboxView.getRefText().isEmpty() && toolboxView.getLevelName().isEmpty()) {
    173                 model.addTagsToOSM(indoorObject);
    174             } else {
    175                 List<Tag> tags = new ArrayList<>();
    176                 if (!toolboxView.getLevelName().isEmpty()) {
    177                     model.getLevelList().get(toolboxView.getSelectedLevelIndex()).setNameTag(toolboxView.getLevelName());
    178                 }
    179                 if (!toolboxView.getNameText().isEmpty()) {
    180                     tags.add(new Tag("name", toolboxView.getNameText()));
    181                 }
    182                 if (!toolboxView.getRefText().isEmpty()) {
    183                     tags.add(new Tag("ref", toolboxView.getRefText()));
    184                 }
    185                 model.addTagsToOSM(indoorObject, tags);
    186             }
    187             //Do the validation process
    188             ValidateAction validateAction = new ValidateAction();
    189             validateAction.doValidate(true);
    190 
    191             refreshPresets();
    192         }
    193     }
    194 
    195     /**
    196      * <pre>The listener which is called when a new item in the level list is selected.
    197      *It also sets the name-tag for a level, if the user has done an input in the textbox.
    198      * </pre>
    199      * @author egru
    200      *
    201      */
    202     class ToolLevelItemListener implements ItemListener {
    203 
    204         @Override
    205         public void itemStateChanged(ItemEvent e) {
    206             if (!toolboxView.levelListIsEmpty()) {
    207 
    208                 if (!toolboxView.getLevelName().isEmpty()) {
    209                     model.getLevelList().get(lastLevelIndex).setNameTag(toolboxView.getLevelName());
    210                 }
    211 
    212                 if (!model.getLevelList().get(toolboxView.getSelectedLevelIndex()).hasEmptyName()) {
    213                     toolboxView.setLevelName(model.getLevelList().get(toolboxView.getSelectedLevelIndex()).getName());
    214                 } else {
    215                     toolboxView.setLevelName("");
    216                 }
    217                 model.setWorkingLevel(toolboxView.getSelectedLevelIndex());
    218 
    219                 lastLevelIndex = toolboxView.getSelectedLevelIndex();
    220             }
    221         }
    222     }
    223 
    224 
    225 
    226     /**
    227      * The listener which is called when a new item in the object list is selected.
    228      *
    229      * @author egru
    230      *
    231      */
    232     class ToolObjectItemListener implements ItemListener {
    233 
    234         @Override
    235         public void itemStateChanged(ItemEvent e) {
    236             if (toolboxView.getSelectedObject().equals(IndoorObject.ROOM)) {
    237                 toolboxView.setTagUiElementsEnabled(true);
    238             } else {
    239                 toolboxView.setTagUiElementsEnabled(false);
    240             }
    241         }
    242     }
    243 
    244     /**
    245      * Listener for preset button 1.
    246      * @author egru
    247      *
    248      */
    249     class Preset1Listener implements ActionListener {
    250 
    251         @Override
    252         public void actionPerformed(ActionEvent e) {
    253             model.addTagsToOSM(toolboxView.getPreset1());
    254 
    255         }
    256     }
    257 
    258     /**
    259      * Listener for preset button 2.
    260      * @author egru
    261      *
    262      */
    263     class Preset2Listener implements ActionListener {
    264 
    265         @Override
    266         public void actionPerformed(ActionEvent e) {
    267             model.addTagsToOSM(toolboxView.getPreset2());
    268 
    269         }
    270 
    271     }
    272 
    273     /**
    274      * Listener for preset button 3.
    275      * @author egru
    276      *
    277      */
    278     class Preset3Listener implements ActionListener {
    279 
    280         @Override
    281         public void actionPerformed(ActionEvent e) {
    282             model.addTagsToOSM(toolboxView.getPreset3());
    283 
    284         }
    285 
    286     }
    287 
    288     /**
    289      * Listener for preset button 4.
    290      * @author egru
    291      *
    292      */
    293     class Preset4Listener implements ActionListener {
    294 
    295         @Override
    296         public void actionPerformed(ActionEvent e) {
    297             model.addTagsToOSM(toolboxView.getPreset4());
    298 
    299         }
    300 
    301     }
    302 
    303     /**
    304      * Updates the preset button from the current ranking.
    305      */
    306     private void refreshPresets() {
    307         toolboxView.setPresetButtons(model.getPresetRanking());
    308     }
    309 
    310 
    311     //*******************
    312     // SELECTOR LISTENERS
    313     //*******************
    314 
    315     /**
    316      * <pre>
    317      * The listener which handles the click on the OK-button of the {@link LevelSelectorView}.
    318      * It sends the data of the view to the model and displays an error message,
    319      * if the level-list couldn't be created.
    320      * </pre>
    321      * @author egru
    322      *
    323      */
    324     class LevelOkButtonListener implements ActionListener {
    325 
    326         @Override
    327         public void actionPerformed(ActionEvent e) {
    328             boolean levelSuccess = model.setBuildingLevels(selectorView.getMin(), selectorView.getMax());
    329 
    330             if (levelSuccess) {
    331                 toolboxView.setLevelList(model.getLevelList());                //set the levels to the ComboBox and
    332                 model.setWorkingLevel(toolboxView.getSelectedLevelIndex());        //sets the working level in the model
    333 
    334                 selectorView.dispose();
    335 
    336                 fittingView = new FittingView();
    337                 addFittingListeners();
    338                 fittingView.setVisible(true);
    339             } else {
    340 
    341                 JOptionPane.showMessageDialog(null, "Lowest Level has to be lower than the highest level",
    342                         "Error", JOptionPane.ERROR_MESSAGE);
    343             }
    344         }
    345     }
    346 
    347     /**
    348      * Closes the level selection view if the user hits the cancel button.
    349      *
    350      * @author egru
    351      *
    352      */
    353     class LevelCancelButtonListener implements ActionListener {
    354 
    355         @Override
    356         public void actionPerformed(ActionEvent e) {
    357             selectorView.dispose();
    358             toolboxView.setPowerButtonDisabled();
    359             setPluginPreferences(false);
    360         }
    361 
    362     }
    363 
    364 
    365 
    366     //*******************
    367     // FITTING LISTENERS
    368     //*******************
    369     /**
    370      * Closes the {@link FittingView} if the OK-Button is clicked.
    371      * Enables the UI elements of the toolbox
    372      *
    373      * @author egru
    374      *
    375      */
    376     class FittingOkButtonListener implements ActionListener {
    377 
    378         @Override
    379         public void actionPerformed(ActionEvent e) {
    380             fittingView.dispose();
    381             toolboxView.setAllUiElementsEnabled(true);
    382             toolboxView.setTagUiElementsEnabled(false);
    383         }
    384 
    385     }
    386 
    387     /*
    388     HELPER METHODS
    389     */
    390 
    391     /**
    392      * Enables or disables the preferences for the mapcss-style and the validator.
    393      *
    394      * @param enabled Activates or disables the settings.
    395      */
    396     private void setPluginPreferences(boolean enabled) {
    397         Collection<Map<String, String>> validatorMaps =
    398                 Main.pref.getListOfMaps("validator.org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.entries",
    399                 new ArrayList<>());
    400         Collection<Map<String, String>> styleMaps =
    401                 Main.pref.getListOfMaps("mappaint.style.entries", new ArrayList<>());
    402 
    403         if (enabled) {
    404             //set the validator active
    405 
    406             List<Map<String, String>> validatorMapsNew = new ArrayList<>();
    407             if (!validatorMaps.isEmpty()) {
    408                 validatorMapsNew.addAll(validatorMaps);
    409             }
    410 
    411             for (Map<String, String> map : validatorMapsNew) {
    412                 if (map.containsValue("Indoor")) {
    413                     validatorMapsNew.remove(map);
    414                     break;
    415                 }
    416             }
    417 
    418             Map<String, String> indoorValidator = new HashMap<>();
    419             indoorValidator.put("title", "Indoor");
    420             indoorValidator.put("active", "true");
    421             indoorValidator.put("url", Main.pref.getDirs().getUserDataDirectory(false)+ sep +"validator" +
    422                     sep + "indoorhelper.validator.mapcss");
    423 
    424             validatorMapsNew.add(indoorValidator);
    425             Main.pref.putListOfMaps("validator.org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.entries",
    426                     validatorMapsNew);
    427 
    428             //set mappaint active
    429 
    430             List<Map<String, String>> styleMapsNew = new ArrayList<>();
    431             if (!styleMaps.isEmpty()) {
    432                 styleMapsNew.addAll(styleMaps);
    433             }
    434 
    435             for (Map<String, String> map : styleMapsNew) {
    436                 if (map.containsValue("Indoor")) {
    437                     styleMapsNew.remove(map);
    438                     break;
    439                 }
    440             }
    441             Map<String, String> indoorMapPaint = new HashMap<>();
    442             indoorMapPaint.put("title", "Indoor");
    443             indoorMapPaint.put("active", "true");
    444             indoorMapPaint.put("url", Main.pref.getDirs().getUserDataDirectory(false) + sep + "styles"
    445                     + sep + "indoor.mapcss");
    446             styleMapsNew.add(indoorMapPaint);
    447             Main.pref.putListOfMaps("mappaint.style.entries", styleMapsNew);
    448 
    449             updateSettings();
    450         } else {
    451             //set the validator inactive
    452 
    453 
    454             List<Map<String, String>> validatorMapsNew = new ArrayList<>();
    455             if (!validatorMaps.isEmpty()) {
    456                 validatorMapsNew.addAll(validatorMaps);
    457             }
    458 
    459             for (Map<String, String> map : validatorMapsNew) {
    460                 if (map.containsValue("Indoor")) {
    461                     validatorMapsNew.remove(map);
    462                     break;
    463                 }
    464             }
    465             Map<String, String> indoorValidator = new HashMap<>();
    466             indoorValidator.put("title", "Indoor");
    467             indoorValidator.put("active", "false");
    468             indoorValidator.put("url", Main.pref.getDirs().getUserDataDirectory(false)+ sep +"validator" +
    469                     sep + "indoorhelper.validator.mapcss");
    470 
    471             validatorMapsNew.add(indoorValidator);
    472             Main.pref.putListOfMaps("validator.org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.entries",
    473                     validatorMapsNew);
    474 
    475 
    476             //set mappaint inactive
    477 
    478 
    479             List<Map<String, String>> styleMapsNew = new ArrayList<>();
    480             if (!styleMaps.isEmpty()) {
    481                 styleMapsNew.addAll(styleMaps);
    482             }
    483             for (Map<String, String> map : styleMapsNew) {
    484                 if (map.containsValue("Indoor")) {
    485                     styleMapsNew.remove(map);
    486                     break;
    487                 }
    488             }
    489             Map<String, String> indoorMapPaint = new HashMap<>();
    490             indoorMapPaint.put("title", "Indoor");
    491             indoorMapPaint.put("active", "false");
    492             indoorMapPaint.put("url", Main.pref.getDirs().getUserDataDirectory(false) + sep + "styles"
    493                     + sep + "indoor.mapcss");
    494             styleMapsNew.add(indoorMapPaint);
    495             Main.pref.putListOfMaps("mappaint.style.entries", styleMapsNew);
    496 
    497             updateSettings();
    498         }
    499     }
     659
     660/*************************************************
     661* HELPER METHODS
     662*
     663*/
     664
     665   /**
     666    * Function which resets the helper for relation adding
     667    */
     668   private void resetHelper() {
     669           InnerHelp = false;
     670           OuterHelp = false;
     671   }
    500672
    501673    /**
     
    511683            MapPaintStyles.readFromPreferences();
    512684    }
     685
     686   /**
     687    * Enables or disables the preferences for the mapcss-style and the validator.
     688    *
     689    * @param enabled Activates or disables the settings.
     690    */
     691        @SuppressWarnings("deprecation")
     692        private void setPluginPreferences(boolean enabled) {
     693       Map<String, Setting<?>> settings = Main.pref.getAllSettings();
     694
     695       MapListSetting validatorMapListSetting = (MapListSetting) settings.
     696               get("validator.org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.entries");
     697       List<Map<String, String>> validatorMaps = new ArrayList<>();
     698       if (validatorMapListSetting != null) {
     699           validatorMaps = validatorMapListSetting.getValue();
     700       }
     701
     702       MapListSetting styleMapListSetting = (MapListSetting) settings.
     703               get("mappaint.style.entries");
     704       List<Map<String, String>> styleMaps = new ArrayList<>();
     705       if (styleMapListSetting != null) {
     706           styleMaps = styleMapListSetting.getValue();
     707       }
     708
     709       if (enabled) {
     710           //set the validator active
     711
     712           List<Map<String, String>> validatorMapsNew = new ArrayList<>();
     713           if (!validatorMaps.isEmpty()) {
     714               validatorMapsNew.addAll(validatorMaps);
     715           }
     716
     717           for (Map<String, String> map : validatorMapsNew) {
     718               if (map.containsValue(tr("Indoor"))) {
     719                   validatorMapsNew.remove(map);
     720                   break;
     721               }
     722           }
     723
     724           Map<String, String> indoorValidator = new HashMap<>();
     725           indoorValidator.put("title", "Indoor");
     726           indoorValidator.put("active", "true");
     727           indoorValidator.put("url", Main.pref.getUserDataDirectory()+ sep +"validator" +
     728                   sep + "indoorhelper.validator.mapcss");
     729
     730           validatorMapsNew.add(indoorValidator);
     731           Main.pref.putListOfMaps("validator.org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.entries",
     732                   validatorMapsNew);
     733
     734           //set mappaint active
     735
     736           List<Map<String, String>> styleMapsNew = new ArrayList<>();
     737           if (!styleMaps.isEmpty()) {
     738               styleMapsNew.addAll(styleMaps);
     739           }
     740
     741           for (Map<String, String> map : styleMapsNew) {
     742               if (map.containsValue(tr("Indoor"))) {
     743                   styleMapsNew.remove(map);
     744                   break;
     745               }
     746           }
     747           Map<String, String> indoorMapPaint = new HashMap<>();
     748           indoorMapPaint.put("title", tr("Indoor"));
     749           indoorMapPaint.put("active", "true");
     750           indoorMapPaint.put("url", Main.pref.getUserDataDirectory() + sep + "styles"
     751                   + sep + "sit.mapcss");
     752           styleMapsNew.add(indoorMapPaint);
     753           Main.pref.putListOfMaps("mappaint.style.entries", styleMapsNew);
     754
     755           updateSettings();
     756       } else {
     757           //set the validator inactive
     758
     759
     760           List<Map<String, String>> validatorMapsNew = new ArrayList<>();
     761           if (!validatorMaps.isEmpty()) {
     762               validatorMapsNew.addAll(validatorMaps);
     763           }
     764
     765           for (Map<String, String> map : validatorMapsNew) {
     766               if (map.containsValue(tr("Indoor"))) {
     767                   validatorMapsNew.remove(map);
     768                   break;
     769               }
     770           }
     771           Map<String, String> indoorValidator = new HashMap<>();
     772           indoorValidator.put("title", tr("Indoor"));
     773           indoorValidator.put("active", "false");
     774           indoorValidator.put("url", Main.pref.getUserDataDirectory()+ sep +"validator" +
     775                   sep + "indoorhelper.validator.mapcss");
     776
     777           validatorMapsNew.add(indoorValidator);
     778           Main.pref.putListOfMaps("validator.org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.entries",
     779                   validatorMapsNew);
     780
     781
     782           //set mappaint inactive
     783
     784
     785           List<Map<String, String>> styleMapsNew = new ArrayList<>();
     786           if (!styleMaps.isEmpty()) {
     787               styleMapsNew.addAll(styleMaps);
     788           }
     789           for (Map<String, String> map : styleMapsNew) {
     790               if (map.containsValue(tr("Indoor"))) {
     791                   styleMapsNew.remove(map);
     792                   break;
     793               }
     794           }
     795           Map<String, String> indoorMapPaint = new HashMap<>();
     796           indoorMapPaint.put("title", tr("Indoor"));
     797           indoorMapPaint.put("active", "false");
     798           indoorMapPaint.put("url", Main.pref.getUserDataDirectory() + sep + "styles"
     799                   + sep + "sit.mapcss");
     800           styleMapsNew.add(indoorMapPaint);
     801           Main.pref.putListOfMaps("mappaint.style.entries", styleMapsNew);
     802
     803           updateSettings();
     804       }
     805   }
     806
     807
     808
     809
     810
     811/**
     812 *
     813 *
     814 *
     815 *
     816 *
     817 *
     818 *
     819 *
     820 *
     821 */
    513822}
    514 
    515 
Note: See TracChangeset for help on using the changeset viewer.