Ignore:
Timestamp:
2010-02-12T08:57:04+01:00 (14 years ago)
Author:
roland
Message:

Public Transport Plugin: Tags tab implemented

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/public_transport/src/public_transport/RoutePatternAction.java

    r19975 r19976  
    66import java.awt.BorderLayout;
    77import java.awt.Container;
     8import java.awt.Dimension;
    89import java.awt.Frame;
    910import java.awt.GridBagConstraints;
     
    4041import javax.swing.event.TableModelListener;
    4142import javax.swing.table.DefaultTableModel;
     43import javax.swing.table.TableCellEditor;
    4244
    4345import org.openstreetmap.josm.Main;
     
    6971  };
    7072 
     73  private class TagTableModel extends DefaultTableModel implements TableModelListener {
     74    Relation relation = null;
     75    TreeSet< String > blacklist = null;
     76    boolean hasFixedKeys = true;
     77   
     78    public TagTableModel(boolean hasFixedKeys) {
     79      this.hasFixedKeys = hasFixedKeys;
     80    }
     81
     82    public boolean isCellEditable(int row, int column) {
     83      if ((column == 0) && (hasFixedKeys))
     84        return false;
     85      return true;
     86    }
     87   
     88    public void readRelation(Relation rel) {
     89      relation = rel;
     90     
     91      for (int i = 0; i < getRowCount(); ++i)
     92      {
     93        String value = rel.get((String)getValueAt(i, 0));
     94        if (value == null)
     95          value = "";
     96        setValueAt(value, i, 1);
     97      }
     98    }
     99   
     100    public void readRelation(Relation rel, TreeSet< String > blacklist) {
     101      relation = rel;
     102      this.blacklist = blacklist;
     103     
     104      setRowCount(0);
     105      Iterator< Map.Entry< String, String > > iter = rel.getKeys().entrySet().iterator();
     106      while (iter.hasNext())
     107      {
     108        Map.Entry< String, String > entry = iter.next();
     109        if (!blacklist.contains(entry.getKey()))
     110        {
     111          Vector< String > newRow = new Vector< String >();
     112          newRow.add(entry.getKey());
     113          newRow.add(entry.getValue());
     114          addRow(newRow);
     115        }
     116      }
     117     
     118      for (int i = 0; i < getRowCount(); ++i)
     119      {
     120        String value = rel.get((String)getValueAt(i, 0));
     121        if (value == null)
     122          value = "";
     123        setValueAt(value, i, 1);
     124      }
     125    }
     126 
     127    public void tableChanged(TableModelEvent e)
     128    {
     129      if (e.getType() == TableModelEvent.UPDATE)
     130      {
     131        String key = (String)getValueAt(e.getFirstRow(), 0);
     132        if (key == null)
     133          return;
     134        if ((blacklist == null) || (!blacklist.contains(key)))
     135        {
     136          relation.setModified(true);
     137          if ("".equals(getValueAt(e.getFirstRow(), 1)))
     138            relation.remove(key);
     139          else
     140            relation.put(key, (String)getValueAt(e.getFirstRow(), 1));
     141        }
     142        else
     143        {
     144          if (e.getColumn() == 0)
     145            setValueAt("", e.getFirstRow(), 0);
     146        }
     147      }
     148    }
     149  };
     150 
     151  private class CustomCellEditorTable extends JTable {
     152    TreeMap< Integer, TableCellEditor > col1 = null;
     153    TreeMap< Integer, TableCellEditor > col2 = null;
     154   
     155    public CustomCellEditorTable() {
     156      col1 = new TreeMap< Integer, TableCellEditor >();
     157      col2 = new TreeMap< Integer, TableCellEditor >();
     158    }
     159   
     160    public TableCellEditor getCellEditor(int row, int column) {
     161      TableCellEditor editor = null;
     162      if (column == 0)
     163        editor = col1.get(new Integer(row));
     164      else
     165        editor = col2.get(new Integer(row));
     166      if (editor == null)
     167        return new DefaultCellEditor(new JTextField());
     168      else
     169        return editor;
     170    }
     171   
     172    public void setCellEditor(int row, int column, TableCellEditor editor) {
     173      if (column == 0)
     174        col1.put(new Integer(row), editor);
     175      else
     176        col2.put(new Integer(row), editor);
     177    }
     178  };
     179 
    71180  private class ItineraryTableModel extends DefaultTableModel {
    72181    public Vector<Way> ways = new Vector<Way>();
     
    277386  private static JTabbedPane tabbedPane = null;
    278387  private static DefaultListModel dlm = null;
     388  private static TagTableModel requiredTagsData = null;
     389  private static CustomCellEditorTable requiredTagsTable = null;
     390  private static TagTableModel commonTagsData = null;
     391  private static CustomCellEditorTable commonTagsTable = null;
     392  private static TagTableModel otherTagsData = null;
     393  private static TreeSet< String > tagBlacklist = null;
     394  private static CustomCellEditorTable otherTagsTable = null;
    279395  private static ItineraryTableModel itineraryData = null;
    280396  private static JTable itineraryTable = null;
     
    364480      contentPane.add(bRefresh);
    365481       
     482      //Tags Tab
     483      /*Container*/ contentPane = tabTags;
     484      /*GridBagLayout*/ gridbag = new GridBagLayout();
     485      /*GridBagConstraints*/ layoutCons = new GridBagConstraints();
     486      contentPane.setLayout(gridbag);
     487     
     488      /*JLabel*/ headline = new JLabel("Required tags:");
     489       
     490      layoutCons.gridx = 0;
     491      layoutCons.gridy = 0;
     492      layoutCons.weightx = 0.0;
     493      layoutCons.weighty = 0.0;
     494      layoutCons.fill = GridBagConstraints.BOTH;
     495      gridbag.setConstraints(headline, layoutCons);
     496      contentPane.add(headline);
     497       
     498      requiredTagsTable = new CustomCellEditorTable();
     499      requiredTagsData = new TagTableModel(true);
     500      requiredTagsData.addColumn("Key");
     501      requiredTagsData.addColumn("Value");
     502      tagBlacklist = new TreeSet< String >();
     503      Vector< String > rowContent = new Vector< String >();
     504      rowContent.add("type");
     505      tagBlacklist.add("type");
     506      rowContent.add("route");
     507      requiredTagsData.addRow(rowContent);
     508      JComboBox comboBox = new JComboBox();
     509      comboBox.addItem("route");
     510      requiredTagsTable.setCellEditor(0, 1, new DefaultCellEditor(comboBox));
     511      rowContent = new Vector< String >();
     512      rowContent.add(0, "route");
     513      tagBlacklist.add("route");
     514      rowContent.add(1, "bus");
     515      requiredTagsData.addRow(rowContent);
     516      /*JComboBox*/ comboBox = new JComboBox();
     517      comboBox.addItem("bus");
     518/*      comboBox.addItem("tram");
     519      comboBox.addItem("light_rail");
     520      comboBox.addItem("subway");
     521      comboBox.addItem("rail");*/
     522      requiredTagsTable.setCellEditor(1, 1, new DefaultCellEditor(comboBox));
     523      rowContent = new Vector< String >();
     524      rowContent.add(0, "ref");
     525      tagBlacklist.add("ref");
     526      rowContent.add(1, "");
     527      requiredTagsData.addRow(rowContent);
     528      rowContent = new Vector< String >();
     529      rowContent.add(0, "to");
     530      tagBlacklist.add("to");
     531      rowContent.add(1, "");
     532      requiredTagsData.addRow(rowContent);
     533      rowContent = new Vector< String >();
     534      rowContent.add(0, "network");
     535      tagBlacklist.add("network");
     536      rowContent.add(1, "");
     537      requiredTagsData.addRow(rowContent);
     538      requiredTagsTable.setModel(requiredTagsData);
     539      JScrollPane tableSP = new JScrollPane(requiredTagsTable);
     540      requiredTagsData.addTableModelListener(requiredTagsData);
     541     
     542      layoutCons.gridx = 0;
     543      layoutCons.gridy = 1;
     544      layoutCons.weightx = 1.0;
     545      layoutCons.weighty = 0.25;
     546      layoutCons.fill = GridBagConstraints.BOTH;
     547      gridbag.setConstraints(tableSP, layoutCons);
     548      Dimension preferredSize = tableSP.getPreferredSize();
     549      preferredSize.setSize(tableSP.getPreferredSize().getWidth(), tableSP.getPreferredSize().getHeight()/4.0);
     550      tableSP.setPreferredSize(preferredSize);
     551      contentPane.add(tableSP);
     552       
     553      headline = new JLabel("Common tags:");
     554       
     555      layoutCons.gridx = 0;
     556      layoutCons.gridy = 2;
     557      layoutCons.weightx = 0.0;
     558      layoutCons.weighty = 0.0;
     559      layoutCons.fill = GridBagConstraints.BOTH;
     560      gridbag.setConstraints(headline, layoutCons);
     561      contentPane.add(headline);
     562     
     563      commonTagsTable = new CustomCellEditorTable();
     564      commonTagsData = new TagTableModel(true);
     565      commonTagsData.addColumn("Key");
     566      commonTagsData.addColumn("Value");
     567      rowContent = new Vector< String >();
     568      rowContent.add(0, "description");
     569      tagBlacklist.add("description");
     570      rowContent.add(1, "");
     571      commonTagsData.addRow(rowContent);
     572      rowContent = new Vector< String >();
     573      rowContent.add(0, "from");
     574      tagBlacklist.add("from");
     575      rowContent.add(1, "");
     576      commonTagsData.addRow(rowContent);
     577      rowContent = new Vector< String >();
     578      rowContent.add(0, "operator");
     579      tagBlacklist.add("operator");
     580      rowContent.add(1, "");
     581      commonTagsData.addRow(rowContent);
     582      rowContent = new Vector< String >();
     583      rowContent.add(0, "color");
     584      tagBlacklist.add("color");
     585      rowContent.add(1, "");
     586      commonTagsData.addRow(rowContent);
     587      rowContent = new Vector< String >();
     588      rowContent.add(0, "name");
     589      tagBlacklist.add("name");
     590      rowContent.add(1, "");
     591      commonTagsData.addRow(rowContent);
     592      commonTagsTable.setModel(commonTagsData);
     593      /*JScrollPane*/ tableSP = new JScrollPane(commonTagsTable);
     594      commonTagsData.addTableModelListener(commonTagsData);
     595     
     596      layoutCons.gridx = 0;
     597      layoutCons.gridy = 3;
     598      layoutCons.weightx = 1.0;
     599      layoutCons.weighty = 0.25;
     600      layoutCons.fill = GridBagConstraints.BOTH;
     601      gridbag.setConstraints(tableSP, layoutCons);
     602      /*Dimension*/ preferredSize = tableSP.getPreferredSize();
     603      preferredSize.setSize(tableSP.getPreferredSize().getWidth(), tableSP.getPreferredSize().getHeight()/4.0);
     604      tableSP.setPreferredSize(preferredSize);
     605      contentPane.add(tableSP);
     606       
     607      headline = new JLabel("Additional tags:");
     608       
     609      layoutCons.gridx = 0;
     610      layoutCons.gridy = 4;
     611      layoutCons.weightx = 0.0;
     612      layoutCons.weighty = 0.0;
     613      layoutCons.fill = GridBagConstraints.BOTH;
     614      gridbag.setConstraints(headline, layoutCons);
     615      contentPane.add(headline);
     616       
     617      otherTagsTable = new CustomCellEditorTable();
     618      otherTagsData = new TagTableModel(false);
     619      otherTagsData.addColumn("Key");
     620      otherTagsData.addColumn("Value");
     621      otherTagsTable.setModel(otherTagsData);
     622      /*JScrollPane*/ tableSP = new JScrollPane(otherTagsTable);
     623      otherTagsData.addTableModelListener(otherTagsData);
     624/*      JComboBox comboBox = new JComboBox();
     625      comboBox.addItem("");
     626      comboBox.addItem("forward");
     627      comboBox.addItem("backward");
     628      itineraryTable.getColumnModel().getColumn(1)
     629      .setCellEditor(new DefaultCellEditor(comboBox));
     630      itineraryData.addTableModelListener(new ItineraryTableModelListener());*/
     631     
     632      layoutCons.gridx = 0;
     633      layoutCons.gridy = 5;
     634      layoutCons.weightx = 1.0;
     635      layoutCons.weighty = 1.0;
     636      layoutCons.fill = GridBagConstraints.BOTH;
     637      gridbag.setConstraints(tableSP, layoutCons);
     638      /*Dimension*/ preferredSize = tableSP.getPreferredSize();
     639      preferredSize.setSize(tableSP.getPreferredSize().getWidth(), tableSP.getPreferredSize().getHeight()/2.0);
     640      tableSP.setPreferredSize(preferredSize);
     641      contentPane.add(tableSP);
     642     
     643      JButton bAddTag = new JButton("Add a new Tag");
     644      bAddTag.setActionCommand("routePattern.tagAddTag");
     645      bAddTag.addActionListener(this);
     646     
     647      layoutCons.gridx = 0;
     648      layoutCons.gridy = 6;
     649      layoutCons.gridwidth = 1;
     650      layoutCons.weightx = 1.0;
     651      layoutCons.weighty = 0.0;
     652      layoutCons.fill = GridBagConstraints.BOTH;
     653      gridbag.setConstraints(bAddTag, layoutCons);
     654      contentPane.add(bAddTag);
     655       
    366656      //Itinerary Tab
    367657      contentPane = tabItinerary;
     
    375665      itineraryData.addColumn("Role");
    376666      itineraryTable.setModel(itineraryData);
    377       JScrollPane tableSP = new JScrollPane(itineraryTable);
    378       JComboBox comboBox = new JComboBox();
     667      /*JScrollPane*/ tableSP = new JScrollPane(itineraryTable);
     668      /*JComboBox*/ comboBox = new JComboBox();
    379669      comboBox.addItem("");
    380670      comboBox.addItem("forward");
     
    485775      /*JComboBox*/ comboBox = new JComboBox();
    486776      comboBox.addItem("");
    487       comboBox.addItem("forward");
    488       comboBox.addItem("backward");
     777      comboBox.addItem("forward_stop");
     778      comboBox.addItem("backward_stop");
    489779      stoplistTable.getColumnModel().getColumn(1)
    490780          .setCellEditor(new DefaultCellEditor(comboBox));
     
    669959    {
    670960      refreshData();
     961    }
     962    else if ("routePattern.tagAddTag".equals(event.getActionCommand()))
     963    {
     964      Vector< String > rowContent = new Vector< String >();
     965      rowContent.add("");
     966      rowContent.add("");
     967      otherTagsData.addRow(rowContent);
    671968    }
    672969    else if ("routePattern.itineraryShow".equals(event.getActionCommand()))
     
    15361833      tabbedPane.setEnabledAt(3, true);
    15371834      tabbedPane.setEnabledAt(4, true);
     1835     
     1836      //Prepare Tags
     1837      requiredTagsData.readRelation(currentRoute);
     1838      commonTagsData.readRelation(currentRoute);
     1839      otherTagsData.readRelation(currentRoute, tagBlacklist);
    15381840     
    15391841      //Prepare Itinerary
Note: See TracChangeset for help on using the changeset viewer.