Changeset 33174 in osm for applications/editors/josm
- Timestamp:
- 2017-03-05T14:36:19+01:00 (8 years ago)
- Location:
- applications/editors/josm/plugins/CommandLine/src/CommandLine
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/CommandLine/src/CommandLine/Command.java
r32779 r33174 154 154 if (obj instanceof String) return true; 155 155 break; 156 default: 157 break; 156 158 } 157 159 return false; -
applications/editors/josm/plugins/CommandLine/src/CommandLine/CommandLine.java
r32780 r33174 80 80 history = new History(100); 81 81 historyField = new DisableShortcutsOnFocusGainedTextField(); 82 textField = new DisableShortcutsOnFocusGainedTextField() { 83 @Override 84 protected void processKeyEvent(KeyEvent e) { 85 if (e.getID() == KeyEvent.KEY_PRESSED) { 86 int code = e.getKeyCode(); 87 if (code == KeyEvent.VK_ENTER) { 88 String commandText = textField.getText().substring(prefix.length()); 89 switch (mode) { 90 case IDLE: 91 if (commandText.isEmpty()) { 92 commandText = history.getLastItem(); 93 } else { 94 history.addItem(commandText); 95 } 96 Command command = findCommand(commandText, true); 97 if (command != null) { 98 startCommand(command); 99 } else { 100 setMode(Mode.IDLE); 101 } 102 break; 103 case SELECTION: 104 if (currentMapFrame.mapMode instanceof WayAction 105 || currentMapFrame.mapMode instanceof NodeAction 106 || currentMapFrame.mapMode instanceof RelationAction 107 || currentMapFrame.mapMode instanceof AnyAction) { 108 Collection<OsmPrimitive> selected = Main.getLayerManager().getEditDataSet().getSelected(); 109 if (selected.size() > 0) 110 loadParameter(selected, true); 111 } else { 112 loadParameter(commandText, currentCommand.parameters.get(currentCommand.currentParameterNum).maxInstances == 1); 113 } 114 break; 115 case ADJUSTMENT: 116 break; 117 } 118 e.consume(); 119 } else if (code == KeyEvent.VK_UP) { 120 textField.setText(prefix + history.getPrevItem()); 121 e.consume(); 122 } else if (code == KeyEvent.VK_DOWN) { 123 textField.setText(prefix + history.getNextItem()); 124 e.consume(); 125 } else if (code == KeyEvent.VK_BACK_SPACE || code == KeyEvent.VK_LEFT) { 126 if (textField.getCaretPosition() <= prefix.length()) 127 e.consume(); 128 } else if (code == KeyEvent.VK_HOME) { 129 setCaretPosition(prefix.length()); 130 e.consume(); 131 } else if (code == KeyEvent.VK_ESCAPE) { 132 if (textField.getText().length() == prefix.length() && mode == Mode.IDLE) 133 deactivate(); 134 else 135 endInput(); 136 e.consume(); 137 } else if (code == KeyEvent.VK_DELETE || code == KeyEvent.VK_RIGHT || code == KeyEvent.VK_END) { 138 } else { 139 e.consume(); 140 } 141 if (textField.getCaretPosition() < prefix.length() || 142 (textField.getSelectionStart() < prefix.length() && textField.getSelectionStart() > 0)) 143 e.consume(); 144 } 145 if (e.getID() == KeyEvent.KEY_TYPED) 146 if (textField.getCaretPosition() < prefix.length() || 147 (textField.getSelectionStart() < prefix.length() && textField.getSelectionStart() > 0)) 148 e.consume(); 149 super.processKeyEvent(e); 150 if (textField.getText().length() < prefix.length()) { // Safe 151 setMode(mode); 152 } 153 if (e.getID() == KeyEvent.KEY_TYPED) { 154 if (e.getKeyChar() > 'A' && e.getKeyChar() < 'z') { 155 Command command = findCommand(textField.getText().substring(prefix.length()), false); 156 if (command != null) { 157 int currentPos = textField.getSelectionStart() == 0 ? textField.getCaretPosition() : textField.getSelectionStart(); 158 textField.setText(prefix + command.name); 159 textField.setCaretPosition(currentPos); 160 textField.select(currentPos, prefix.length() + command.name.length()); 161 } 162 } 163 } 164 } 165 166 @Override 167 protected void processMouseEvent(MouseEvent e) { 168 super.processMouseEvent(e); 169 if (e.getButton() == MouseEvent.BUTTON1 && e.getID() == MouseEvent.MOUSE_RELEASED) { 170 if (textField.getSelectionStart() > 0 && textField.getSelectionStart() < prefix.length()) 171 textField.setSelectionStart(prefix.length()); 172 else if (textField.getCaretPosition() < prefix.length()) 173 textField.setCaretPosition(prefix.length()); 174 } 175 } 176 }; 82 textField = new CommandTextField(); 177 83 178 84 if (Main.main.menu != null) { 179 commandMenu = Main.main.menu.addMenu("Commands", tr("Commands"), KeyEvent.VK_O, Main.main.menu.getDefaultMenuPos(), ht("/Plugin/CommandLine")); 85 commandMenu = Main.main.menu.addMenu("Commands", tr("Commands"), KeyEvent.VK_O, 86 Main.main.menu.getDefaultMenuPos(), ht("/Plugin/CommandLine")); 180 87 MainMenu.add(commandMenu, new CommandLineAction(this)); 181 88 } … … 366 273 } 367 274 } 368 ImageryInfo info = ((ImageryLayer) layer).getInfo(); 369 String url = info.getUrl(); 370 String itype = info.getImageryType().getTypeString(); 371 loadParameter((url.equals("") ? itype : url), true); 275 if (layer != null) { 276 ImageryInfo info = ((ImageryLayer) layer).getInfo(); 277 String url = info.getUrl(); 278 loadParameter(url.isEmpty() ? info.getImageryType().getTypeString() : url, true); 279 } 372 280 action = new DummyAction(currentMapFrame, this); 373 281 break; … … 458 366 } 459 367 368 private final class CommandTextField extends DisableShortcutsOnFocusGainedTextField { 369 @Override 370 protected void processKeyEvent(KeyEvent e) { 371 if (e.getID() == KeyEvent.KEY_PRESSED) { 372 int code = e.getKeyCode(); 373 if (code == KeyEvent.VK_ENTER) { 374 String commandText = textField.getText().substring(prefix.length()); 375 switch (mode) { 376 case IDLE: 377 if (commandText.isEmpty()) { 378 commandText = history.getLastItem(); 379 } else { 380 history.addItem(commandText); 381 } 382 Command command = findCommand(commandText, true); 383 if (command != null) { 384 startCommand(command); 385 } else { 386 setMode(Mode.IDLE); 387 } 388 break; 389 case SELECTION: 390 if (currentMapFrame.mapMode instanceof WayAction 391 || currentMapFrame.mapMode instanceof NodeAction 392 || currentMapFrame.mapMode instanceof RelationAction 393 || currentMapFrame.mapMode instanceof AnyAction) { 394 Collection<OsmPrimitive> selected = Main.getLayerManager().getEditDataSet().getSelected(); 395 if (!selected.isEmpty()) 396 loadParameter(selected, true); 397 } else { 398 loadParameter(commandText, currentCommand.parameters.get(currentCommand.currentParameterNum).maxInstances == 1); 399 } 400 break; 401 case ADJUSTMENT: 402 default: 403 break; 404 } 405 e.consume(); 406 } else if (code == KeyEvent.VK_UP) { 407 textField.setText(prefix + history.getPrevItem()); 408 e.consume(); 409 } else if (code == KeyEvent.VK_DOWN) { 410 textField.setText(prefix + history.getNextItem()); 411 e.consume(); 412 } else if (code == KeyEvent.VK_BACK_SPACE || code == KeyEvent.VK_LEFT) { 413 if (textField.getCaretPosition() <= prefix.length()) 414 e.consume(); 415 } else if (code == KeyEvent.VK_HOME) { 416 setCaretPosition(prefix.length()); 417 e.consume(); 418 } else if (code == KeyEvent.VK_ESCAPE) { 419 if (textField.getText().length() == prefix.length() && mode == Mode.IDLE) 420 deactivate(); 421 else 422 endInput(); 423 e.consume(); 424 } else if (code == KeyEvent.VK_DELETE || code == KeyEvent.VK_RIGHT || code == KeyEvent.VK_END) { 425 } else { 426 e.consume(); 427 } 428 if (textField.getCaretPosition() < prefix.length() || 429 (textField.getSelectionStart() < prefix.length() && textField.getSelectionStart() > 0)) 430 e.consume(); 431 } 432 if (e.getID() == KeyEvent.KEY_TYPED) 433 if (textField.getCaretPosition() < prefix.length() || 434 (textField.getSelectionStart() < prefix.length() && textField.getSelectionStart() > 0)) 435 e.consume(); 436 super.processKeyEvent(e); 437 if (textField.getText().length() < prefix.length()) { // Safe 438 setMode(mode); 439 } 440 if (e.getID() == KeyEvent.KEY_TYPED) { 441 if (e.getKeyChar() > 'A' && e.getKeyChar() < 'z') { 442 Command command = findCommand(textField.getText().substring(prefix.length()), false); 443 if (command != null) { 444 int currentPos = textField.getSelectionStart() == 0 ? textField.getCaretPosition() : textField.getSelectionStart(); 445 textField.setText(prefix + command.name); 446 textField.setCaretPosition(currentPos); 447 textField.select(currentPos, prefix.length() + command.name.length()); 448 } 449 } 450 } 451 } 452 453 @Override 454 protected void processMouseEvent(MouseEvent e) { 455 super.processMouseEvent(e); 456 if (e.getButton() == MouseEvent.BUTTON1 && e.getID() == MouseEvent.MOUSE_RELEASED) { 457 if (textField.getSelectionStart() > 0 && textField.getSelectionStart() < prefix.length()) 458 textField.setSelectionStart(prefix.length()); 459 else if (textField.getCaretPosition() < prefix.length()) 460 textField.setCaretPosition(prefix.length()); 461 } 462 } 463 } 464 460 465 private static class ToolProcess { 461 466 public Process process; … … 508 513 509 514 // redirect child process's stderr to JOSM stderr 510 new Thread(new Runnable() { 511 @Override 512 public void run() { 513 try { 514 byte[] buffer = new byte[1024]; 515 InputStream errStream = tp.process.getErrorStream(); 516 int len; 517 while ((len = errStream.read(buffer)) > 0) { 518 synchronized (debugstr) { 519 debugstr.append(new String(buffer, 0, len, StandardCharsets.UTF_8)); 520 } 521 System.err.write(buffer, 0, len); 522 } 523 } catch (IOException e) { 524 Main.warn(e); 525 } 515 new Thread(() -> { 516 try { 517 byte[] buffer = new byte[1024]; 518 InputStream errStream = tp.process.getErrorStream(); 519 int len; 520 while ((len = errStream.read(buffer)) > 0) { 521 synchronized (debugstr) { 522 debugstr.append(new String(buffer, 0, len, StandardCharsets.UTF_8)); 523 } 524 System.err.write(buffer, 0, len); 525 } 526 } catch (IOException e) { 527 Main.warn(e); 526 528 } 527 529 }).start(); 528 530 529 531 // Write stdin stream 530 Thread osmWriteThread = new Thread(new Runnable() { 531 @Override 532 public void run() { 533 BBox bbox = null; 534 final OutputStream outputStream = tp.process.getOutputStream(); 535 PrintWriter printWriter = null; 536 try { 537 printWriter = new PrintWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)); 538 } catch (Exception e) { 539 Main.error(e); 540 } 541 final OsmWriter osmWriter = OsmWriterFactory.createOsmWriter(printWriter, true, null); 542 Collection<OsmPrimitive> refObjects = currentCommand.getDepsObjects(); 543 Collection<OsmPrimitive> pObjects; 532 Thread osmWriteThread = new Thread(() -> { 533 BBox bbox = null; 534 final OutputStream outputStream = tp.process.getOutputStream(); 535 PrintWriter printWriter = null; 536 try { 537 printWriter = new PrintWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)); 538 } catch (Exception e1) { 539 Main.error(e1); 540 } 541 final OsmWriter osmWriter = OsmWriterFactory.createOsmWriter(printWriter, true, null); 542 Collection<OsmPrimitive> refObjects = currentCommand.getDepsObjects(); 543 Collection<OsmPrimitive> pObjects; 544 osmWriter.header(); 545 Collection<OsmPrimitive> contents = new ArrayList<>(); 546 for (OsmPrimitive primitive1 : refObjects) { 547 contents.add(primitive1); 548 if (bbox == null) 549 bbox = new BBox(primitive1.getBBox()); 550 else 551 bbox.addPrimitive(primitive1, 0.0); 552 } 553 osmWriter.writeNodes(new SubclassFilteredCollection<OsmPrimitive, Node>(contents, Node.class::isInstance)); 554 osmWriter.writeWays(new SubclassFilteredCollection<OsmPrimitive, Way>(contents, Way.class::isInstance)); 555 osmWriter.writeRelations(new SubclassFilteredCollection<OsmPrimitive, Relation>(contents, Relation.class::isInstance)); 556 osmWriter.footer(); 557 osmWriter.flush(); 558 559 for (Parameter parameter : parameters) { 560 if (!parameter.isOsm()) 561 continue; 562 contents = new ArrayList<>(); 544 563 osmWriter.header(); 545 Collection<OsmPrimitive> contents = new ArrayList<>();546 for (OsmPrimitive primitive : refObjects) {547 contents.add(primitive );564 pObjects = parameter.getParameterObjects(); 565 for (OsmPrimitive primitive2 : pObjects) { 566 contents.add(primitive2); 548 567 if (bbox == null) 549 bbox = new BBox(primitive .getBBox());568 bbox = new BBox(primitive2.getBBox()); 550 569 else 551 bbox.addPrimitive(primitive , 0.0);570 bbox.addPrimitive(primitive2, 0.0); 552 571 } 553 572 osmWriter.writeNodes(new SubclassFilteredCollection<OsmPrimitive, Node>(contents, Node.class::isInstance)); … … 556 575 osmWriter.footer(); 557 576 osmWriter.flush(); 558 559 for (Parameter parameter : parameters) { 560 if (!parameter.isOsm()) 561 continue; 562 contents = new ArrayList<>(); 563 osmWriter.header(); 564 pObjects = parameter.getParameterObjects(); 565 for (OsmPrimitive primitive : pObjects) { 566 contents.add(primitive); 567 if (bbox == null) 568 bbox = new BBox(primitive.getBBox()); 569 else 570 bbox.addPrimitive(primitive, 0.0); 571 } 572 osmWriter.writeNodes(new SubclassFilteredCollection<OsmPrimitive, Node>(contents, Node.class::isInstance)); 573 osmWriter.writeWays(new SubclassFilteredCollection<OsmPrimitive, Way>(contents, Way.class::isInstance)); 574 osmWriter.writeRelations(new SubclassFilteredCollection<OsmPrimitive, Relation>(contents, Relation.class::isInstance)); 575 osmWriter.footer(); 576 osmWriter.flush(); 577 } 578 579 if (tracks) { 580 try (GpxWriter gpxWriter = new GpxWriter(printWriter)) { 581 GpxFilter gpxFilter = new GpxFilter(); 582 gpxFilter.initBboxFilter(bbox); 583 List<GpxLayer> gpxLayers = Main.getLayerManager().getLayersOfType(GpxLayer.class); 584 for (GpxLayer gpxLayer : gpxLayers) { 585 gpxFilter.addGpxData(gpxLayer.data); 586 } 587 gpxWriter.write(gpxFilter.getGpxData()); 588 } catch (IOException e) { 589 Main.warn(e); 590 } 591 } 592 Utils.close(osmWriter); 593 synchronized (syncObj) { 594 if (currentCommand.asynchronous) { 595 tp.running = false; 596 syncObj.notifyAll(); 597 } 577 } 578 579 if (tracks) { 580 try (GpxWriter gpxWriter = new GpxWriter(printWriter)) { 581 GpxFilter gpxFilter = new GpxFilter(); 582 gpxFilter.initBboxFilter(bbox); 583 List<GpxLayer> gpxLayers = Main.getLayerManager().getLayersOfType(GpxLayer.class); 584 for (GpxLayer gpxLayer : gpxLayers) { 585 gpxFilter.addGpxData(gpxLayer.data); 586 } 587 gpxWriter.write(gpxFilter.getGpxData()); 588 } catch (IOException e2) { 589 Main.warn(e2); 590 } 591 } 592 Utils.close(osmWriter); 593 synchronized (syncObj) { 594 if (currentCommand.asynchronous) { 595 tp.running = false; 596 syncObj.notifyAll(); 598 597 } 599 598 } -
applications/editors/josm/plugins/CommandLine/src/CommandLine/Parameter.java
r32779 r33174 68 68 out = String.valueOf(value); 69 69 break; 70 default: 71 break; 70 72 } 71 73 return out;
Note:
See TracChangeset
for help on using the changeset viewer.