Ticket #2275: 3_make_direct_upload_resizeable.patch

File 3_make_direct_upload_resizeable.patch, 10.5 KB (added by xeen, 15 years ago)

Likely depends on the patch in #2713

  • DirectUpload/src/org/openstreetmap/josm/plugins/DirectUpload/UploadDataGui.java

     
    5959    private String password = Main.pref.get("osm-server.password");
    6060
    6161    // Fields are declared here for easy access
    62     private JMultilineLabel OutputDisplay = new JMultilineLabel("");
    63     private JTextField descriptionField = new JTextField();
    64     private JTextField tagsField = new JTextField();
     62    // Do not remove the space in JMultilineLabel. Otherwise the label will be empty
     63    // as we don't know its contents yet and therefore have a height of 0. This will
     64    // lead to unnecessary scrollbars.
     65    private JMultilineLabel OutputDisplay = new JMultilineLabel(" ");
     66    private JTextField descriptionField = new JTextField(50);
     67    private JTextField tagsField = new JTextField(50);
    6568    private JCheckBox publicCheckbox = new JCheckBox();
    66    
     69
    6770    // Constants used when generating upload request
    6871    private static final String API_VERSION = "0.6";
    6972    private static final String BOUNDARY = "----------------------------d10f7aa230e8";
     
    7376    // Filename and current date. Date will be used as fallback if filename not available
    7477    private String datename = new SimpleDateFormat("yyMMddHHmmss").format(new Date());
    7578    private String filename = "";
    76    
     79
    7780    private boolean cancelled = false;
    7881
    7982    public UploadDataGui() {
     
    8588        );
    8689        JPanel content = initComponents();
    8790        autoSelectTrace();
    88        
    89         contentConstraints = GBC.eol().fill().insets(5,10,5,0);
     91
    9092        setupDialog(content, new String[] { "uploadtrace.png", "cancel.png" });
    91        
     93
    9294        buttons.get(0).setEnabled(!checkForGPXLayer());
    93        
    94         setSize(findMaxDialogSize());
    9595    }
    96    
     96
    9797    /**
    9898     * Sets up the dialog window elements
    9999     * @return JPanel with components
     
    108108        JLabel tagsLabel = new JLabel(tr("Tags"));
    109109        tagsField.setToolTipText(tr("Please enter tags about your trace."));
    110110
    111         JPanel p = new JPanel();
    112         p.setLayout(new GridBagLayout());
     111        JPanel p = new JPanel(new GridBagLayout());
    113112
    114113        OutputDisplay.setMaxWidth(findMaxDialogSize().width-10);
    115114        p.add(OutputDisplay, GBC.eol());
     
    124123
    125124        return p;
    126125    }
    127    
     126
    128127    /**
    129128     * This function will automatically select a GPX layer if it's the only one.
    130129     * If a GPX layer is found, its filename will be parsed and displayed
     
    167166    private void upload(String description, String tags, Boolean isPublic, GpxData gpxData) throws IOException {
    168167        if(checkForErrors(username, password, description, gpxData))
    169168            return;
    170        
     169
    171170        // Clean description/tags from disallowed chars
    172171        description = description.replaceAll("[&?/\\\\]"," ");
    173172        tags = tags.replaceAll("[&?/\\\\.,;]"," ");
    174        
     173
    175174        // Set progress dialog to indeterminate while connecting
    176175        Main.pleaseWaitDlg.progress.setValue(0);
    177         Main.pleaseWaitDlg.setIndeterminate(true); 
     176        Main.pleaseWaitDlg.setIndeterminate(true);
    178177        Main.pleaseWaitDlg.currentAction.setText(tr("Connecting..."));
    179178
    180179        try {
    181180            // Generate data for upload
    182             ByteArrayOutputStream baos  = new ByteArrayOutputStream();           
     181            ByteArrayOutputStream baos  = new ByteArrayOutputStream();
    183182            writeGpxFile(baos, "file", gpxData);
    184183            writeField(baos, "description", description);
    185184            writeField(baos, "tags", (tags != null && tags.length() > 0) ? tags : "");
    186185            writeField(baos, "public", isPublic ? "1" : "0");
    187186            writeString(baos, "--" + BOUNDARY + "--" + LINE_END);
    188            
    189             ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());           
     187
     188            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    190189            HttpURLConnection conn = setupConnection(baos.size());
    191            
     190
    192191            Main.pleaseWaitDlg.progress.setMaximum(baos.size());
    193             Main.pleaseWaitDlg.setIndeterminate(false); 
    194            
     192            Main.pleaseWaitDlg.setIndeterminate(false);
     193
    195194            try {
    196195                flushToServer(bais, conn.getOutputStream());
    197196            } catch(Exception e) {}
    198            
     197
    199198            if(cancelled) {
    200199                conn.disconnect();
    201200                OutputDisplay.setText(tr("Upload cancelled"));
    202201                buttons.get(0).setEnabled(true);
    203202                cancelled = false;
    204203            } else {
    205                 boolean success = finishUpConnection(conn);           
     204                boolean success = finishUpConnection(conn);
    206205                buttons.get(0).setEnabled(!success);
    207206                if(success)
    208207                    buttons.get(1).setText(tr("Close"));
     
    212211            e.printStackTrace();
    213212        }
    214213    }
    215    
     214
    216215    /**
    217216     * This function sets up the upload URL and logs in using the username and password given
    218217     * in the preferences.
     
    224223        CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
    225224        String auth = username + ":" + password;
    226225        ByteBuffer bytes = encoder.encode(CharBuffer.wrap(auth));
    227        
     226
    228227        // Upload URL
    229228        URL url = new URL("http://www.openstreetmap.org/api/" + API_VERSION + "/gpx/create");
    230        
     229
    231230        // Set up connection and log in
    232231        HttpURLConnection c = (HttpURLConnection) url.openConnection();
    233232        c.setFixedLengthStreamingMode(contentLength);
    234233        c.setConnectTimeout(15000);
    235234        c.setRequestMethod("POST");
    236235        c.setDoOutput(true);
    237         c.addRequestProperty("Authorization", "Basic " + Base64.encode(bytes));           
     236        c.addRequestProperty("Authorization", "Basic " + Base64.encode(bytes));
    238237        c.addRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
    239238        c.addRequestProperty("Connection", "close"); // counterpart of keep-alive
    240239        c.addRequestProperty("Expect", "");
    241240        c.connect();
    242        
     241
    243242        return c;
    244243    }
    245    
     244
    246245    /**
    247246     * This function checks if the given connection finished up fine, closes it and returns the result.
    248247     * It also posts the result (or errors) to OutputDisplay.
    249      
     248
    250249     * @param HttpURLConnection The connection to check/finish up
    251250     */
    252251    private boolean finishUpConnection(HttpURLConnection c) throws Exception {
    253252        String returnMsg = c.getResponseMessage();
    254253        boolean success = returnMsg.equals("OK");
    255        
     254
    256255        if (c.getResponseCode() != 200) {
    257256            if (c.getHeaderField("Error") != null)
    258257                returnMsg += "\n" + c.getHeaderField("Error");
    259258        }
    260        
     259
    261260        OutputDisplay.setText(success
    262261            ? tr("GPX upload was successful")
    263262            : tr("Upload failed. Server returned the following message: ") + returnMsg);
     
    265264        c.disconnect();
    266265        return success;
    267266    }
    268    
     267
    269268    /**
    270269     * Uploads a given InputStream to a given OutputStream and sets progress
    271270     * @param InputSteam
     
    283282                out.flush();
    284283                Main.pleaseWaitDlg.progress.setValue(cur);
    285284                Main.pleaseWaitDlg.currentAction.setText(getProgressText(cur));
    286                
     285
    287286                if(cancelled)
    288287                    break;
    289288                        }
     
    293292        Main.pleaseWaitDlg.currentAction.setText("Waiting for server reply...");
    294293                buffer = null;
    295294        }
    296    
     295
    297296    /**
    298297     * Generates the output string displayed in the PleaseWaitDialog.
    299298     * @param int Bytes already uploaded
     
    305304        return tr("Uploading GPX track: {0}% ({1} of {2})",
    306305                        percent, formatBytes(cur), formatBytes(max));
    307306    }
    308    
     307
    309308    /**
    310309     * Nicely calculates given bytes into MB, kB and B (with units)
    311310     * @param int Bytes
     
    350349    }
    351350
    352351    /**
    353      * Checks if a GPX layer is selected and returns the result. Also writes an error 
     352     * Checks if a GPX layer is selected and returns the result. Also writes an error
    354353     * message to OutputDisplay if result is false.
    355354     * @return boolean True, if /no/ GPX layer is selected
    356355     */
     
    365364        return false;
    366365    }
    367366
    368    
     367
    369368    /**
    370369     * This creates the uploadTask that does the actual work and hands it to the main.worker to be executed.
    371370     */
    372371    private void setupUpload() {
    373372        if(checkForGPXLayer()) return;
    374        
     373
    375374        // Disable Upload button so users can't just upload that track again
    376375        buttons.get(0).setEnabled(false);
    377376
     
    388387                cancelled = true;
    389388            }
    390389        };
    391        
     390
    392391        Main.worker.execute(uploadTask);
    393392    }
    394    
     393
    395394    /**
    396395     * Writes textfields (like in webbrowser) to the given ByteArrayOutputStream
    397396     * @param ByteArrayOutputStream
     
    424423        new GpxWriter(baos).write(gpxData);
    425424        writeLineEnd(baos);
    426425    }
    427    
     426
    428427    /**
    429428     * Writes a String to the given ByteArrayOutputStream
    430429     * @param ByteArrayOutputStream
     
    435434            baos.write(s.getBytes());
    436435        } catch(Exception e) {}
    437436    }
    438    
     437
    439438    /**
    440439     * Writes a newline to the given ByteArrayOutputStream
    441440     * @param ByteArrayOutputStream
     
    443442    private void writeLineEnd(ByteArrayOutputStream baos) {
    444443        writeString(baos, LINE_END);
    445444    }
    446    
     445
    447446    /**
    448447     * Writes a boundary line to the given ByteArrayOutputStream
    449448     * @param ByteArrayOutputStream
     
    452451        writeString(baos, "--" + BOUNDARY);
    453452        writeLineEnd(baos);
    454453    }
    455    
     454
    456455    /**
    457456     * Returns the filename of the GPX file to be upload. If not available, returns current date
    458457     * as an alternative
     
    461460    private String getFilename() {
    462461       return filename.equals("") ? datename : filename;
    463462    }
    464    
     463
    465464    /**
    466      * Defines a default size for the dialog
    467      */
    468     @Override protected Dimension findMaxDialogSize() {
    469         setResizable(false);
    470         return new Dimension(300, 230);
    471     }
    472    
    473     /**
    474465     * Overrides the default actions. Will not close the window when upload trace is clicked
    475466     */
    476467    @Override protected void buttonAction(ActionEvent evt) {