Index: DirectUpload/src/org/openstreetmap/josm/plugins/DirectUpload/UploadDataGui.java
===================================================================
--- DirectUpload/src/org/openstreetmap/josm/plugins/DirectUpload/UploadDataGui.java	(revision 13357)
+++ DirectUpload/src/org/openstreetmap/josm/plugins/DirectUpload/UploadDataGui.java	(working copy)
@@ -7,9 +7,14 @@
  */
 
 package org.openstreetmap.josm.plugins.DirectUpload;
-import java.awt.event.ItemEvent;
-import static org.openstreetmap.josm.tools.I18n.tr;
 
+import java.awt.BorderLayout;
+import java.awt.Container;
+import java.awt.Dimension;
+import java.awt.event.ItemEvent;
+import java.awt.GridBagLayout;
+import java.awt.GridBagConstraints;
+import java.awt.Toolkit;
 import java.io.BufferedOutputStream;
 import java.io.DataOutputStream;
 import java.io.IOException;
@@ -22,327 +27,262 @@
 import java.util.Date;
 import java.util.logging.Level;
 import java.util.logging.Logger;
+
+import javax.swing.BorderFactory;
+import javax.swing.Box;
+import javax.swing.JButton;
+import javax.swing.JCheckBox;
+import javax.swing.JLabel;
 import javax.swing.JOptionPane;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.JTextArea;
 import javax.swing.JTextField;
-import java.awt.Container;
-import java.awt.GridBagLayout;
-import java.awt.GridBagConstraints;
+import javax.swing.UIManager;
+
 import org.openstreetmap.josm.Main;
 import org.openstreetmap.josm.data.gpx.GpxData;
 import org.openstreetmap.josm.io.GpxWriter;
 import org.openstreetmap.josm.gui.layer.Layer;
 import org.openstreetmap.josm.gui.layer.GpxLayer;
 import org.openstreetmap.josm.gui.MapView;
+import org.openstreetmap.josm.tools.Base64;
+import org.openstreetmap.josm.tools.GBC;
+import static org.openstreetmap.josm.tools.I18n.tr;
 
 /**
  *
  * @author  subhodip
  */
 public class UploadDataGui extends javax.swing.JFrame {
+    private JTextArea OutputDisplay = new JTextArea();
+    private JTextField descriptionField = new JTextField();
+    private JTextField tagsField = new JTextField();
+    private JCheckBox publicCheckbox = new JCheckBox();
+    
+    public static final String API_VERSION = "0.5";
+    private static final String BOUNDARY = "----------------------------d10f7aa230e8";
+    private static final String LINE_END = "\r\n";
+    
+    private String datename = new SimpleDateFormat("yyMMddHHmmss").format(new Date());
 
- String tagging;
- String Descriptionfield;
- String urlDescription;
- String urlTags;
- String UserName;
- String PassWord;
- //GpxData gpxData;
-
-        public static final String API_VERSION = "0.5";
-        private static final String BASE64_ENC = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-        private static final String BOUNDARY = "----------------------------d10f7aa230e8";
-        private static final String LINE_END = "\r\n";
-
-        boolean dfield;
-        boolean taggy;
-        boolean choosy;
-        boolean user;
-        boolean pass;
-        boolean publicheck;
-
-        DateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");
-        Date date = new Date();
-        String datename = dateFormat.format(date);
-
     /** Creates new form UploadDataGui */
     public UploadDataGui() {
+        setTitle(tr("Upload Traces"));
         initComponents();
     }
-
-
-    /** This method is called from within the constructor to
-     * initialize the form.
-     * WARNING: Do NOT modify this code. The content of this method is
-     * always regenerated by the Form Editor.
-     */
-    @SuppressWarnings("unchecked")
-    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
+    
     private void initComponents() {
-
-        jScrollPane1 = new javax.swing.JScrollPane();
-        OutputDisplay = new javax.swing.JTextArea();
-        OkButton = new javax.swing.JButton();
-        CancelButton = new javax.swing.JButton();
-        jLabel2 = new javax.swing.JLabel();
-        PublicTrace = new javax.swing.JCheckBox();
-        jLabel1 = new javax.swing.JLabel();
-        descriptionfield = new javax.swing.JTextField();
-        jLabel3 = new javax.swing.JLabel();
-        tagfield = new javax.swing.JTextField();
-
         setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
         setAlwaysOnTop(true);
-
-        OutputDisplay.setColumns(20);
-        OutputDisplay.setRows(5);
+        setPreferredSize(new Dimension(350,200));
+        
+        // Display Center Screen
+        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
+        Dimension labelSize = getPreferredSize();
+        setLocation(screenSize.width / 2 - (labelSize.width / 2), screenSize.height / 2 - (labelSize.height / 2));
+        
+        OutputDisplay.setBackground(UIManager.getColor("Panel.background"));
+        OutputDisplay.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
+        OutputDisplay.setEditable(false);
+        OutputDisplay.setFont(new JLabel().getFont()); 
+        OutputDisplay.setLineWrap(true);
+        OutputDisplay.setWrapStyleWord(true);
+        
+        JScrollPane jScrollPane1 = new JScrollPane();
         jScrollPane1.setViewportView(OutputDisplay);
-
-        OkButton.setText("Ok");
+        
+        JButton OkButton = new JButton(tr("Upload GPX track"));
         OkButton.addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent evt) {
                 OkButtonActionPerformed(evt);
             }
         });
