Ignore:
Timestamp:
2017-10-03T22:55:07+02:00 (7 years ago)
Author:
donvip
Message:

fix #josm15384

Location:
applications/editors/josm/plugins/ext_tools
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/ext_tools/build.xml

    r32680 r33698  
    55    <property name="commit.message" value="ExtTools: help shortcut paser, rebuild"/>
    66    <!-- enter the *lowest* JOSM version this plugin is currently compatible with -->
    7     <property name="plugin.main.version" value="10580"/>
     7    <property name="plugin.main.version" value="12726"/>
    88
    99    <!-- Configure these properties (replace "..." accordingly).
  • applications/editors/josm/plugins/ext_tools/src/ext_tools/DataSetToCmd.java

    r30737 r33698  
    1414import org.openstreetmap.josm.command.Command;
    1515import org.openstreetmap.josm.data.osm.*;
     16import org.openstreetmap.josm.gui.MainApplication;
    1617
    1718final class DataSetToCmd {
     
    6566        target.mergeFrom(source);
    6667        mergedMap.put(source.getPrimitiveId(), target);
    67         cmds.add(new AddCommand(target));
     68        cmds.add(new AddCommand(MainApplication.getLayerManager().getEditDataSet(), target));
    6869    }
    6970
     
    123124            newNodes.add(targetNode);
    124125        }
    125         cmds.add(new ChangeNodesCommand(target, newNodes));
     126        cmds.add(new ChangeNodesCommand(MainApplication.getLayerManager().getEditDataSet(), target, newNodes));
    126127    }
    127128
     
    163164        Relation newRelation = new Relation(target);
    164165        newRelation.setMembers(newMembers);
    165         cmds.add(new ChangeCommand(target, newRelation));
     166        cmds.add(new ChangeCommand(MainApplication.getLayerManager().getEditDataSet(), target, newRelation));
    166167    }
    167168
  • applications/editors/josm/plugins/ext_tools/src/ext_tools/ExtTool.java

    r32688 r33698  
    2727import org.openstreetmap.josm.data.coor.LatLon;
    2828import org.openstreetmap.josm.data.osm.DataSet;
     29import org.openstreetmap.josm.gui.MainApplication;
    2930import org.openstreetmap.josm.gui.MainMenu;
    3031import org.openstreetmap.josm.gui.MapView;
     
    3536import org.openstreetmap.josm.io.OsmReader;
    3637import org.openstreetmap.josm.tools.GBC;
     38import org.openstreetmap.josm.tools.Logging;
    3739
    3840public class ExtTool {
     
    5759            if (action == null)
    5860                action = new ExtToolAction(this);
    59             menuItem = MainMenu.add(Main.main.menu.toolsMenu, action);
     61            menuItem = MainMenu.add(MainApplication.getMenu().toolsMenu, action);
    6062        } else {
    61             Main.main.menu.toolsMenu.remove(menuItem);
     63            MainApplication.getMenu().toolsMenu.remove(menuItem);
    6264        }
    6365    }
     
    107109    }
    108110
    109     private class ToolProcess {
     111    private static class ToolProcess {
    110112        public Process process;
    111113        public volatile boolean running;
     
    113115
    114116    static double getPPD() {
    115         ProjectionBounds bounds = Main.map.mapView.getProjectionBounds();
    116         return Main.map.mapView.getWidth() /
     117        ProjectionBounds bounds = MainApplication.getMap().mapView.getProjectionBounds();
     118        return MainApplication.getMap().mapView.getWidth() /
    117119                (bounds.maxEast - bounds.minEast);
    118120    }
     
    129131
    130132    private double getTMSZoom() {
    131         if (Main.map == null || Main.map.mapView == null) return 1;
    132         MapView mv = Main.map.mapView;
     133        if (!MainApplication.isDisplayingMapView()) return 1;
     134        MapView mv = MainApplication.getMap().mapView;
    133135        LatLon topLeft = mv.getLatLon(0, 0);
    134136        LatLon botRight = mv.getLatLon(mv.getWidth(), mv.getHeight());
     
    145147
    146148    protected void showErrorMessage(final String message, final String details) {
    147         SwingUtilities.invokeLater(new Runnable() {
    148             public void run() {
    149                 final JPanel p = new JPanel(new GridBagLayout());
    150                 p.add(new JMultilineLabel(message),GBC.eol());
    151                 if (details != null) {
    152                     JTextArea info = new JTextArea(details, 20, 60);
    153                     info.setCaretPosition(0);
    154                     info.setEditable(false);
    155                     p.add(new JScrollPane(info), GBC.eop());
    156                 }
    157                 JOptionPane.showMessageDialog(Main.parent, p, tr("External tool error"), JOptionPane.ERROR_MESSAGE);
    158             }
     149        SwingUtilities.invokeLater(() -> {
     150            final JPanel p = new JPanel(new GridBagLayout());
     151            p.add(new JMultilineLabel(message),GBC.eol());
     152            if (details != null) {
     153                JTextArea info = new JTextArea(details, 20, 60);
     154                info.setCaretPosition(0);
     155                info.setEditable(false);
     156                p.add(new JScrollPane(info), GBC.eop());
     157            }
     158            JOptionPane.showMessageDialog(Main.parent, p, tr("External tool error"), JOptionPane.ERROR_MESSAGE);
    159159        });
    160160    }
    161161
    162162    public void runTool(LatLon pos) {
    163         Main.map.mapView.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
     163        MainApplication.getMap().mapView.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    164164        // parse cmdline and build cmdParams array
    165165        HashMap<String, String> replace = new HashMap<>();
     
    201201            tp.process = builder.start();
    202202        } catch (final IOException e) {
    203             e.printStackTrace();
     203            Logging.error(e);
    204204            synchronized (debugstr) {
    205205                showErrorMessage(
     
    212212
    213213        // redirect child process's stderr to JOSM stderr
    214         new Thread(new Runnable() {
    215             public void run() {
    216                 try {
    217                     byte[] buffer = new byte[1024];
    218                     InputStream errStream = tp.process.getErrorStream();
    219                     int len;
    220                     while ((len = errStream.read(buffer)) > 0) {
    221                         synchronized (debugstr) {
    222                             debugstr.append(new String(buffer, 0, len));
    223                         }
    224                         System.err.write(buffer, 0, len);
    225                     }
    226                 } catch (IOException e) {
    227                 }
     214        new Thread(() -> {
     215            try {
     216                byte[] buffer = new byte[1024];
     217                InputStream errStream = tp.process.getErrorStream();
     218                int len;
     219                while ((len = errStream.read(buffer)) > 0) {
     220                    synchronized (debugstr) {
     221                        debugstr.append(new String(buffer, 0, len));
     222                    }
     223                    System.err.write(buffer, 0, len);
     224                }
     225            } catch (IOException e) {
     226                Logging.error(e);
    228227            }
    229228        }).start();
    230229
    231230        // read stdout stream
    232         Thread osmParseThread = new Thread(new Runnable() {
    233             public void run() {
    234                 try {
    235                     final InputStream inputStream = tp.process.getInputStream();
    236                     final DataSet ds = OsmReader.parseDataSet(inputStream,
    237                             NullProgressMonitor.INSTANCE);
    238                     final List<Command> cmdlist = new DataSetToCmd(ds).getCommandList();
    239                     if (!cmdlist.isEmpty()) {
    240                         SequenceCommand cmd =
    241                                 new SequenceCommand(getName(), cmdlist);
    242                         Main.main.undoRedo.add(cmd);
    243                     }
    244                 } catch (IllegalDataException e) {
    245                     e.printStackTrace();
    246                     if (tp.running) {
    247                         tp.process.destroy();
    248                         synchronized (debugstr) {
    249                             showErrorMessage(
    250                                     tr("Child script have returned invalid data.\n\nstderr contents:"),
    251                                     debugstr.toString());
    252                         }
    253                     }
    254                 } finally {
    255                     synchronized (syncObj) {
    256                         tp.running = false;
    257                         syncObj.notifyAll();
    258                     }
    259                 }
    260             }
    261 
     231        Thread osmParseThread = new Thread(() -> {
     232            try {
     233                final InputStream inputStream = tp.process.getInputStream();
     234                final DataSet ds = OsmReader.parseDataSet(inputStream,
     235                        NullProgressMonitor.INSTANCE);
     236                final List<Command> cmdlist = new DataSetToCmd(ds).getCommandList();
     237                if (!cmdlist.isEmpty()) {
     238                    Main.main.undoRedo.add(new SequenceCommand(getName(), cmdlist));
     239                }
     240            } catch (IllegalDataException e) {
     241                Logging.error(e);
     242                if (tp.running) {
     243                    tp.process.destroy();
     244                    synchronized (debugstr) {
     245                        showErrorMessage(
     246                                tr("Child script have returned invalid data.\n\nstderr contents:"),
     247                                debugstr.toString());
     248                    }
     249                }
     250            } finally {
     251                synchronized (syncObj) {
     252                    tp.running = false;
     253                    syncObj.notifyAll();
     254                }
     255            }
    262256        });
    263257        osmParseThread.start();
     
    267261                syncObj.wait(10000);
    268262            } catch (InterruptedException e) {
     263                Logging.trace(e);
    269264            }
    270265        }
  • applications/editors/josm/plugins/ext_tools/src/ext_tools/ExtToolAction.java

    r27847 r33698  
    77import java.awt.event.MouseEvent;
    88
    9 import org.openstreetmap.josm.Main;
    109import org.openstreetmap.josm.actions.mapmode.MapMode;
     10import org.openstreetmap.josm.gui.MainApplication;
    1111import org.openstreetmap.josm.gui.layer.Layer;
    1212import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     
    2424            Shortcut.registerShortcut(tr("exttool:{0}", tool.name), tr("External Tool: {0}", tool.name),
    2525                KeyEvent.CHAR_UNDEFINED, Shortcut.NONE),
    26             null, ImageProvider.getCursor("crosshair", null));
     26            ImageProvider.getCursor("crosshair", null));
    2727        this.tool = tool;
    2828        setEnabled(true);
     
    3131    @Override
    3232    public void actionPerformed(ActionEvent e) {
    33         if (Main.map == null || Main.map.mapView == null)
     33        if (!MainApplication.isDisplayingMapView())
    3434            return;
    35         oldMapMode = Main.map.mapMode;
     35        oldMapMode = MainApplication.getMap().mapMode;
    3636        super.actionPerformed(e);
    3737    }
     
    4040    public void enterMode() {
    4141        super.enterMode();
    42         Main.map.mapView.addMouseListener(this);
     42        MainApplication.getMap().mapView.addMouseListener(this);
    4343    }
    4444
     
    4646    public void exitMode() {
    4747        super.exitMode();
    48         Main.map.mapView.removeMouseListener(this);
     48        MainApplication.getMap().mapView.removeMouseListener(this);
    4949    }
    5050
    5151    @Override
    5252    public void mouseClicked(MouseEvent e) {
    53         if (Main.map == null || Main.map.mapView == null) {
     53        if (!MainApplication.isDisplayingMapView()) {
    5454            return;
    5555        }
    5656
    57         tool.runTool(Main.map.mapView.getLatLon(e.getX(), e.getY()));
    58         Main.map.selectMapMode(oldMapMode);
     57        tool.runTool(MainApplication.getMap().mapView.getLatLon(e.getX(), e.getY()));
     58        MainApplication.getMap().selectMapMode(oldMapMode);
    5959    }
    6060
  • applications/editors/josm/plugins/ext_tools/src/ext_tools/ToolsInformation.java

    r30738 r33698  
    99import java.util.List;
    1010
    11 import org.openstreetmap.josm.Main;
     11import org.openstreetmap.josm.tools.Logging;
    1212
    1313public class ToolsInformation {
     
    3434            }
    3535        } catch (Exception e) {
    36             Main.warn("Ext_Tools warning: can not load file "+filename);
     36            Logging.warn("Ext_Tools warning: can not load file "+filename);
    3737        }
    3838    }
     
    4343                w.write(tool.serialize());
    4444        } catch (Exception e) {
    45             Main.warn("Ext_Tools warning: can not save file "+filename);
     45            Logging.warn("Ext_Tools warning: can not save file "+filename);
    4646        }
    4747    }
  • applications/editors/josm/plugins/ext_tools/src/ext_tools/preferences/MyToolsPanel.java

    r21930 r33698  
    4242            final JCheckBox cbTool = new JCheckBox(tool.getName());
    4343            cbTool.setSelected(tool.isEnabled());
    44             cbTool.addActionListener(new ActionListener() {
    45                 public void actionPerformed(ActionEvent e) {
    46                     tool.setEnabled(cbTool.isSelected());
    47                 }
    48             });
     44            cbTool.addActionListener(e -> tool.setEnabled(cbTool.isSelected()));
    4945            add(cbTool, gbc);
    5046
  • applications/editors/josm/plugins/ext_tools/src/ext_tools/preferences/ToolsRepositoryPanel.java

    r21930 r33698  
    4141            final JCheckBox cbTool = new JCheckBox(tool.getName());
    4242            cbTool.setSelected(tool.isEnabled());
    43             cbTool.addActionListener(new ActionListener() {
    44                 public void actionPerformed(ActionEvent e) {
    45                     tool.setEnabled(cbTool.isSelected());
    46                 }
    47             });
     43            cbTool.addActionListener(e -> tool.setEnabled(cbTool.isSelected()));
    4844            add(cbTool, gbc);
    4945
Note: See TracChangeset for help on using the changeset viewer.