Ejemplo n.º 1
0
    @Override
    public void actionPerformed(ActionEvent arg0) {
      SimpleDateFormat dateFormat =
          (SimpleDateFormat) DateUtils.getDateTimeFormat(DateFormat.SHORT, DateFormat.MEDIUM);

      panel = new JPanel(new BorderLayout());
      panel.add(
          new JLabel(
              tr(
                  "<html>Take a photo of your GPS receiver while it displays the time.<br>"
                      + "Display that photo here.<br>"
                      + "And then, simply capture the time you read on the photo and select a timezone<hr></html>")),
          BorderLayout.NORTH);

      imgDisp = new ImageDisplay();
      imgDisp.setPreferredSize(new Dimension(300, 225));
      panel.add(imgDisp, BorderLayout.CENTER);

      JPanel panelTf = new JPanel(new GridBagLayout());

      GridBagConstraints gc = new GridBagConstraints();
      gc.gridx = gc.gridy = 0;
      gc.gridwidth = gc.gridheight = 1;
      gc.weightx = gc.weighty = 0.0;
      gc.fill = GridBagConstraints.NONE;
      gc.anchor = GridBagConstraints.WEST;
      panelTf.add(new JLabel(tr("Photo time (from exif):")), gc);

      lbExifTime = new JLabel();
      gc.gridx = 1;
      gc.weightx = 1.0;
      gc.fill = GridBagConstraints.HORIZONTAL;
      gc.gridwidth = 2;
      panelTf.add(lbExifTime, gc);

      gc.gridx = 0;
      gc.gridy = 1;
      gc.gridwidth = gc.gridheight = 1;
      gc.weightx = gc.weighty = 0.0;
      gc.fill = GridBagConstraints.NONE;
      gc.anchor = GridBagConstraints.WEST;
      panelTf.add(new JLabel(tr("Gps time (read from the above photo): ")), gc);

      tfGpsTime = new JosmTextField(12);
      tfGpsTime.setEnabled(false);
      tfGpsTime.setMinimumSize(new Dimension(155, tfGpsTime.getMinimumSize().height));
      gc.gridx = 1;
      gc.weightx = 1.0;
      gc.fill = GridBagConstraints.HORIZONTAL;
      panelTf.add(tfGpsTime, gc);

      gc.gridx = 2;
      gc.weightx = 0.2;
      panelTf.add(new JLabel(" [" + dateFormat.toLocalizedPattern() + ']'), gc);

      gc.gridx = 0;
      gc.gridy = 2;
      gc.gridwidth = gc.gridheight = 1;
      gc.weightx = gc.weighty = 0.0;
      gc.fill = GridBagConstraints.NONE;
      gc.anchor = GridBagConstraints.WEST;
      panelTf.add(new JLabel(tr("I am in the timezone of: ")), gc);

      String[] tmp = TimeZone.getAvailableIDs();
      List<String> vtTimezones = new ArrayList<>(tmp.length);

      for (String tzStr : tmp) {
        TimeZone tz = TimeZone.getTimeZone(tzStr);

        String tzDesc =
            new StringBuilder(tzStr)
                .append(" (")
                .append(formatTimezone(tz.getRawOffset() / 3600000.0))
                .append(')')
                .toString();
        vtTimezones.add(tzDesc);
      }

      Collections.sort(vtTimezones);

      cbTimezones = new JosmComboBox<>(vtTimezones.toArray(new String[0]));

      String tzId = Main.pref.get("geoimage.timezoneid", "");
      TimeZone defaultTz;
      if (tzId.isEmpty()) {
        defaultTz = TimeZone.getDefault();
      } else {
        defaultTz = TimeZone.getTimeZone(tzId);
      }

      cbTimezones.setSelectedItem(
          new StringBuilder(defaultTz.getID())
              .append(" (")
              .append(formatTimezone(defaultTz.getRawOffset() / 3600000.0))
              .append(')')
              .toString());

      gc.gridx = 1;
      gc.weightx = 1.0;
      gc.gridwidth = 2;
      gc.fill = GridBagConstraints.HORIZONTAL;
      panelTf.add(cbTimezones, gc);

      panel.add(panelTf, BorderLayout.SOUTH);

      JPanel panelLst = new JPanel(new BorderLayout());

      imgList =
          new JList<>(
              new AbstractListModel<String>() {
                @Override
                public String getElementAt(int i) {
                  return yLayer.data.get(i).getFile().getName();
                }

                @Override
                public int getSize() {
                  return yLayer.data != null ? yLayer.data.size() : 0;
                }
              });
      imgList.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      imgList
          .getSelectionModel()
          .addListSelectionListener(
              new ListSelectionListener() {

                @Override
                public void valueChanged(ListSelectionEvent arg0) {
                  int index = imgList.getSelectedIndex();
                  Integer orientation = null;
                  try {
                    orientation = ExifReader.readOrientation(yLayer.data.get(index).getFile());
                  } catch (Exception e) {
                    Main.warn(e);
                  }
                  imgDisp.setImage(yLayer.data.get(index).getFile(), orientation);
                  Date date = yLayer.data.get(index).getExifTime();
                  if (date != null) {
                    DateFormat df =
                        DateUtils.getDateTimeFormat(DateFormat.SHORT, DateFormat.MEDIUM);
                    lbExifTime.setText(df.format(date));
                    tfGpsTime.setText(df.format(date));
                    tfGpsTime.setCaretPosition(tfGpsTime.getText().length());
                    tfGpsTime.setEnabled(true);
                    tfGpsTime.requestFocus();
                  } else {
                    lbExifTime.setText(tr("No date"));
                    tfGpsTime.setText("");
                    tfGpsTime.setEnabled(false);
                  }
                }
              });
      panelLst.add(new JScrollPane(imgList), BorderLayout.CENTER);

      JButton openButton = new JButton(tr("Open another photo"));
      openButton.addActionListener(
          new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
              AbstractFileChooser fc =
                  DiskAccessAction.createAndOpenFileChooser(
                      true,
                      false,
                      null,
                      JpgImporter.FILE_FILTER_WITH_FOLDERS,
                      JFileChooser.FILES_ONLY,
                      "geoimage.lastdirectory");
              if (fc == null) return;
              File sel = fc.getSelectedFile();

              Integer orientation = null;
              try {
                orientation = ExifReader.readOrientation(sel);
              } catch (Exception e) {
                Main.warn(e);
              }
              imgDisp.setImage(sel, orientation);

              Date date = null;
              try {
                date = ExifReader.readTime(sel);
              } catch (Exception e) {
                Main.warn(e);
              }
              if (date != null) {
                lbExifTime.setText(
                    DateUtils.getDateTimeFormat(DateFormat.SHORT, DateFormat.MEDIUM).format(date));
                tfGpsTime.setText(DateUtils.getDateFormat(DateFormat.SHORT).format(date) + ' ');
                tfGpsTime.setEnabled(true);
              } else {
                lbExifTime.setText(tr("No date"));
                tfGpsTime.setText("");
                tfGpsTime.setEnabled(false);
              }
            }
          });
      panelLst.add(openButton, BorderLayout.PAGE_END);

      panel.add(panelLst, BorderLayout.LINE_START);

      boolean isOk = false;
      while (!isOk) {
        int answer =
            JOptionPane.showConfirmDialog(
                Main.parent,
                panel,
                tr("Synchronize time from a photo of the GPS receiver"),
                JOptionPane.OK_CANCEL_OPTION,
                JOptionPane.QUESTION_MESSAGE);
        if (answer == JOptionPane.CANCEL_OPTION) return;

        long delta;

        try {
          delta =
              dateFormat.parse(lbExifTime.getText()).getTime()
                  - dateFormat.parse(tfGpsTime.getText()).getTime();
        } catch (ParseException e) {
          JOptionPane.showMessageDialog(
              Main.parent,
              tr("Error while parsing the date.\n" + "Please use the requested format"),
              tr("Invalid date"),
              JOptionPane.ERROR_MESSAGE);
          continue;
        }

        String selectedTz = (String) cbTimezones.getSelectedItem();
        int pos = selectedTz.lastIndexOf('(');
        tzId = selectedTz.substring(0, pos - 1);
        String tzValue = selectedTz.substring(pos + 1, selectedTz.length() - 1);

        Main.pref.put("geoimage.timezoneid", tzId);
        tfOffset.setText(Long.toString(delta / 1000));
        tfTimezone.setText(tzValue);

        isOk = true;
      }
      statusBarUpdater.updateStatusBar();
      yLayer.updateBufferAndRepaint();
    }