-
-        CancelButton.setText("Cancel");
+        
+        JButton CancelButton = new JButton(tr("Cancel"));
         CancelButton.addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent evt) {
                 CancelButtonActionPerformed(evt);
             }
         });
+        
+        JLabel directUploadLabel = new JLabel(tr("Direct Upload to OpenStreetMap"));
+        
+        publicCheckbox.setText(tr("Public"));
+        publicCheckbox.setToolTipText(tr("Selected makes your trace public in openstreetmap.org"));
+        
+        JLabel descriptionLabel = new JLabel(tr("Description"));
+        descriptionField.setToolTipText("Please enter Description about your trace.");
 
-        jLabel2.setFont(new java.awt.Font("DejaVu Sans", 1, 14));
-        jLabel2.setText("Direct Upload to OpenStreetMap");
+        JLabel tagsLabel = new JLabel(tr("Tags"));
+        tagsField.setToolTipText("Please enter tags about your trace.");
+      
+        JPanel p = new JPanel();        
+        p.setLayout(new GridBagLayout());
 
-        PublicTrace.setText("Public");
-        PublicTrace.setToolTipText("Selected makes your trace public in openstreetmap.org");
-        PublicTrace.addItemListener(new java.awt.event.ItemListener() {
-            public void itemStateChanged(java.awt.event.ItemEvent evt) {
-                PublicTraceItemStateChanged(evt);
-            }
-        });
+        p.add(OutputDisplay, GBC.eol().fill());
 
-        jLabel1.setText("Description");
+        p.add(tagsLabel, GBC.std().insets(0,10,0,0));
+        p.add(tagsField, GBC.eol().fill(GBC.HORIZONTAL));
+        
+        p.add(descriptionLabel, GBC.std().insets(0,10,0,0));
+        p.add(descriptionField, GBC.eol().fill(GBC.HORIZONTAL));
 
-        descriptionfield.setToolTipText("Please enter Description about your trace.");
-        descriptionfield.addFocusListener(new java.awt.event.FocusAdapter() {
-            public void focusLost(java.awt.event.FocusEvent evt) {
-                descriptionfieldFocusLost(evt);
-            }
-        });
+        p.add(publicCheckbox, GBC.eol());
 
-        jLabel3.setText("Tags");
-
-        tagfield.setToolTipText("Please enter tags about your trace.");
-        tagfield.addFocusListener(new java.awt.event.FocusAdapter() {
-            public void focusLost(java.awt.event.FocusEvent evt) {
-                tagfieldFocusLost(evt);
-            }
-        });
-
-
-
-    Container contentPane = getContentPane();
-    contentPane.setLayout(new GridBagLayout());
-    GridBagConstraints c = new GridBagConstraints();
-
-    c.gridx = 0;
-    c.gridy = 0;
-    c.gridwidth = 2;
-    c.fill = 1;
-    contentPane.add(OutputDisplay, c);
-
-    c.gridwidth = 1;
-    c.gridy = 1;
-    contentPane.add(jLabel3, c);
-    c.gridx = 1;
-    contentPane.add(tagfield, c);
-
-    c.gridy = 2; c.gridx = 0;
-    contentPane.add(jLabel1, c);
-    c.gridx = 1;
-    contentPane.add(descriptionfield, c);
-
-    c.gridy = 3; c.gridx = 0;
-    contentPane.add(jLabel2, c);
-    c.gridx = 1;
-    contentPane.add(PublicTrace, c);
-
-    c.gridy = 4; c.gridx = 0;
-    contentPane.add(CancelButton, c);
-    c.gridx = 1;
-    contentPane.add(OkButton, c);
-
+        p.add(CancelButton, GBC.std());
+        p.add(OkButton, GBC.eol().fill(GBC.HORIZONTAL));
+        
+        getContentPane().setLayout(new GridBagLayout());
+        getContentPane().add(p, GBC.eol().insets(10,10,10,10).fill());
+        
         pack();
-    }// </editor-fold>//GEN-END:initComponents
-
-
-
-public void upload(String username, String password, String Descriptionfield, String tagging , GpxData gpxData ) throws IOException {
-        OutputDisplay.setText("Starting to upload selected file to openstreetmap.org");
-
-    try {
-
-        urlDescription = Descriptionfield.replaceAll("\\.;&?,/","_");
-        if (urlDescription == null || urlDescription.length() == 0) {
-             OutputDisplay.setText("No description provided .Please provide some description . For the time being ignore the exception error ");
-
+        
+        // If no GPX layer is selected, select one for the user if there is only one GPX layer
+        if(Main.map != null && Main.map.mapView != null) {
+            MapView mv=Main.map.mapView;
+            if(!(mv.getActiveLayer() instanceof GpxLayer)) {                
+                Layer lastLayer=null;
+                int layerCount=0;
+                for (Layer l : mv.getAllLayers()) {
+                    if(l instanceof GpxLayer) {
+                        lastLayer = l;
+                        layerCount++;
+                    }
+                }
+                if(layerCount == 1) mv.setActiveLayer(lastLayer);
+            }
+            
+            if(mv.getActiveLayer() instanceof GpxLayer) {
+                GpxData data=((GpxLayer)Main.map.mapView.getActiveLayer()).data;
+                descriptionField.setText(data.storageFile.getName().replaceAll("[&?/\\\\]"," ").replaceAll("(\\.[^.]*)$",""));
+            }
         }
-           urlTags = tagging.replaceAll("\\\\.;&?,/","_");
+        
+        boolean x=checkForGPXLayer();
+    }
 
+    public void upload(String username, String password, String description, String tags, Boolean isPublic, GpxData gpxData) throws IOException {
+        if(checkForErrors(username, password, description, gpxData))
+            return;
+        
+        description = description.replaceAll("[&?/\\\\]"," ");        
+        tags = tags.replaceAll("[&?/\\\\.,;]"," ");
+    
+        OutputDisplay.setText(tr("Starting to upload selected file to openstreetmap.org"));
 
-        URL url = new URL("http://www.openstreetmap.org/api/" + API_VERSION + "/gpx/create");
-            System.err.println("url: " + url);
-            OutputDisplay.setText("Uploading in Progress");
-        HttpURLConnection connect = (HttpURLConnection) url.openConnection();
-        connect.setConnectTimeout(15000);
-        connect.setRequestMethod("POST");
-        connect.setDoOutput(true);
-        connect.addRequestProperty("Authorization", "Basic "+encodeBase64(username+":"+password));
-        connect.addRequestProperty("Content-Type", "multipart/form-data; boundary="+BOUNDARY);
-        connect.addRequestProperty("Connection", "close"); // counterpart of keep-alive
-        connect.addRequestProperty("Expect", "");
-        connect.connect();
-        DataOutputStream out  = new DataOutputStream(new BufferedOutputStream(connect.getOutputStream()));
-        writeContentDispositionGpxData(out, "file", gpxData);
-        writeContentDisposition(out, "description", urlDescription);
-        writeContentDisposition(out, "tags", urlTags);
-           if(publicheck) {
-        System.out.println(publicheck);
-        writeContentDisposition(out, "public", "1");
-           } else {
-        writeContentDisposition(out, "public", "0");
-           }
-        out.writeBytes("--" + BOUNDARY + "--" + LINE_END);
-        out.flush();
+        try {
+            URL url = new URL("http://www.openstreetmap.org/api/" + API_VERSION + "/gpx/create");
+            //System.err.println("url: " + url);
+            //OutputDisplay.setText("Uploading in Progress");
+            HttpURLConnection connect = (HttpURLConnection) url.openConnection();
+            connect.setConnectTimeout(15000);
+            connect.setRequestMethod("POST");
+            connect.setDoOutput(true);
+            connect.addRequestProperty("Authorization", "Basic " + Base64.encode(username + ":" + password));
+            connect.addRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
+            connect.addRequestProperty("Connection", "close"); // counterpart of keep-alive
+            connect.addRequestProperty("Expect", "");
+            connect.connect();
+            DataOutputStream out  = new DataOutputStream(new BufferedOutputStream(connect.getOutputStream()));
+            
+            writeContentDispositionGpxData(out, "file", gpxData);
+            writeContentDisposition(out, "description", description);
+            writeContentDisposition(out, "tags", (tags!=null && tags.length()>0) ? tags : "");
+            writeContentDisposition(out, "public", isPublic ? "1" : "0");
+            
+            out.writeBytes("--" + BOUNDARY + "--" + LINE_END);
+            out.flush();
 
-        int returnCode = connect.getResponseCode();
+            int returnCode = connect.getResponseCode();
             String returnMsg = connect.getResponseMessage();
-            System.err.println(returnCode);
+            //System.err.println(returnCode);
             OutputDisplay.setText(returnMsg);
-        if (returnCode != 200) {
-            if (connect.getHeaderField("Error") != null)
-               returnMsg += "\n" + connect.getHeaderField("Error");
+            
+            if (returnCode != 200) {
+                if (connect.getHeaderField("Error") != null)
+                    returnMsg += "\n" + connect.getHeaderField("Error");
+                connect.disconnect();
+            }
+            out.close();
             connect.disconnect();
-       }
-        out.close();
-        connect.disconnect();
 
         } catch(UnsupportedEncodingException ignore) {
         } catch (MalformedURLException e) {
-                OutputDisplay.setText("Cant Upload .");
-                e.printStackTrace();
+            OutputDisplay.setText(tr("Error while uploading"));
+            e.printStackTrace();
+        }
     }
 
-}
-
-
-
- public static String getPreferencesDir() {
-    if (System.getenv("APPDATA") != null)
-       return System.getenv("APPDATA")+"/JOSM/";
-       return System.getProperty("user.home")+"/.josm/";
+    public static String getPreferencesDir() {
+      if (System.getenv("APPDATA") != null)
+          return System.getenv("APPDATA")+"/JOSM/";
+      return System.getProperty("user.home")+"/.josm/";
     }
-
-private void OkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OkButtonActionPerformed
-     GpxData gpxData;
-
-        UserName  =  Main.pref.get("osm-server.username");
-        PassWord =   Main.pref.get("osm-server.password");
-
-        if(Main.map == null || Main.map.mapView == null ||Main.map.mapView.getActiveLayer() == null ||
-                !(Main.map.mapView.getActiveLayer() instanceof GpxLayer)){
-                JOptionPane.showMessageDialog(Main.parent,
-                tr("No GPX layer selected. Cannot upload a trace.")
-                );
-                return;
+    
+    private boolean checkForErrors(String username, String password, String description, GpxData gpxData) {
+        String errors="";
+        if(description == null || description.length() == 0)
+            errors += tr("No description provided. Please provide some description.");
+            
+        if(gpxData == null)
+            errors += tr("No GPX layer selected. Cannot upload a trace.");
+            
+        if(username == null || username.length()==0)
+            errors += tr("No username provided.");  
+            
+        if(password == null || password.length()==0)
+            errors += tr("No password provided.");
+        
+        OutputDisplay.setText(errors);
+        return errors.length() > 0;
+    }
+    
+    private boolean checkForGPXLayer() {
+        if(Main.map == null || Main.map.mapView == null || Main.map.mapView.getActiveLayer() == null || !(Main.map.mapView.getActiveLayer() instanceof GpxLayer)) {
+            OutputDisplay.setText(tr("No GPX layer selected. Cannot upload a trace."));
+            return true;
         }
-        gpxData = ((GpxLayer)Main.map.mapView.getActiveLayer()).data;
-                System.out.println(Descriptionfield);
+        return false;
+    }
+
+    private void OkButtonActionPerformed(java.awt.event.ActionEvent evt) {
+        if(checkForGPXLayer()) return;
+        
+        //System.out.println(Descriptionfield);
         try {
-                upload(UserName,PassWord,Descriptionfield ,tagging ,gpxData) ;
+            upload(Main.pref.get("osm-server.username"),
+                   Main.pref.get("osm-server.password"),
+                   descriptionField.getText(),
+                   tagsField.getText(),
+                   publicCheckbox.isSelected(),
+                   ((GpxLayer)Main.map.mapView.getActiveLayer()).data
+            );
         } catch (IOException ex) {
-                Logger.getLogger(UploadDataGui.class.getName()).log(Level.SEVERE, null, ex);
+            Logger.getLogger(UploadDataGui.class.getName()).log(Level.SEVERE, null, ex);
+        }
     }
 
-}//GEN-LAST:event_OkButtonActionPerformed
-
-private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
+    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
         dispose();
-}//GEN-LAST:event_CancelButtonActionPerformed
-
-private void PublicTraceItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_PublicTraceItemStateChanged
-
-    if (evt.getStateChange() == ItemEvent.SELECTED) {
-        publicheck = true;
-
     }
-}//GEN-LAST:event_PublicTraceItemStateChanged
 
-private void descriptionfieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_descriptionfieldFocusLost
-
-     JTextField Descsel = (JTextField)evt.getSource();
-        Descriptionfield =  Descsel.getText();
-
-}//GEN-LAST:event_descriptionfieldFocusLost
-
-private void tagfieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tagfieldFocusLost
-
-     JTextField Tagsel = (JTextField)evt.getSource();
-        tagging =  Tagsel.getText();
-
-}//GEN-LAST:event_tagfieldFocusLost
-
-
     private void writeContentDisposition(DataOutputStream out, String name, String value) throws IOException {
         out.writeBytes("--" + BOUNDARY + LINE_END);
         out.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"" + LINE_END);
         out.writeBytes(LINE_END);
         out.writeBytes(value + LINE_END);
-
     }
 
-    private void writeContentDispositionGpxData(DataOutputStream out, String name, GpxData gpxData ) throws IOException {
-
+    private void writeContentDispositionGpxData(DataOutputStream out, String name, GpxData gpxData) throws IOException {
         out.writeBytes("--" + BOUNDARY + LINE_END);
         out.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + datename +".gpx" + "\"" + LINE_END);
-        //out.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + gpxData.storageFile.getName() + "\"" + LINE_END);
         out.writeBytes("Content-Type: application/octet-stream" + LINE_END);
         out.writeBytes(LINE_END);
 
-        OutputDisplay.setText("Transferring data to server");
-            new GpxWriter(out).write(gpxData);
+        OutputDisplay.setText(tr("Transferring data to server"));
+        new GpxWriter(out).write(gpxData);
         out.flush();
         out.writeBytes(LINE_END);
-
     }
-
- // Taken from Christof Dallermassal java class :
-public String encodeBase64(String s) {
-        StringBuilder out = new StringBuilder();
-        for (int i = 0; i < (s.length()+2)/3; ++i) {
-            int l = Math.min(3, s.length()-i*3);
-            String buf = s.substring(i*3, i*3+l);
-            out.append(BASE64_ENC.charAt(buf.charAt(0)>>2));
-            out.append(BASE64_ENC.charAt((buf.charAt(0) & 0x03) << 4 | (l==1?0:(buf.charAt(1) & 0xf0) >> 4)));
-            out.append(l>1 ? BASE64_ENC.charAt((buf.charAt(1) & 0x0f) << 2 | (l==2 ? 0 : (buf.charAt(2) & 0xc0) >> 6)) : '=');
-            out.append(l>2 ? BASE64_ENC.charAt(buf.charAt(2) & 0x3f) : '=');
-
-        }
-        return out.toString();
-        }
-
-    // Variables declaration - do not modify//GEN-BEGIN:variables
-    private javax.swing.JButton CancelButton;
-    private javax.swing.JButton OkButton;
-    private javax.swing.JTextArea OutputDisplay;
-    private javax.swing.JCheckBox PublicTrace;
-    private javax.swing.JTextField descriptionfield;
-    private javax.swing.JLabel jLabel1;
-    private javax.swing.JLabel jLabel2;
-    private javax.swing.JLabel jLabel3;
-    private javax.swing.JScrollPane jScrollPane1;
-    private javax.swing.JTextField tagfield;
-    // End of variables declaration//GEN-END:variables
-
-}
\ No newline at end of file
+}
